Comprehensive Guide to Binary Trees, Traversals, and Binary Search Trees

Introduction to Trees and Non-Linear Data Structures

  • Classification of Data Structures:

    • Linked Lists, Stacks, and Queues are classified as Linear Data Structures.

    • Trees are classified as Non-Linear Data Structures.

  • Pointer and Node Connectivity:

    • In a Linked List, each node contains a link that points strictly to one other node.

    • In a Tree, each node may point to several other nodes, providing exceptional structural flexibility.

  • Application Example:

    • A tree data structure can represent hierarchical relationships, such as a person and all of their descendants.

  • Node Architecture:

    • Each tree node contains a data element (e.g., a name) alongside one or more pointers directed toward other tree nodes.

Fundamentals and Formal Definition of Binary Trees

  • Pointer Constraint:

    • A binary tree node can possess at most 22 pointers directed to other nodes.

  • Formal Definition:

    • A Binary Tree is a finite set of elements that is either empty or partitioned into 33 disjoint sub-sets:

      1. The first sub-set contains a single element known as the root of the tree.

      2. The other two sub-sets are themselves binary trees, termed the left sub-tree and the right sub-tree of the original tree.

  • Sub-tree Characteristics:

    • A left sub-tree or a right sub-tree can be completely empty.

    • Every individual element within a binary tree is termed a node.

  • Structural Examination of an Example Binary Tree:

    

Binary tree structure with six nodes A, B, C, D, E, F
*   The tree consists of 66 nodes in total, with node AA serving as the root.
*   The left sub-tree is rooted at node BB, and the right sub-tree is rooted at node CC.
*   Two branches emanate directly from node AA: branch to BB on the left and branch to CC on the right.
*   The absence of an emanating branch indicates an empty sub-tree (e.g., the right sub-tree of node BB is empty).

Binary Tree Terminology and Hierarchical Relationships

  • Parent-Child (Father-Son) Terminology:

    • Node AA is the father of node BB.

    • Node BB is the left son of node AA.

    • Node CC is the right son of node AA.

  • Leaf Nodes:

    • A leaf node is defined as any node that has no sons.

    • In the reference 66-node tree, the leaf nodes are: DD, EE, and FF.

  • Non-Leaf Nodes:

    • A non-leaf node is any node that has one or more sons.

    • In the reference 66-node tree, the non-leaf nodes are: AA, BB, and CC.

  • Ancestor Relationships:

    • A node n1n_1 is said to be an ancestor of a node n2n_2 if n1n_1 is either the father of n2n_2 or the father of some ancestor of n2n_2.

    • Example: Node AA is an ancestor of node BB (and node DD).

  • Brother (Sibling) Relationships:

    • Two nodes are brothers if they are the left and right sons of the same father.

    • Example: Nodes BB and CC are brothers; nodes EE and FF are brothers.

Non-Binary Trees and Non-Tree Structures

  • Structural Criteria for Non-Trees:

    

Examples of non-tree structures and cycles
*   Graphs containing cycles (closed circular paths) are non-trees.
*   Structures where a single node has multiple incoming parent links violate tree topology.
*   Structures where nodes have more than 22 children violate the strict definition of a binary tree.

Binary Tree Metrics and Specialized Classifications

  • Strictly Binary Tree:

    • A binary tree is classified as a Strictly Binary Tree if every non-leaf node has a non-empty left sub-tree and a non-empty right sub-tree.

    • Example node role partition:

      • Leaf nodes: BB, FF, GG, EE

      • Non-leaf nodes: AA, CC, DD

  • Degree of a Node:

    • The degree of a node is defined as the total number of nodes connected directly to that particular node.

    • The degree of a leaf node is always 11.

    • Degree breakdown for example nodes:

      • Node A=2\text{Node } A = 2

      • Node B=1\text{Node } B = 1

      • Node C=3\text{Node } C = 3

      • Node D=3\text{Node } D = 3

      • Node E=1\text{Node } E = 1

      • Node F=1\text{Node } F = 1

      • Node G=1\text{Node } G = 1

  • Level of a Node:

    • The root node of a tree is established at level 00.

    • Level assignments for example nodes:

      • Level 00: AA

      • Level 11: BB, CC

      • Level 22: DD, EE

      • Level 33: FF, GG

  • Depth of a Binary Tree:

    • The depth of a binary tree is defined as the maximum level attained by any leaf within the tree.

    • This metric is equivalent to the length of the longest path from the root node to any leaf node.

    • In the reference example, the maximum depth is 33.

  • Complete Binary Tree:

    • A complete binary tree of depth dd is a strictly binary tree in which all leaf nodes are situated at the exact same level dd.

    

Complete binary tree diagrams
*   A complete binary tree of depth 22 contains all leaves strictly positioned at level 22.

