2. Trees:
• Definition of Tree
• Properties of Tree
• Binary Tree
• Representation of Tree using array and linked lists
• Operations on binary tree
• Binary tree traversal (recursive)
• Binary Search Tree (BST)
• B-Tree
• B+ Tree
• AVL Tree
• Threaded Binary Tree
3. Tree:
It is non-linear data structure.
It represents a hierarchical data structure.
It represents a parent child relationship between various pieces of data and thus allow us to arrange our
records of data and files in a hierarchical fashion.
e.g. Family tree
It is depicted upside down with root at the top and leaves at bottom.
The root is a node having no parent, It can have only a child nodes.
Leaves have no children.
A single node by itself is a tree. Root node
Root node
Edge
Leaf nodes or external nodes
Parent node
Subtree
Siblings
4. Each node is reachable from root, by having a path from root to leaf.
Tree is a collection of elements called nodes. So node is the main component of tree. It stores he actual data
along with link to other nodes.
Binary Tree:
A binary tree is a tree in which each node has at most two children i.e the left child and right child.
Data
Left child Right child
Binary Tree
A
B C
D E F
5. Properties of Binary Tree:
1. The number of external nodes is one more than the number of internal nodes.
2. The number of external nodes is at least h+1, h is the height of tree and at most 2h.
3. The number of internal nodes is at least h, and at most 2h-1.
4. The total number of nodes in binary tree is at least h+1 and at most 2h+1-1
5. A binary tree with n nodes has exactly (n-1) edges
A
B C
D E F
6. Representation of binary tree:
1. Using array (linear)
2. Using linked list (link)
Array representation:
In this we use one dimensional array to represent a binary tree. The elements of tree are stored level by level
starting from the root node (root node is at level 0).
• The root of the tree will be in position 1 of the array (i.e. at position 0)
• The left child of a node at position n is at position 2n. (n=1, 2*1=2)
• The right child of a node at position n is at position 2n+1 (n=1, 2*1+1=3)
• The parent of a node at position n is at position n/2
B C
D F
A
E
Root node at level 0
A B C D E F
0
1
1 2 3 4 5 6
2 3 4 5 6 7
7. Linked list representation of binary tree:
In this we use double linked list to represent a binary tree. In double linked list the first field for storing left child
address, second field for storing data and third for storing right child address.
Data
Left child Right child
1008 1210
A
1004 NULL
B 1300 1018
C
NULL NULL
D NULL NULL
E NULL NULL
F
1000 root node
8. Operations on binary tree:
1. Insertion
2. Deletion
3. Traversal
Binary tree traversal:
It is the process of accessing(visiting) every node of the tree exactly once. A tree is defined in a recursive in a
recursive manner. Binary tree traversal are also defined recursively.
During creation we use three parameters Node(N), left subtree (L), right subtree (R).
There are three techniques for traversal:
1. Preorder traversal (NLR)
2. Inorder traversal (LNR)
3. Postorder traversal(LRN)
Preorder(NLR): -Visit the Node (N)
-Traverse the left subtree(L)
-Traverse the right subtree(R)
Inorder(LNR): -Traverse the left subtree(L)
-Visit the Node (N)
-Traverse the right subtree(R)
Postorder(LRN): -Traverse the left subtree(L)
-Traverse the right subtree(R)
-Visit the Node (N)
9. B C
D F
A
E
G
e.g. Binary tree
Preorder(NLR): A B D E G C F
B C
D F
A
E
G
Inorder(LNR): D B E G A F C
1
2
3
4
5
6
7
B C
D F
A
E
G
Postorder(LRN): D G E B F C A
1
2
3
4
5
6
7
10. There are two different ways of creating binary tree using traversals:
1. Preorder and inorder traversals
2. Postorder and inorder traversals
e.g. draw binary tree using following traversals:
Preorder(NLR): A B D H E C F G
Inorder (LNR): D H B E A F C G
Using preorder we determine which is the root node because root is the first node to be traversed so A is the
root node.
Using Inorder we determine the nodes to the left of root and the nodes to the right of root (i.e. LNR)
Preorder(NLR): A B D H E C F G
Inorder (LNR): D H B E A F C G
A
D H B E F C G
11. A
F C G
B
E
D H
Preorder(NLR): A B D H E C F G
Inorder (LNR): D H B E A F C G
A
F C G
B
H
D E
A
B
H
D E
C
F G
12. a). Preorder : F A E C K D H G B
Inorder : E A C K F H D B G
b). Postorder : H D I E B J F K L G C A
Inorder : H D B I E A F J C K G L
c). Inorder : B C A E G D H F I J
Preorder : A B C D E G F H I J
13. Binary Search Tree(BST):
Binary Search Tree is a binary tree data structure in which the nodes are arranged in a specific order i.e. value at
a node is greater than every value to the left of that node and less than every value to the right of that node.
BST properties:
• No two elements have same key
• The left subtree of a node contains only nodes with keys lesser than the node’s key.
• The right subtree of a node contains only nodes with keys greater than the node’s key.
• The left and right subtree each must also be a binary search tree.
BST operations: -Searching : Finding the location of some specific element in a binary search tree.
-Insertion : Adding a new element to the binary search tree at the appropriate location so
that the property of BST do not violate.
-Deletion : Deleting some specific node from a binary search tree. However, there can be
various cases in deletion depending upon the number of children, the node have.
20
>20
<20
A
B C
D E F
14. Create the binary search tree using the following data elements:
45, 21, 85, 99, 10, 64, 19
1.Insert 45 into the tree as the root of the tree.
2.Read the next element, if it is lesser than the root node element, insert it as the root of the left sub-tree.
3.Otherwise, insert it as the root of the right of the right sub-tree.
Step-1
45 45
Step-2
21
45
21
Step-3
85
45
21
Step-4
85
99
45
21
Step-5
85
99
10
45
21
Step-6
85
99
10 64
45
21
Step-7
85
99
10 64
19
17. Deletion:
To delete a node from BST, there are three cases:
1. Node having no children (leaf node)
2. Node having one children (right child or left child)
3. Node having two children
Case 1: Leaf node
If the node is leaf (both left and right will be NULL), remove the node directly and free its memory.
45
21 85
99
10 64
delete 64
45
21 85
99
10
18. Case 2: Node having one children (right child or left child)
If the node has only right child (left will be NULL), make the node points to the right node and free the
node.
If the node has only left child (right will be NULL), make the node points to the left node and free the node.
45
21 85
99
10 64
115
delete 99
45
21 85
10 64 115
19. Case 3: If the node has both left and right child
1. Find the smallest node in the right subtree. say min
2. Make node->data = min
3. Delete the min node.
45
21 85
10 64 115
delete 45
64
21 85
10 115
21. AVL Tree:
It is named after their inventor Adelson, Velski & Landis. It is height balanced tree. It is a binary search tree
that has an additional balance condition. AVL tree checks the height of the left and the right sub-trees and
assures that the difference is not more than 1. This difference is called the Balance Factor.
For an AVL tree, the value of balance factor of any node is -1, 0 or 1. If it is other than these three values then
the tree is not balanced or it is not AVL tree
Balance Factor = height(left-sutree) − height(right-sutree)
In the second tree, the left subtree of C has height 2 and the right subtree has height 0, so the difference is 2. In
the third tree, the right subtree of A has height 2 and the left is missing, so it is 0, and the difference is 2 again.
AVL tree permits difference (balance factor) to be only 1.
B
A C
C
B
A
A
B
C
0
0 0
Balanced Not balanced Not balanced
0
1
2
0
-1
-2
22. If the difference in the height of left and right sub-trees is more than 1, the tree is balanced using some rotation
techniques.
AVL Rotations
To balance itself, an AVL tree may perform the following four kinds of rotations −
•Single Left rotation(LL rotation)
•Single Right rotation(RR rotation)
•Left-Right rotation
•Right-Left rotation
The first two rotations are single rotations and the next two rotations are double rotations.
Left Rotation:
If a tree becomes unbalanced, when a node is inserted into the right subtree of the right subtree, then we
perform a single left rotation −
A
B
C
0
-1
-2 A
B
C
B
A C
0
0 0
Right unbalanced Left rotation Balanced
23. Right Rotation:
AVL tree may become unbalanced, if a node is inserted in the left subtree of the left subtree. The tree then
needs a right rotation.
The unbalanced node becomes the right child of its left child by performing a right rotation.
A
B
C
0
1
2
B
A C
0
0 0
Left unbalanced Right rotation
Balanced
A
B
C
24. Left-Right Rotation:
Double rotations are slightly complex. In this unbalance occurred due to the insertion of node in the right subtree
of the left subtree of node. It involves two rotations-left rotation followed by right rotation.
A node has been inserted into the right
subtree of the left subtree. This
makes C an unbalanced node. These
scenarios cause AVL tree to perform
left-right rotation.
We first perform the left rotation on
the left subtree of C. This makes A,
the left subtree of B.
Node C is still unbalanced,
however now, it is because
of the left-subtree of the
left-subtree.
We shall now right-rotate the tree,
making B the new root node of this
subtree. C now becomes the right
subtree of its own left subtree.
The tree is now balanced.
C
A
B
2
-1
0
C
A
B
C
B
A
2
1
0
C
B
A
B
A C
0
0 0
25. Right-Left Rotation
The second type of double rotation is Right-Left Rotation. In this unbalance occurred due to the insertion of
node in the left subtree of the right subtree of node. It is a combination of right rotation followed by left
rotation.
A node has been inserted
into the left subtree of the
right subtree. This makes A,
an unbalanced node with
balance factor 2.
First, we perform the right rotation
along C node, making C the right subtree of
its own left subtree B. Now, B becomes the
right subtree of A.
Node A is still
unbalanced because of
the right subtree of its
right subtree and
requires a left rotation.
A left rotation is performed by
making B the new root node
of the subtree. A becomes the
left subtree of its right
subtree B.
The tree is now balanced.
A
C
B
-2
1
0
A
C
B
A
B
C
0
-1
-2 A
B
C
B
A C
0
0 0
26. Construct the AVL tree for the following set of data:
50 , 20 , 60 , 10 , 8 , 15 , 32 , 46 , 11 , 48
Step-01: Insert 50 Step-02: Insert 20
•As 20 < 50, so insert 20 in 50’s left sub tree.
Step-03: Insert 60
•As 60 > 50, so insert 60 in 50’s right sub tree.
Step-04: Insert 10
•As 10 < 50 , so insert 10 in 50’s left sub tree.
•As 10 < 20, so insert 10 in 20’s left sub tree.
0
0
1
0
0 0
0
0
1
1
50 50
20
Tree is balanced
Tree is balanced
50
20 60
Tree is balanced
50
20 60
10
Tree is balanced
27. Step-05: Insert 8
•As 8 < 50, so insert 8 in 50’s left sub tree.
•As 8 < 20, so insert 8 in 20’s left sub tree.
•As 8 < 10, so insert 8 in 10’s left sub tree.
0
0
1
2
2
To balance the tree,
•Find the first imbalanced node on the path from the newly inserted node (node 8) to the root node.
•The first imbalanced node is node 20.
•Now, count three nodes from node 20 in the direction of leaf node.
•Then, use AVL tree rotation to balance the tree.
0
0
0
0
1
50
20 60
10
8
Tree is imbalanced
50
20 60
10
8
50
10 60
8 20
Tree is imbalanced Tree is balanced
RR rotation
28. Step-06: Insert 15
•As 15 < 50, so insert 15 in 50’s left sub tree.
•As 15 > 10, so insert 15 in 10’s right sub tree.
•As 15 < 20, so insert 15 in 20’s left sub tree.
0
0
0
1
-1
2
To balance the tree,
•Find the first imbalanced node on the path from the newly inserted node (node 15) to the root node.
•The first imbalanced node is node 50.
•Now, count three nodes from node 50 in the direction of leaf node.
•Then, use AVL tree rotation to balance the tree. 0
0
-1
0
0
0
50
10 60
8 20
15
Tree is imbalanced
50
10 60
8 20
15
20
10 50
60
15
8
LR rotation
Tree is imbalanced Tree is balanced
29. Step-07: Insert 32
•As 32 > 20, so insert 32 in 20’s right sub tree.
•As 32 < 50, so insert 32 in 50’s left sub tree.
Step-08: Insert 46
•As 46 > 20, so insert 46 in 20’s right sub tree.
•As 46 < 50, so insert 46 in 50’s left sub tree.
•As 46 > 32, so insert 46 in 32’s right sub tree.
0
0
0 0
0
0 0
20
10 50
60
15
8 32
Tree is balanced
0
0
0
20
10 50
60
15
8 32
0
46
0
0
-1
1
Tree is balanced
30. Step-09: Insert 11
•As 11 < 20, so insert 11 in 20’s left sub tree.
•As 11 > 10, so insert 11 in 10’s right sub tree.
•As 11 < 15, so insert 11 in 15’s left sub tree.
Step-10: Insert 48
•As 48 > 20, so insert 48 in 20’s right sub tree.
•As 48 < 50, so insert 48 in 50’s left sub tree.
•As 48 > 32, so insert 48 in 32’s right sub tree.
•As 48 > 46, so insert 48 in 46’s right sub tree.
0
0
0
0
0
1
0
0
0
1
-1
-1
-2
2
-1
-1
20
10 50
60
15
8 32
46
Tree is balanced
11
0
1
-1
20
10 50
60
15
8 32
46
11
0
48
Tree is imbalanced
31. To balance the tree,
•Find the first imbalanced node on the path from the newly inserted node (node 48) to the root node.
•The first imbalanced node is node 32.
•Now, count three nodes from node 32 in the direction of leaf node.
•Then, use AVL tree rotation to balance the tree.
This is the final balanced AVL tree after inserting all the given elements.
0
0
0
0
0
0
1
-1
20
10 50
60
15
8 32
46
11
48
20
10 50
60
15
8
32
46
11
48
0
1
1
Tree is balanced
LL rotation
32. Deletion Operation(AVL tree):
The deletion of a node in AVL is exactly as the deletion of a node from BST.
• Search for the node to be deleted
• After deletion check the balance factor of each node
• If the tree is unbalance after deletion, to balance again AVL rotations are used
33. Delete the node 30 from the AVL tree 20
10 30
15
8
0 0
0 0
1
20
30
15
8
10
Node to be deleted
0 0
0 0
1
Deleting node 30
20
15
8
10
0 0
0
2
Performing R0
rotation
Critical node
20
8
10
15
0
0 1
-1
34. B-tree:
It is a self-balanced search tree in which every node contains multiple keys and has more than
two children.
Insertion of node increases the height of tree. Access time of a tree s totally dependent on the
level of the tree. So we minimize the access time through balance tree only.
There is a need to take all the leaf nodes at the same level and non-leaf nodes not contain
empty subtree.
A B-tree is also known as balanced M-way tree. It is used in external sorting.
For order n , maximum number of children be n and each node contain k keys, where k<=n-1
For balancing each node contains n/2 keys (except root)
20 30 20 55 80 20
Three way
n=3, k=n-1=2
Four way
n=4, k=n-1=3
Two way
n=2, k=n-1=1
35. B-tree is a special type of search tree in which a node contains more than one value and
more than two children
B-Tree of Order n has the following properties...
• All leaf nodes must be at same level.
• All nodes except root must have at least [n/2]-1 keys and maximum of n-1 keys.
• All non leaf nodes except root (i.e. all internal nodes) must have at least n/2 children.
• If the root node is a non leaf node, then it must have atleast 2 children.
• A non leaf node with n-1 keys must have n number of children.
• All the key values in a node must be in Ascending Order.
Application of B-tree:
The main application of B-tree is the organization of huge collection of records into a file
structure. In this insertion, deletion and modification can be carried out perfectly and
efficiently.
36. 20
30 40
5 10
4 6 8 12 14 25 38 42 68
All leaf nodes are at same level.
All non-leaf nodes have no empty subtree and they have keys 1 less than the number of
children
.
37. Operations on a B-Tree
The following operations are performed on a B-Tree...
1.Insertion
2.Deletion
3.Search
Insertion Operation in B-Tree
In a B-Tree, a new element must be added only at the leaf node. That means, the new key Value is always
attached to the leaf node only.
The insertion operation is performed as follows:
Step 1 - Check whether tree is Empty.
Step 2 - If tree is Empty, then create a new node with new key value and insert it into the tree as a root node.
Step 3 - If tree is Not Empty, then find the suitable leaf node to which the new key value is added using Binary
Search Tree logic.
Step 4 - If that leaf node has empty position, add the new key value to that leaf node in ascending order of key
value within the node.
Step 5 - If that leaf node is already full, split that leaf node by sending middle value to its parent node. Repeat
the same until the sending value is fixed into a node.
Step 6 - If the splitting is performed at root node then the middle value becomes new root node for the tree and
the height of the tree is increased by one.
40. e.g.
Construct a B-tree of order 5 with following data:
D, H, Z, K, B, P, Q, E, A, S, W, T, C
41. B+Tree:
It is an extension of B-tree which allows efficient insertion, deletion and search operations.
In B-tree keys are stored in internal as well as external(leaf) nodes, whereas in B+ tree data(key) can be stored
on the leaf nodes.
A B+Tree is called a balanced tree because all leaves are on the same level and every path from a root to leaf
is of same length.
The main drawback of B-tree is the difficulty of traversing the keys sequentially. B+Tree retains the rapid
access property of the B-Tree, allowing rapid sequential access.
In B+Tree all keys are maintained in leaves and keys are replicated in non-leaf nodes to define the path for
locating individual records. The leaves are linked together to provide a sequential path for traversing the keys
in the tree.
B+Tree are used to store large amount of data which cannot be stored in main memory (size of main memory
is limited). The internal nodes(key to access records) whereas leaf nodes are stored in secondary memory.
The internal nodes of B+Tree are called index nodes.
42. B+Tree:
It is a structure of nodes linked by pointers.
There is a unique path to each leaf and all paths are of equal length.
Stores keys only at leaves and stores reference values in other internal nodes.
Less disk access time due to fever levels in the tree.
It provides faster sequential access of data.
50
35 70
25 30 40 45 59 65 80 90
B+ Tree
43. Advantages Of B+ Trees
• Data can be fetched in an equal number of disk accesses.
• Compared to the B tree, the height of the B+ tree is less and remains balanced.
• We use keys for indexing.
• Data in the B+ tree can be accessed sequentially or directly as the leaf nodes are arranged in a linked list.
• Search is faster as data is stored in leaf nodes only and as a linked list.
B-Tree B+ Tree
Data is stored in leaf nodes as well as internal nodes. Data is stored only in leaf nodes.
Searching is a bit slower as data is stored in internal as well as
leaf nodes.
Searching is faster as the data is stored only in the leaf nodes.
No redundant search keys are present. Redundant search keys may be present.
Deletion operation is complex. Deletion operation is easy as data can be directly deleted
from the leaf nodes.
Leaf nodes cannot be linked together. Leaf nodes are linked together to form a linked list.
Difference Between B-Tree And B+ Tree
44. Insertion in B+Tree:
1. Find the leaf node in which key value of the node has to be inserted
2. If key value already exist, no more insertion
Else if the key value does not exist , insert key value in leaf node in an ordered fashion
3. When a node is split, the middle key is retained in the left half as well as promoted to the parent node
B+ Tree of order 4:
Insert 18:
20 40 60
10 12 20 25 30 40 45 60 65 70 80
40
12 20 60
10 12 18 20 25 30 40 45 60 65 70 80
45. Deletion in B+ Tree:
1. Delete the key and data from the leaves.
2. If the leaf node contains less than minimum number of elements, merge down the node with its sibling and
delete the key in between them.
3. If the index node contains less than minimum number of elements, merge the node with the sibling and move
down the key in between them.
Consider B+ Tree of order 5:
Delete 210
210 is present in the right sub-tree of 190, after 195. delete it.
Merge the two nodes by using 195, 190, 138 and 125.
108
60 78 120 190
40 50 54 65 70 83 95 110 115 125 138 195 210
46. Merge the two nodes by using 195, 190, 138 and 125.
Now, element 120 is the single element present in the node which is violating the B+ Tree properties.
Therefore, we need to merge it by using 60, 78, 108 and 120.
Now, the height of B+ tree will be decreased by 1.
108
60 78 120
40 50 54 65 70 83 95 110 115 125 138 190 195
40 50 54 65 70 83 95 110 115 125 138 190 195
60 78 108 120
47. Threaded Binary Tree:
A binary tree can be represented by using an array or linked list. When binary tree is
represented using linked , if any node I is not having a child we use a NULL pointer. At least
half of the entries in Left and Right pointer will contain NULL entries.
These NULL pointer does not play any role except indicating there is no child (Link).
This space may be more efficiently used by replacing NULL pointer entries by special pointer,
called Threads, which points to the node higher in the tree. Such trees are called Threaded
Binary Tree.
Threaded Binary Tree is also a binary tree in which left child pointers that are NULL (in linked
list representation) points to its in-order predecessor and right child pointers that are NULL
points to its in-order successor.
If there is no predecessor or successor then points to the root node.
48. There are three way to thread a binary tree:
1. In an in-order traversal when the right pointer in NULL then it can be replaced by a
thread to the successor of that node and it is called Right Threaded Tree.
2. In an in-order traversal when the left pointer is NULL then it is replaced by a thread to
the predecessor of that node and it is Left Threaded Tree.
3. In an in-order traversal when both left and right pointers are NULL then they are
replaced by threads points to predecessor and successor of that node respectively. It is
called Fully Threaded Tree.
When one thread is used it is called one way threaded tree and when both the threads are
used it is called two way threaded tree.
The idea threaded binary tree is to make in-order traversal faster and do it without stack and
without recursion.
49. A
B C
D F
E
Right Threaded Tree:
A
B C
D E F
No successor
Inorder Traversal: D B E A F C
50. A
B C
D F
E
Left Threaded Tree:
A
B C
D E F
G
G
Inorder Traversal: D B G E A F C
51. A
B C
D F
E
Fully Threaded Tree:
A
B C
D E F
G
G
Inorder Traversal: D B G E A F C
52. Preorder : F A E C K D H G B
Inorder : E A C K F H D B G
F
E A C K H D B G
F
A H D B G
E C K
F
A H D B G
E C
K
53. F
A H D B G
E C
K
Preorder : F A E C K D H G B
Inorder : E A C K F H D B G
F
A D
E C
K
H B G
F
A D
E C
K
H G
B