Unit3 Trees
Unit3 Trees
Code: CSPC-213
By
Dr Kunwar Pal
05/23/2025 2
Trees
Tree Terminology
• Root: node without parent (A)
• Siblings: nodes share the same parent
• Subtree: tree consisting of a
• Degree of a Node: Number of subtrees of a node node and its descendants
in a given tree.
• Degree of Tree: It is the maximum degree of a
node in a tree
• Internal: node with at least one child (A, B, C, A
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- E F G H
grandchild, etc.
• Edge: It is a connecting line between two nodes:
like (A,B) or (B,F) etc. I J K
• Path: It is a sequence of consecutive edges from subtree
the source node to destination node. In the
example tree the path from A to K is:
05/23/2025
(A,B), (B,F) & (F,K) or A,B,F,K 3
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.
05/23/2025 4
Trees
Tree Properties
Property Value
A
Number of nodes
Height
B C Root Node
Leaves
Interior nodes
D E F Ancestors of H
Descendants of B
Siblings of E
G Right subtree of A
Degree of this tree
H I
05/23/2025 5
Trees
Types of Tree
The tree data structure can be classified into six different categories.
05/23/2025 6
Trees
General Tree
General Tree stores the elements in a hierarchical order in which the top
level element is always present at level 0 as the root element. All the
nodes except the root node are present at number of levels. The nodes
which are present on the same level are called siblings while the nodes
which are present on the different levels exhibit the parent-child
relationship among them. A node may contain any number of sub-trees.
Forests
Forest can be defined as the set of disjoint trees which can be obtained
by deleting the root node and the edges which connects root node to the
first level node.
05/23/2025 7
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.
Expression Tree
Expression trees are used to evaluate the simple arithmetic expressions.
Expression tree is basically a binary tree where internal nodes are
represented by operators while the leaf nodes are represented
by operands.
05/23/2025 8
Trees
05/23/2025 9
Trees
Tournament Tree
Tournament tree are used to record the winner of the match in each
round being played between two players. Tournament tree can also be
called as selection tree or winner tree. External nodes represent the
players among which a match is being played while the internal nodes
represent the winner of the match played. At the top most level, the
winner of the tournament is present as the root node of the tree.
05/23/2025 10
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.
05/23/2025 11
Trees
Binary Tree(Properties)
1) The maximum number of nodes at level ‘L’ of a binary tree is 2 L.
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 2 L
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 2 h+1 – 1.
3. In a Binary Tree with N nodes, minimum possible height or the minimum
number of levels is? log2(N+1) -1
05/23/2025 12
Trees
05/23/2025 13
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.
05/23/2025 14
Trees
FULL Binary Tree properties
05/23/2025 15
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
05/23/2025 16
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.
05/23/2025 17
Trees
Balanced Binary Tree
A binary tree is balanced if the height of the tree is O(Log n) where n is the
number of nodes. For Example, the AVL tree maintains O(Log n) height by
making sure that the difference between the heights of the left and right subtrees is
almost 1. Red-Black trees maintain O(Log n) height by making sure that the
number of Black nodes on every root to leaf paths is the same and there are no
adjacent red nodes. Balanced Binary Search trees are performance-wise good as
they provide O(log n) time for search, insert and delete
05/23/2025 18
Trees
A degenerate (or pathological) tree: A Tree where every internal node has one
child. Such trees are performance-wise same as linked list.
05/23/2025 19
Trees
A binary tree data structure is represented using two methods. Those methods
are as follows...
Array Representation
Linked List Representation
05/23/2025 20
Trees
05/23/2025 21
Trees
2. Linked List Representation of Binary Tree
We use a double linked list to represent a binary tree. In a
double linked list, every node consists of three fields. First field
for storing left child address, second for storing actual data
and third for storing right child address.
05/23/2025 22
Trees
05/23/2025 23
Trees
Binary Tree Traversal
– Preorder (Node,Left,Right)
– Postorder (Left, Right, Node)
– Inorder(Left, Node, Right)
05/23/2025 24
Trees
05/23/2025 25
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
05/23/2025 26
05/23/2025 27
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){
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.
05/23/2025 29
Trees
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
05/23/2025 30
Trees
Inorder: 1 2 3 4 5 7 8
Postorder: 1 3 4 2 7 8 5
05/23/2025 31
Trees
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
05/23/2025 32
Trees
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
05/23/2025 33
Create Tree
• Preorder: A B D G C E H I F
• Inorder: D G B A H E I C F
05/23/2025 34
Trees
05/23/2025 35
Trees
05/23/2025 36
Trees
05/23/2025 37
Trees
05/23/2025 38
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.
05/23/2025 39
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.
05/23/2025 40
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;
};
05/23/2025 41
Trees
BST Basic Operations
The basic operations that can be performed on a binary search tree data
structure, are the following −
05/23/2025 43
Trees
Insertion in BST
05/23/2025 44
05/23/2025 45
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
05/23/2025 46
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
05/23/2025 47
Trees
2. To insert an element in Complete Binary search tree in the worst case, the no.
comparison required are bounded by O(log2n).
05/23/2025 48
Trees
05/23/2025 49
Trees
•In-order Traversal
•Pre-order Traversal
•Post-order Traversal
05/23/2025 50
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
05/23/2025 53
Trees
Binary Search Tree Deletion
Case 1 : Deleting the leaf node from BST
05/23/2025 54
Trees
Binary Search Tree Deletion
Case 2 : Deleting the node which has only one child
05/23/2025 55
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
05/23/2025 56
Trees
10, 20,30
05/23/2025 57
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.
05/23/2025 58
Trees
AVL Tree
05/23/2025 59
Trees
05/23/2025 60
Trees
AVL Rotations
The first two rotations LL and RR are single rotations and the next two
rotations LR and RL are double rotations.
05/23/2025 61
Trees
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 −
05/23/2025 62
Trees
LL 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.
As depicted, the unbalanced node becomes the right child of its left
child by performing a right rotation.
05/23/2025 63
Trees
LR Rotation ()
State Action
05/23/2025 64
Trees
05/23/2025 65
Trees
RL Rotation
The second type of double rotation is Right-Left Rotation. It is a
combination of right rotation followed by left rotation.
State Action
05/23/2025 66
Trees
05/23/2025 67
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
05/23/2025 68
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:
05/23/2025 69
Trees
05/23/2025 70
Trees
3. Insert E On inserting E,
05/23/2025 71
Trees
3 a) We first perform RR rotation on node B
The resultant tree after RR rotation is:
05/23/2025 72
Trees
05/23/2025 73
Trees
4. Insert C, F, D
05/23/2025 75
Trees
4b) We then perform RR rotation on node B
The resultant balanced tree after RR rotation is:
05/23/2025 76
Trees
5. Insert G
05/23/2025 77
Trees
05/23/2025 78
Trees
05/23/2025 79
Trees
6. Insert K
05/23/2025 80
Trees
05/23/2025 81
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
05/23/2025 82
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.
05/23/2025 83
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.
05/23/2025 84
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.
05/23/2025 85
Trees
R1 Rotation (Node B has balance factor 1)
05/23/2025 86
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.
05/23/2025 87
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.
05/23/2025 88
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.
05/23/2025 89
AVL Tree Deletion
05/23/2025 90
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
05/23/2025 91
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
05/23/2025 92
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
05/23/2025 93
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.
05/23/2025 94
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.
05/23/2025 95
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.
05/23/2025 96
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.
05/23/2025 97
M-way Search Tree
05/23/2025 98
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)
05/23/2025 99
Searching in M-way search tree
05/23/2025 100
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.
05/23/2025 101
B-tree of degree 5
05/23/2025 102
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.
05/23/2025 103
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
05/23/2025 104
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
05/23/2025 105
Trees
4. Insert X
5. Insert C
6. Insert L
05/23/2025 106
Trees
7. Insert Z
8. Insert Y
9. Insert T
05/23/2025 107
Trees
10. Insert G
11 Insert J
12 Insert I
05/23/2025 108
Trees
13. Insert D
14. Insert H
05/23/2025 109
Trees
15. Insert R
16. Insert V
05/23/2025 110
Trees
17. Insert E
18. Insert U
05/23/2025 111
Trees
19. Insert F
20. Insert O
05/23/2025 112
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
05/23/2025 113
Trees
Step3. Same process applied until root node full. if root node full then
splitting process applied.
05/23/2025 114
Trees
05/23/2025 115
Trees
05/23/2025 116
Trees
Example
key :-J, R, D, G, T, E, M, H, P, A, F, Q
Order = 4
05/23/2025 117
Trees
Example
key :-J, R, D, G, T, E, M, H, P, A, F, Q
Order = 4
05/23/2025 118
Trees
Example
key :-J, R, D, G, T, E, M, H, P, A, F, Q
Order = 4
05/23/2025 119
Trees
Example
key :-J, R, D, G, T, E, M, H, P, A, F, Q
Order = 4
05/23/2025 120
Trees
05/23/2025 121
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".
05/23/2025 122
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.
05/23/2025 123
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.
05/23/2025 124
05/23/2025 125
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:
05/23/2025 126
The "gap" is filled in with the immediate predecessor:
05/23/2025 127
Huffman Coding
Huffman Coding is a famous Greedy Algorithm.
05/23/2025 129
Huffman Coding
Step-01:
Create a leaf node for all the given characters.
Leaf node contains the occurring frequency of that character.
Step-02:
05/23/2025 130
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.
05/23/2025 131
Huffman Coding
Important Formulas-
Formula-01:
05/23/2025 132
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 )
05/23/2025 133
Huffman Coding
Problem-
05/23/2025 134
Huffman Coding
Characters Frequencies
a 10
e 15
i 12
o 3
u 4
s 13
t 1
05/23/2025 135
Huffman Coding
Solution-
First let us construct the Huffman Tree.
Huffman Tree is constructed in the
following steps-
Step-01:
Step-02:
05/23/2025 136
Huffman Coding
Step-03:
05/23/2025 137
Huffman Coding
Step-04:
05/23/2025 138
Huffman Coding
Step-05:
05/23/2025 139
Huffman Coding
Step-06:
05/23/2025 140
Huffman Coding
Step-07:
05/23/2025 141
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-
05/23/2025 142
Huffman Coding
05/23/2025 143
Huffman Coding
05/23/2025 144
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.
05/23/2025 145
Huffman Coding
05/23/2025 146
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.
05/23/2025 147
Trees
05/23/2025 148
Trees
05/23/2025 149
Trees
05/23/2025 150
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.
05/23/2025 151
05/23/2025 152
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.
05/23/2025 153
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.
05/23/2025 154
05/23/2025 155
Trees
05/23/2025 156
Trees
05/23/2025 157
Trees
05/23/2025 158
Trees
05/23/2025 159
Thank you
05/23/2025 160