Professional Documents
Culture Documents
Session5 180119051749
Session5 180119051749
Tree
Session V
• 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:
Inorder
The general strategy is Left-Root-Right. If T is not empty,
Preorder Traversal
•Then a visit to the next level from left to right and so on.
•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
• 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
Parse Tree
• When traversing in post-order will produce:
ABC+DE**F+*
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
• 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.
• 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.
• 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’.
• 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.
• Table may be derived from the input itself or from data which is
representative of the input.
• This means that the encoding for each character must have a unique prefix.
•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.
• 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.
• 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.
• 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.