DSA.unit4
DSA.unit4
Semester- III
1
UNIT 4.
◻ Root
It is the mother node of a tree structure. This tree does not
have parent. It is the first node in hierarchical arrangement.
◻ Node
The node of a tree stores the data and its role is the same as
in the linked list. Nodes are connected by the means of links
with other nodes.
◻ Parent
It is the immediate predecessor of a node. In the figure A is
the parent of B and C. A
B C
A
Tree Terminologies
B C
10
◻ Child
When a predecessor of a node is parent then all successor
nodes are called child nodes. In the figure B and C are the
child nodes of A
◻ Link / Edge
An edge connects the two nodes. The line drawn from one
node to other node is called edge / link. Link is nothing but a
pointer to node in a tree structure.
◻ Leaf
This node is located at the end of the tree. It does not have
any child hence it is called leaf node.
Tree Terminologies
11
◻ Level
Level is the rank of tree hierarchy. The whole tree structured is leveled.
The level of the root node is always at 0. the immediate children of root
are at level 1 and their children are at level 2 and so no.
◻ Height
The highest number of nodes that is possible in a way starting from the
first node (ROOT) to a leaf node is called the height of tree. The formula
for finding the height of a tree , where h is the height and I
is the max level of the tree
Root node
Leaf nodes
Tree Terminologies
12
◻ Sibling
The child node of same parent are called sibling. They are also called
brother nodes.
◻ Degree of a Node
The maximum number of children that exists for a node is called as
degree of a node.
◻ Terminal Node
The node with out degree zero is called terminal node or leaf.
◻ Path length.
Is the number of successive edges from source node to destination
node.
◻ Ancestor and descendant
Proper ancestor and proper descendant
Tree Terminologies
13
◻ Depth
Depth of a binary tree is the maximum level of any leaf of a tree.
◻ Forest
It is a group of disjoint trees. If we remove a root node from a tree then
it becomes the forest.
In the following example, if we remove a root A then two disjoint
sub-trees will be observed. They are left sub-tree B and right sub-tree C.
A
B C
D E F
Notation
14
node
A
•root
•left subtree B C
•right subtree
D E F
G H I
B C
D E F
G H I
edge –
there is an edge from the root to its children
Notation
16
children
B C
D E F
G H I
Notation
17
children
Who are node C’s children?
A
D E F
G H I
Notation
18
descendants
B C
G H I
parents A
B C
D E F
Who is node E’s parent?
ancestors
B C
D E F
Who are node D’s ancestors?
from J to A
A
path – B C
J
Notation
22
B C
2 D E F
depth –
the length of the path
G H I
from the root of the tree
to the node
Notation
23
0 A
1 B C
2 D E F
3 G H I
level –
all nodes of depth d are at level d in the tree
Notation
24
B C
D E F
G H I
leaf node –
any node that has two empty children
Notation
25
B C
D E F
G H I
internal node –
any node that has at least one non-empty Child
Or
An internal node of a tree is any node which has degree greater than one.
Binary Trees
26
Three nodes
Degenerate (or pathological) tree
30
◻ A Tree where every internal node has one child. Such trees are
performance-wise same as linked list.
◻ It is a tree having a single child either left or right.
Skewed Binary tree
31
B C
D E
F G
Types- On basis of the completion of levels
33
Complete Binary Tree
A Complete binary tree is:
▪ A tree in which each level of the tree is completely filled.
B C
D E F G
H I J
Types- On basis of the completion of levels
34
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, Red-Black trees
Static Implementation of Binary Tree
35
- Sequential Representation
(1) waste space [1] A
A [1] A (2) insertion/deletion [2] B
[2] B problem [3] C
[3] --
B A [4] D
[4] C
-- [5] E
[5] [6] F
C --
[6] [7] G
-- B C
[7] D [8] H
D [8] [9] I
[9] -- F G
D E
. .
E E
[16
] H I
Dynamic Implementation of Binary Tree
36
- Linked Implementation
typedef struct node *tree_pointer;
typedef struct node {
int data;
tree_pointer left_child, right_child;
};
dat
a
left_child dat right_child
a
left_child right_child
Dynamic Implementation of Binary Tree
37
- Linked Implementation
Dynamic Implementation of Binary Tree
38
- Linked Implementation
Dynamic Implementation of Binary Tree
- Linked Implementation Structure Definition
39
◻ Create
Create an empty binary tree
◻ Empty
Return true when binary tree is empty else return false.
◻ Lchild
A pointer is returned to left child of a node, when a node is without
left child, NULL pointer is returned.
◻ Rchild
A pointer is returned to right child of a node, when a node is without
right child, NULL pointer is returned.
◻ Father/Parent
A pointer to father of a node is returned.
Operations on Binary Tree
41
◻ Sibling
A pointer to brother of the node is returned or else NULL pointer is returned.
◻ Tree Traversal
Inorder Traversal infix A+B
Preorder Traversal prefix +AB
Postorder Traversal postfix AB+
◻ Insert
To insert a node
◻ Deletion
To delete a node
◻ Search
To search a given node
◻ Copy
Copy one tree into another.
Traversal of a Binary Tree
42
◻ Inorder traversal
left, node, right.
infix expression
■ a+b*c+d*e+f*g
Inorder Traversal
47
◻ Inorder traversal
left, node, right.
infix expression
■ a+b*c+d*e+f*g
Inorder Traversal Function
48
◻ Preorder traversal
RootNode – Left – Right
Prefix expression
■ ++a*bc*+*defg
Preorder Traversal Function
50
◻ Postorder Traversal
left, right, node
postfix expression
■ abc*+de*f+g*+
Postorder Traversal Function
52
Pre-order Traversal?
Post-order Traversal?
In-order Traversal?
Traversal Exercise
57
Pre-order Traversal?
Post-order Traversal?
In-order Traversal?
Converting General Tree to Binary Tree
58
Example 1 dat
left childa right sibling
Converting General Tree to Binary Tree
59
Converting General Tree to Binary Tree
60
Example 2
Converting General Tree to Binary Tree
61
Example 3
Converting General Tree to Binary Tree
62
Example 3
Special Types of Trees
63
5
2 7
10 2
1
5 ,6 7,8
2, 3, 4
Examples
66
14 15 4 9 7 18 3 5 16 4 20 17 9 14 5
1
4
Example
Input list of numbers:
70
14 15 4 9 7 18 3 5 16 4 20 17 9 14 5
1
4
1
5
Example
Input list of numbers:
71
14 15 4 9 7 18 3 5 16 4 20 17 9 14 5
1
4
1
4
5
Example
Input list of numbers:
72
14 15 4 9 7 18 3 5 16 4 20 17 9 14 5
1
4
1
4
5
9
Example
Input list of numbers:
73
14 15 4 9 7 18 3 5 16 4 20 17 9 14 5
1
4
1
4
5
7
Example
Input list of numbers:
74
14 15 4 9 7 18 3 5 16 4 20 17 9 14 5
1
4
1
4
5
1
9
8
7
Example
Input list of numbers:
75
14 15 4 9 7 18 3 5 16 4 20 17 9 14 5
1
4
1
4
5
1
3 9
8
7
Example
Input list of numbers:
76
14 15 4 9 7 18 3 5 16 4 20 17 9 14 5
1
4
1
4
5
1
3 9
8
5
Example
Input list of numbers:
77
14 15 4 9 7 18 3 5 16 4 20 17 9 14 5
1
4
1
4
5
1
3 9
8
1
7 6
5
Example
Input list of numbers:
78
14 15 4 9 7 18 3 5 16 4 20 17 9 14 5
1
4
1
4
5
1
3 9
8
1
7 6
4
Example
Input list of numbers:
79
14 15 4 9 7 18 3 5 16 4 20 17 9 14 5
1
4
1
4
5
1
3 9
8
1 2
7 6 0
4
Example
Input list of numbers:
80
14 15 4 9 7 18 3 5 16 4 20 17 9 14 5
1
4
1
4
5
1
3 9
8
1 2
7 6 0
1
5 7
4
Example
Input list of numbers:
81
14 15 4 9 7 18 3 5 16 4 20 17 9 14 5
1
4
1
4
5
1
3 9
8
1 2
7 9 6 0
1
5 7
4
Example
Input list of numbers:
82
14 15 4 9 7 18 3 5 16 4 20 17 9 14 5
1
4
1
4
5
1 1
3 9
4 8
1 2
7 9 6 0
1
5 7
4
Example
Input list of numbers:
83
14 15 4 9 7 18 3 5 16 4 20 17 9 14
1
5
4
1
4
5
1 1
3 9
4 8
1 2
7 9 6 0
1
5 7
Class BinaryTree{
private:
struct node{
int data;
node* LTree;
node* RTree;
};
public:
node* root;
BinaryTree( ){ root = NULL; }
node* Insert(node* , int);
void Search(node* , int);
void InorderTraversal(node*);
void PreorderTraversal(node*);
void PostorderTraversal(node*);
};
Binary Search Tree
85
◻ The above process will continue till the item is found or you
reached end of the tree.
Searching in Binary Search Tree
87
Search Function
88
Three cases:
(1) The node is a leaf
Delete it immediately
(2) The node has one child
Adjust a pointer from the parent to bypass that node
Deletion in BST
93
◻ Inorder traversal of a Binary tree can either be done using recursion or with
the use of a auxiliary stack.
◻ The idea of TBST is to make inorder traversal faster and do it without stack
and without recursion.
◻ A binary tree is made threaded by making all right child pointers that would
normally be NULL point to the inorder successor of the node (if it exists).
◻ A TBST is a type of binary tree data structure where the empty left and right
child pointers in a binary tree are replaced with threads that link nodes
directly to their in-order predecessor or successor, thereby providing a way to
traverse the tree without using recursion or a stack.
◻ TBST can be useful when space is a concern, as they can eliminate the need
for a stack during traversal.
◻ TBST can be more complex to implement than standard binary trees.
Threaded BST (TBST)
99
Threaded BST (TBST)
100
◻ Binary tree a nodes may have at most two children. But if they have only one
children, or no children, the link part in the linked list representation remains null.
◻ In TBST representation, empty links can be reused by making some threads.
◻ If one node has some vacant left or right child area, that will be used as thread.
◻ Right threaded Single TBST: if some node has no right child, then the
right pointer will point to its inorder successor
◻ Double (Two-way/ Fully ) TBST: Where both left and right NULL pointers
are made to point to inorder predecessor and inorder successor
respectively.
◻ The predecessor threads are useful for reverse inorder traversal and
postorder traversal.
◻ The threads are also useful for fast accessing ancestors of a node.
Threaded BST (TBST)
Double (Two-way/ Fully) TBST: -
106
◻ Each node has five fields. Three fields like normal binary tree node,
another two fields to store Boolean value to denote whether link of that
side is actual link or thread.
◻ If no successor or predecessor is present, then it will point to header node.
Threaded BST (TBST)
Double (Two-way/ Fully) TBST: -
107
Threaded BST (TBST)
Double (Two-way/ Fully) TBST: -
108
Threaded BST (TBST)
Double (Two-way/ Fully) TBST: -
109
Threaded BST (TBST)
Double (Two-way/ Fully) TBST: -
110
Threaded BST - Operations
Insert, Delete, Search
111
Threaded BST - Operations
Insert, Delete, Search
112
Threaded BST - Operations
Insert, Delete, Search
113
Threaded BST - Operations
Insert, Delete, Search
114
Threaded BST - Operations
Insert, Delete, Search
115
Threaded BST- InOrder Traversal
116
Threaded BST- InOrder Traversal Algo
117
Algorithm Inorder(I)
{
ThreadedTreeNode *Header;
Header=I;
while(1)
{
I=fnFindInorder_Successor(H);
if(I==Header)
return;
else
print(I->info);
}
}
struct node
{
struct node *left;
boolean lthread;
int info;
boolean rthread;
struct node *right;
}
Threaded BST - Advantages
118
◻ Limited flexibility: Threaded binary trees are specialized data structures that
are optimized for specific types of traversal. While they can be more efficient
than regular binary trees for these types of operations, they may not be as
useful in other scenarios. For example, they cannot be easily modified (e.g.
inserting or deleting nodes) without breaking the threading.
◻ Difficulty in parallelizing: It can be challenging to parallelize operations on a
threaded binary tree, as the threading can introduce data dependencies that
make it difficult to process nodes independently. This can limit the performance
gains that can be achieved through parallelism.
Threaded BST - Applications
121
◻ The time complexity for insertion, deletion, and searching in a threaded binary
tree is the same as that of a BST, as we need to perform operations maximum
up to the depth of the tree. The depth of a threaded BST is
also log(n) where n is the total number of nodes in the tree
◻ However, since we do not use any extra space for any of the operations, the
auxilliary space complexity is O(1)
Threaded BST - Revisited
124
◻ Compression unlike ASCII or Unicode encoding, which use the name number of
bits to encode each character,
◻ Huffman code uses different numbers of bits to encode the letters:
◻ More bits for rare letters, and fewer bits for common letters.
◻ Compression can be achieved further at words level and so on.
◻ Image compression/music compression/text compression/genomic sequence
compression
Huffman Tree /Coding
127
◻ It assigns codes to characters such that the length of the code depends on the
relative frequency or weight of the corresponding character.
◻ Huffman codes are of variable-length, and prefix-free (no code is prefix of
any other). Any prefix-free binary code can be visualized as a binary tree with
the encoded characters stored at the leaves.
◻ A Huffman coding tree or Huffman tree is a full binary tree in which
each leaf of the tree corresponds to a letter in the given alphabet.
◻ Define: the weighted path length of a leaf to be its weight times its depth.
◻ The Huffman tree is the binary tree with minimum external path weight, i.e.,
the one with the minimum sum of weighted path lengths for the given set of
leaves.
◻ So the goal is to build a tree with the minimum external path weight.
Huffman Tree /Coding
128