0% found this document useful (0 votes)
198 views122 pages

DSA Trees

The document discusses trees and binary trees as data structures. It defines key terms like root, parent, child, leaf nodes, internal nodes, siblings, path, level, degree of a node, height/depth of a tree. It describes different types of binary trees like strictly binary tree, complete binary tree, perfect binary tree. It also explains two representations of binary trees - array and linked representation. Finally, it covers three tree traversal techniques - preorder, inorder and postorder traversal and provides examples.

Uploaded by

Aryan Jain
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
198 views122 pages

DSA Trees

The document discusses trees and binary trees as data structures. It defines key terms like root, parent, child, leaf nodes, internal nodes, siblings, path, level, degree of a node, height/depth of a tree. It describes different types of binary trees like strictly binary tree, complete binary tree, perfect binary tree. It also explains two representations of binary trees - array and linked representation. Finally, it covers three tree traversal techniques - preorder, inorder and postorder traversal and provides examples.

Uploaded by

Aryan Jain
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 122

Radhika Chapaneri

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

Basic Concepts of Tree


• A tree is defined as a finite set of elements or nodes such
that
• 1. One of the nodes present at the top of the tree is
marked as a root node
• 2. The remaining elements are partitioned across multiple
subtree present below the root node.
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

Binary tree example

A
B C
D E F
G H3 I
Radhika Chapaneri

Types of Binary tree.


Strictly Binary tree: A binary tree is called strictly
binary tree if all its nodes except the leaf nodes contain
two child nodes.
A
B C
D E
K
F G3 J
Radhika Chapaneri

Types of Binary tree.


Complete Binary tree: A binary tree of depth d is called
complete binary tree if all its level from 0 to d-1 contain
maximum possible number of nodes and all the leaf
nodes present at level d are placed towards the left
side.
A
B C
D E F G3
H I J K L
Radhika Chapaneri

Types of Binary tree.


Perfect/ Full Binary tree: A binary tree is called perfect
binary tree if all its leaf nodes are at lowest level and all
non leaf nodes contain two child nodes.

A
B C
D E F G3
H I J K L M N O
Radhika Chapaneri

Binary tree representation


• There are two types of representation:
• Sequential representation or Array representation
• Linked representation
Radhika Chapaneri

Binary tree representation


• Sequential representation or Array representation:
• In the array representation of binary tree, one dimensional
array is used for storing the node elements in the array.
• The following rules are applied while storing the node
elements in the array
• 1. The root node is stored at first position in the array
while its left and right child nodes are at successive
position.
• 2. If a node is stored at index location i, then its left child
node will be stored at location 2i+1 while right child will
stored at location 2i+2.
Radhika Chapaneri

Binary tree representation(Array)


• Let us consider a binary tree T

B C

D E F G
• Array representation will be

A[0] A[1] A[2] A[3] A[4] A[5] A[6]


A B C D E F G
Radhika Chapaneri

Binary tree representation(Array)


• Now let us modify tree by deleting node E and F

B C

D G
• The revised array representation will be

A[0] A[1] A[2] A[3] A[4] A[5] A[6]


A B C D - - G
Radhika Chapaneri

Binary tree representation(Array)


• As seen for above example, even after removing two
elements from the tree, it still requires the same number
of memory locations for storing the node elements. This is
the main disadvantage of array representation of binary
trees.
• It efficiently utilizes the memory space only when the tree
is complete binary tree. Otherwise there are always some
memory locations lying vacant in the array.
Radhika Chapaneri

Binary tree representation(Linked)


• Linked representation:
• In the linked representation of binary tree, every node will
have 3 part : data element, pointer to left node and pointer
to right node.
• So in C, binary tree is built with a node type given below
struct node
{
int data;
struct node* left;
struct node* right;
};
Radhika Chapaneri

Binary tree representation(Linked)

B C

X D X X E X X F X X G X
Radhika Chapaneri

Binary Tree Traversal


• There are three types of traversal
• 1. Preorder Traversal
• 2. Inorder Traversal
• 3. Post order traversal
Radhika Chapaneri

Binary Tree Traversal (Preorder)


