Trees MCH

Download as pdf or txt
Download as pdf or txt
You are on page 1of 50

TREES

Dr. Maumita Chakraborty


University of Engineering and Management Kolkata
TREES-I
BINARY TREES
 A collection of elements called nodes. Every node contains a "left" pointer, a
"right" pointer, and a data element.
 Has a root element pointed by a "root" pointer to the topmost node in the
tree. If root = NULL, tree is empty.
 If the root node R is not NULL, then the two trees T1 and T2 are the left and
right subtrees of R.
 If T1 is non-empty, then T1 is said to be the left successor of R. Likewise, if T2
is non-empty then, it is called the right successor of R.

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).

Level number: Every node in the binary tree is assigned a level


number. The root node is defined to be at level 0. All child nodes are
defined to have level number as parent’s level number + 1.

 Degree: The number of children that a node has. The degree of a


leaf node is zero.
In-degree of a node is the number of edges arriving at that node.
The root node is the only node that has an in-degree equal to zero.
Out-degree of a node is the number of edges leaving that node.

Leaf node: A leaf node has no children.


KEY TERMS
 Height: Total number of edges from leaf
node to a particular node in the longest
path is called as HEIGHT of that node. In a
tree, height of the root node is said to
be height of the tree. In a tree, height of all
leaf nodes is '0'.
 Depth: Total number of edges from root
node to a particular node is called
as DEPTH of that node. In a tree, the total
number of edges from root node to a leaf
node in the longest path is said to be Depth
of the tree. The highest depth of any leaf
node in a tree is said to be depth of that
tree. In a tree, depth of the root node is '0'.
 If there are n nodes in binary
tree, maximum height of the binary tree
is n-1 and minimum height is floor(log2n).
 Path: The sequence of Nodes and Edges
from one node to another node is called
as PATH between that two Nodes. Length
of a Path is total number of nodes in that
path. In below example the path A - B - E -
J has length 4.
 A complete binary tree is a binary tree which satisfies two
properties.
 First, in a complete binary tree every level, except possibly the
last, is completely filled.
 Second, all nodes appear as far left as possible.

2 3

4 7
5 6

8 9 10 11 13
12
 Full Binary Tree: If
every node (other than
leaves) has 2 children.

 Strictly Binary Tree:


If every node (other
than leaves) has either
0 or 2 children.
 In computer’s memory, a binary tree can be maintained either using
a linked representation (as in case of a linked list) or using
sequential representation (as in case of single arrays).
Linked Representation of Binary Trees
 In linked representation of binary tree, every node will have three
parts: the data element, a pointer to the left node and a pointer to the
right node.
 In C, the binary tree is built with a node type given as below.
struct node {
struct node* left; 1

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

Following operations are performed


recursively at each node. The algorithm D E
starts with the root node of the tree and
continues by: F

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

each node. The algorithm starts with the root node


G
of the tree and continues by:

 Traversing the left subtree. H I


 Traversing the right subtree.
 Visiting the parent node. D, B, H, I, G, F, E, C and A
Non-recursive In-order Traversal
 nonRcrsvInorder(stack *s, bNode *T)
1. Start
2. while(1)
1. while(T≠NULL)
1. push (s, T)
2. T := T->left
2. End while
3. if (isEmpty(s))
1. return
4. End if
5. T := pop(s)
6. Print (T->data)
7. T := T->right
3. End while
TREES-II
Binary Search Trees
BINARY SEARCH TREES
 A variant of binary tree in which the nodes are arranged in order such that
 All the nodes in the left sub-tree have a value less than that of the root node,
and all the nodes in the right sub-tree have a value either equal to or greater
than the root node. The same rule is applicable to every sub-tree in the tree.
 At every step, we eliminate half of the sub-trees from the search process. Due to
its efficiency in searching elements, binary search trees are widely used in
dictionary problems where the code always inserts and searches the elements
that are indexed by some key value.
39

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.

Algorithm to search in a BST

Step 1: IF ROOT->DATA = VAL


Step 2: Return 1
Step 3: ELSE IF ROOT=NULL
Step 4: Return 0
Step 5: ElSE IF VAL < ROOT->DATA
Step 6: Return SEARCH(ROOT->LEFT, VAL)
Step 7: ELSE
Step 8: Return SEARCH(ROOT->RIGHT, VAL)
Step 9: END IF
Step 10: End
Algorithm to insert a value in the binary search tree-

Step 1: IF(ROOT->DATA > NEW_NODE->DATA)


Step 2: IF(ROOT->LEFT = NULL)
Step 3: ROOT->LEFT = NEW_NODE
Step 4: ELSE
Step 5: INSERT_TREE(ROOT->LEFT, NEW_NODE)
Step 6: END IF
Step 7: ELSE
Step 8: IF(ROOT->RIGHT = NULL)
Step 9: ROOT->RIGHT = NEW_NODE
Step 10: ELSE
Step 11: INSERT_TREE(ROOT->RIGHT, NEW_NODE)
Step 12: END IF
Step 13: END IF
Step 14: END
 During deletion, utmost care should be taken that the properties of the binary
search tree does not get violated and nodes are not lost in the process. The
deletion of a node can be done in any of the three cases.

 Case 1: Deleting a node that has no children.

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)

1. If ((loc->left=NULL) & (loc->right=NULL))


