Tree Data Structures: S. Sudarshan
Tree Data Structures: S. Sudarshan
S. Sudarshan
Tree
Nodes Each node can have 0 or more children A node can have at most one parent Tree with 02 children per node
Binary tree
Tree
Binary Tree
Trees
Terminology
Root no parent Leaf no child Interior non-leaf Height distance from root to leaf
Root node
Interior nodes
Leaf nodes
Height
Key property
Value at node
Smaller values in left subtree Larger values in right subtree X>Y X<Z
Example
Examples
5
10
2 5 2 25 30 45 5
10 45
30
45 10
2 25
25
30
void insert ( int data ) { } void delete ( int data ) { } Node *find ( int data ) { }
}
Find ( root, 2 )
root 10 5 30
10 > 2, left
5 > 2, left 2 = 2, found
5 > 2, left
2
30 10 25
45
2 = 2, found
25
45
Find (root, 25 )
10
5 30 25 45 10 10 < 25, right 30 > 25, left 25 = 25, found 2 5 45 5 < 25, right 45 > 25, left 30 > 25, left
30
25
Degenerate only one child Complete always two children Balanced mostly two children
Degenerate
Balanced
Time of search
Degenerate tree
Insertion Deletion
Smaller values in left subtree Larger values in right subtree
Algorithm
1.
2. 3.
4.
Perform search for value X Search will end at node Y (if X not in tree) If X < Y, insert new leaf X as new left subtree for Y If X > Y, insert new leaf X as new right subtree for Y
O( log(n) ) operation for balanced tree Insertions may unbalance tree
Observations
Example Insertion
Insert ( 20 )
10
5 2 25 30 45
Insert 20 on left
20
Algorithm
1.
2. 3.
Perform search for value X If X is a leaf, delete X Else // must delete internal node
a) Replace with largest value Y on left subtree OR smallest value Z on right subtree b) Delete replacement value (Y or Z) from subtree
Observation
Delete ( 25 )
10 5 30 25 45 10 < 25, right 30 > 25, left 5 2 10 30 45
25 = 25, delete
2
Delete ( 10 )
10 5 30 25 45 2 5 25 5 30 45 2 2 25 5 30 45
Deleting leaf
Delete ( 10 )
10 5 30 25 45 2 5 25 25 30 45 2 5 25 30 45
Deleting leaf
Resulting tree
height balanced vs. weight balanced Tree rotations used to maintain balance on insert/delete
2/3 trees
each internal node has 2 or 3 children all leaves at same depth (height balanced)
Generalization of 2/3 trees Each internal node has between k/2 and k children
B-trees
Parse trees
called DOM (Document Object Model) tree Like HTML, but used to represent data Tree structured
XML
Parse Trees
+
*
% 4
D 5
Tree Traversal
+
* * 2
% 4
A / C 5
D 5
void Node::inOrder () { if (left != NULL) { cout << (; left->inOrder(); cout << ); } cout << data << endl; if (right != NULL) right->inOrder() } Output: A C / 5 * 2 + D * 5 % 4 To disambiguate: print brackets
+ * * 2
% 4
A / C 5
D 5
Output: + - A * / C 5 2 % * D 5 4
void Node::postOrder () { if (left != NULL) left->preOrder (); if (right != NULL) right->preOrder (); cout << data << endl; } Output: A C 5 / 2 * - D 5 * 4 % +
XML
Data Representation
E.g. <dependency> <object>sample1.o</object> <depends>sample1.cpp</depends> <depends>sample1.h</depends> <rule>g++ -c sample1.cpp</rule> </dependency> Tree representation
dependency object sample1.o depends sample1.cpp depends sample1.h rule g++ -c
E.g: Airline networks, road networks, electrical circuits Nodes and Edges E.g. representation: class Node
i,e. edge == pointer To store multiple pointers: use array or linked list
Mumbai
Ahmbad Calcutta
Delhi Chennai
Madurai
End of Chapter