• Preorder: The preorder method perform the following
operation
• A) Process the root node N
• B) Traverse the left subtree of N (L)
• C) Traverse the right subtree of N (R)
• N- L -R
Radhika Chapaneri

Binary Tree Traversal (Preorder)

B C

D E F

G
• Preorder: A – B – D – E – G – C - F
Radhika Chapaneri

Binary Tree Traversal (Preorder)


void preorder(struct node* root)
{
if(root!=NULL)
{
cout<<root->data;
preorder(root->left);
preorder(root->right);
}
}
Radhika Chapaneri

Binary Tree Traversal (Inorder)


• Inorder: The inorder method perform the following
operation
• A) Traverse the left subtree of N (L)
• B) Process the root node N
• C) Traverse the right subtree of N (R)
• L- N -R
Radhika Chapaneri

Binary Tree Traversal (Inorder)

B C

D E F

G
• Inorder: D - B – G – E – A – C - F
Radhika Chapaneri

Binary Tree Traversal (Inorder)


void inorder(struct node* root)
{
if(root!=NULL)
{
inorder(root->left);
cout<<root->data;
inorder(root->right);
}
}
Radhika Chapaneri

Binary Tree Traversal (Post order)


• Postorder: The postorder method perform the following
operation
• A) Traverse the left subtree of N (L)
• B) Traverse the right subtree of N (R)
• C) Process the root node N

• L –R- N
Radhika Chapaneri

Binary Tree Traversal (Post order)

B C

D E F

G
• Postorder: D – G – E – B- F – C - A
Radhika Chapaneri

Binary Tree Traversal (Post order)


void postorder(struct node* root)
{
if(root!=NULL)
{
postorder(root->left);
postorder (root->right);
cout<<root->data;
}
}
Radhika Chapaneri

Reconstruction of Binary tree


• For the given sequence construct the binary tree
• Pre order: A, B, C , D, E, F, G, H, I
• Inorder: D, C, B, A, G, F, H, I , E
• Step 1:

D, C, B G, F, H , I, E
Radhika Chapaneri

Reconstruction of Binary tree


• Step 2:
A

B G, F, H , I, E

D, C
Radhika Chapaneri

Reconstruction of Binary tree


• Step 3:
A

B G, F, H , I, E

D
Radhika Chapaneri

Reconstruction of Binary tree


• Step 4:
A

B E

C G, F, H , I

D
Radhika Chapaneri

Reconstruction of Binary tree


• Step 5:
A

B E

C F

G H,I
D
Radhika Chapaneri

Reconstruction of Binary tree


• Step 6:
A

B E

C F

G H
D
I
Radhika Chapaneri

Reconstruction of Binary tree


• For the given sequence construct the binary tree
• Inorder: INFORMATION
• Post order: INOFMAINOTR
• Step 1:

INFO MATION
Radhika Chapaneri

Reconstruction of Binary tree


• Step 2:

INFO T

MA ION
Radhika Chapaneri

Reconstruction of Binary tree


• Step 3:
R

INFO T

MA O

I N
Radhika Chapaneri

Reconstruction of Binary tree


• Step 3:
R

INFO T

A O

M I N
Radhika Chapaneri

Reconstruction of Binary tree


• Step 3:
R

F T

IN O A O

M I N
Radhika Chapaneri

Reconstruction of Binary tree


• Step 3:
R

F T

N O A O

I M I N
Radhika Chapaneri

Reconstruction of Binary tree


• For the given sequence construct the binary tree
• Inorder: GFDABEC
• Post order: ABDCEFG
• Step 1:

F
Radhika Chapaneri

Reconstruction of Binary tree


• Step 2:
G

D
Radhika Chapaneri

Reconstruction of Binary tree


• Step 3:
G

D CE
Radhika Chapaneri

Reconstruction of Binary tree


• Step 4:
G

D CE

A
Radhika Chapaneri

Reconstruction of Binary tree


• Step 5:
G

D CE

A B
Radhika Chapaneri

Reconstruction of Binary tree


• Step 6:
G

D E

A B C
Radhika Chapaneri

Binary Search Tree


