Unit-IV Tree structures
Unit-IV Tree structures
Tree ADT
Descendant: Any successor node on the path from that node to leaf node.
{D,I,J} or {E,K}are descendant node of node{B}
Types of Binary Tree in Data Structure
Full Binary Tree
A full binary tree is a tree where every node has either 0 or 2
children.
In-order Traversal: 4, 2, 5, 1, 3
Steps:
Visit the left subtree: 4
Visit the root: 2
Visit the right subtree: 5
Visit the root: 1
Visit the right subtree: 3
2. Pre-order Traversal (Root, Left, Right)
In pre-order traversal, the nodes are recursively visited in this
order: root node, left subtree, and then the right subtree.
Pre-order Traversal: 1, 2, 4, 5, 3
Steps:
Visit the root: 1
Visit the left subtree: 2
Visit the left subtree: 4
Visit the right subtree: 5
Visit the right subtree: 3
3. Post-order Traversal (Left, Right, Root)
In post-order traversal, the nodes are recursively visited in
this order: left subtree, right subtree, and then the root node.
Post-order Traversal: 4, 5, 2, 3, 1
Steps:
Visit the left subtree: 4
Visit the right subtree: 5
Visit the root: 2
Visit the right subtree: 3
Visit the root: 1
Example:
Insert the value 8 into the following BST:
Step-by-Step Insertion:
Start at the root (10).
8 is less than 10, move to the left child (5).
8 is greater than 5, move to the right child (7).
8 is greater than 7, move to the right child of 7 (which is
empty).
Insert 8 as the right child of 7.
Result:
2. Deletion
Binary search tree node deletion involves removing a node and
reorganizing the tree to maintain the BST property.
There are three cases to handle:
Node with no children (leaf node)
Node with one child
Node with two children
Algorithm:
No Child (Leaf Node): Simply remove the node.
One Child: Remove the node and replace it with its child.
Two Children: Find the in-order predecessor (largest value in
the left subtree) or in-order successor (smallest value in the
right subtree), replace the node's value with the
predecessor/successor, and delete the predecessor/successor.
Example:
Delete the value 5 from the following BST:
Step-by-Step Deletion:
Start at the root (10).
5 is less than 10, move to the left child (5).
Node 5 has two children. Find the in-order successor (smallest
value in the right subtree of 5), which is 7.
Replace 5 with 7.
Delete node 7.
Result:
3. Searching
Searching in a binary search tree involves finding a node with a given
value by leveraging the BST property to reduce the number of
comparisons.
Algorithm:
Start at the root node.
Compare the target value with the current node's value.
If the value is equal, the search is successful.
If the value is smaller, move to the left child.
If the value is greater, move to the right child.
Repeat steps 2-5 until you find the value or reach an empty spot
(unsuccessful search).
Example:
Search for the value 8 in the following BST:
Step-by-Step Searching:
Start at the root (10).
8 is less than 10, move to the left child (5).
8 is greater than 5, move to the right child (7).
8 is greater than 7, move to the right child (8).
8 is equal to 8, search is successful.
AVL Tree
In an AVL data structure, each node has a balance factor.
This balance factor is the difference height between the left
and right subtrees.
The balance factor can only be -1, 0, or 1. If the balance factor
gets outside this range, the tree does a rotation to fix itself.
This keeps the tree short and wide, making it faster to search,
insert, and delete items.
This makes AVL trees very useful in applications like databases
and memory management, where we need to quickly find or
update information.
Min Heap
Properties of Heaps
Property Max Heap Min Heap
Root node Largest element Smallest element
Relationship Parent ≥ Children Parent ≤ Children
Complete Binary Complete Binary
Structure
Tree Tree
Implementation Array-based Array-based
PSEUDOCODE
MAX-HEAPIFY(A, i)
1. l = 2i
2. r = 2i + 1
3. largest = i
4. if l ≤ heap-size and A[l] > A[largest]
5. largest = l
6. if r ≤ heap-size and A[r] > A[largest]
7. largest = r
8. if largest ≠ i
9. swap A[i] and A[largest]
10. MAX-HEAPIFY(A, largest)
Time Complexity
BUILD-MAX-HEAP: O(n)
MAX-HEAPIFY: O(log n)
PSEUDOCODE
MIN-HEAPIFY(A, i)
1. l = 2i
2. r = 2i + 1
3. smallest = i
4. if l ≤ heap-size and A[l] < A[smallest]
5. smallest = l
6. if r ≤ heap-size and A[r] < A[smallest]
7. smallest = r
8. if smallest ≠ i
9. swap A[i] and A[smallest]
10. MIN-HEAPIFY(A, smallest)
Time Complexity
BUILD-MIN-HEAP: O(n)
MIN-HEAPIFY: O(log n)
Applications of Heap
Priority Queues
Heap Sort
Graph algorithms like Dijkstra and Prim
********************************************************
2. Deletion Operation in Binary Heap
Steps for Deletion (Delete Root):
1. Replace root element with the last element in the heap.
2. Decrease heap size.
3. Apply heapify (percolate down) from root.
4. Restore heap property.
Algorithm – Delete (Max Heap):
DELETE-MAX(A)
1. if heap_size < 1
2. return error "heap underflow"
3. max = A[1]
4. A[1] = A[heap_size]
5. heap_size = heap_size - 1
6. MAX-HEAPIFY(A, 1)
7. return max
Multi-way Search Tree
Definition:
A Multi-way Search Tree (also called an m-way search tree) is a
generalization of a Binary Search Tree (BST) where each internal node
can have more than two children.
In an m-way search tree:
Each node can contain up to m–1 keys.
It can have at most m children.
Keys are stored in sorted order within each node.
Children are organized such that:
o All values in the i-th child are between the (i–1)th and i-th
key.
Multi-way search trees are efficient structures when multiple
keys need to be stored in sorted order and accessed quickly.
They are widely used in database indexing and file systems.
Properties:
1. Each node with k keys has k + 1 children.
2. Keys in each node are kept in sorted order.
3. All subtrees follow the same multi-way search rule.
4. Efficient for indexing large databases and external memory
access.
Step-by-step Insertion:
1. Insert 10: becomes the root.
2. Insert 20: root = [10, 20]
3. Insert 5: root = [5, 10, 20] (node now has 3 keys)
4. Insert 6: node is full → split
o Middle key 10 promoted
o Left = [5, 6], Right = [20], Root = [10]
5. Insert 12: Goes to right child → [20, 12] → sorted → [12, 20]
6. Insert 30: Right child becomes [12, 20, 30] → Full → split
o Promote 20, left = [12], right = [30], update root = [10, 20]
7. Insert 7: Goes to left child → [5, 6], becomes [5, 6, 7]
8. Insert 17: Goes to middle child → [12], becomes [12, 17]