SlideShare a Scribd company logo
Data Structures & Algorithms
Tree
Session V
Dr. V.Umadevi M.Sc(CS &IT). M.Tech
(IT)., M.Phil., PhD., D.Litt.,
Director, Department of Computer
Science, Jairams Arts and Science
College, Karur.
Data Structures & Algorithms
• Binary Tree Traversal
• Application of Trees
• The Huffman Algorithm
Data Structures & AlgorithmsBinary Tree Traversal
• Traversal of a tree is to visit each node exactly once.
• For example searching for a particular node.
• Let T be a binary tree, there are different ways to proceed and the methods
differ primarily in the order in which they visit the nodes.
• The different traversals of T are:
• Inorder
• Postorder
• Preorder
• Level by level traversal
-
+ *
A * D E
B C
Expression tree
• Example of an expression of tree for (A+B*C)-(D*E).
Data Structures & Algorithms
A binary tree can be traversed in a number of ways:
pre-order
1. Visit the root
2. Traverse the left sub-tree,
3. Traverse the right sub-tree
In-order
1. Traverse the left sub-tree,
2. Visit the root
3. Traverse the right sub-tree
post-order
1. Traverse the left sub-tree,
2. Traverse the right sub-tree
3. Visit the root
Inorder
The general strategy is Left-Root-Right. If T is not empty,
• First traverse (inorder) the left subtree.
• Visit the root.
• Traverse the right subtree in inorder.
Data Structures & Algorithms
Postorder Traversal
In this traversal, it is a left-right-root strategy.
• Traverse the left subtree in postorder.
• Traverse the right subtree in postorder.
• Visit the root.
Preorder Traversal
• In this traversal strategy is root-left-right.
• Preorder traversal is employed in depth-first search.
The first root is visited, then recursively as follows:
• Visiting the root.
• Traversing the left subtree preorder.
• Traversing the right subtree preorder.
Data Structures & Algorithms
Level by Level Traversal
•This method involves level-wise
i.e. the node root at level ‘0’. i.e. root one is visited.
•The next level is to traverse from left to right.
•Then a visit to the next level from left to right and so on.
•This is same as breadth first search.
•This traversal need not be recursive because the other traversals are
recursive.
•Therefore queue may be used as a data structure to implement this and the
stack kind of data structure for other three traversals.
 Inorder : (A+B*C)-(D*E)
 Preorder : -+A*BC*DE
 Postorder : ABC*+DE*-
 Level by level : -+*A*DEBC