• A binary search tree is a special type of a binary tree.
• For each node n in a binary search tree, the value of the
item stored by node is greater than all of the values in left
subtree and less than all of the values in the right subtree.
• This implies that all sub trees must be binary search tree.
Radhika Chapaneri

Binary search tree

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

Binary search tree


• struct node{
• int data;
• struct node* left;
• struct node* right;
• };
Radhika Chapaneri

Algorithm to insert a value in the BST

• Algorithm to insert a value in the binary search tree-


• Insert(Tree , val)
• Step 1: IF TREE = NULL, then
• Allocate memory for TREE
• SET TREE->DATA = VAL
• SET TREE->LEFT = TREE ->RIGHT = NULL
• ELSE
• IF VAL < TREE->DATA
• Insert(TREE->LEFT, VAL)
• ELSE
• Insert(TREE->RIGHT, VAL)
• [END OF IF]
• [END OF IF]
• Step 2: End
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

Deletion from BST


• The delete function deletes a node from the binary serach
tree.
• However utmost care should be taken that the properties
of the binary search tree does not get violated and nodes
are not lost in the process.
• There are three case in deletion
• Case 1. Deletion of leaf node
• Case 2. Deletion of node having one child
• Case 3. Deletion of node having two children.
Radhika Chapaneri

Deletion from BST(Case 1)


• Case 1: Deletion of leaf node or node that has no
children
18
18
3 37
3 37
1 11 25 40
1 11 25 40
8 13
8 13 21
21 42
• Before deletion After deletion of 42
Radhika Chapaneri

Deletion from BST(Case 1)


• This is the simplest deletion in which we set the left or
right pointer of parent node as NULL
• From the tree we want to delete node having value 42
then we set right pointer of its parent node as NULL that is
right pointer of node having value 40 is set to NULL and
node with value 42 is deleted.
Radhika Chapaneri

Deletion from BST(Case 2)


• Case 2: Deletion of a node having one child

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

Deletion from BST(Case 2)


• If the node to be deleted is the left child of its parent, the
node’s child becomes the left child of the node’s parent.
• Similarly, If the node was the right child of its parent, the
node’s child becomes the right child of node’s parent.
Radhika Chapaneri

Deletion from BST(Case 3)


• Case 3: Deletion of node having two children
• If the node contains the value to be deleted has two
children, we must find replacement value to place in the
node.
• To handle this case, replace node’s value with its inorder
predecessor ( right most child of the left subtree) or
inorder successor ( left most child of right subtree)
Radhika Chapaneri

Deletion from BST(Case 3)


• Case 3: Deletion of node having two children

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

Delete Root (Case 1)


struct node* deleteRoot(struct node*root,int value)
{
if(root->left == NULL && root->right == NULL)
{
root = NULL;
delete root;

}
Radhika Chapaneri

Delete Root (Case 2)


else if(root->left == NULL)
{
struct node* temp = root;
root = root->right;
delete temp;
}
else if (root->right == NULL)
{
struct node* temp = root;
root = root->left;
delete temp;
}
Radhika Chapaneri

Delete Root ( Case 3)


else
{
node* temp = FindMin(root->right);
root->data = temp->data;
root->right = deleteNode(root->right,temp->data);
}
return root;
Radhika Chapaneri

Delete Root ( Case 3)


• node* FindMin(node* root)
•{
• while(root->left != NULL)
• root = root->left;
• return root;
•}
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

• Step 4: operand e is subtracted from the results of


a*(b+c)/d so now construct the expression tree for
a*(b+c)/d -e
-
/ e

* 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

Expression tree using postfix expression


• Example:
• Consider a postfix expression stored in array

a b + c d - *

• 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 the stack.
• If we read operator then pop two nodes from stack the fist
popped node will be attached as right child to operator
node and second popped node will be attached as left
child of the operator node.
Radhika Chapaneri

Expression tree using postfix expression


• a b + c d - *

• Step 1: operand ‘a’ push it onto the stack


• Step 2: operand ‘b’ push it onto the stack

b
a
Radhika Chapaneri

Expression tree using postfix expression


• a b + c d - *

• Step 3: operator ‘+’ so pop two operands make a node


and push it onto stack

+
a b
Radhika Chapaneri

Expression tree using postfix expression


• a b + c d - *

• Step 4: operand ‘c’ push it onto the stack


• Step 5: operand ‘d’ push it onto the stack

d
c
+
a b
Radhika Chapaneri

Expression tree using postfix expression


• a b + c d - *

• Step 6: operator ‘-’ so pop two operands make a node


and push it onto stack

-
c d
+
a b
Radhika Chapaneri

Expression tree using postfix expression


• a b + c d - *

• Step 7: operator ‘* so pop two operands make a node.


• As we reached last element of the array.This is our final
expression tree

*
+ -
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

Threaded Tree Example


6
3 8
1 5 7 11
9 13
Radhika Chapaneri

Threaded Tree node


• The node structure will be

struct tnoode {
struct tnode *left, *right;
boolean LThread, RThread;
}

• We need to know if a pointer is an actual link or a thread,


so we keep a boolean for each pointer
Radhika Chapaneri

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

Lthread left Data Right


0 – if thread present
1- if no thread
Radhika Chapaneri

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

Threaded Tree Traversal


• We start at the leftmost node in the tree, print it, and
follow its right thread
• If we follow a thread to the right, we output the node and
continue to its right
• If we follow a link to the right, we go to the leftmost node,
print it, and continue
Radhika Chapaneri

Threaded Tree Traversal


Output
6 1

3 8
1 5 7 11
9 13
Start at leftmost node, print it
Radhika Chapaneri

Threaded Tree Traversal


Output
6 1
3

3 8
1 5 7 11
9 13
Follow thread to right, print node
Radhika Chapaneri

Threaded Tree Traversal


Output
6 1
3

3 8 5

1 5 7 11
9 13
Follow link to right, go to
leftmost node and print
Radhika Chapaneri

Threaded Tree Traversal


Output
6 1
3

3 8 5
6

1 5 7 11
9 13
Follow thread to right, print node
Radhika Chapaneri

Threaded Tree Traversal


Output
6 1
3

3 8 5
6
7
1 5 7 11
9 13
Follow link to right, go to
leftmost node and print
Radhika Chapaneri

Threaded Tree Traversal


Output
6 1
3

3 8 5
6
7
1 5 7 11 8

9 13
Follow thread to right, print node
Radhika Chapaneri

Threaded Tree Traversal


Output
6 1
3

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

Threaded Tree Traversal


Output
6 1
3

3 8 5
6
7
1 5 7 11 8
9
11
9 13
Follow thread to right, print node
Radhika Chapaneri

Threaded Tree Traversal


Output
6 1
3

3 8 5
6
7
1 5 7 11 8
9
11
9 13 13

Follow link to right, go to


leftmost node and print
Radhika Chapaneri

Advantages of threaded binary tree


• It enables linear traversal of elements in tree
• Linear traversal eliminates the use of stacks which in turn
consume lot of memory space and computer time.
• It enables to find parent of a given element without explicit
use of parent pointer
• Since nodes contains pointer to inorder predecessor and
successor, the threaded tree enables forward and
backward traversal of the nodes.
Radhika Chapaneri

Algorithm for inorder traversal of Threaded binary


tree
• Inorder(node)
• Step1: start
• Step 2: set current = leftmost(node)
• Step 3: while (current !=Null) repeat steps 4-7
• Step4 : Display current
• Step 5: if current->Rthread ! = 1 goto step 6 else goto
step 7
• Step 6: set current = current->right
• Step 7: set current = leftmost(current->right)
Radhika Chapaneri

Algorithm for inorder traversal of Threaded binary


tree
• Leftmost(node)
• Step1: start
• Step 2: set ptr = node
• Step3: if(ptr =NULL) go to step 4 else go to step 5
• Step 4: Return NULL and go to step 8
• Step 5: while(ptr->left) != Null repeat step 6
• Step 6: set ptr = ptr->left
• Step 7: return ptr
• Step 8: stop
Radhika Chapaneri

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

Huffman Code Example


Symbol A B C D
Frequency 13% 25% 50% 12%
Original 00 01 10 11
Encoding 2 bits 2 bits 2 bits 2 bits
Huffman 110 10 0 111
Encoding 3 bits 2 bits 1 bit 3 bits

• Expected size
• Original  1/82 + 1/42 + 1/22 + 1/82 = 2 bits / symbol
• Huffman  1/83 + 1/42 + 1/21 + 1/83 = 1.75 bits / symbol
Radhika Chapaneri

Prefix Property : Huffman Code


