You are on page 1of 22

• A binary tree T has 12 nodes.

The in-order and


pre-order traversals of T yield the following
sequence of nodes:
In-order: VPNAQRSOKBTM

Pre-order: SPVQNARTOKBM

Construct the binary tree T showing each step.


Explain, how you arrive at solution in brief?

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

Fig: Final Binary Tree

8
Homework

• A binary tree T has 9 nodes. The inorder and


preorder traversals of T yield the following
sequence of nodes:
In-order: E A C K F H D B G
Pre-order: F A E K C D H G B
Draw the tree T.

9
Answer: F

A D

E K H G

C B

Fig: Final Binary Tree


10
Homework

• The following sequence gives the preorder


and inorder of the binary tree T:
Preorder: A B D G C E H I F
Inorder: D G B A H E I C F
Draw the diagram of the tree T.

11
Answer:
A

B C

D E F

G H I

Fig: Final Binary Tree


12
Home Work
Construct a binary tree considering the following
sequences:
In order:20, 30, 35, 40, 45, 50, 55, 60, 70
Post order: 20, 35, 30, 45, 40, 55, 70, 60, 50
Huffman Algorithm (Huffman Coding)
• Huffman codes are used to compress data by representing
each alphabet by a unique binary code (or bit-string) in an
optimal way.
• Consider the problem of using bit strings to encode the letters of the
English alphabet (where no distinction is made between lowercase
and uppercase letters). We can represent each letter by a bit string
of length 5, because there are only 26 letters and there are 2 5 (=32)
bit strings of length 5. Here, the total number of bits used to encode
data is 5 times the number of characters in the text when each
character is encoded with 5 bits.
• Problem and Solution: If it is possible to find a coding
scheme with fewer bits to code these letters then we can
save memory and also reduce the transmittal time of the
data. 14
Huffman Coding…
• To encode letters using different length bit-strings, the letters
that occur more frequently are encoded using shorter bit-
strings and letters occurring rarely are encoded using longer
bit-strings.
• When letters are encoded using varying numbers of bits, some
method must be used to determine where the bits for each
character start and end.
• For example, if e were encoded with 0, a with 1, and t with 01,
then the bit string 0101 could represent eat, tea, eaea, or tt.
• One way to ensure that no bit-string corresponds to more than
one sequence of letters is to encode letters so that the bit
string for a letter never occurs as the first part of the bit string
for another letter. Codes with this property are called prefix
codes.
15
Huffman Coding…
• The Huffman coding or Huffman algorithm takes as input the
frequencies (which are the probabilities of occurrences or
repetitions known in advance) of symbols in a string of text and
produces as output a prefix code that encodes the string using the
fewest possible bits, among all possible binary prefix codes for
these symbols.
• Given symbols and their frequencies, the goal is to construct a
rooted binary tree where the symbols are the labels of the leaves.
• The algorithm begins with a forest of trees each consisting of one vertex, where
each vertex has a symbol as its label and where the weight of this vertex equals the
frequency of the symbol that is its label. At each step, we combine two trees having
the least total weight into a single tree by introducing a new root and placing the
tree with larger weight as its left subtree and the tree with smaller weight as its
right subtree. Furthermore, we assign the sum of the weights of the two subtrees of
this tree as the total weight of the tree. The algorithm is finished when it has
constructed a tree, that is, when the forest is reduced to a single tree.
16
Algorithm
1. Start
2. Take characters and their frequencies and sort them by
increasing order of their frequencies.
3. Add two lowest frequencies and form a tree and assume the
root of this tree as new item.
4. Insert the new tree in to its proper position in the sorted list.
5. Repeat 3 and 4 until a single tree is formed containing all the
characters.
6. Assign a prefix code for binary tree. Assign 0 for left child and 1
for right child.
7. Stop
Applications:
 Both .MP3 and .jpg file format use this coding at one stage of compression.
 Used for standard data compression
Question: Use Huffman coding to encode the following symbols with
the frequencies listed: A:0.08, B:0.10, C:0.12, D:0.15, E:0.20, F:0.35.
What is the average number of bits used to encode a character?

Answer:

0.08 0.10 0.12 0.15 0.20 0.35


Initial
A B C D E F Forest

0.12 0.15 0.18 0.20 0.35

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.27 0.35 0.38


0 1 Step3
F 0 1
D C E 0 1
B A
0.38 0.62
0 1 0 1
E 0 1 0 1 Step4
F
B A D C 19
1.00

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

You might also like