Data Structures & Algorithms
• If traversal is as the standard ordered binary tree in-order, then all the nodes in
sorted order are visited.
• Pre-order tree traversal -Traversing a tree in the order: root | left | right
• In-order tree traversal -Traversing a tree in the order: left | root | right
• Post-order tree traversal-Traversing a tree in the order: left | right | root
• Generally, a heap is used to maintain a balanced tree for a priority queue.
• For a tree used to store information for retrieval (but not removal), it is
necessary to search any item quickly in such a tree based on the key value.
Search Tree
Search Trees
Data Structures & Algorithms
Parse Trees
The following expression:
A*(((B+C)*(D*E))+F)
is presented a tree as follows:
• When traversing in post-order will produce:
A B C + D E * * F + *
• Familiar reverse-polish notation used by a compiler for evaluating the
expression.
• The search routine on a binary tree is simple and provides with a O(log n)
searching routine as long as the tree is kept balanced.
• if items are added to a tree, it results in the producing of an unbalanced tree
Parse Tree
Data Structures & Algorithms
Applications of Trees
One important application of tree is set representation.
Sets and Disjoints set Unions
• Tree is used to represent the set.
• A set is a collection of elements.
• Two sets are said to be disjoint if they do not share any common element.
• This means that any element that belongs to one of the set does not belong to
the other set.
• Let us consider three disjoint sets S1, S2 and S3.
•Let the elements belonging to these sets be as follows:
S1={a,g,h,i}
S2={b,e,j}
S3={c,d,f}
•Note that for each of the set the nodes from the children are linked to the
parent and not vice versa.
Data Structures & Algorithms
a
g h i
b
e
j
c
d f
a
g h i b
e j
a
g h i
b
e j
S1 U S2
S2 U S1
S1
S2
S3
Data Structures & Algorithms
Operations to perform in sets, are namely:
Disjoint set union:
• Union of two disjoint sets S1 and S2 is represented as S1US2
• Contain all elements x such that x is in either S1 or S2.
Thus, S1US2 = {a, g, h, i, b, e, j}.
• Considered disjoint sets, assume that after the union of S1 and S2, the
sets S1 and S2 do not exist independently; that is, they are
replaced by S1US2 in the collection of sets.
Find(i): This is for finding the set to which a particular element belongs.
Thus, ‘d’ is in set S3, and ‘a’ is in set S1.
• In order to find the union of two sets, all that has to be done is to set the
parent field of one of the roots to the other root.
Operations in set
Data Structures & Algorithms
• Union can be accomplished easily if, with each set name to keep a pointer to the
root of the tree representing that set.
• In addition, each root has a pointer to the set name, then to determine which set an
element is currently in, to follow parent links to the root of its tree and use
the pointer to the set name.
• Unite sets Si and Sj to unite the trees with roots.
• Roots for our example are FindPointer(Si) and FindPointer(Sj).
• FindPointer is a function that determines the root of a set, done by an examination
of the [set name, pointer] table.
• Operation of Find (i) is to determine the root of the tree containing the element i.
• Function Union (i, j) requires two trees with roots i and j be joined.
Data Structures & Algorithms
• Given coins a, b, c, d, e, f, g, h, one coin differs in weight from the
others.
• Coin is to be determined, making use of an equal arm balance
• Use a minimum number of Comparison and at the same time
determine whether the false coin is heavier or lighter than rest.
• Tree represents a set of decisions by which the solution is acquired.
Decision Trees
Eight Coins Problem
Decision trees are the applications of binary trees in decision making.
Data Structures & Algorithms
In game tree, the starting position is at the top of the tree and the winning
position is at the bottom of the tree.
• Initial state is an empty tic-tac-toe board and final state is one of the several
winning positions consisting of three makers in a row or column or
diagonal.
• If ‘X‘ is to be placed, there are nine possible places from where X can be
drawn first.
• For each of the possible first move, the opponent has 8 places in which to
draw a ’0’.
• A tree begins to branch very rapidly.
• On the second level there are 9 x 8= 12 nodes, and the third level consists of
72X7=504 nodes and so on.
• This problem is known as combinational explosion.
Game of Tic - Tac –Toe
X
x
0
0
Data Structures & AlgorithmsHuffman Algorithm
• Huffman problem is that of finding the minimum length bit string which can
be used to encode a string of symbols.
• One application is text compression.
• Huffman's scheme uses a table of frequency of occurrence for each symbol
(or character) in the input.
• Table may be derived from the input itself or from data which is
representative of the input.
• For instance, the frequency of occurrence of letters in normal English
might be derived from processing a large number of text documents
and then used for encoding all text documents.
• A variable-length bit string is assigned to each character that
unambiguously represents that character.
• This means that the encoding for each character must have a unique prefix.
• If the characters to be encoded are arranged in a binary tree,
Data Structures & AlgorithmsEncoding tree for ETASNO
•An encoding for each character is found by following the tree from the root to
the character in the leaf: the encoding is the string of symbols on each
branch followed.
•For example:
String Encoding
TEA 10 00 010
SEA 011 00 010
TEN 10 00 110
•As desired, the highest frequency letters - E and T - have two digit encodings,
whereas all the others have three digit encodings.
• Encoding would be done with a lookup table.
• A divide-and-conquer approach might lead to the question, which characters
should appear in the left and right sub trees, and trying to build the
tree from the top down.
• As with the optimal binary search tree, this will lead to an exponential time
algorithm.
Data Structures & Algorithms
Operation of the Huffman Algorithm
• following diagrams show how a Huffman encoding tree is built using a
straight-forward greedy algorithm which combines the two smallest-weight
trees at every step.
Initial data sorted by frequency
Combine the two lowest frequencies,
F and E, to form a sub-tree Of weight 14.
Move it into its correct place
Data Structures & Algorithms
Again combine the two lowest frequencies,
C and B, to form a sub-tree
of weight 25.
Move it into its correct place.
Now the sub-tree with weight, 14, and
D are combined to make a tree of
weight, 30.
Move it to its correct place.
Now the two lowest weights are held by
the "25" and "30" sub-trees, so combine
them to make one of weight, 55.
Move it after the A.
Data Structures & Algorithms
Finally, combine the A and the "55" sub-tree
to produce the final tree.
The encoding table is:
A 0
C 100
B 101
F 1100
E 1101
D 111
• A greedy approach places our n characters in n sub-trees and starts by
combining the two least weight nodes into a tree which is assigned the sum of the
two leaf node weights as the weight for its root node.
• The time complexity of the Huffman algorithm is O(n log n).
• Using a heap to store the weight of each tree, each iteration requires O(log
n) time to determine the cheapest weight and insert the new weight.
• There are O(n) iterations, one for each item.
Data Structures & Algorithms
Decoding Huffman-Encoded Data
• With these variable length strings, it's not possible to break up an encoded
string of bits into characters.
• In the decoding procedure, starting with the first bit in the stream,
successive bits are used from the stream to determine whether to go
left or right in the decoding tree.
• When a leaf of the tree is reached, a character is decoded, and placed
onto the (uncompressed) output stream.
• The next bit in the input stream is the first bit of the next character.
Huffman – Decoding Algorithm
Data Structures & Algorithms
• If the system is continually dealing with data in which the symbols have similar
frequencies of occurrence, then both encoders and decoders can use a
standard encoding table/decoding tree.
• Even text data from various sources will have quite different
characteristics.
• Example, ordinary English text will have generally 'e' at the root of the tree,
with short encodings for 'a' and 't', whereas C programs would generally have ';' at
the root, with short encodings for other punctuation marks such as '(' and ')‘
• If the data has varying frequencies, then, for optimal encoding, an encoding
tree for each data set is to be generated and stored or transmitting the
encoding with the data has to take place.
• The extra cost of transmitting the encoding tree means that there is no gain
and overall benefit unless the data stream to be encoded is quite long
• so that the savings through compression more than compensate for the
cost of the transmitting the encoding tree also.
Transmission and Storage of Huffman-Encoded Data

More Related Content

PPT
358 33 powerpoint-slides_11-efficient-binary-trees_chapter-11
PPT
358 33 powerpoint-slides_10-trees_chapter-10
PDF
Trees, Binary Search Tree, AVL Tree in Data Structures
PPT
Lecture 5 trees
PPTX
trees in data structure
PDF
Unit 4.1 (tree)
PPTX
Tree - Data Structure
PPT
Trees - Data structures in C/Java
358 33 powerpoint-slides_11-efficient-binary-trees_chapter-11
358 33 powerpoint-slides_10-trees_chapter-10
Trees, Binary Search Tree, AVL Tree in Data Structures
Lecture 5 trees
trees in data structure
Unit 4.1 (tree)
Tree - Data Structure
Trees - Data structures in C/Java

What's hot (20)

PPT
PPT
Cinterviews Binarysearch Tree
PPT
Data Structure and Algorithms Binary Tree
PDF
7 chapter4 trees_binary
PDF
Binary Search Tree
PDF
Binary tree
PPT
Binary search tree(bst)
PPTX
Trees data structure
PPT
Ch13 Binary Search Tree
PPTX
Tree
PPT
Binary trees
PDF
Unit 4.2(graphs)
PPTX
Tree traversal techniques
PPT
introduction to_trees
PPTX
Mca iii dfs u-4 tree and graph
PPTX
Lecture 8 data structures and algorithms
PPT
Binary tree
PDF
Tree Data Structure by Daniyal Khan
PPTX
R-Trees and Geospatial Data Structures
PDF
Lecture notes data structures tree
Cinterviews Binarysearch Tree
Data Structure and Algorithms Binary Tree
7 chapter4 trees_binary
Binary Search Tree
Binary tree
Binary search tree(bst)
Trees data structure
Ch13 Binary Search Tree
Tree
Binary trees
Unit 4.2(graphs)
Tree traversal techniques
introduction to_trees
Mca iii dfs u-4 tree and graph
Lecture 8 data structures and algorithms
Binary tree
Tree Data Structure by Daniyal Khan
R-Trees and Geospatial Data Structures
Lecture notes data structures tree
Ad

Similar to Data Structures 5 (20)

