Download as pdf or txt
Download as pdf or txt
You are on page 1of 21

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
Binary Tree Traversal Data Structures & Algorithms
• 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:

1. Visit the root


pre-order
2. Traverse the left sub-tree,
3. Traverse the right sub-tree

1. Traverse the left sub-tree,


In-order
2. Visit the root
3. Traverse the right sub-tree

1. Traverse the left sub-tree,


post-order
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

Search Trees

• 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
Data Structures & Algorithms
• 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 Trees

The following expression:


A*(((B+C)*(D*E))+F)
is presented a tree as follows:

Parse Tree
• When traversing in post-order will produce:
ABC+DE**F+*

• Familiar reverse-polish notation used by a compiler for evaluating the


expression.
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

a
g h i

g h i b
S1
b
S1 U S2
e j
j
e
b
S2
c

a e j

d f

S3 g h i

S2 U S1
Operations in set 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.
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
Decision Trees
Decision trees are the applications of binary trees in decision making.

Eight Coins Problem

• 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.


Game of Tic - Tac –Toe 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.
X 0
• This problem is known as combinational explosion.
x

0
Huffman Algorithm Data Structures & Algorithms

• 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,


Encoding tree for ETASNO Data Structures & Algorithms

•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

Huffman – Decoding Algorithm


• 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.
Data Structures & Algorithms
Transmission and Storage of Huffman-Encoded Data
• 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.

You might also like