Professional Documents
Culture Documents
Traversal To Binary Tree
Traversal To Binary Tree
Pre-order: SPVQNARTOKBM
1
Answer: The binary tree T is constructed from its root
downwards as follows:
a) The root of binary tree T is obtained by choosing
the first node in its preorder (since preorder is root-
left-right). Thus the node S is the root of tree T.
b) The left child of the root node S is obtained as follows: First we
use the inorder of T to find the nodes in the left subtree T1 of S
S Thus the left subtree T1 consists
(since inorder is left-root-right). root
of the nodes V, P, N, A, Q and R. Then the left child of S is
obtained by choosing the first node in the preorder of T1 (which
clearly appears in the preorder of T). Thus P is the left child of S.
S
P 2
c) Similarly, the right subtree T2 of S consists of the nodes O, K, B, T
and M. Thus the right child of S is obtained by choosing the first
node in the preorder of T2. Thus the node T is the right child of S.
P T
d) Now, we need to find the left child of P. By seeing the inorder of tree
T, we find that the left subtree T1 of P consists of only one node i.e. V.
Thus the left child of P is V.
S
P T
V 3
e) The right subtree T2 of P consists of the nodes N, A, Q
and R. Thus the right child of P is obtained by choosing
the first node in the preorder of T2. Thus the node Q is
the right child of S. S
P T
V Q
f) The left subtree T1 of node T consists of the nodes
O, K and B. By seeing the preorder of T1, we find
that the left child of T is O.
S
P T
V Q O 4
g) By seeing the inorder traversal, we find that the right subtree T2
of node T consists of only one node M. Thus the right child of
node T is M.
S
P T
V Q O M
h) Now node V does not have any child because it is the leftmost node
according to the inorder traversal. Therefore it’s a leaf.
i) To find the left child of node Q, the left subtree T1 of node Q consists
of the nodes N and A (since V and P are already occupied). By seeing
the preorder, it is clear that the left child of node Q is N.
S
P T
V Q O M
N 5
j) The right subtree T2 of node Q consists of only one node R. Thus
the right child of node Q is R.
S
P T
V Q O M
N R
k) Now node O does not have a left child. By seeing the inorder, the
right subtree of node O consists of nodes K and B. By seeing the
preorder, node K is the right child of node O.
S
P T
V Q O M
N R K 6
l) Node M is also a leaf node since it is the rightmost node in the
inorder traversal.
m) Node N does not have a left child since its left subtree is empty.
n) The right subtree of node N consists of the only one node i.e. A.
S
P T
V Q O M
N R K
A
o) Node K does not have a left child as its left subtree is empty
which is obvious from its inorder.
p) The right subtree of node K contains the only remaining node i.e.
B.
7
Hence the final binary tree T is:
S
P T
V Q O M
N R K
A B
8
Homework
9
Answer: F
A D
E K H G
C B
11
Answer:
A
B C
D E F
G H I
Answer:
0 1 Step1
C D E F
B A
18
0.18 0.20 0.27 0.35
0 1 0 1 Step2
E F
B A D C
0 1
0 1 0 1
0 1 E 0 1 Step5
F
D C B A
Fig: Final Huffman Tree
(or Huffman Coding of symbols)
Thus the Huffman encoding produced encode A by 111, B by 110, C by 011, D
by 010, E by 10, and F by 00.
Thus the average number of bits used to encode a symbol using Huffman
encoding = 3*0.08 + 3*0.10 + 3*0.12 + 3*0.15 + 2*0.20 + 2*0.35 = 2.45.
20
The Huffman Algorithm
Assumptions:
1. Let |C| be the number of symbols ai with frequencies wi, i=1, 2, …, n.
2. The tree is constructed in bottom-up manner starting from the |C| leaves and |C|-1 merging operations.
3. We use priority queue Q to keep nodes ordered by their frequencies.
Algorithm:
HuffmanAlgorithm(C)
{
n=|C|;
Q=C;
for(i=1;i<=n-1;i++)
{
z=Allocate_Node();
x=Extract_Min(Q);
y=Extract_Min(Q);
right(z)=x;
label_edge(z, x)=1;
left(z)=y;
label_edge(z, y)=0;
w(z)=w(x)+w(y);
Insert(Q, z);
}
} // The Huffman coding for the symbol ai is the concatenation of the labels // of the edges in the unique path
21
from the root to the vertex ai.
Home work
• Construct Huffman tree for the following
symbols with their frequencies: F:22, E:18,
B:15, D:12, A: 10, G: 14, C:9