PPTX
Unit-VStackStackStackStackStackStack.pptx
PPT
Tree
PPT
Trees - Non Linear Data Structure
PDF
Chapter 5_Trees.pdf
PPTX
Saikat techhnology of techtechhnology of techGhorai.pptx
PPTX
UNIT III Non Linear Data Structures - Trees.pptx
PPT
Data Structures 4
PPT
Hub102 - Lesson4 - Data Structure
PPTX
Why Tree is considered a non-linear data structure?
PPTX
Data structures and Algorithm analysis_Lecture4.pptx
PPT
6_1 (1).ppt
PDF
LEC 5-DS ALGO(updated).pdf
PPT
Chap 5 Tree.ppt
PDF
Chapter 09-Trees
PPTX
UNIT III Non Linear Data Structures - Trees.pptx
PPT
Review session2
PPTX
unit 4 for trees data structure notes it is
PPT
ds 10-Binary Tree.ppt
PPTX
Week 8 (trees)
PPTX
DSA-Unit-2.pptx
Unit-VStackStackStackStackStackStack.pptx
Tree
Trees - Non Linear Data Structure
Chapter 5_Trees.pdf
Saikat techhnology of techtechhnology of techGhorai.pptx
UNIT III Non Linear Data Structures - Trees.pptx
Data Structures 4
Hub102 - Lesson4 - Data Structure
Why Tree is considered a non-linear data structure?
Data structures and Algorithm analysis_Lecture4.pptx
6_1 (1).ppt
LEC 5-DS ALGO(updated).pdf
Chap 5 Tree.ppt
Chapter 09-Trees
UNIT III Non Linear Data Structures - Trees.pptx
Review session2
unit 4 for trees data structure notes it is
ds 10-Binary Tree.ppt
Week 8 (trees)
DSA-Unit-2.pptx
Ad

More from Dr.Umadevi V (11)

PPT
Data Structures 8
PPT
Data Structures 7
PPT
Data Structures 6
PPT
Data Structures 3
PPT
Data Structures 2
PPT
Data Structures
PPT
computer architecture 4
PPT
Computer architecture 3
PPT
computer architecture
PPT
computer architecture
PPTX
Multiple access techniques for wireless communication
Data Structures 8
Data Structures 7
Data Structures 6
Data Structures 3
Data Structures 2
Data Structures
computer architecture 4
Computer architecture 3
computer architecture
computer architecture
Multiple access techniques for wireless communication

Recently uploaded (20)

PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PDF
01-Introduction-to-Information-Management.pdf
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PDF
Basic Mud Logging Guide for educational purpose
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PPTX
PPH.pptx obstetrics and gynecology in nursing
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PDF
Insiders guide to clinical Medicine.pdf
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PPTX
master seminar digital applications in india
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PDF
O7-L3 Supply Chain Operations - ICLT Program
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PDF
Complications of Minimal Access Surgery at WLH
PPTX
Institutional Correction lecture only . . .
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
Microbial diseases, their pathogenesis and prophylaxis
01-Introduction-to-Information-Management.pdf
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
Basic Mud Logging Guide for educational purpose
Abdominal Access Techniques with Prof. Dr. R K Mishra
PPH.pptx obstetrics and gynecology in nursing
Supply Chain Operations Speaking Notes -ICLT Program
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
Insiders guide to clinical Medicine.pdf
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
master seminar digital applications in india
102 student loan defaulters named and shamed – Is someone you know on the list?
O7-L3 Supply Chain Operations - ICLT Program
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
Complications of Minimal Access Surgery at WLH
Institutional Correction lecture only . . .
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
2.FourierTransform-ShortQuestionswithAnswers.pdf