2. child=NULL
3. Else if (loc->left ≠ NULL)
4. child := loc->left
5. Else
6. child := loc->right
7. End if
8. If (parent ≠ NULL)
1. If (loc = parent->left)
2. parent->left := child
3. Else
4. parent->right :=child
9. Else
10. Root := child
11. End if
12. loc->right := loc->left := NULL
13. Free(loc)
14. Return Root
Case 3: Deleting a node with two children. Replace the node’s value with
its in-order successor (leftmost child of the right sub-tree). The in-order
successor can then be deleted using any of the previous cases.

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)

Deletion_C (root, loc, parent)

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.

 Also known as a height-balanced tree.

 Advantage: It takes O(logn) time to perform search, insert


and delete operations in average case as well as worst
case (because the height of the tree is limited to O(logn).

 The structure of an AVL tree is same as that of a binary


search tree but with a little difference. In its structure, it
stores an additional variable called the BalanceFactor.
 Balance factor = Height (left sub-tree) – Height
(right sub-tree)
 A BST where every node has a balance factor of -1,
0 or 1. A node with any other balance factor is
considered to be unbalanced and requires
rebalancing the tree.
 If the balance factor of a node is more than 1, then
the tree is called Left-heavy tree.
 If the balance factor of a node is 0, then it means
that the height of the left sub-tree (longest path in
the left sub-tree) is equal to the height of the right
sub-tree.
 If the balance factor of a node is less than -1, then
the tree is called Right-heavy tree.
4 -4
82 14
0
1 -1
45
45 45
0
1 0 -1 0
36
63
36 63 0 36 63
0 0 0
1
0 0 0 0 0
27 39
1 27 27 54 72
72
39 54 72 39 54
0
0 0
0 18 Right heavy AVL tree 70 Balanced AVL tree

Left heavy AVL tree

Search Operation

Searching in an AVL tree is performed exactly the same way as it is


performed in a binary search tree. Because of the height-balancing of the
tree, the search operation takes O(log n) time to complete. Since the
operation does not modify the structure of the tree, no special provisions
needs to be taken.
 As in binary search tree, the new node is always inserted
as the leaf node. But the step of insertion is usually
followed by an additional step of rotation.
 Rotation is done to restore the balance of the tree.
However, if insertion of the new node does not disturb the
balance factor, that is, if the balance factor of every node is
still -1, 0 or 1, then rotations are not needed.
 During insertion, the new node is inserted as the leaf node,
so it will always have balance factor equal to zero. The only
nodes whose balance factors will change are those which
lie on the path between the root of the tree and the newly
inserted node.
 To perform rotation, our first work is to find the critical node. Critical node is
the nearest ancestor node on the path from the root to the inserted node
whose balance factor is neither -1, 0 nor 1.
 The second task in rebalancing the tree is to determine which type of
rotation has to be done. There are four types of rebalancing rotations and
application of these rotations depends on the position of the inserted node
with reference to the critical node.:
 LL rotation: if the left child of the critical node is left-heavy; then, a right
rotation is applied to the critical node.
 RR rotation: if the right child of the critical node is right-heavy; then, a left
rotation is applied to the critical node.
 LR rotation: if the left child of the critical node is right-heavy; then, a left
rotation to the left sub-tree followed by a right rotation to the critical node
is applied.
 RL rotation: if the right child of the critical node is left-heavy; then, a right
rotation to the right sub-tree followed by a left rotation to the critical node
is applied.
Example: Consider the AVL tree given below and insert 9 into it.
1
45 2
1 1
45
2 45
0 1
36 63
36 63 0
1 0 36 63 0
0 2 0
27 0 0 0
39 54 72 27
39 54 72 18
39 54 72
1 0
0 0
18 0 0 0
18
9 27
0
0
9

The tree is balanced using LL rotation

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

The tree is balanced using RR rotation


Consider the AVL tree given below and insert 37 into it.

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

The tree is balanced using LR rotation


Consider the AVL tree given below and insert 51 into it.
-1 -2
45 0
45
0 54
0 0 0
36 63 1
36 63
45 63 -1
0 1
72 0
0 54 54 72 0

36 51 72 0
0
51
0

The tree is balanced using RL rotation


 Deletion of a node in the AVL tree is similar to that of BST. But it may disturb the
AVLness of the tree, so to re-balance the AVL tree we need to perform rotations.
There are two classes of rotation that can be performed on an AVL tree after
deleting a given node. These rotations are- R rotation and L rotation.
 If the node to be deleted is present in the left sub-tree of the critical node, then L
rotation is applied. Else, if it is on the right sub-tree, R rotation is performed.
 Further, there are three categories of L and R rotations. The variations of L
rotation are: L-1, L0 and L1 rotation. Correspondingly for R rotation, there are
R0, R-1 and R1 rotations.
R0 Rotation
 Let B be the root of the left or right sub-tree of A (critical node). R0 rotation is
applied if the balance factor of B is 0. Consider the AVL tree given below and
delete 30 from it.
R1Rotation
Let B be the root of the left or right sub-tree of the critical node. R1 rotation is applied if
the balance factor of B is 1. Consider the AVL tree given below and delete 55 from it.

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:

 Every node in the B-tree has at most (maximum) m children.


 Every node in the B-tree except the root node and leaf nodes have at
least (minimum) m⁄2 children.
 The root node has at least two children if it is not a terminal (leaf) node.
 All leaf nodes are at the same level.
 An internal node in the B tree can have n number of children, where 0
≤n ≤ m. it is not necessary that every node has the same number of
children, but the only restriction is that the node should have at least
m/2 children.
O 45 O

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

You might also like