Professional Documents
Culture Documents
Lec6 Tree Dsa
Lec6 Tree Dsa
Introduction
Disadvantage :
Disadvantage:
Disadvantage :
Disadvantage :
Slow search.
Introduction to Trees
Yes No
Yes No Yes No
Starbucks Spike’s Al Forno Café Paragon
Trees
Arithematic Expression
• Binary Trees are used to represent arithemetic
expressions
E =2*(a - 1)+(3*b)
This expression can be represented in binary
Tree and every algebraic expression will
correspond to a unique tree and vice versa .
Cont.
• Below is the Corresponding Tree
2 - 3 b
a 1
Expression tree
(1) Pre-order
(2) In-order
(3) Post-order
Conti
(a) PreOrder : Node-Left-Right (NLF)
Truman
Taft Zuniga
Ralson
Davidson Rollins
Another Example
• Pre-order : ..?
• Post-order : ?
root
Rollins
Truman
Davidson
Taft Zuniga
Brown Ralson
In-order Traversal
Truman
Davidson
Taft Zuniga
Brown Ralson
Another Example
• In-order – Brown, Davidson, Ralson, Rollins, Taft, Truman,
Zuniga
root
Brown
Truman
Taft Zuniga
Ralson
Davidson Rollins
Binary Tree Representation
1. Linked Structure
2. Array List
Linked representation
• Uses three (3) parallel arrays INFO ,LEFT and
RIGHT
• Uses two pointers ROOT and AVAIL
• The ROOT contains the location of the ROOT
of tree
• AVAIL contains address of any invalid address
and denoted by NULL
Cont.
• Example 1 : a tree with node A , B ,C ,D , E
,F ,G ,H , J, K
• Example 2 : Given the tree table , draw the
corresponding binary tree.
Sequential representation
• If the tree is complete or nearly complete ,
then sequential representation is an efficient
way of maintaining T in memory.
• It uses Linear Array TREE as follows:
1. Root is sorted in TREE [1]
2. If a node occupies TREE [K] , then its left child is
stored in TREE[2*K] and its right child is stored in
TREE [2 * K +1]
Null is used to indicate empty subtree
Sequential representation
• Example : A tree with nodes as follows 45 ,
22 , 77 , 11 , 30 , 90, 15 ,25, 85 .
• Represent the above tree in a memory using
sequential representation
Cont.
• The example shows that we require 14 locations
in the array TREE , even though , the tree has
only 9 nodes
• If we included the null entries for the successor
of the terminal nodes , then we would actually
require TREE [29] for the right successor of the
TREE [14]
• Generally , sequential representation of a tree
with depth d requires array with 2d+1
Cont.
• It is inefficient to use sequential
representation , unless if T is complete or
nearly complete
THANK YOU