Data Structures 5

  • 1. Data Structures & Algorithms Tree Session V Dr. V.Umadevi M.Sc(CS &IT). M.Tech (IT)., M.Phil., PhD., D.Litt., Director, Department of Computer Science, Jairams Arts and Science College, Karur.
  • 2. Data Structures & Algorithms • Binary Tree Traversal • Application of Trees • The Huffman Algorithm
  • 3. Data Structures & AlgorithmsBinary Tree Traversal • Traversal of a tree is to visit each node exactly once. • For example searching for a particular node. • Let T be a binary tree, there are different ways to proceed and the methods differ primarily in the order in which they visit the nodes. • The different traversals of T are: • Inorder • Postorder • Preorder • Level by level traversal - + * A * D E B C Expression tree • Example of an expression of tree for (A+B*C)-(D*E).
  • 4. Data Structures & Algorithms A binary tree can be traversed in a number of ways: pre-order 1. Visit the root 2. Traverse the left sub-tree, 3. Traverse the right sub-tree In-order 1. Traverse the left sub-tree, 2. Visit the root 3. Traverse the right sub-tree post-order 1. Traverse the left sub-tree, 2. Traverse the right sub-tree 3. Visit the root Inorder The general strategy is Left-Root-Right. If T is not empty, • First traverse (inorder) the left subtree. • Visit the root. • Traverse the right subtree in inorder.
  • 5. Data Structures & Algorithms Postorder Traversal In this traversal, it is a left-right-root strategy. • Traverse the left subtree in postorder. • Traverse the right subtree in postorder. • Visit the root. Preorder Traversal • In this traversal strategy is root-left-right. • Preorder traversal is employed in depth-first search. The first root is visited, then recursively as follows: • Visiting the root. • Traversing the left subtree preorder. • Traversing the right subtree preorder.
  • 6. Data Structures & Algorithms Level by Level Traversal •This method involves level-wise i.e. the node root at level ‘0’. i.e. root one is visited. •The next level is to traverse from left to right. •Then a visit to the next level from left to right and so on. •This is same as breadth first search. •This traversal need not be recursive because the other traversals are recursive. •Therefore queue may be used as a data structure to implement this and the stack kind of data structure for other three traversals.  Inorder : (A+B*C)-(D*E)  Preorder : -+A*BC*DE  Postorder : ABC*+DE*-  Level by level : -+*A*DEBC
  • 7. Data Structures & Algorithms • If traversal is as the standard ordered binary tree in-order, then all the nodes in sorted order are visited. • Pre-order tree traversal -Traversing a tree in the order: root | left | right • In-order tree traversal -Traversing a tree in the order: left | root | right • Post-order tree traversal-Traversing a tree in the order: left | right | root • Generally, a heap is used to maintain a balanced tree for a priority queue. • For a tree used to store information for retrieval (but not removal), it is necessary to search any item quickly in such a tree based on the key value. Search Tree Search Trees
  • 8. Data Structures & Algorithms Parse Trees The following expression: A*(((B+C)*(D*E))+F) is presented a tree as follows: • When traversing in post-order will produce: A B C + D E * * F + * • Familiar reverse-polish notation used by a compiler for evaluating the expression. • The search routine on a binary tree is simple and provides with a O(log n) searching routine as long as the tree is kept balanced. • if items are added to a tree, it results in the producing of an unbalanced tree Parse Tree
  • 9. Data Structures & Algorithms Applications of Trees One important application of tree is set representation. Sets and Disjoints set Unions • Tree is used to represent the set. • A set is a collection of elements. • Two sets are said to be disjoint if they do not share any common element. • This means that any element that belongs to one of the set does not belong to the other set. • Let us consider three disjoint sets S1, S2 and S3. •Let the elements belonging to these sets be as follows: S1={a,g,h,i} S2={b,e,j} S3={c,d,f} •Note that for each of the set the nodes from the children are linked to the parent and not vice versa.
  • 10. Data Structures & Algorithms a g h i b e j c d f a g h i b e j a g h i b e j S1 U S2 S2 U S1 S1 S2 S3
  • 11. Data Structures & Algorithms Operations to perform in sets, are namely: Disjoint set union: • Union of two disjoint sets S1 and S2 is represented as S1US2 • Contain all elements x such that x is in either S1 or S2. Thus, S1US2 = {a, g, h, i, b, e, j}. • Considered disjoint sets, assume that after the union of S1 and S2, the sets S1 and S2 do not exist independently; that is, they are replaced by S1US2 in the collection of sets. Find(i): This is for finding the set to which a particular element belongs. Thus, ‘d’ is in set S3, and ‘a’ is in set S1. • In order to find the union of two sets, all that has to be done is to set the parent field of one of the roots to the other root. Operations in set
  • 12. Data Structures & Algorithms • Union can be accomplished easily if, with each set name to keep a pointer to the root of the tree representing that set. • In addition, each root has a pointer to the set name, then to determine which set an element is currently in, to follow parent links to the root of its tree and use the pointer to the set name. • Unite sets Si and Sj to unite the trees with roots. • Roots for our example are FindPointer(Si) and FindPointer(Sj). • FindPointer is a function that determines the root of a set, done by an examination of the [set name, pointer] table. • Operation of Find (i) is to determine the root of the tree containing the element i. • Function Union (i, j) requires two trees with roots i and j be joined.
  • 13. Data Structures & Algorithms • Given coins a, b, c, d, e, f, g, h, one coin differs in weight from the others. • Coin is to be determined, making use of an equal arm balance • Use a minimum number of Comparison and at the same time determine whether the false coin is heavier or lighter than rest. • Tree represents a set of decisions by which the solution is acquired. Decision Trees Eight Coins Problem Decision trees are the applications of binary trees in decision making.
  • 14. Data Structures & Algorithms In game tree, the starting position is at the top of the tree and the winning position is at the bottom of the tree. • Initial state is an empty tic-tac-toe board and final state is one of the several winning positions consisting of three makers in a row or column or diagonal. • If ‘X‘ is to be placed, there are nine possible places from where X can be drawn first. • For each of the possible first move, the opponent has 8 places in which to draw a ’0’. • A tree begins to branch very rapidly. • On the second level there are 9 x 8= 12 nodes, and the third level consists of 72X7=504 nodes and so on. • This problem is known as combinational explosion. Game of Tic - Tac –Toe X x 0 0
  • 15. Data Structures & AlgorithmsHuffman Algorithm • Huffman problem is that of finding the minimum length bit string which can be used to encode a string of symbols. • One application is text compression. • Huffman's scheme uses a table of frequency of occurrence for each symbol (or character) in the input. • Table may be derived from the input itself or from data which is representative of the input. • For instance, the frequency of occurrence of letters in normal English might be derived from processing a large number of text documents and then used for encoding all text documents. • A variable-length bit string is assigned to each character that unambiguously represents that character. • This means that the encoding for each character must have a unique prefix. • If the characters to be encoded are arranged in a binary tree,
  • 16. Data Structures & AlgorithmsEncoding tree for ETASNO •An encoding for each character is found by following the tree from the root to the character in the leaf: the encoding is the string of symbols on each branch followed. •For example: String Encoding TEA 10 00 010 SEA 011 00 010 TEN 10 00 110 •As desired, the highest frequency letters - E and T - have two digit encodings, whereas all the others have three digit encodings. • Encoding would be done with a lookup table. • A divide-and-conquer approach might lead to the question, which characters should appear in the left and right sub trees, and trying to build the tree from the top down. • As with the optimal binary search tree, this will lead to an exponential time algorithm.
  • 17. Data Structures & Algorithms Operation of the Huffman Algorithm • following diagrams show how a Huffman encoding tree is built using a straight-forward greedy algorithm which combines the two smallest-weight trees at every step. Initial data sorted by frequency Combine the two lowest frequencies, F and E, to form a sub-tree Of weight 14. Move it into its correct place
  • 18. Data Structures & Algorithms Again combine the two lowest frequencies, C and B, to form a sub-tree of weight 25. Move it into its correct place. Now the sub-tree with weight, 14, and D are combined to make a tree of weight, 30. Move it to its correct place. Now the two lowest weights are held by the "25" and "30" sub-trees, so combine them to make one of weight, 55. Move it after the A.
  • 19. Data Structures & Algorithms Finally, combine the A and the "55" sub-tree to produce the final tree. The encoding table is: A 0 C 100 B 101 F 1100 E 1101 D 111 • A greedy approach places our n characters in n sub-trees and starts by combining the two least weight nodes into a tree which is assigned the sum of the two leaf node weights as the weight for its root node. • The time complexity of the Huffman algorithm is O(n log n). • Using a heap to store the weight of each tree, each iteration requires O(log n) time to determine the cheapest weight and insert the new weight. • There are O(n) iterations, one for each item.
  • 20. Data Structures & Algorithms Decoding Huffman-Encoded Data • With these variable length strings, it's not possible to break up an encoded string of bits into characters. • In the decoding procedure, starting with the first bit in the stream, successive bits are used from the stream to determine whether to go left or right in the decoding tree. • When a leaf of the tree is reached, a character is decoded, and placed onto the (uncompressed) output stream. • The next bit in the input stream is the first bit of the next character. Huffman – Decoding Algorithm
  • 21. Data Structures & Algorithms • If the system is continually dealing with data in which the symbols have similar frequencies of occurrence, then both encoders and decoders can use a standard encoding table/decoding tree. • Even text data from various sources will have quite different characteristics. • Example, ordinary English text will have generally 'e' at the root of the tree, with short encodings for 'a' and 't', whereas C programs would generally have ';' at the root, with short encodings for other punctuation marks such as '(' and ')‘ • If the data has varying frequencies, then, for optimal encoding, an encoding tree for each data set is to be generated and stored or transmitting the encoding with the data has to take place. • The extra cost of transmitting the encoding tree means that there is no gain and overall benefit unless the data stream to be encoded is quite long • so that the savings through compression more than compensate for the cost of the transmitting the encoding tree also. Transmission and Storage of Huffman-Encoded Data