Unit3 Trees
Unit3 Trees
What is a Tree
• Tree is non-linear Data
Structure. Computers”R”Us
• A tree is a finite nonempty set
of elements.
• It is an abstract model of a Sales Manufacturing R&D
hierarchical structure.
• Consists of nodes with a
parent-child relation. US International Laptops Desktops
• Applications:
– Organization charts
– File systems Europe Asia Canada
– Programming
environments
11/7/2024 1
Trees
Tree Terminology
• Root: node without parent (A)
• Subtree: tree consisting of a
• Siblings: nodes share the same parent
node and its descendants
• Degree of a Node: Number of subtrees of a node
in a given tree.
• Degree of Tree: It is the maximum degree of a
node in a tree
A
• Internal: node with at least one child (A, B, C, F)
• External or terminal or leaf node: node without
children (E, I, J, K, G, H, D) or degree 0
B C D
• Ancestors of a node: parent, grandparent, grand-
grandparent, etc.
• Descendant of a node: child, grandchild, grand-
grandchild, etc. E F G H
• Edge: It is a connecting line between two nodes:
like (A,B) or (B,F) etc.
• Path: It is a sequence of consecutive edges from I J K
the source node to destination node. In the subtree
example tree the path from A to K is:
11/7/2024 (A,B), (B,F) & (F,K) or A,B,F,K 2
Trees
Tree Terminology
Depth of the tree- no. of edges from root to leaf Level 0
node in max path.
Height of a tree: no. of edges from leaf node to root
node in max path.
Depth of the node – no. of edges from root node to
that node. Level 1
Height of the node – no. of edges from leaf node
to that node.
Level: The entire tree structure is levelled in such a
way that the root node is always at level 0. Then its Level 2
immediate children are at level 1 and their
immediate children are at level 2 and so on. In
general if a node is at level n then its children will
be at n+1 level.
Non-Terminal Nodes: Any node (except the Level 3
root node) whose degree is not zero is called non-
terminal node.
11/7/2024 3
Trees
Tree Properties
Property Value
A
Number of nodes
Height
B C
Root Node
Leaves
Ancestors of H
D E F
Descendants of B
Siblings of E
Right subtree of A
G
Degree of this tree
H I
11/7/2024 4
Trees
Binary Tree
Binary tree is a data structure in which each node can have at most 2
children. The node present at the top most level is called the root
node. A node with the 0 children is called leaf node. Binary Trees are
used in the applications like expression evaluation and many more.
11/7/2024 5
Trees
11/7/2024 6
Trees
Binary Tree
A tree whose elements have at most 2 children is called a binary tree.
Since each element in a binary tree can have only 2 children, we typically
name them the left and right child.
11/7/2024 7
Trees
Binary Tree(Properties)
1) The maximum number of nodes at level ‘L’ of a binary tree is 2L.
Here level is the number of nodes on the path from the root to the node
(including root and node). Level of the root is 0.
This can be proved by induction.
For root, L = 0, number of nodes = 20 = 1
Assume that the maximum number of nodes on level ‘L’ is 2L
Since in Binary tree every node has at most 2 children, next level would have
twice nodes, i.e. 2 * 2L = 2L+1 for L+1 level
2. The Maximum number of nodes in a binary tree of height ‘h’ is 2h+1 – 1.
3. In a Binary Tree with N nodes, minimum possible height or the minimum
number of levels is? log2(N+1) -1
11/7/2024 8
Trees
11/7/2024 9
Trees
Full Binary Tree or Strictly Binary Tree: A Binary
Tree is a full binary tree if every node has 0 or 2
children. The following are the examples of a full
binary tree. We can also say a full binary tree is a
binary tree in which all nodes except leaf nodes have
two children.
11/7/2024 10
Trees
FULL Binary Tree theorem
11/7/2024 11
Trees
Complete Binary Tree: A Binary Tree is a complete Binary Tree if all the levels
are completely filled except possibly the last level and the last level has all keys as
left as possible
The leftmost side of the leaf node must always be filled first.
The following are examples of Complete Binary Trees
11/7/2024 12
Trees
Perfect Binary Tree: A Binary tree is a Perfect
Binary Tree in which all the internal nodes have two
children and all leaf nodes are at the same level.
The following are the examples of Perfect Binary
Trees.
11/7/2024 13
Trees
A degenerate (or pathological) tree: A Tree where every internal node has one
child. Such trees are performance-wise same as linked list.
11/7/2024 14
Trees
11/7/2024 15
Trees
11/7/2024 16
Trees
A binary tree data structure is represented using two methods. Those methods
are as follows...
Array Representation
Linked List Representation
11/7/2024 17
Trees
11/7/2024 18
• The Root of the tree is stored at T[1]
• If node N occupy T[k], then its left child is stored in T[2*k]
and right child T[2*k+1].
• If T[1]= Null Then tree is empty.
• A tree with depth d required an array approximately 2𝑑+1
11/7/2024 20
Trees
11/7/2024 21
Trees
Binary Tree Traversal
– Preorder (Node, Left, Right)
– Inorder(Left, Node, Right)
– Postorder (Left, Right, Node)
11/7/2024 22
Trees
11/7/2024 23
Trees
PreOrder : (NLR)
Ans – 1 7 2 6 3 8 5 9 4
InOrder: (LNR)
Ans – 2 7 3 6 8 1 5 4 9
PostOrder: (LRN)
Ans - 2 3 8 6 7 4 9 5 1
11/7/2024 24
11/7/2024 25
Trees
Algorithm Inorder(tree
1. Traverse the left subtree, i.e., call Inorder(left-subtree)
2. Visit the root.
3. Traverse the right subtree, i.e., call Inorder(right-subtree)
Algorithm Preorder(tree)
1. Visit the root.
2. Traverse the left subtree, i.e., call Preorder(left-subtree)
3. Traverse the right subtree, i.e., call Preorder(right-subtree)
Algorithm Postorder(tree)
1. Traverse the left subtree, i.e., call Postorder(left-subtree)
2. Traverse the right subtree, i.e., call Postorder(right-subtree)
3. Visit the root.
11/7/2024 26
Traversal Algorithms
• In-order Traversal:
void inorder(node *p){
if(p!=NULL){
inorder(p->left);
printf(“%d\n”, p->num);
inorder(p->right);
}
} Post-order Traversal:
void postorder(node *p){
if(p!=NULL){
11/7/2024 27
Trees
Construct Tree from given Inorder and Postorder traversals
1. Pickup last key of post order sequence and create node of each.
2. Create left subtree of root node recursively by selecting keys which are
preceding the root node in Inorder.
3. Create right subtree of root node recursively by selecting keys which are
following the root node in Inorder
11/7/2024 28
Trees
Construct Tree from given Inorder and Postorder traversals
Inorder: 1 2 3 4 5 7 8
Postorder: 1 3 4 2 7 8 5
11/7/2024 29
Trees
Construct Tree from given Inorder and Preorder traversals
1. Pickup first key of pre order sequence and create node of each.
2. Create left subtree of root node recursively by selecting keys which are preceding
the root node in Inorder.
3. Create right subtree of root node recursively by selecting keys which are
following the root node in Inorder
11/7/2024 30
Trees
Construct Tree from given Inorder and Preorder traversals
Inorder: 1 2 3 4 5 7 8
Preorder: 5 2 1 4 3 8 7
Inorder: 1 2 3 4 5 6 7 8
Preorder: 5 3 1 2 4 6 8 7
11/7/2024 31
Create Tree
• Preorder: A B D G C E H I F
• Inorder: D G B A H E I C F
11/7/2024 32
Trees
11/7/2024 33
Trees
11/7/2024 34
Trees
11/7/2024 35
Trees
11/7/2024 36
Trees
A node's left child must have a value less than its parent's value and the
node's right child must have a value greater than its parent value.
11/7/2024 37
Trees
Binary Search Tree Representation
Post Order Traversal of BST is 10,9,23,22,27,25,15,95,60,40,29.
Find PreOrder traversal.
11/7/2024 38
Trees
BST
Tree Node
The code to write a tree node would be similar to what is
given below. It has a data part and references to its left and
right child nodes.
struct node {
int data;
struct node *leftChild;
struct node *rightChild;
};
11/7/2024 39
Trees
BST Basic Operations
The basic operations that can be performed on a binary search tree data
structure, are the following −
11/7/2024 41
Trees
Insertion in BST
11/7/2024 42
11/7/2024 43
Creation of Binary Search Tree
• Create a Binary Search Tree for following keys
14,10,17,12,10,11,20,12,18,25,20,8,22,11,23
11/7/2024 44
Creation of Binary Search Tree
• Create a Binary Search Tree for following keys
S,T,P,Q,M,N,O,R,K,V,A,B
11/7/2024 45
Trees
2. To insert an element in Complete Binary search tree in the worst case, the no.
comparison required are bounded by O(log2n).
11/7/2024 46
Trees
•In-order Traversal
•Pre-order Traversal
•Post-order Traversal
11/7/2024 48
Trees
In-order Traversal
Algorithm
Until all nodes are traversed −
Step 1 − Recursively traverse left subtree.
Step 2 − Visit root node.
Step 3 − Recursively traverse right subtree.
Post-order Traversal
Algorithm
Until all nodes are traversed −
Step 1 − Recursively traverse left subtree.
Step 2 − Recursively traverse right subtree.
Step 3 − Visit root node.
We start from A, and following Post-order traversal, we first visit the left
subtree B. B is also traversed post-order. The process goes on until all the
nodes are visited. The output of post-order traversal of this tree will be −
D→E→B→F→G→C→A
11/7/2024 51
Trees
Binary Search Tree Deletion
Case 1 : Deleting the leaf node from BST
11/7/2024 52
Trees
Binary Search Tree Deletion
Case 2 : Deleting the node which has only one child
11/7/2024 53
Trees
Binary Search Tree Deletion
Case 3 : Deleting the node which has left subtree or right subtree.
Task 1 : Finding key takes either log2(n) or O(n) times.
Task 2 : Replace the deleted node with the max of LST or min of RST
i.e the node which is to be deleted, is replaced with its in-order
successor
11/7/2024 54
Trees
10, 20,30
11/7/2024 55
Trees
AVL Tree
AVL Tree is invented by GM Adelson - Velsky and EM Landis in 1962.
The tree is named AVL in honour of its inventors.
11/7/2024 56
Trees
AVL Tree
11/7/2024 57
Trees
11/7/2024 58
Trees
AVL Rotations
The first two rotations LL and RR are single rotations and the next two
rotations LR and RL are double rotations.
11/7/2024 59
Trees
Left Rotation/RR 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 −
11/7/2024 60
Trees
As depicted, the unbalanced node becomes the right child of its left
child by performing a right rotation.
11/7/2024 61
Trees
Left-Right Rotation ()
State Action
11/7/2024 62
Trees
11/7/2024 63
Trees
Right-Left Rotation
The second type of double rotation is Right-Left Rotation. It is a
combination of right rotation followed by left rotation.
State Action
11/7/2024 64
Trees
11/7/2024 65
Trees
Q: Construct an AVL tree having the following elements
H, I, J, B, A, E, C, F, D, G, K, L
1. Insert H, I, J
11/7/2024 66
Trees
On inserting the above 2. Insert B, A
elements, especially in the case
of H, the BST becomes
unbalanced as the Balance
Factor of H is -2. Since the BST
is right-skewed, we will perform
RR Rotation on node H.
The resultant balance tree is:
11/7/2024 67
Trees
11/7/2024 68
Trees
3. Insert E On inserting E,
11/7/2024 69
Trees
3 a) We first perform RR rotation on node B
The resultant tree after RR rotation is:
11/7/2024 70
Trees
11/7/2024 71
Trees
4. Insert C, F, D
On inserting C, F, D, BST
becomes unbalanced as the
Balance Factor of B and H is -2,
since if we travel from D to B we
find that it is inserted in the right
subtree of left subtree of B,
11/7/2024 73
Trees
11/7/2024 74
Trees
5. Insert G
11/7/2024 75
Trees
11/7/2024 76
Trees
11/7/2024 77
Trees
6. Insert K
11/7/2024 78
Trees
11/7/2024 79
Trees
7. Insert L
On inserting the L tree is still balanced as the Balance Factor of each
node is now either, -1, 0, +1. Hence the tree is a Balanced AVL tree
11/7/2024 80
Trees
Rotations
There are two types of rotations L rotation (RR) and R rotation (LL). Here, we
will discuss R rotations. L rotations are the mirror images of them.
If the node which is to be deleted is present in the left sub-tree of the critical node,
then L rotation needs to be applied else if, the node which is to be deleted is present
in the right sub-tree of the critical node, the R rotation will be applied.
11/7/2024 81
Trees
Let us consider that, A is the critical node and B is the root node of its
left sub-tree. If node X, present in the right sub-tree of A, is to be
deleted, then there can be three different situations:
If the node B has 0 balance factor, and the balance factor of node A
disturbed upon deleting the node X, then the tree will be rebalanced
by rotating tree using R0 rotation.
The critical node A is moved to its right and the node B becomes the
root of the tree with T1 as its left sub-tree. The sub-trees T2 and T3
becomes the left and right sub-tree of the node A. the process
involved in R0 rotation is shown in the following image.
11/7/2024 82
Trees
Example:
Delete the node 30 from the AVL tree shown in the following image.
Solution
In this case, the node B has balance factor 0, therefore the tree will be
rotated by using R0 rotation as shown in the following image. The node
B(10) becomes the root, while the node A is moved to its right. The right
child of node B will now become the left child of node A.
11/7/2024 83
Trees
R1 Rotation (Node B has balance factor 1)
11/7/2024 84
Trees
Example
Delete Node 55 from the AVL tree shown in the following image.
Deleting 55 from the AVL Tree disturbs the balance factor of the node
50 i.e. node A which becomes the critical node. This is the condition of
R1 rotation in which, the node A will be moved to its right (shown in the
image below). The right of B is now become the left of A (i.e. 45).
The process involved in the solution is shown in the following image.
11/7/2024 85
Trees
R-1 Rotation (Node B has balance factor -1)
R-1 rotation is to be performed if the node B has balance factor -1. This case
is treated in the same way as LR rotation. In this case, the node C, which is
the right child of node B, becomes the root node of the tree with B and A as
its left and right children respectively.
The sub-trees T1, T2 becomes the left and right sub-trees of B whereas, T3,
T4 become the left and right sub-trees of A.
The process involved in R-1 rotation is shown in the following image.
11/7/2024 86
Trees
Example
Delete the node 60 from the AVL tree shown in the following image.
Solution:
in this case, node B has balance factor -1. Deleting the node
60, disturbs the balance factor of the node 50 therefore, it
needs to be R-1 rotated. The node C i.e. 45 becomes the
root of the tree with the node B(40) and A(50) as its left and
right child.
11/7/2024 87
AVL Tree Deletion
11/7/2024 88
Trees
Motivation for B-Trees
• So far we have assumed that we can store an entire data
structure in main memory
• What if we have so much data that it won’t fit?
• We will have to use disk storage but when this happens our time
complexity fails
11/7/2024 89
Trees
Motivation (cont.)
• Assume that a disk spins at 3600 RPM
• In 1 minute it makes 3600 revolutions, hence one revolution
occurs in 1/60 of a second, or 16.7ms
• On average what we want is half way round this disk – it will take
8ms
• This sounds good until you realize that we get 120 disk accesses
a second – the same time as 25 million instructions
• In other words, one disk access takes about the same time as
200,000 instructions
• It is worth executing lots of instructions to avoid a disk access
11/7/2024 90
Trees
Motivation (cont.)
• Assume that we use an Binary tree to store all the details of
people in Canada (about 32 million records)
• We still end up with a very deep tree with lots of different disk
accesses; log2 20,000,000 is about 25, so this takes about
1.21 seconds (if there is only one user of the program)
• We know we can’t improve on the log n for a binary tree
• But, the solution is to use more branches and thus less height!
• As branching increases, depth decreases
11/7/2024 91
Trees
2. The goal is to minimize the access time( Time required to load the data from the
memory) while retrieving a key value from the file.
A m-way search tree is an empty tree if tree is not empty then it satisfies the
following properties
(i) Each node has at most m-child nodes called order of the tree.
(ii) If m is the order of the tree, then max no. of child pointers are m and max
number of key stores m-1.
11/7/2024 92
Trees
2. If a node has k keys than, k<=m-1
than the keys are k1,k2,k3,….km
such that ki<ki+1 also each of the
keys in the node partition the sub
trees into k+1 subsets.
11/7/2024 93
M-way Trees
An M-way(multi-way) tree is a tree that has the following properties:
• Each node in the tree can have at most m children.
• Nodes in the tree have at most (m-1) key fields and pointers(references) to
the children.
11/7/2024 94
M-way Search Trees
• An M-way search tree is a more constrained m-way tree, and these
constrain mainly apply to the key fields and the values in them. The
constraints on an M-way tree that makes it an M-way search tree
are:
• Each node in the tree can associate with m children and m-1 key
fields.
• The keys in any node of the tree are arranged in a sorted
order(ascending).
• The keys in the first K children are less than the Kth key of this
node.
• The keys in the last (m-K) children are higher than the Kth key.
11/7/2024 95
M-way Search Tree
11/7/2024 96
M-way Search Trees
• If we want to search for a value say X in an M-way search tree and
currently we are at a node that contains key values from Y1, Y2,
Y3,.....,Yk. Then in total 4 cases are possible to deal with this scenario,
these are:
• If X < Y1, then we need to recursively traverse the left subtree of Y1.
• If X > Yk, then we need to recursively traverse the right subtree of Yk.
• If X = Yi, for some i, then we are done, and can return.
• Last and only remaining case is that when for some i we have Yi < X <
Y(i+1), then in this case we need to recursively traverse the subtree that is
present in between Yi and Y(i+1).
• An m-Way search tree of n elements ranges from a minimum height
of log_m(n+1) to a maximum of n (It is not height balanced)
11/7/2024 97
Searching in M-way search tree
11/7/2024 98
B Trees Data Structure
A B tree is an extension of an M-way search tree. Besides having all the
properties of an M-way search tree, it has some properties of its own,
these mainly are:
• All the leaf nodes in a B tree are at the same level.
• The root node has at least two child nodes and most M child nodes
• All internal nodes except root node must have at least
⌈ M/2⌉ child nodes and at most M child nodes
• The number of keys in each internal node is one less than the
number of child nodes and these keys and these keys partition the
keys in the subtrees of the node in a manner similar to that of M-
way search tree.
11/7/2024 99
B-tree of degree 5
11/7/2024 100
Trees
Insertion in the B-Tree
Search the element in the B-Tree is the same manner as in the m-way search
tree.
If the element is not present then search tends to finish at some leaf node.
Case 1. When leaf element is not full then insert element into it.
11/7/2024 101
Trees
Constructing a B Tree
Example
key :- P,A,Q,S,B,X,C,L,Z,Y,T,G,J,I,D,H,R,V,E,U,F,R.
Order = 5
1. Insert P
2. Insert P
11/7/2024 102
Trees
Constructing a B Tree
Example
key :- P,A,Q,S,B,X,C,L,Z,Y,T,G,J,I,D,H,R,V,E,U,F,R,O.
1. Insert Q
2. Insert S
3. Insert B
11/7/2024 103
Trees
4. Insert X
5. Insert C
6. Insert L
11/7/2024 104
Trees
7. Insert Z
8. Insert Y
9. Insert T
11/7/2024 105
Trees
10. Insert G
11 Insert J
12 Insert I
11/7/2024 106
Trees
13. Insert D
14. Insert H
11/7/2024 107
Trees
15. Insert R
16. Insert V
11/7/2024 108
Trees
17. Insert E
18. Insert U
11/7/2024 109
Trees
19. Insert F
20. Insert O
11/7/2024 110
Trees
Constructing a B Tree
Example
key :- 1,12,8,2,25,6,14,28,17,7,52,16,48,68,3,26,29,53,55,45,67.
Order = 5
Procedure for adding key in b-tree
11/7/2024 111
Trees
Step3. Same process applied until root node full. if root node full then
splitting process applied.
11/7/2024 112
Trees
11/7/2024 113
Trees
11/7/2024 114
Trees
Example
key :-J, R, D, G, T, E, M, H, P, A, F, Q
Order = 4
11/7/2024 115
Trees
Example
key :-J, R, D, G, T, E, M, H, P, A, F, Q
Order = 4
11/7/2024 116
Trees
Example
key :-J, R, D, G, T, E, M, H, P, A, F, Q
Order = 4
11/7/2024 117
Trees
Example
key :-J, R, D, G, T, E, M, H, P, A, F, Q
Order = 4
11/7/2024 118
Trees
11/7/2024 119
Case 1: Deletion from a leaf
1a) If the leaf is at least half full after deleting the desired value, the remaining larger values are
moved to "fill the gap".
11/7/2024 120
1b) If the leaf is less than half full after deleting the desired value (known as underflow), two
things could happen:
Deleting 7 from the tree above results in
1b-1) If there is a left or right sibling with the number of keys exceeding the minimum
requirement, all of the keys from the leaf and sibling will be redistributed between them
by moving the separator key from the parent to the leaf and moving the middle key from
the node and the sibling combined to the parent.
11/7/2024 121
Now delete 8 from the tree:
1b-2) If the number of keys in the sibling does not exceed the minimum requirement, then the
leaf and sibling are merged by putting the keys from the leaf, the sibling, and the separator
from the parent into the leaf. The sibling node is discarded and the keys in the parent are
moved to "fill the gap". It's possible that this will cause the parent to underflow. If that is the
case, treat the parent as a leaf and continue repeating step 1b-2 until the minimum requirement
is met or the root of the tree is reached.
Special Case for 1b-2: When merging nodes, if the parent is the root with only one key, the
keys from the node, the sibling, and the only key of the root are placed into a node and this will
become the new root for the B-tree. Both the sibling and the old root will be discarded.
11/7/2024 122
11/7/2024 123
Case 2: Deletion from a non-leaf
This case can lead to problems with tree reorganization but it will be solved in a manner
similar to deletion from a binary search tree.
The key to be deleted will be replaced by its immediate predecessor (or successor) and then
the predecessor (or successor) will be deleted since it can only be found in a leaf node.
Deleting 16 from the tree above results in:
11/7/2024 124
The "gap" is filled in with the immediate predecessor:
11/7/2024 125
Huffman Coding
• Huffman Coding is a famous Greedy Algorithm.
11/7/2024 127
Huffman Coding
Step-01:
• Create a leaf node for all the given characters.
• Leaf node contains the occurring frequency of that character.
Step-02:
11/7/2024 128
Huffman Coding
Step-04:
• Keep repeating Step-02 and Step-03 until all the nodes form a
single tree.
• The tree finally obtained will be the desired Huffman Tree.
11/7/2024 129
Huffman Coding
Important Formulas-
Formula-01:
11/7/2024 130
Huffman Coding
Formula-02:
Total number of bits in Huffman encoded message
= Total number of characters in the message x Average code
length per character
= ∑ ( frequencyi x Code lengthi )
11/7/2024 131
Huffman Coding
Problem-
11/7/2024 132
Huffman Coding
Characters Frequencies
a 10
e 15
i 12
o 3
u 4
s 13
t 1
11/7/2024 133
Huffman Coding
Solution-
First let us construct the Huffman Tree.
Huffman Tree is constructed in the
following steps-
Step-01:
Step-02:
11/7/2024 134
Huffman Coding
Step-03:
11/7/2024 135
Huffman Coding
Step-04:
11/7/2024 136
Huffman Coding
Step-05:
11/7/2024 137
Huffman Coding
Step-06:
11/7/2024 138
Huffman Coding
Step-07:
11/7/2024 139
Huffman Coding
Now,
• We assign weight to all the edges of the constructed Huffman
tree.
• Let us assign weight ‘0’ to the left edges and weight ‘1’ to the
right edges.
Rule
• If you assign weight ‘0’ to the left edges, then assign
weight ‘1’ to the right edges.
• If you assign weight ‘1’ to the left edges, then assign
weight ‘0’ to the right edges.
• But follow the same convention at the time of decoding
that was adopted at the time of encoding.
After assigning weight to all the edges, the modified Huffman Tree is-
11/7/2024 140
Huffman Coding
11/7/2024 141
Huffman Coding
11/7/2024 142
Huffman Coding
From here, We can observe-
• Characters occurring less frequently in the text are assigned
the larger code.
• Characters occurring more frequently in the text are assigned
the smaller code.
11/7/2024 143
Huffman Coding
11/7/2024 144
Trees
Threaded Binary Tree
• In the linked representation of binary trees, more than one half of the link fields
contain NULL values which results in wastage of storage space.
• If a binary tree consists of n nodes then n+1 link fields contain NULL values. So in order
to effectively manage the space, a method was devised by Perlis and Thornton in which
the NULL links are replaced with special links known as threads.
• Such binary trees with threads are known as threaded binary trees. Each node in a
threaded binary tree either contains a link to its child node or thread to other nodes in
the tree.
11/7/2024 145
Trees
11/7/2024 146
Trees
11/7/2024 147
Trees
11/7/2024 148
Trees
One Way Threaded Binary Tree
• In one-way threaded binary trees, a thread will appear either in the right or left link
field of a node.
• If it appears in the right link field of a node then it will point to the next node that will
appear on performing inorder traversal. Such trees are called Right threaded binary
trees.
• If thread appears in the left field of a node then it will point to the nodes inorder
predecessor. Such trees are called Left threaded binary trees.
• In one-way threaded binary trees, the right link field of last node and left link field of
first node contains a NULL.
• In order to distinguish threads from normal links they are represented by dotted lines.
11/7/2024 149
11/7/2024 150
Trees
Two-way threaded Binary Trees:
In two-way threaded Binary trees, the right link field of a node containing NULL values
is replaced by a thread that points to nodes inorder successor and left field of a node
containing NULL values is replaced by a thread that points to nodes inorder
predecessor.
11/7/2024 151
In the above figure of two-way threaded Binary tree, we noticed that no left thread is
possible for the first node and no right thread is possible for the last node.
This is because they don't have any inorder predecessor and successor respectively. This
is indicated by threads pointing nowhere.
So in order to maintain the uniformity of threads, we maintain a special node called
the header node.
11/7/2024 152
11/7/2024 153
Trees
11/7/2024 154
Trees
11/7/2024 155
Trees
11/7/2024 156
Trees
11/7/2024 157
Faculty Video Links, Youtube & NPTEL
Video Links and Online Courses Details
• Self Made Video Link:
https://www.youtube.com/watch?v=qH6yxkw0u78&t=96s
https://www.youtube.com/watch?v=YAdLFsTG70w
11/7/2024 158
Daily Quiz
11/7/2024 159
Daily Quiz
Q4. The worst case of AVL Tree with ‘n’ nodes is:
A. 2logn
B. n log(n+1)
C. 1.44 log (n+2)
D. 1.44 nlogn
11/7/2024 160
Daily Quiz
Q6. Traversing a binary tree first root node and then left sub-tree and right sub-
tree called __Pre-Order____ traversal.
Q7. A B.S.T. contains the value 1, 2,3,4,5,6,7,8. The tree is traversed in Pre-Order
and the values are printed out. Which of the following is a valid output?
A. 53124786
B. 53126487
C. 53241678
D. 53124768
11/7/2024 161
Daily Quiz
Q8. Which of the following statement is false?
A. A tree with ‘n’ nodes has (n-1) edges.
B. A labeled sorted binary tree can be uniquely constructed given the post-order and
in-order traversal result.
C. A complete binary tree with’ n’ internal nodes has (n+1) leaves.
D. The maximum number of nodes in a binary tree of height ‘h’ is (2n+1-1).
Q9. The following number is inserted into an empty B.S.T. in given order:
10,1,3,5,15,12,16. What is the height of B.S.T.?
A. 2
B. 3
C. 4
D. 6
11/7/2024 162
Weekly Assignment
11/7/2024 163
Weekly Assignment
11/7/2024 164
MCQ s
11/7/2024 165
MCQ s
3. What must be the ideal size of array if the height of tree is ‘l’?
a) 2l-1
b) l-1
c) l
d) 2l
11/7/2024 166
MCQ s
5. For the tree below, write the 6. For the tree below, write the
pre-order traversal. post-order traversal.
a) 2, 7, 2, 6, 5, 11, 5, 9, 4
a) 2, 7, 2, 6, 5, 11, 5, 9, 4
b) 2, 7, 5, 2, 6, 9, 5, 11, 4
b) 2, 7, 5, 2, 6, 9, 5, 11, 4
c) 2, 5, 11, 6, 7, 4, 9, 5, 2
c) 2, 5, 11, 6, 7, 4, 9, 5, 2
d) 2, 7, 5, 6, 11, 2, 5, 4, 9
d) 2, 7, 5, 6, 11, 2, 5, 4, 9
11/7/2024 167
MCQ s
7. What is the maximum number of children that a binary tree node can
have?
a) 0
b) 1
c) 2
d) 3
11/7/2024 168
MCQ s
9. A binary tree is a rooted tree but not an ordered tree.
a) True
b) False
10. What is the traversal strategy used in the binary tree?
a) depth-first traversal
b) breadth-first traversal
c) random traversal
d) Priority traversal
11. How many types of insertion are performed in a binary tree?
a) 1
b) 2
c) 3
d) 4
11/7/2024 169
Expected Questions for University Exam
11/7/2024 170
Expected Questions for University Exam
7. What is binary search tree? Write the important applications of binary search
tree. Write algorithm to delete a node from a binary search tree.
11/7/2024 171
Old Question Papers
https://drive.google.com/open?id=15fAkaWQ5c
cZRZPzwlP4PBh1LxcPp4VAd
11/7/2024 172
Summary
• Unlike Array and Linked List, which are linear data structures,
tree is hierarchical (or non-linear) data structure.
• Binary Search Tree is a tree that allows fast search, insert, delete
on a sorted data. It also allows finding closest item
11/7/2024 173
References
11/7/2024 174
Thank you
11/7/2024 175