Binary Tree Traversals

  • Concept of Traversal:

    • Traversal of a binary tree entails visiting every single node in the tree exactly once.

    • While linear lists have an inherent order from first to last, trees possess no single natural sequence for node visitation.

  • Memory Representation of a Node:

    • A binary tree node is represented in storage with three distinct fields:

      • DATA: The substantive value or data element stored within the node.

      • LST LINK: Pointer to the Left Sub-Tree.

      • RST LINK: Pointer to the Right Sub-Tree.

  • Traversal Orders and Algorithmic Steps:

    • Pre-order Traversal (N,L,RN, L, R):

      • Shorthand: (root node, left st, right st)

      • Step 1: Visit the Root.

      • Step 2: Traverse the left sub-tree in pre-order.

      • Step 3: Traverse the right sub-tree in pre-order.

    • In-order Traversal (L,N,RL, N, R):

      • Shorthand: (Left st, root node, right st)

      • Step 1: Traverse the left sub-tree in pre-order.

      • Step 2: Visit the Root.

      • Step 3: Traverse the right sub-tree in pre-order.

    • Post-order Traversal (L,R,NL, R, N):

      • Shorthand: (left st, right st, root node)

      • Step 1: Traverse the left sub-tree in pre-order.

      • Step 2: Traverse the right sub-tree in pre-order.

      • Step 3: Visit the Root.

  • Demonstration on Reference Binary Tree:

    

Five-node binary tree for traversal demonstration
*   Tree Layout: Root is node 11; left child is node 22; right child is node 33. Node 22 has left child 44 and right child 55.
*   **In-order Traversal** (Left, Root, Right): 4,2,5,1,34, 2, 5, 1, 3
*   **Pre-order Traversal** (Root, Left, Right): 1,2,4,5,31, 2, 4, 5, 3
*   **Post-order Traversal** (Left, Right, Root): 4,5,2,3,14, 5, 2, 3, 1

Binary Search Trees (BST)

  • Core Properties:

    • A Binary Search Tree (BST) is a node-based binary tree data structure characterized by three fundamental properties:

      1. The left sub-tree of a node contains exclusively nodes with values less than the parent (root) node.

      2. The right sub-tree of a node contains exclusively nodes with values greater than the parent (root) node.

      3. Both the left and right sub-trees must each independently satisfy the requirements of a binary search tree.

  • Invariant Validation Example:

    

Binary Search Tree with root node 30
*   For the root node 3030:
    *   The left sub-tree contains values: 1515, 77, 2222, 1717, 2727. None of these values are greater than or equal to 3030.
    *   The right sub-tree contains values: 6060, 4545, 7575. None of these values are less than 3030.
*   Sub-tree evaluations:
    *   At node 1515: Left child is 77 (7<157 < 15); right child is 2222 (22>1522 > 15).
    *   At node 2222: Left child is 1717 (17<2217 < 22); right child is 2727 (27>2227 > 22).
    *   At node 6060: Left child is 4545 (45<6045 < 60); right child is 7575 (75>6075 > 60).

Step-by-Step Construction of a Binary Search Tree

  • Insertion Dataset:

    • Input sequence: 43,10,79,90,12,54,11,9,5043, 10, 79, 90, 12, 54, 11, 9, 50

  • BST Construction Algorithm:

    1. Insert the first element (4343) as the root of the tree.

    2. Read the next element: if it is lesser than the root node element, insert it into the left sub-tree.

    3. Otherwise, insert it into the right sub-tree.

  • Detailed Step Progression:

    

Step-by-step BST construction from elements 43 to 50
*   **Step 1**: Insert 4343.
    *   4343 is the first element and becomes the tree root.
*   **Step 2**: Insert 1010.
    *   Compare with root 4343: 10<4310 < 43.
    *   1010 is attached as the left child of 4343.
*   **Step 3**: Insert 7979.
    *   Compare with root 4343: 79>4379 > 43.
    *   7979 is attached as the right child of 4343.
*   **Step 4**: Insert 9090.
    *   Compare with root 4343: 90>4390 > 43 (navigate right to 7979).
    *   Compare with 7979: 90>7990 > 79.
    *   9090 is attached as the right child of 7979.
*   **Step 5**: Insert 1212.
    *   Compare with root 4343: 12<4312 < 43 (navigate left to 1010).
    *   Compare with 1010: 12>1012 > 10.
    *   1212 is attached as the right child of 1010.
*   **Step 6**: Insert 5454.
    *   Compare with root 4343: 54>4354 > 43 (navigate right to 7979).
    *   Compare with 7979: 54<7954 < 79.
    *   5454 is attached as the left child of 7979.
*   **Step 7**: Insert 1111.
    *   Compare with root 4343: 11<4311 < 43 (navigate left to 1010).
    *   Compare with 1010: 11>1011 > 10 (navigate right to 1212).
    *   Compare with 1212: 11<1211 < 12.
    *   1111 is attached as the left child of 1212.
*   **Step 8**: Insert 99.
    *   Compare with root 4343: 9<439 < 43 (navigate left to 1010).
    *   Compare with 1010: 9<109 < 10.
    *   99 is attached as the left child of 1010.
*   **Step 9**: Insert 5050.
    *   Compare with root 4343: 50>4350 > 43 (navigate right to 7979).
    *   Compare with 7979: 50<7950 < 79 (navigate left to 5454).
    *   Compare with 5454: 50<5450 < 54.
    *   5050 is attached as the left child of 5454.