Data Structures: Tree Fundamentals and Binary Trees

Introduction to Trees

  • General Definition: A tree is a non-linear data structure in which items are arranged in a sorted sequence. It is used to represent hierarchical relationships existing amongst several data items.
  • Graph Theoretic Definition: A tree is a finite set of one or more data items (nodes) such that:     * There is a special data item called the root of the tree.     * The remaining data items are partitioned into a number of mutually exclusive (disjoint) subsets, each of which is itself a tree, known as subtrees.
  • Growth Direction: Natural trees grow upwards from the ground into the air; however, tree data structures grow downwards from top to bottom.

Tree Terminology

  • Root: A specially designed data item in a tree and the first in the hierarchical arrangement. In an example tree, node AA is the root.
  • Node: The basic structure in a tree representing each data item. It specifies the data information and links (branches) to other data items.
  • Degree of a Node: The number of subtrees of a node in a given tree.     * Example: In a specific tree, node AA has a degree of 33, node CC has a degree of 11, node BB has a degree of 22, node HH has a degree of 00, and node II has a degree of 33.
  • Degree of a Tree: The maximum degree among all nodes in the tree. If node AA and node II both have a maximum degree of 33, the degree of the tree is 33.
  • Terminal Node (Leaf): A node with a degree of zero. In an example tree, terminal nodes include EE, JJ, GG, HH, KK, LL, and MM.
  • Non-Terminal Node: Any node (except the root) whose degree is not zero. These are intermediate nodes encountered when traversing from the root to the terminal nodes.
  • Siblings: Children nodes belonging to the same parent node, also referred to as brothers.     * Examples: EE and FF are siblings of parent node BB; KK, LL, and MM are siblings of parent node II.
  • Level: A hierarchy where the root node is at level 00. Immediate children are at level 11, and their children are at level 22. In general, if a node is at level nn, its children are at level n+1n+1.
  • Edge: The connecting line drawn from one node to another.
  • Path: A sequence of consecutive edges from a source node to a destination node.     * Example: For a path between node AA and node JJ, the node pairs are (A,B)(A, B), (B,F)(B, F), and (F,J)(F, J).
  • Depth (Height): The maximum level of any node in a given tree. It represents the number of levels one can descend from the root to the leaves.
  • Forest: A set of disjoint trees. Removing the root node from a tree results in a forest of its subtrees.

Binary Trees

  • Definition: A binary tree consists of a finite set of elements partitioned into three distinct sub-sets: the root, the left sub-tree, and the right sub-tree.
  • Empty Binary Tree: A binary tree with no elements.
  • Recursive Nature: The left and right sub-sets are themselves binary trees and can be empty.
  • Structural Visuals: Left and right sub-trees are shown using branches from the root node. If no branch exists, the sub-tree is empty.
  • Father and Son: If node AA is the root and node BB is the root of its left sub-tree, AA is the father of BB, and BB is the left son of AA.
  • Ancestor and Descendant:     * Node AA is an ancestor of node BB if AA is the father of BB or the father of some ancestor of BB.     * Node BB is a left descendant of node AA if BB is the left son of AA or a descendant of the left son of AA. Right descendants are defined similarly.
  • Directional Convention: "Down" refers to moving from the root to leaf nodes; "Up" refers to moving from leaves toward the root.
  • Climbing and Descending: Traversing from a leaf to the root is climbing; traversing from the root to leaves is descending.

Specialized Binary Tree Types

  • Strictly Binary Trees: A binary tree where every non-leaf node has non-empty left and right sub-trees. Each non-leaf node must have exactly two children.
  • Degree in Binary Trees: The number of nodes connected to a particular node. The degree of a leaf node is always 11.
  • Complete Binary Tree: A strictly binary tree in which all leaf nodes are at the same level.
  • Extended Binary Tree (2-Tree):     * Created by adding new nodes to leaf nodes and nodes with only one child so that every node has either 00 or 22 children.     * Internal Nodes: Original nodes of the tree.     * External Nodes: The new nodes added to create the 2-tree (often represented by square shapes).     * Mathematical Properties of Extended Binary Trees:         1. If a tree has nn nodes, it has n−1n-1 branches.         2. Every node except the root has exactly one parent.         3. Any two nodes are connected by only one single path.         4. For a binary tree of height hh, the maximum number of nodes is 2h+1−12^{h+1}-1.         5. A binary tree with nn internal nodes has n+1n+1 external nodes.

Binary Tree Representation

Array Representation

  • Nodes are stored sequentially in an array, specified by a maximum size MAXSIZE.
  • Indexing: Starting from root at index 00. Nodes are numbered left to right, level by level, top to bottom. Empty nodes are included in the numbering to maintain structural integrity.
  • Formulas for index nn (where 0≤n≤MAXSIZE−10 \le n \le MAXSIZE-1):     * Father(n): Given by ⌊n−12⌋\lfloor \frac{n-1}{2} \rfloor, provided n≠0n \ne 0. If n=0n=0, it is the root.     * Left Child (Lchild): Given by (2n+1)(2n + 1).     * Right Child (Rchild): Given by (2n+2)(2n + 2).     * Siblings: if a left child is at index nn, its right sibling is at n+1n+1. If a right child is at index nn, its left sibling is at n−1n-1.

Linked Representation

  • Uses a linked list where each node is a structure with three fields:     * Data: Holds the value.     * Left Child: Pointer to the address of the left node.     * Right Child: Pointer to the address of the right node.
  • C Structure Definition:
