SlideShare a Scribd company logo
1 of 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
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

What's hot

Cinterviews Binarysearch Tree
Cinterviews Binarysearch TreeCinterviews Binarysearch Tree
Cinterviews Binarysearch Treecinterviews
 
Data Structure and Algorithms Binary Tree
Data Structure and Algorithms Binary TreeData Structure and Algorithms Binary Tree
Data Structure and Algorithms Binary TreeManishPrajapati78
 
7 chapter4 trees_binary
7 chapter4 trees_binary7 chapter4 trees_binary
7 chapter4 trees_binarySSE_AndyLi
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search TreeShivam Singh
 
Ch13 Binary Search Tree
Ch13 Binary Search TreeCh13 Binary Search Tree
Ch13 Binary Search Treeleminhvuong
 
introduction to_trees
introduction to_treesintroduction to_trees
introduction to_treesDanish Aakash
 
Mca iii dfs u-4 tree and graph
Mca iii dfs u-4 tree and graphMca iii dfs u-4 tree and graph
Mca iii dfs u-4 tree and graphRai University
 
Lecture 8 data structures and algorithms
Lecture 8 data structures and algorithmsLecture 8 data structures and algorithms
Lecture 8 data structures and algorithmsAakash deep Singhal
 
Tree Data Structure by Daniyal Khan
Tree Data Structure by Daniyal KhanTree Data Structure by Daniyal Khan
Tree Data Structure by Daniyal KhanDaniyal Khan
 
R-Trees and Geospatial Data Structures
R-Trees and Geospatial Data StructuresR-Trees and Geospatial Data Structures
R-Trees and Geospatial Data StructuresAmrinder Arora
 
Lecture notes data structures tree
Lecture notes data structures   treeLecture notes data structures   tree
Lecture notes data structures treemaamir farooq
 

What's hot (20)

Tree
TreeTree
Tree
 
Cinterviews Binarysearch Tree
Cinterviews Binarysearch TreeCinterviews Binarysearch Tree
Cinterviews Binarysearch Tree
 
Data Structure and Algorithms Binary Tree
Data Structure and Algorithms Binary TreeData Structure and Algorithms Binary Tree
Data Structure and Algorithms Binary Tree
 
7 chapter4 trees_binary
7 chapter4 trees_binary7 chapter4 trees_binary
7 chapter4 trees_binary
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
 
Binary tree
Binary treeBinary tree
Binary tree
 
Binary search tree(bst)
Binary search tree(bst)Binary search tree(bst)
Binary search tree(bst)
 
Trees data structure
Trees data structureTrees data structure
Trees data structure
 
Ch13 Binary Search Tree
Ch13 Binary Search TreeCh13 Binary Search Tree
Ch13 Binary Search Tree
 
Tree
TreeTree
Tree
 
Binary trees
Binary treesBinary trees
Binary trees
 
Unit 4.2(graphs)
Unit 4.2(graphs)Unit 4.2(graphs)
Unit 4.2(graphs)
 
Tree traversal techniques
Tree traversal techniquesTree traversal techniques
Tree traversal techniques
 
introduction to_trees
introduction to_treesintroduction to_trees
introduction to_trees
 
Mca iii dfs u-4 tree and graph
Mca iii dfs u-4 tree and graphMca iii dfs u-4 tree and graph
Mca iii dfs u-4 tree and graph
 
Lecture 8 data structures and algorithms
Lecture 8 data structures and algorithmsLecture 8 data structures and algorithms
Lecture 8 data structures and algorithms
 
Binary tree
Binary  treeBinary  tree
Binary tree
 
Tree Data Structure by Daniyal Khan
Tree Data Structure by Daniyal KhanTree Data Structure by Daniyal Khan
Tree Data Structure by Daniyal Khan
 
R-Trees and Geospatial Data Structures
R-Trees and Geospatial Data StructuresR-Trees and Geospatial Data Structures
R-Trees and Geospatial Data Structures
 
Lecture notes data structures tree
Lecture notes data structures   treeLecture notes data structures   tree
Lecture notes data structures tree
 

Similar to Data Structures 5

Bsc cs ii dfs u-3 tree and graph
Bsc cs  ii dfs u-3 tree and graphBsc cs  ii dfs u-3 tree and graph
Bsc cs ii dfs u-3 tree and graphRai University
 
tree-160731205832.pptx
tree-160731205832.pptxtree-160731205832.pptx
tree-160731205832.pptxMouDhara1
 
Bca ii dfs u-3 tree and graph
Bca  ii dfs u-3 tree and graphBca  ii dfs u-3 tree and graph
Bca ii dfs u-3 tree and graphRai University
 
splaytree-171227043127.pptx NNNNNNNNNNNNNNNNNNNNNNN
splaytree-171227043127.pptx NNNNNNNNNNNNNNNNNNNNNNNsplaytree-171227043127.pptx NNNNNNNNNNNNNNNNNNNNNNN
splaytree-171227043127.pptx NNNNNNNNNNNNNNNNNNNNNNNratnapatil14
 
Unit 2 linear data structures
Unit 2   linear data structuresUnit 2   linear data structures
Unit 2 linear data structuresSenthil Murugan
 
B tree ,B plus and graph
B tree ,B plus and graph B tree ,B plus and graph
B tree ,B plus and graph RaaviKapoor
 