 Consider a 7-symbol string a1 a3 a2 a1 a3 a3 a4

 Encoding this string with Code1

1| 010 | 01 | 1 | 010 | 010 | 001 |

 But what happens when we decode the above signal?


Radhika Chapaneri

Prefix Property : Huffman Code


 Code1 is ambiguous. In contrast, Code2, which has the same
average size as Code1, can be decoded unambiguously.

 Prefix Property: once a certain bit pattern has been assigned as


the code of a symbol, no other codes should start with that pattern
(the pattern cannot be the prefix of any other code).
Example
Huffman(“I”)  00
Huffman(“X”)  001 // not legal prefix code
Radhika Chapaneri

Construction of Huffman tree


• Leaf nodes are characters.
• Left branches are labeled with a 0, and right branches are
labeled with a 1.
• If you follow a path from root to leaf, you get the encoding
• of the character in the leaf . example: 101 = ‘i’
Radhika Chapaneri

Steps of Huffman Algorithm


1.Begin by reading through the text to determine the frequencies.
2. Create a list of nodes that contain (character, frequency) pairs
for each character that appears in the text.
3. Remove and “merge” the nodes with the two lowest
frequencies, forming a new node that is their parent.
• left child = lowest frequency node
• •right child = the other node
• •frequency of parent = sum of the frequencies of its children
4. Add the parent to the list of nodes:
5.Repeat steps 3 and 4 until there is only a single node in the list,
which will be the root of the Huffman tree.
Radhika Chapaneri

Huffman Tree Construction 1


A C E H I

3 5 8 2 7
Radhika Chapaneri

Huffman Tree Construction 2


C E I

10 5 8 7

1
0

2 3

H A
Radhika Chapaneri

Huffman Tree Construction 2


E I

10 8 7
1
0
5
5

1
0

3 2

A H
Radhika Chapaneri

Huffman Tree Construction 2


10 15
1
0
0 1

5
5
I 7 8 E
C
1
0

3 2

A H
Radhika Chapaneri

Huffman Tree Construction


E = 10
25
I = 11
1
0
C = 01
10 15 A = 000
0
1 0 1
H = 001
5
5 7
C E 8 I
1
0

2 3 H
A
Radhika Chapaneri

Huffman Coding Example


• Huffman code
E = 01
I = 00
C = 10
A = 111
• Input H = 110
• ACE
• Output
• (111)(10)(01) = 1111001
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

Huffman Tree Decoding


E = 10
25
I = 11
1
0
C = 01
10 15 A = 000
0
1 0 1
H = 001
5
5 7
C E 8 I
Decode: 0000110
1
0

2 3 H
A
Radhika Chapaneri

Huffman Tree Decoding


E = 10
25
I = 11
1
0
C = 01
10 15 A = 000
0
1 0 1
H = 001
5
5
C 8 E 7 I
Decode: 0000110
1
0

3 2 A
H
A
Radhika Chapaneri

Huffman Tree Decoding


E = 10
25
I = 11
1
0
C = 01
10 15 A = 000
0
1 0 1
H = 001
5
5
C 8 E 7 I
Decode: 0000110
1
0

3 2 AC
H
A
Radhika Chapaneri

Huffman Tree Decoding


E = 10
25
I = 11
1
0
C = 01
10 15 A = 000
0
1 0 1
H = 001
5
5
C 8 E 7 I
Decode: 0000110
1
0

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

Huffman Tree Algorithm


• Q- Minimum priority Queue
• huffman (C)
• n = |C|
•Q=C
• for (i = 1 to n − 1) do
• allocate a new node z
• left[z] = x = extractMin(Q)
• right[z] = y = extractMin(Q)
• f[z] = f[x] + f[y]
• insert(Q, z)
• return extractMin(Q)

You might also like