Академический Документы
Профессиональный Документы
Культура Документы
Trees
Hikmat Farhat
April 2, 2014
Hikmat Farhat
Data Structures
April 2, 2014
1 / 59
Introduction
Hikmat Farhat
Data Structures
April 2, 2014
2 / 59
If R is the root of the tree then the root of each subtree is called the
child of R.
R is called the parent of the root of each subtree.
Using the recursive definition we can deduce that a tree with N nodes
has N 1 edges since each node, expect the root, has exactly one
parent.
Each node of the tree can have 0 or more children.
A node that has 0 children is called a leaf
Nodes that have the same parent are called siblings
Hikmat Farhat
Data Structures
April 2, 2014
3 / 59
Hikmat Farhat
Data Structures
April 2, 2014
4 / 59
Example
Hikmat Farhat
Data Structures
April 2, 2014
5 / 59
Traversal of Trees
Hikmat Farhat
Data Structures
April 2, 2014
6 / 59
Preorder Traversal
Output
usr
Hikmat Farhat
Data Structures
April 2, 2014
7 / 59
Preorder Traversal
Output
usr
mark
Hikmat Farhat
Data Structures
April 2, 2014
7 / 59
Preorder Traversal
Output
usr
mark
book
Hikmat Farhat
Data Structures
April 2, 2014
7 / 59
Preorder Traversal
Output
usr
mark
book
ch1.r
Hikmat Farhat
Data Structures
April 2, 2014
7 / 59
Preorder Traversal
Output
usr
mark
book
ch1.r
ch2.r
Hikmat Farhat
Data Structures
April 2, 2014
7 / 59
Preorder Traversal
Output
usr
mark
book
ch1.r
ch2.r
ch3.r
Hikmat Farhat
Data Structures
April 2, 2014
7 / 59
Preorder Traversal
Output
usr
mark
book
ch1.r
ch2.r
ch3.r
course
Hikmat Farhat
Data Structures
April 2, 2014
7 / 59
Preorder Traversal
Output
usr
mark
book
ch1.r
ch2.r
ch3.r
course
cop3530
Hikmat Farhat
Data Structures
April 2, 2014
7 / 59
Preorder Traversal
Output
usr
mark
book
ch1.r
ch2.r
ch3.r
course
cop3530
fall05
Hikmat Farhat
Data Structures
April 2, 2014
7 / 59
Preorder Traversal
Output
usr
mark
book
ch1.r
ch2.r
ch3.r
course
cop3530
fall05
sy1.r
Hikmat Farhat
Data Structures
April 2, 2014
7 / 59
Postorder Traversal
Output
Hikmat Farhat
Data Structures
April 2, 2014
8 / 59
Postorder Traversal
Output
Hikmat Farhat
Data Structures
April 2, 2014
8 / 59
Postorder Traversal
Output
Hikmat Farhat
Data Structures
April 2, 2014
8 / 59
Postorder Traversal
Output
ch1.r
Hikmat Farhat
Data Structures
April 2, 2014
8 / 59
Postorder Traversal
Output
ch1.r
ch2.r
Hikmat Farhat
Data Structures
April 2, 2014
8 / 59
Postorder Traversal
Output
ch1.r
ch2.r
ch3.r
Hikmat Farhat
Data Structures
April 2, 2014
8 / 59
Postorder Traversal
Output
ch1.r
ch2.r
ch3.r
book
Hikmat Farhat
Data Structures
April 2, 2014
8 / 59
Postorder Traversal
Output
ch1.r
ch2.r
ch3.r
book
Hikmat Farhat
Data Structures
April 2, 2014
8 / 59
Postorder Traversal
Output
ch1.r
ch2.r
ch3.r
book
Hikmat Farhat
Data Structures
April 2, 2014
8 / 59
Postorder Traversal
Output
ch1.r
ch2.r
ch3.r
book
Hikmat Farhat
Data Structures
April 2, 2014
8 / 59
Postorder Traversal
Output
ch1.r
ch2.r
ch3.r
book
sy1.r
Hikmat Farhat
Data Structures
April 2, 2014
8 / 59
Recursive Implementation
v o i d p o s t o r d e r ( Node t ) {
i f ( t==NULL) r e t u r n ;
p o s t o r d e r ( t> l e f t ) ;
p o s t o r d e r ( t>r i g h t ) ;
cout <<t>data<<e n d l ;
}
v o i d p r e o r d e r ( Node t ) {
i f ( t==NULL) r e t u r n ;
cout <<t>data<<e n d l ;
p r e o r d e r ( t> l e f t ) ;
p r e o r d e r ( t>r i g h t ) ;
Hikmat Farhat
Data Structures
April 2, 2014
9 / 59
Binary Trees
A binary tree is a tree in which no node can have more than two
children.
The height of a tree is equal to the height of its root.
The depth of a tree is equal to the depth of its deepest leaf which is
equal to the height of the tree.
Hikmat Farhat
Data Structures
April 2, 2014
10 / 59
Inorder Traversal of BT
v o i d i n o r d e r ( Node t ) {
i f ( t==NULL) r e t u r n ;
i n o r d e r ( t> l e f t ) ;
cout <<t>data<<e n d l ;
i n o r d e r ( t>r i g h t ) ;
}
Hikmat Farhat
Data Structures
April 2, 2014
11 / 59
Hikmat Farhat
Data Structures
April 2, 2014
12 / 59
Example
Hikmat Farhat
Data Structures
April 2, 2014
13 / 59
If x > y we go right
If x < y then we proceed to the left
Hikmat Farhat
Data Structures
April 2, 2014
14 / 59
BST interface
BST( c o n s t Comparable &x ) {
r o o t=new Node ( x , NULL , NULL ) ;
}
i n t numNodes ( ) ;
i n t numLeaves ( ) ;
v o i d i n s e r t ( c o n s t Comparable & x ) ;
v o i d remove ( c o n s t Comparable & x ) ;
Comparable f i n d M i n ( ) ;
Comparable f i n d M a x ( ) ;
int height ();
void print ( ) ;
void printPreorder (){ printPreorder ( root );}
void printPostorder (){ printPostorder ( root );}
void p r i n t I n o r d e r (){ p r i n t I n o r d e r ( root );}
Hikmat Farhat
Data Structures
April 2, 2014
15 / 59
BST interface
private :
s t r u c t Node {
Node l e f t ;
Node r i g h t ;
Comparable d a t a ;
Node ( c o n s t Comparable &x , Node l , Node r )
: data ( x ) , l e f t ( l ) , r i g h t ( r ){}
};
Node r o o t ;
v o i d p r i n t ( Node n , i n t l e v e l ) ;
v o i d i n s e r t ( c o n s t Comparable &x , Node &t ) ;
v o i d remove ( c o n s t Comparable &x , Node & t ) ;
v o i d p r i n t P r e o r d e r ( Node n ) ;
v o i d p r i n t P o s t o r d e r ( Node n ) ;
v o i d p r i n t I n o r d e r ( Node n ) ;
Hikmat Farhat
Data Structures
April 2, 2014
16 / 59
BST interface
Node f i n d M i n ( Node t ) ;
Node f i n d M a x ( Node t ) ;
i n t h e i g h t ( Node t ) ;
i n t numNodes ( Node t ) ;
i n t numLeaves ( Node t ) ;
Hikmat Farhat
Data Structures
April 2, 2014
17 / 59
If
If
If
If
the
the
the
the
Hikmat Farhat
Data Structures
April 2, 2014
18 / 59
Example Search
?
=3
Hikmat Farhat
Data Structures
April 2, 2014
19 / 59
Example Search
=3
Hikmat Farhat
Data Structures
April 2, 2014
19 / 59
Example Search
=3
Hikmat Farhat
Data Structures
April 2, 2014
19 / 59
Example Search
=3
Hikmat Farhat
Data Structures
April 2, 2014
19 / 59
Inserting an Element
Hikmat Farhat
Data Structures
April 2, 2014
20 / 59
Example Insert
?
=5
Hikmat Farhat
Data Structures
April 2, 2014
21 / 59
Example Insert
=5
Hikmat Farhat
Data Structures
April 2, 2014
21 / 59
Example Insert
=5
Hikmat Farhat
Data Structures
April 2, 2014
21 / 59
Example Insert
5
Hikmat Farhat
Data Structures
April 2, 2014
21 / 59
Inserting An Element
t e m p l a t e <typename Comparable>
v o i d BST<Comparable > : : i n s e r t ( c o n s t Comparable &x ) {
i n s e r t (x , root );
}
t e m p l a t e <typename Comparable>
v o i d BST<Comparable > : : i n s e r t ( c o n s t Comparable &x , Node &t )
{
i f ( t==NULL) {
t=new Node ( x , NULL , NULL ) ;
}
e l s e i f ( x< t>d a t a ) {
i n s e r t ( x , t> l e f t ) ;
}
else{
i n s e r t ( x , t>r i g h t ) ;
}
return ;
}
Hikmat Farhat
Data Structures
April 2, 2014
22 / 59
Deleting An Element
Hikmat Farhat
Data Structures
April 2, 2014
23 / 59
Hikmat Farhat
Data Structures
April 2, 2014
24 / 59
Hikmat Farhat
Data Structures
April 2, 2014
25 / 59
Deleting An Element
t e m p l a t e <typename Comparable>
v o i d BST<Comparable > : : remove ( c o n s t Comparable &x , Node &t ) {
i f ( t==NULL) r e t u r n ;
i f ( x<t>d a t a ) remove ( x , t> l e f t ) ;
e l s e i f ( x> t>d a t a ) remove ( x , t>r i g h t ) ;
/ f o u n d t h e node /
e l s e i f ( t> l e f t !=NULL && t>r i g h t !=NULL) {
t>d a t a=f i n d M i n ( t>r i g h t )>d a t a ;
remove ( t>data , t>r i g h t ) ;
}
else {
Node o ld N o d e=t ;
t =(t> l e f t !=NULL) ? t> l e f t : t>r i g h t ;
d e l e t e ol d N o de ;
}
}
Hikmat Farhat
Data Structures
April 2, 2014
26 / 59
AVL Trees
Hikmat Farhat
Data Structures
April 2, 2014
27 / 59
Worst-case Height
Hikmat Farhat
Data Structures
April 2, 2014
28 / 59
Hikmat Farhat
Data Structures
April 2, 2014
29 / 59
Example AVL
Figure : Two BSTs. Only the left is an AVL tree. In the tree to the right,
h(8)=0, h(2)=2
Hikmat Farhat
Data Structures
April 2, 2014
30 / 59
Insertion/Deletion
Inserting or deleting a node from an AVL tree might destroy the AVL
property.
To restore the AVL property we need to perform rotations
We will study two types of rotations:
1
2
Single rotation
Double rotation
Hikmat Farhat
Data Structures
April 2, 2014
31 / 59
Example Insertion
Hikmat Farhat
Data Structures
April 2, 2014
32 / 59
Hikmat Farhat
Data Structures
April 2, 2014
33 / 59
Single Rotation
Hikmat Farhat
Data Structures
April 2, 2014
34 / 59
Hikmat Farhat
Data Structures
April 2, 2014
35 / 59
Double Rotation
Hikmat Farhat
Data Structures
April 2, 2014
36 / 59
General Rules
Hikmat Farhat
Data Structures
April 2, 2014
37 / 59
Priority Queue
Hikmat Farhat
Data Structures
April 2, 2014
38 / 59
Simple Implementation
insertion is done at the head (or tail if we keep track of it) of the list in
O(1)
deleteMin can be done in O(n).
Hikmat Farhat
Data Structures
April 2, 2014
39 / 59
Binary Heap
A binary heap is a binary tree of height h where the subtree of height
h 1 is perfect.
This means that all levels, with the possible exception of the bottom
one, are completely filled.
Data Structures
April 2, 2014
40 / 59
Hikmat Farhat
Data Structures
April 2, 2014
41 / 59
Hikmat Farhat
Data Structures
April 2, 2014
42 / 59
Inserting an Element
4
5
Hikmat Farhat
Data Structures
April 2, 2014
43 / 59
Example
14
14
Hikmat Farhat
Data Structures
April 2, 2014
44 / 59
Implementation
v o i d i n s e r t ( i n t v a l ){
A[++ s i z e ]= v a l ;
i n t h o l e=s i z e ;
w h i l e ( h o l e /2>=1){
i f ( v a l <A [ h o l e / 2 ] ) {
A [ h o l e ]=A [ h o l e / 2 ] ;
h o l e=h o l e / 2 ;
}
else
break ;
}
A [ h o l e ]= v a l ;
}
Hikmat Farhat
Data Structures
April 2, 2014
45 / 59
Hikmat Farhat
Data Structures
April 2, 2014
46 / 59
Deleting Minimum
Hikmat Farhat
Data Structures
April 2, 2014
47 / 59
DeleteMin: Take 1
h o l e =1;
w h i l e ( 2 h o l e <=s i z e ) {
c h i l d =2 h o l e ;
i f (A [ c h i l d +1]<A [ c h i l d ] ) c h i l d ++;
A [ h o l e ]=A [ c h i l d ] ;
h o l e=c h i l d ;
}
The above code will leave a hole in one of the leaves which breaks the
heap property.
We can fill it with the last element of the heap and reduce the size by
one. Does it work?
Hikmat Farhat
Data Structures
April 2, 2014
48 / 59
Hikmat Farhat
Data Structures
April 2, 2014
49 / 59
16
14
19
65
19
21
26
32
68
24
14
19
16
26
65
Hikmat Farhat
21
24
19
68
32
Data Structures
April 2, 2014
50 / 59
Data Structures
April 2, 2014
51 / 59
Building a Heap
Hikmat Farhat
Data Structures
April 2, 2014
52 / 59
Hikmat Farhat
Data Structures
April 2, 2014
53 / 59
Hikmat Farhat
Data Structures
April 2, 2014
54 / 59
Implementation
Hikmat Farhat
Data Structures
April 2, 2014
55 / 59
Complexity of BuildHeap
Hikmat Farhat
Data Structures
April 2, 2014
56 / 59
h
X
d=0
2d (h d)
Hikmat Farhat
Data Structures
April 2, 2014
57 / 59
H = h + 2 + 4 + 8 + . . . + 2h1 + 2h
= (h + 1) + (1 + 2 + 4 + . . . + 2h )
= (h + 1) + (2h+1 1)
Hikmat Farhat
Data Structures
April 2, 2014
58 / 59
Hikmat Farhat
Data Structures
April 2, 2014
59 / 59