Farhana shaikh webinar_treesindiscretestructure
Farhana shaikh webinar_treesindiscretestructureFarhana shaikh webinar_treesindiscretestructure
Farhana shaikh webinar_treesindiscretestructureFarhana Shaikh
 
data_structures_and_applications_-_module-4.ppt
data_structures_and_applications_-_module-4.pptdata_structures_and_applications_-_module-4.ppt
data_structures_and_applications_-_module-4.pptssuser5c874e
 
Trees in data structure
Trees in data structureTrees in data structure
Trees in data structureAnusruti Mitra
 
lecture10 date structure types of graph and terminology
lecture10 date structure types of graph and terminologylecture10 date structure types of graph and terminology
lecture10 date structure types of graph and terminologyKamranAli649587
 
1.Introduction to Data Structures and Algorithms.pptx
1.Introduction to Data Structures and Algorithms.pptx1.Introduction to Data Structures and Algorithms.pptx
1.Introduction to Data Structures and Algorithms.pptxBlueSwede
 
1.1 introduction to Data Structures.ppt
1.1 introduction to Data Structures.ppt1.1 introduction to Data Structures.ppt
1.1 introduction to Data Structures.pptAshok280385
 
ntroduction to Algorithm 2.pptx
ntroduction to Algorithm 2.pptxntroduction to Algorithm 2.pptx
ntroduction to Algorithm 2.pptxMarisse Presilda
 

Similar to Data Structures 5 (20)

Data Structures 4
Data Structures 4Data Structures 4
Data Structures 4
 
Bsc cs ii dfs u-3 tree and graph
Bsc cs  ii dfs u-3 tree and graphBsc cs  ii dfs u-3 tree and graph
Bsc cs ii dfs u-3 tree and graph
 
tree-160731205832.pptx
tree-160731205832.pptxtree-160731205832.pptx
tree-160731205832.pptx
 
Bca ii dfs u-3 tree and graph
Bca  ii dfs u-3 tree and graphBca  ii dfs u-3 tree and graph
Bca ii dfs u-3 tree and graph
 
splaytree-171227043127.pptx NNNNNNNNNNNNNNNNNNNNNNN
splaytree-171227043127.pptx NNNNNNNNNNNNNNNNNNNNNNNsplaytree-171227043127.pptx NNNNNNNNNNNNNNNNNNNNNNN
splaytree-171227043127.pptx NNNNNNNNNNNNNNNNNNNNNNN
 
Splay tree
Splay treeSplay tree
Splay tree
 
Unit 2 linear data structures
Unit 2   linear data structuresUnit 2   linear data structures
Unit 2 linear data structures
 
Tree.pptx
Tree.pptxTree.pptx
Tree.pptx
 
B tree ,B plus and graph
B tree ,B plus and graph B tree ,B plus and graph
B tree ,B plus and graph
 
Tree chapter
Tree chapterTree chapter
Tree chapter
 
Farhana shaikh webinar_treesindiscretestructure
Farhana shaikh webinar_treesindiscretestructureFarhana shaikh webinar_treesindiscretestructure
Farhana shaikh webinar_treesindiscretestructure
 
Tree
TreeTree
Tree
 
data_structures_and_applications_-_module-4.ppt
data_structures_and_applications_-_module-4.pptdata_structures_and_applications_-_module-4.ppt
data_structures_and_applications_-_module-4.ppt
 
Trees in data structure
Trees in data structureTrees in data structure
Trees in data structure
 
Tree
TreeTree
Tree
 
Tree 11.ppt
Tree 11.pptTree 11.ppt
Tree 11.ppt
 
lecture10 date structure types of graph and terminology
lecture10 date structure types of graph and terminologylecture10 date structure types of graph and terminology
lecture10 date structure types of graph and terminology
 
1.Introduction to Data Structures and Algorithms.pptx
1.Introduction to Data Structures and Algorithms.pptx1.Introduction to Data Structures and Algorithms.pptx
1.Introduction to Data Structures and Algorithms.pptx
 
1.1 introduction to Data Structures.ppt
1.1 introduction to Data Structures.ppt1.1 introduction to Data Structures.ppt
1.1 introduction to Data Structures.ppt
 
ntroduction to Algorithm 2.pptx
ntroduction to Algorithm 2.pptxntroduction to Algorithm 2.pptx
ntroduction to Algorithm 2.pptx
 

More from Dr.Umadevi V

computer architecture 4
computer architecture 4 computer architecture 4
computer architecture 4 Dr.Umadevi V
 
Computer architecture 3
Computer architecture 3Computer architecture 3
Computer architecture 3Dr.Umadevi V
 
computer architecture
computer architecture computer architecture
computer architecture Dr.Umadevi V
 
computer architecture
computer architecture computer architecture
computer architecture Dr.Umadevi V
 
Multiple access techniques for wireless communication
Multiple access techniques for wireless communicationMultiple access techniques for wireless communication
Multiple access techniques for wireless communicationDr.Umadevi V
 

More from Dr.Umadevi V (11)

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

Recently uploaded

Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsKarinaGenton
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Celine George
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17Celine George
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesFatimaKhan178732
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docxPoojaSen20
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 

Recently uploaded (20)

Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its Characteristics
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and Actinides
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
9953330565 Low Rate Call Girls In Rohini Delhi NCR
9953330565 Low Rate Call Girls In Rohini  Delhi NCR9953330565 Low Rate Call Girls In Rohini  Delhi NCR
9953330565 Low Rate Call Girls In Rohini Delhi NCR
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docx
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 

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