Trees MCH
Trees MCH
Trees MCH
1 ROOT NODE
In a binary tree every node has 0, 1 or at
T1
2 3
T2
the most 2 successors.
4
A node that has no successor is the leaf
5 6 7
node or the terminal node.
8
9 10 1 12
1
Sibling: All nodes that are at the same level and share the same
parent are called siblings (brothers).
2 3
4 7
5 6
8 9 10 11 13
12
Full Binary Tree: If
every node (other than
leaves) has 2 children.
int data;
struct node* right; 2 3
};
4 5 6 7
X 8 X X 9 X X 10 X X 11 X X 12 X
Done using single or one dimensional array.
Simplest technique for memory
representation but very inefficient as it
requires a lot of memory space. A sequential
binary tree follows the rules given below:
One dimensional array called TREE, will be
used.
The root of the tree will be stored in the first
location. That is, TREE[0] will store the data of
the root element.
The maximum size of the array TREE is given
as (2d+1-1), where d is the depth of the tree.
An empty tree or sub-tree is specified using
NULL. If TREE[0] = NULL (or default value),
then the tree is empty.
Binary trees are widely used to store algebraic expressions. For
example, consider the algebraic expression Exp given as,
Exp = (a – b ) + ( c * d)
This expression can be represented using a binary tree as shown in
figure below:
- *
a b c d
Process of visiting each node in the tree exactly once, in a systematic way.
Tree, being a non-linear data structure, the elements can be traversed in many
different ways.
Different algorithms for tree traversals differ in the order in which the nodes are
visited.
Pre-order traversal
B C
G
Visiting the parent node.
Traversing the left subtree.
Traversing the right subtree. H I
A, B, D, C, E, F, G, H and I
In-order traversal
Following operations are performed recursively at each node. The algorithm
starts with the root node of the tree and continues by:
A
Traversing the left subtree.
Visiting the parent node.
B C
Traversing the right subtree.
B, D, A, E, H, G, I, F AND C
D E
Post-order traversal
Following operations are performed recursively at F
27 45
18
29 40 54
9 21 28 36 59
10 19 65
60
Searching a Value in BST
Search algorithm checks to see if the key value of the current node is equal
to the value to be searched. If not, it checks if the value to be searched for is
less than the value of the node, in which case it should be recursively called
on the left child node. In case the value is greater than the value of the node,
it should be recursively called on the right child node.
45 45
39 56 39 56
54 54
78
55
55
Case 2: Deleting a node with one child (either left or right). Replace the
node with its child. Now, if the node was the left child of its parent, the node’s
child becomes the left child of the node’s parent. Similarly, if the node was the
right child of its parent, the node’s child becomes the right child of the node’s
parent.
45 45
39 56 39 56
54 78 55 78
55
Algorithm for deletion of a node in BST (Case 1 and Case 2)
Deletion_AB (root, loc, parent)
45 45 45 45
39 56 39 56 39 74 39 74
54 78 54 78 54 78 54 78
55 74 55 74 55 74 55
Algorithm for deletion of a node in BST (Case 3)
1. in_suc := loc->right
2. par_in_suc := loc
3. While (in_suc->left ≠ NULL)
4. par_in_suc := in_suc
5. in_suc := in_suc->left
6. End while
7. loc->data := in_suc->data
8. Deletion_AB(root, in_suc, par_in_suc)
9. Return root
Algorithm to delete a value in the binary search tree
Step 1: IF ROOT = NULL, then
Step 2: RETURN NULL
Step 3: ELSE IF VAL < ROOT->DATA
Step 4: ROOT->LEFT = Delete(ROOT->LEFT, VAL)
Step 5: ELSE IF VAL > ROOT->DATA
Step 6: ROOT->RIGHT = Delete(ROOT->RIGHT, VAL)
Step 7: ELSE
Step 8: IF ROOT->LEFT = NULL, then
Step 9: SET TEMP = ROOT->RIGHT
Step 10: FREE (ROOT)
Step 11: RETURN TEMP
Step 12: ELSE IF ROOT->RIGHT = NULL
Step 13: TEMP = ROOT->LEFT
Step 14: FREE (ROOT)
Step 15: RETURN TEMP
Step 16: END IF
Step 17: TEMP = Inorder_Successor(ROOT->RIGHT)
Step 18: ROOT->DATA = TEMP->DATA
Step 19: ROOT->RIGHT = Delete(ROOT->RIGHT, TEMP->DATA)
Step 20: END IF
Step 21: RETURN ROOT
Step 22: End
Space that is wasted in a binary tree in storing a NULL pointer in the linked
representation of trees can be efficiently used to store some other useful piece
of information, like the in-order predecessor, or the in-order successor of the
node.
These special pointers are called threads and binary trees containing threads
are called threaded trees. In the linked representation of a threaded binary tree,
threads will be denoted using dotted lines.
Presence of a thread indicates absence of that corresponding child.
If a node has no left child, then the thread appears in the left field, and the left
field will be made to point to the in-order predecessor of the node.
On the contrary, if a node has no right child, then the thread appears in the right
field, and it will point to the in-order successor of the node.
If a node has no left and right child, then threads appear in both left and right
field pointing to the in-order predecessor and successor of the node
respectively.
Similarly, we can have pre-order as well as post-order threaded tree.
1 In-order Threaded Binary tree
2 3
4
5 6 7
8 9 10 11 12
1
2 3
4 5 6 7
X 8 9 10 11 12 X
Structure of a node and its In-order
Successor in a Binary Threaded Tree
typedef struct inorderSuccessor
{ (bThreadNode *root)
int rightFlag, leftFlag; 1. Start
char data; 2. if (root->rightFlag=1)
struct bThreadNode *left, *right; 1. return (root->right)
} 3. End if
4. root=root->right
5. while (root->leftFlag=0)
Left Left Flag Data Right Right 1. root := root->left
Flag 6. End while
7. return root
8. Stop
Generation of In-order Threaded Tree
inorderThreadedTree(bThreadNode *root)
1. Start
2. x := root
3. while(x->leftFlag=0)
1. x := x->left
4. End while
5. while(x≠NULL)
1. print (x->data)
2. x := inorderSuccessor(x)
6. End while
7. Stop
Generation of Pre-order Threaded Tree
preorderThreadedTree(bThreadNode *root)
1. Start
2. X := root
3. while(x ≠NULL)
1. Print (x->data)
2. if (leftFlag=0)
1. x := x->left
3. Else if (x->rightFlag=0)
1. x := x->right
4. Else
1. while ((x ≠NULL) and (x->rightFlag=1))
1. x := x->right
2. End while
3. if (x ≠NULL)
1. x := x->right
4. End if
5. End if
4. Endwhile
5. Stop
TREES-III
Self-balancing binary search tree in which the heights of
the two sub-trees of a node may differ by at most one.
Search Operation
Consider the AVL tree given below and insert 91 into it.
-1 -2 -1
45 45
45 0 0
0
36 63 -2 -1
-1 36 63
36 63
0 0 0
0 0 27 -2 27
27 -1 39 54 72 39 54 89
0
39 54 72 0
0 0 -1
0 89 0 0
0 72 91
89 0
91
2
1 0
45 45 39
0 0
-1
0
36 63 36 63 36 45 -1
0 0
27 27 27
39 0 39 37 63
1 0 0
0
0
0 37
36 51 72 0
0
51
0
R-1Rotation
Let B be the root of the left or right sub-tree of the (critical node. R-1 rotation is applied if
the balance factor of B is -1. Consider the AVL tree given below and delete 60 from it.
In such a tree M is called the order of the tree. Note in a binary search tree M
= 2, so it has one value and 2 sub-trees. In other words, every internal node of
an M-way search tree consists of pointers to M sub-trees and contains M – 1
keys, where M > 2.
……
P0 K0 P1 K1 P2 K2 Pn-1 Kn-1 Pn
…
In the structure, P0, P1, P2, …, Pn are pointers to the node’s sub-trees and K0, K1,
K2,…, Kn-1 are the key values of the node. All the key values are stored in
ascending order. That is, Ki < Ki+1 for 0 ≤ i ≤ n-2.
In an M-way search tree, it is not compulsory that every node has exactly (M-1)
values and have exactly M sub-trees. Rather, the node can have anywhere from 1
to (M-1) values, and the number of sub-trees may vary from 0 (for a leaf node) to 1
+ i, where i is the number of key values in the node. M is thus a fixed upper limit
that defines how much key values can be stored in the node.
18 45
9 11 27 36 54 63
29 30 72 81
Using the above 3-way search tree, let us devise some basic properties of an M-
way search tree.
All key values in the sub-tree pointed by Pi are less than Ki, where 0 ≤ i ≤ n-1.
All key values in the sub-tree pointed by Pi are greater than Ki-1, where 0 ≤ i ≤ n-
1.
In an M-way search tree, every sub-tree is also an M-way search tree and
follows the same rules.
A specialized m-way tree. A B tree of order m can have maximum m-1 keys
and m pointers to its sub-trees.
A B-tree is designed to store sorted data and allows search, insert, and
delete operations to be performed in logarithmic time. A B-tree of order m
(the maximum number of children that each node can have) is a tree with all
the properties of an m-way search tree and in addition has the following
properties:
O 29 O 32 O
O 49 O 63 O
O 54 O 59 O 61 O
O 30 O 31 O O 67 O 72 O
O 36 O 39 O
O 18 O 27 O
O 46 O 47 O
Insert Operation
In a B tree all insertions are done at the leaf node level.
1. Search the B tree to find the leaf node where the new key value should be
inserted.
2. If the leaf node is not full, that is it contains less than m-1 key values, then
insert the new element in the node, keeping the node's elements ordered.
3. if the leaf node is full, that is the leaf node already contain m-1 key values,
then insert the new value in order into the existing set of keys.
4. Split the node at its median into two nodes. Note that the split nodes are half
full.
5. Push the median element up to its parent’s node.
6. If the parent’s node is already full, then split the parent node by following the
same steps.
Example: Look at the B tree of order 5 given below and insert 8, 9, 39 into it.
O 18 O 45 O 72 O
O 7 O 11 O O 21 O 27 O 36 O 42 O O 81 O 89 O 90 O
O 54 O 63 O
O 18 O 45 O 72 O
O 21 O 27 O 36 O 42 O O 54 O 63 O O 81 O 89 O 90 O
O 7 O 8 O 11 O
O 18 O 45 O 72 O
O 81 O 89 O 90 O
O 54 O 63 O
O 7 O 8 O 9 O 11 O O 21 O 27 O 36 O 42 O
O 18 O 36 O 45 O 72 O
O 81 O 89 O 90 O
O 7 O 8 O 9 O 11 O O 21 O 27 O O 39 O 42 O
O 54 O 63 O
Like insertion, deletion is also done from the leaf nodes. There are two cases
of deletion. First, a leaf node has to be deleted. Second, an internal node has
to be deleted. Let us first see the steps involved in deleting a leaf node.
1. Locate the leaf node which has to be deleted
2. If the leaf node contains more than minimum number of key values (that is,
m/2 elements), then delete the value.
3. Else, if the leaf node does not contain even m/2 elements, then fill the
node by taking an element either from the left or from the right sibling
4. Else, if both left and right siblings contain only minimum number of
elements, then create a new leaf node by combining the two leaf nodes and
the intervening element of the parent node (ensuring that the number of
elements do not exceed the maximum number of elements a node can have,
that is, m). If pulling the intervening element from the parent node leaves it
with less than minimum number of keys in the node, then propagate the
process upwards thereby reducing the height of the B tree.
To delete an internal node, promote the successor or predecessor of
the key to be deleted to occupy the position of the deleted key. This
predecessor or successor will always be in the leaf node. So further
processing will be done as if a value from the leaf node has been deleted.
O 108 O
O 117 O 201 O
O 63 O 81 O
O 151 O 180 O
O 36 O 45 O O 90 O 93 O 101 O
O 243 O 256 O 333 O 450 O
O 72 O 79 O
O 111 O 114 O
O 108 O
O 117 O 243 O
O 63 O 81 O
O 151 O 180 O
O 36 O 45 O O 90 O 101 O
O 256 O 333 O 450 O
O 111 O 114 O
O 72 O 79 O
A variant of a B tree which stores sorted data in a way that allows for
efficient insertion, retrieval and removal of records, each of which is
identified by a key. While a B tree can store both keys and records in its
interior nodes, a B+ tree, in contrast, stores all records at the leaf level of
the tree; only keys are stored in interior nodes.
The leaf nodes of the B+ tree are often linked to one another in a linked
list.
B+-tree stores data only in the leaf nodes. All other nodes (internal nodes)
are called index nodes or i-nodes and store index values which allow us to
traverse the tree from the root down to the leaf node that stores the
desired data item.
2-3 Trees
Every internal node (non-leaf node) has either one
data element and two children or two data
elements and three children.
Height of 2-3 trees is always O(log n).
In a 2-3 tree of height h
If all non-leaf nodes have 3 children, then total number of
elements in the tree is 3h+1-1.
If all non-leaf nodes have 2 children, then total number of
elements in the tree is 2h+1-1.
In a 2-3 tree with n elements having height h,
2h+1-1 ≤ n ≤ 3h+1-1 and
log2 (n+1) ≤ h ≤ log3 (n+1).
Example of 2-3 Tree
THANK YOU