Trees Datastructure

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

Trees

(Data Structure)

Trupti agrawal 1
Trees Data Structures
 Tree
 Nodes
 Each node can have 0 or more children
 A node can have at most one parent
 Binary tree
 Tree with 0–2 children per node

TreeTrupti agrawal Binary Tree 2


Trees
 Terminology
 Root  no parent
 Leaf  no child
 Interior  non-leaf
 Height  distance from root to leaf

Root node

Interior nodes Height

Leaf nodes

Trupti agrawal 3
Binary Search Trees
 Key property
 Value at node
 Smaller values in left subtree
 Larger values in right subtree
 Example X
 X>Y
 X<Z

Y Z

Trupti agrawal 4
Binary Search Trees
 Examples
5
10 10
2 45
5 30 5 45
30
2 25 45 2 25 30
10

25
Binary Not a binary
search trees search tree

Trupti agrawal 5
Binary Tree Implementation
Class Node {
int data; // Could be int, a class, etc
Node *left, *right; // null if empty

void insert ( int data ) { … }


void delete ( int data ) { … }
Node *find ( int data ) { … }

}

Trupti agrawal 6
Iterative Search of Binary Tree
Node *Find( Node *n, int key) {
while (n != NULL) {
if (n->data == key) // Found it
return n;
if (n->data > key) // In left subtree
n = n->left;
else // In right subtree
n = n->right;
}
return null;
}
Node * n = Find( root, 5);
Trupti agrawal 7
Recursive Search of Binary Tree
Node *Find( Node *n, int key) {
if (n == NULL) // Not found
return( n );
else if (n->data == key) // Found it
return( n );
else if (n->data > key) // In left subtree
return Find( n->left, key );
else // In right subtree
return Find( n->right, key );
}
Node * n = Find( root, 5);
Trupti agrawal 8
Example Binary Searches
 Find ( root, 2 )

root
10 5
10 > 2, left 5 > 2, left
5 30 5 > 2, left 2 45 2 = 2, found
2 = 2, found
2 25 45 30

10

25
Trupti agrawal 9
Example Binary Searches
 Find (root, 25 )

10 5
10 < 25, right 5 < 25, right
5 30 30 > 25, left 2 45 45 > 25, left
25 = 25, found 30 > 25, left
2 25 45 30
10 < 25, right
10 25 = 25, found

25

Trupti agrawal 10
Types of Binary Trees
 Degenerate – only one child
 Complete – always two children
 Balanced – “mostly” two children
 more formal definitions exist, above are intuitive ideas

Degenerate Balanced Complete


binary tree binary tree binary tree
Trupti agrawal 11
Binary Trees Properties
 Degenerate  Balanced
 Height = O(n) for n  Height = O( log(n) )
nodes for n nodes
 Similar to linked list  Useful for searches

Degenerate Balanced
binary tree binary tree
Trupti agrawal 12
Binary Search Properties
 Time of search
 Proportional to height of tree
 Balanced binary tree
 O( log(n) ) time
 Degenerate tree
 O( n ) time
 Like searching linked list / unsorted array

Trupti agrawal 13
Binary Search Tree Construction
 How to build & maintain binary trees?
 Insertion
 Deletion
 Maintain key property (invariant)
 Smaller values in left subtree
 Larger values in right subtree

Trupti agrawal 14
Binary Search Tree – Insertion
 Algorithm
1. Perform search for value X
2. Search will end at node Y (if X not in tree)
3. If X < Y, insert new leaf X as new left subtree for Y
4. If X > Y, insert new leaf X as new right subtree for Y
 Observations
 O( log(n) ) operation for balanced tree
 Insertions may unbalance tree

Trupti agrawal 15
Example Insertion
 Insert ( 20 )
10 10 < 20, right
30 > 20, left
5 30
25 > 20, left
2 25 45 Insert 20 on left

20

Trupti agrawal 16
Binary Search Tree – Deletion
 Algorithm
1. Perform search for value X
2. If X is a leaf, delete X
3. 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
 O( log(n) ) operation for balanced tree
 Deletions may unbalance tree

Trupti agrawal 17
Example Deletion (Leaf)
 Delete ( 25 )
10 10
10 < 25, right
5 30 30 > 25, left 5 30
25 = 25, delete
2 25 45 2 45

Trupti agrawal 18
Example Deletion (Internal Node)
 Delete ( 10 )
10 5 5

5 30 5 30 2 30

2 25 45 2 25 45 2 25 45

Replacing 10 Replacing 5 Deleting leaf


with largest with largest
value in left value in left
subtree subtree
Trupti agrawal 19
Example Deletion (Internal Node)
 Delete ( 10 )
10 25 25

5 30 5 30 5 30

2 25 45 2 25 45 2 45

Replacing 10 Deleting leaf Resulting tree


with smallest
value in right
subtree
Trupti agrawal 20
Balanced Search Trees
 Kinds of balanced binary search trees
 height balanced vs. weight balanced
 “Tree rotations” used to maintain balance on insert/delete
 Non-binary search trees
 2/3 trees
 each internal node has 2 or 3 children
 all leaves at same depth (height balanced)
 B-trees
 Generalization of 2/3 trees
 Each internal node has between k/2 and k children
 Each node has an array of pointers to children
 Widely used in databases

Trupti agrawal 21
Other (Non-Search) Trees
 Parse trees
 Convert from textual representation to tree
representation
 Textual program to tree
 Used extensively in compilers
 Tree representation of data
 E.g. HTML data can be represented as a tree
 called DOM (Document Object Model) tree

 XML
 Like HTML, but used to represent data

 Tree structured

Trupti agrawal 22
Parse Trees
 Expressions, programs, etc can be represented by tree
structures
 E.g. Arithmetic Expression Tree
 A-(C/5 * 2) + (D*5 % 4)

+
- %

A * * 4

/ 2 D 5

C 5

Trupti agrawal 23
+
Tree Traversal - %

 Goal: visit every node of a tree A * * 4


 in-order traversal / 2 D 5

C 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

Trupti agrawal 24
+
- %
Tree Traversal (contd.) A * * 4
 pre-order and post-order:
void Node::preOrder () { / 2 D 5
cout << data << endl;
if (left != NULL) left->preOrder (); C 5
if (right != NULL) right->preOrder ();
}
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 % +

Trupti agrawal 25
THANK YOU…!!!

Trupti agrawal 26

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy