DSA Trees
DSA Trees
DATA STRUCTURES
Module4
Radhika Chapaneri
SE Computer Science
Sem III
Radhika Chapaneri
Trees
• Tree is a nonlinear data structure which stores the data
elements in a hierarchical manner.
• A tree consist of a finite set of elements called nodes and
finite set of directed lines called branches that connect the
nodes.
• A very good example of a tree data structure is directory
systems in computers.
Radhika Chapaneri
T is a simple tree containing ten nodes with A being the root node. The
node A contains two subtrees.
A
B C
D E F
G H3 I J
Tree T
Radhika Chapaneri
Key Terms
• Root: It is the top node of tree.
• Example A.
• Parent: A node that has one or more child nodes present
below it is referred as Parent node. Eg: B is parent of D
and E
• Child: All nodes in a tree except the root node are child
nodes of their immediate predecessor. Eg. H, I ,J are child
nodes of E
• Leaf: It is terminal node that does not have any child
nodes. Eg. G,H,I,J and F are leaf nodes.
• Internal nodes: All nodes except root and leaf nodes are
referred as internal nodes. Eg. B,C,D,E are internal nodes.
Radhika Chapaneri
Key Terms
• Siblings: All the child nodes of same parent nodes are referred
as siblings. Eg. D and E are sibling.
• Path: It is the sequence of node from source node till
destination node. Eg. A-B-E-J
• Level: The level of the node is equal to length of the path from
root to the node.
• Eg: The root has level 0
• Level of B, C =1, Level of D,E,F= 2, Level of G,H, I, J = 3
• Degree of node: The degree of node is number of children it
has. The degree of leaf = 0, Degree of E = 3.
• Degree of a tree: The degree of a tree is the maximum of its
node degrees.
• The degree of Tree = 3
Radhika Chapaneri
Key Terms
• Height/ Depth of a tree : Of the level of the root is
denoted by 0, then
• Height/ Depth of tree = maximum{ level of all nodes in the
tree}
• Height = 3 for the tree T
Radhika Chapaneri
Binary tree
• Binary Tree is one of the most widely used non linear data
structure in the field of computer science.
• It is restricted form of general tree. The restriction that it
applies to a general tree is that its nodes can have
maximum degree of 2. that means, the node of a binary
tree can have zero, one or two child nodes but not more
than that.
• The maximum number of nodes that can be present at
level n is 2n
Radhika Chapaneri
A
B C
D E F
G H3 I
Radhika Chapaneri
A
B C
D E F G3
H I J K L M N O
Radhika Chapaneri
B C
D E F G
• Array representation will be
B C
D G
• The revised array representation will be
B C
X D X X E X X F X X G X
Radhika Chapaneri
B C
D E F
G
• Preorder: A – B – D – E – G – C - F
Radhika Chapaneri
B C
D E F
G
• Inorder: D - B – G – E – A – C - F
Radhika Chapaneri
• L –R- N
Radhika Chapaneri
B C
D E F
G
• Postorder: D – G – E – B- F – C - A
Radhika Chapaneri
D, C, B G, F, H , I, E
Radhika Chapaneri
B G, F, H , I, E
D, C
Radhika Chapaneri
B G, F, H , I, E
D
Radhika Chapaneri
B E
C G, F, H , I
D
Radhika Chapaneri
B E
C F
G H,I
D
Radhika Chapaneri
B E
C F
G H
D
I
Radhika Chapaneri
INFO MATION
Radhika Chapaneri
INFO T
MA ION
Radhika Chapaneri
INFO T
MA O
I N
Radhika Chapaneri
INFO T
A O
M I N
Radhika Chapaneri
F T
IN O A O
M I N
Radhika Chapaneri
F T
N O A O
I M I N
Radhika Chapaneri
F
Radhika Chapaneri
D
Radhika Chapaneri
D CE
Radhika Chapaneri
D CE
A
Radhika Chapaneri
D CE
A B
Radhika Chapaneri
D E
A B C
Radhika Chapaneri
18
3 37
1 25 40
11
8 13 21 42
Create a binary search trees using the following data values
45, 39, 56, 12, 34, 78, 32, 10, 89, 54, 67
45 45
45 45
39 56 39 56
39
12
45 45 45
39 56 39 56 39 56
12 12 78 12 78
34 34 34
32
Create a binary search trees using the following data values
45, 39, 56, 12, 34, 78, 32, 10, 89, 54, 67
45 45
45
39 56 39 56
39 56
12 78 12 54 78
12 54 78
10 34 10 34 67 89
10 34 89
32
32
32
Radhika Chapaneri
Search in BST
void search(struct node* root,int n)
{
if(root == NULL)
{
printf("\n%d not present in the tree",n);
return;
}
else if(n== root->data)
printf("\n Element %d is present in the tree",n);
else if(n< root->data)
search(root->left,n);
else
search(root->right,n);
}
Radhika Chapaneri
18
18
3 37
3 37
1 11 25 42
1 11 25 40
8 13
8 13 21
21 42
• Before deletion After deletion of 40
Radhika Chapaneri
21
18
3 37
3 37
1 11 25 40
1 11 25 40
8 13 42
8 13 21 42
• Before deletion After deletion of 18
Radhika Chapaneri
Delete Node
struct node* deleteNode(struct node*root,int value)
{
if(root == NULL)
cout<<"No node to delete";
else if(value == root->data)
{
root= deleteRoot(root,value);
return root;
}
else if(value<root->data)
root->left= deleteNode(root->left, value);
else
root->right = deleteNode(root->right, value);
return root;
}
Radhika Chapaneri
}
Radhika Chapaneri
Expression tree
• Expression tree is a binary tree containing mathematical
expression
• The internal nodes of the tree used to store operator while
the leaf nodes used to store operands
• Various compilers and parsers use expression tree for
evaluating arithmetic and logical expression
• The expression tree is constructed according to their
order of precedence and associative law of operators
• For Eg: The parenthesis are evaluated first then the
exponent, followed by multiplication or division and then
addition/ subtraction
Radhika Chapaneri
Expression tree
• The multiplication and division is associated from left to
right so whichever comes first will be evaluated first
• Similarly addition and subtraction is also assoaciative
from left to right.
• The expression tree can be easily constructed by dividing
the expression into sub expression and then combining
according to the order of precedence.
• Although the expression may contain parenthesis but
these are not included in the expression tree.
Radhika Chapaneri
Expression tree
• Example: Construct the expression tree for the expression
• a * (b + c) / d – e
• Step1: First parenthesis is evaluated so construct the tree
for b+c
+
b c
Radhika Chapaneri
Expression tree
• Step2: The result of (b+c) will be multiplied by a so now
construct the expression tree for a * (b+c)
*
a +
b c
Radhika Chapaneri
Expression tree
• Step 3: The result of a*(b+c) will be divided by d so now
the expression tree for a*(b+c) / d
/
* d
a +
b c
Radhika Chapaneri
* d
a +
b c
Radhika Chapaneri
Expression tree
• Since expression tree is also binary tree it can be
traversed in all three ways and
• Inorder traversal -> infix expression
• Preorder traversal -> prefix expression
• Postorder traversal -> postfix expression
• For the above example
• Inorder traversal -> infix expression = a*b+c/d-e
• Preorder traversal -> prefix expression = -/*a+bcde
• Postorder traversal -> postfix expression = abc+*d/e-
Radhika Chapaneri
Expression tree
• Creation of an expression tree using postfix expression
• Steps:
• Read each symbol from left to right one character at a time
• If we read an operand then we will make a node of it and
push it onto stack
• If we read operator then pop two nodes from stack, the first
popped node will be attached as a right child to operator
node and second popped node will be attached as a left
child to operator node.
Radhika Chapaneri
b
a
Radhika Chapaneri
+
a b
Radhika Chapaneri
d
c
+
a b
Radhika Chapaneri
-
c d
+
a b
Radhika Chapaneri
*
+ -
a b c d
Radhika Chapaneri
Threaded Trees
• Binary trees have a lot of wasted space: the leaf nodes
each have 2 null pointers.
• Threaded binary tree offers an innovative alternate to
avoid this memory wastage.
• The Null entries can be replaced to store a pointer to an
inorder predecessor or inorder successor of the node.
• These special pointers are called threads and binary tree
containing threads are called threaded tree.
Radhika Chapaneri
struct tnoode {
struct tnode *left, *right;
boolean LThread, RThread;
}
Threaded tree
• A binary tree is threaded in two ways
• One way threading
• Two way threading
• One way threading: In one way threading a thread will
appear either in the right field or left field of the node.
• A one way threading is also called single threaded tree
• They are again of two type
• Left threaded: If the thread appears in the left field, then
the left field will be made to point to inorder predecessor.
• Right threaded: If the thread appears in the right field,
then the right field will be made to point to inorder
successor.
Radhika Chapaneri
Left threaded: If the thread appears in the left field, then the left field will
be made to point to inorder predecessor.
6
3 8
1 5 7 11
9 13
Inorder traversal: 1, 3, 5,6,7,8,9,11,13
Right threaded: If the thread appears in the right field, then the right
field will be made to point to inorder successor.
6
3 8
1 5 7 11
9 13
Left Data Right Rthread
0 – if thread
present
1- if no thread
Radhika Chapaneri
Two way threaded is also called double threaded tree, threads will
appear in both the left and right field of the node . The left field will point
to inorder predecessor and the right field will point to inorder successor
6
3 8
1 5 7 11
9 13
Rthread Left Data Right Rthread
0 – if thread present 0 – if thread
1- if no thread present
1- if no thread
Radhika Chapaneri
3 8
1 5 7 11
9 13
Start at leftmost node, print it
Radhika Chapaneri
3 8
1 5 7 11
9 13
Follow thread to right, print node
Radhika Chapaneri
3 8 5
1 5 7 11
9 13
Follow link to right, go to
leftmost node and print
Radhika Chapaneri
3 8 5
6
1 5 7 11
9 13
Follow thread to right, print node
Radhika Chapaneri
3 8 5
6
7
1 5 7 11
9 13
Follow link to right, go to
leftmost node and print
Radhika Chapaneri
3 8 5
6
7
1 5 7 11 8
9 13
Follow thread to right, print node
Radhika Chapaneri
3 8 5
6
7
1 5 7 11 8
9
9 13
Follow link to right, go to
leftmost node and print
Radhika Chapaneri
3 8 5
6
7
1 5 7 11 8
9
11
9 13
Follow thread to right, print node
Radhika Chapaneri
3 8 5
6
7
1 5 7 11 8
9
11
9 13 13
Huffman Algorithm
• Fixed Length Character Encoding:
• Computers usually use fixed-length character encodings.
• •ASCII uses 8 bits per character.
• example: “bat” is stored in a text file as the following
sequence of bits:
• 01100010 01100001 01110100
• Fixed-length encodings are simple, because
• All character encodings have the same length
• A given character always has the same encoding
Radhika Chapaneri
Huffman Algorithm
• Variable Length Character Encoding:
• Problem: fixed-length encodings waste space.
• Solution: use a variable-length encoding.
• Assign shorter encodings to frequently occurring
characters
Radhika Chapaneri
Huffman Code
• Approach
• Variable length encoding of symbols
• Principle
• Use fewer bits to represent frequent symbols
• Use more bits to represent infrequent symbols
• Obey prefix property.
A A B A
A A B A
Radhika Chapaneri
• Expected size
• Original 1/82 + 1/42 + 1/22 + 1/82 = 2 bits / symbol
• Huffman 1/83 + 1/42 + 1/21 + 1/83 = 1.75 bits / symbol
Radhika Chapaneri
3 5 8 2 7
Radhika Chapaneri
10 5 8 7
1
0
2 3
H A
Radhika Chapaneri
10 8 7
1
0
5
5
1
0
3 2
A H
Radhika Chapaneri
5
5
I 7 8 E
C
1
0
3 2
A H
Radhika Chapaneri
2 3 H
A
Radhika Chapaneri
Huffman Decoding
• Read one bit at a time and traverse the tree, starting from
the root:
• when you read a bit of 1, go to the right child
• when you read a bit of 0, go to the left child
• when you reach a leaf node, record the character,
• return to the root, and continue reading bits
Radhika Chapaneri
2 3 H
A
Radhika Chapaneri
3 2 A
H
A
Radhika Chapaneri
3 2 AC
H
A
Radhika Chapaneri
3 2 ACE
H
A
Radhika Chapaneri
Example
• Ax={ a , b , c , d , e }
• Px={0.25, 0.25, 0.2, 0.15, 0.15}
1.0
0
0.55 1
0
0.45 0.3
0 1 0 1
a b c d e
0.25 0.25 0.2 0.15 0.15
00 10 11 010 011
Radhika Chapaneri
Huffman Steps
Radhika Chapaneri