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 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 has a degree of , node has a degree of , node has a degree of , node has a degree of , and node has a degree of .
- Degree of a Tree: The maximum degree among all nodes in the tree. If node and node both have a maximum degree of , the degree of the tree is .
- Terminal Node (Leaf): A node with a degree of zero. In an example tree, terminal nodes include , , , , , , and .
- 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: and are siblings of parent node ; , , and are siblings of parent node .
- Level: A hierarchy where the root node is at level . Immediate children are at level , and their children are at level . In general, if a node is at level , its children are at level .
- 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 and node , the node pairs are , , and .
- 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 is the root and node is the root of its left sub-tree, is the father of , and is the left son of .
- Ancestor and Descendant: * Node is an ancestor of node if is the father of or the father of some ancestor of . * Node is a left descendant of node if is the left son of or a descendant of the left son of . 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 .
- 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 or 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 nodes, it has 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 , the maximum number of nodes is . 5. A binary tree with internal nodes has 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 . 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 (where ): * Father(n): Given by , provided . If , it is the root. * Left Child (Lchild): Given by . * Right Child (Rchild): Given by . * Siblings: if a left child is at index , its right sibling is at . If a right child is at index , its left sibling is at .
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
lchildandrchildpointers set toNULL.
Binary Tree Operations
| Operation | Description |
|---|---|
| Create | Creates an empty binary tree. |
| MakeBT | Creates a new binary tree with a single node and a specific data value. |
| EmptyBT | Returns true if the tree is empty, false otherwise. |
| Lchild | Returns a pointer to the left child, or NULL if none exists. |
| Rchild | Returns a pointer to the right child, or NULL if none exists. |
| Father | Returns a pointer to the father node, or NULL. |
| Brother | Returns a pointer to the sibling node, or NULL. |
| Data | Returns 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.
- Pre-order (NLR - Node, Left, Right):
* Algorithm: Visit root , 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); } } - In-order (LNR - Left, Node, Right):
* Algorithm: Traverse left sub-tree in in-order, visit root , 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); } } - Post-order (LRN - Left, Right, Node):
* Algorithm: Traverse left sub-tree in post-order, traverse right sub-tree in post-order, visit root .
* 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
NULLonto stack. SetPTR=ROOT. WhilePTRis notNULL: process the node, push its right child (if any) onto the stack, updatePTRto the left child. If left child isNULL, pop from stack and assign toPTR. - Inorder Algorithm: Push
NULL. SetPTR=ROOT. Descend left-most path, pushing nodes onto the stack untilNULLleft child. Pop and process node. If node has right child, setPTRto 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 , all elements in the left sub-tree are less than the contents of , and all elements in the right sub-tree are greater than or equal to the contents of .
- Sorting Application: If a BST is traversed in-order, numbers are printed in ascending order.
- Construction Example: Given the list , , , , , , , , , , the first number 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 and at most children. 4. Root has at most and at least children. 5. If a node has children, it contains values in increasing order. 6. Values in the sub-tree are between the and 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
NULLright pointers with a pointer to the in-order successor. - Left Threads: Replace
NULLleft 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: , , , , , , , , * Preorder: , , , , , , , , * Root is . Left subtree nodes: . Right subtree nodes: .