struct tree {
    char info;
    struct node *left;
    struct node *right;
};
  • Optional Father Field: In some applications, a fourth field is added to store the parent address:
struct tree {
    char data;
    struct node *father;
    struct node *lchild;
    struct node *rchild;
};
  • Internal vs External: Terminal nodes (external) have their lchild and rchild pointers set to NULL.

Binary Tree Operations

OperationDescription
CreateCreates an empty binary tree.
MakeBTCreates a new binary tree with a single node and a specific data value.
EmptyBTReturns true if the tree is empty, false otherwise.
LchildReturns a pointer to the left child, or NULL if none exists.
RchildReturns a pointer to the right child, or NULL if none exists.
FatherReturns a pointer to the father node, or NULL.
BrotherReturns a pointer to the sibling node, or NULL.
DataReturns the contents (data) of the node.
  • Additional Operations: Tree traversal, insertion, deletion, searching, and copying the tree.

Binary Tree Traversals

Recursive Traversals

Traversal involves visiting each node exactly once. Because of the recursive nature of trees, methods use recursive functions.

  1. Pre-order (NLR - Node, Left, Right):     * Algorithm: Visit root RR, traverse left sub-tree in pre-order, traverse right sub-tree in pre-order.     * C Code Implementation: c void preorder(struct tree *root) { if(root != NULL) { printf("%c\t", root->info); preorder(root->left); preorder(root->right); } }     
  2. In-order (LNR - Left, Node, Right):     * Algorithm: Traverse left sub-tree in in-order, visit root RR, traverse right sub-tree in in-order.     * Values are processed in ascending order if applied to a Binary Search Tree.     * C Code Implementation: c void inorder(struct tree *root) { if(root != NULL) { inorder(root->left); printf("%c\t", root->info); inorder(root->right); } }     
  3. Post-order (LRN - Left, Right, Node):     * Algorithm: Traverse left sub-tree in post-order, traverse right sub-tree in post-order, visit root RR.     * C Code Implementation: c void postorder(struct tree *root) { if(root != NULL) { postorder(root->left); postorder(root->right); printf("%c\t", root->info); } }     

Non-Recursive Traversals using Stacks

Uses a STACK to store node addresses and a PTR for the current node.

  • Preorder Algorithm: Push NULL onto stack. Set PTR=ROOT. While PTR is not NULL: process the node, push its right child (if any) onto the stack, update PTR to the left child. If left child is NULL, pop from stack and assign to PTR.
  • Inorder Algorithm: Push NULL. Set PTR=ROOT. Descend left-most path, pushing nodes onto the stack until NULL left child. Pop and process node. If node has right child, set PTR to it and repeat.
  • Postorder Algorithm: More complex. Push N or its negative -N to distinguish states. A node is processed only when positive when popped.

Binary Search Tree (BST)

  • Property: For every node nn, all elements in the left sub-tree are less than the contents of nn, and all elements in the right sub-tree are greater than or equal to the contents of nn.
  • Sorting Application: If a BST is traversed in-order, numbers are printed in ascending order.
  • Construction Example: Given the list 2020, 1717, 66, 88, 1010, 77, 1818, 1313, 1212, 55, the first number 2020 becomes the root. Subsequent numbers are placed left if smaller, and right if greater/equal.

Advanced Tree Structures

B-Tree

  • A balanced N-way search tree (not a binary tree).
  • Purpose: Reduces disk accesses in external sorting.
  • Conditions:     1. Height is kept to a minimum; no empty sub-trees above leaves.     2. Leaves are all on the same level.     3. Non-leaf nodes (except root) have at least n2\frac{n}{2} and at most nn children.     4. Root has at most nn and at least 22 children.     5. If a node has nn children, it contains n−1n-1 values in increasing order.     6. Values in the (i+1)th(i+1)^{th} sub-tree are between the ithi^{th} and (i+1)th(i+1)^{th} values of the parent.

B+ Tree

  • Implementation technique for indexed sequential organization.
  • Structure: Indexed part (interior nodes) and sequence set (leaves linked as a list).
  • Allows both direct and efficient sequential access to keys.

AVL Tree

  • Invented in 1962 by G. M. Adelson-Velskii and E. M. Landis.
  • Definition: A balanced binary search tree where the height difference between the left and right sub-trees of all nodes is at most one.
  • Balance Factor: Calculated as (Height of Left Sub-tree) - (Height of Right Sub-tree).
  • Node contains four fields: Data, Left Pointer, Right Pointer, and Balance Factor.

Threaded Binary Tree

  • Eliminates stack/recursion overhead for traversals by using empty pointer fields.
  • Right Threads: Replace NULL right pointers with a pointer to the in-order successor.
  • Left Threads: Replace NULL left pointers with a pointer to the in-order predecessor.

Constructing Trees from Traversal Sequences

  • Root Identification: The first node in a Preorder sequence is the root of the tree.
  • Subtree Identification: Locating the root in the Inorder sequence separates elements into the Left Subtree (elements to the left of the root) and Right Subtree (elements to the right).
  • Example (Example 4):     * Inorder: EE, AA, CC, KK, FF, HH, DD, BB, GG     * Preorder: FF, AA, EE, KK, CC, DD, HH, GG, BB     * Root is FF. Left subtree nodes: E,A,C,KE, A, C, K. Right subtree nodes: H,D,B,GH, D, B, G.