ADT Binary Search Tree Ellen Walker CPSC 201 Data Structures Hiram College.
§3 The Search Tree ADT -- Binary Search Trees
-
Upload
sopoline-vance -
Category
Documents
-
view
71 -
download
4
description
Transcript of §3 The Search Tree ADT -- Binary Search Trees
§3 The Search Tree ADT -- Binary Search Trees
【 Definition 】 A binary search tree is a binary tree. It may be empty. If it is not empty, it satisfies the following properties:
(1) Every node has a key which is an integer, and the keys are distinct.
(2) The keys in a nonempty left subtree must be smaller than the key in the root of the subtree.
(3) The keys in a nonempty right subtree must be larger than the key in the root of the subtree.
(4) The left and right subtrees are also binary search trees.30
5
2
40
20
15
12
25
10 22
60
70
8065
1. Definition
1/22
§3 Binary Search Trees
2. ADT
Objects: A finite ordered list with zero or more elements.
Operations:
SearchTree MakeEmpty( SearchTree T );
Position Find( ElementType X, SearchTree T );
Position FindMin( SearchTree T );
Position FindMax( SearchTree T );
SearchTree Insert( ElementType X, SearchTree T );
SearchTree Delete( ElementType X, SearchTree T );
ElementType Retrieve( Position P );
2/22
§3 Binary Search Trees
3. Implementations
Find
Position Find( ElementType X, SearchTree T ) { if ( T == NULL ) return NULL; /* not found in an empty tree */ if ( X < T->Element ) /* if smaller than root */ return Find( X, T->Left ); /* search left subtree */ else if ( X > T->Element ) /* if larger than root */
return Find( X, T->Right ); /* search right subtree */ else /* if X == root */
return T; /* found */}
T( N ) = S ( N ) = O( d ) where d is the depth of X
Must this test be performed first?
These aretail recursions.
3/22
§3 Binary Search Trees
Position Iter_Find( ElementType X, SearchTree T ) { /* iterative version of Find */ while ( T ) { if ( X == T->Element )
return T ; /* found */ if ( X < T->Element ) T = T->Left ; /*move down along left path */ else T = T-> Right ; /* move down along right path */ } /* end while-loop */ return NULL ; /* not found */}
4/22
§3 Binary Search Trees FindMin
Position FindMin( SearchTree T ) { if ( T == NULL ) return NULL; /* not found in an empty tree */ else if ( T->Left == NULL ) return T; /* found left most */ else return FindMin( T->Left ); /* keep moving to left */}
FindMax
Position FindMax( SearchTree T ) { if ( T != NULL ) while ( T->Right != NULL )
T = T->Right; /* keep moving to find right most */ return T; /* return NULL or the right most */}
T( N ) = O ( d )
T( N ) = O ( d )
5/22
§3 Binary Search Trees
Insert
30
5
2
40
Sketch of the idea:
Insert 80
check if 80 is already in the tree
80 > 40, so it must be the right child of 4080
Insert 35 check if 35 is already in the tree
35 < 40, so it must be the left child of 40
35
Insert 25 check if 25 is already in the tree
25 > 5, so it must be the right child of 5
25
This is the last node we encounter
when search for the key number.It will be the parent
of the new node.
6/22
§3 Binary Search Trees
SearchTree Insert( ElementType X, SearchTree T ) { if ( T == NULL ) { /* Create and return a one-node tree */
T = malloc( sizeof( struct TreeNode ) ); if ( T == NULL ) FatalError( "Out of space!!!" ); else { T->Element = X; T->Left = T->Right = NULL; }
} /* End creating a one-node tree */ else /* If there is a tree */ if ( X < T->Element )
T->Left = Insert( X, T->Left ); else if ( X > T->Element ) T->Right = Insert( X, T->Right ); /* Else X is in the tree already; we'll do nothing */
return T; /* Do not forget this line!! */ }
How would youHandle duplicated
Keys?T( N ) = O ( d )
7/22
§3 Binary Search Trees
Delete
Delete a leaf node : Reset its parent link to NULL.
Delete a degree 1 node : Replace the node by its single child.
Delete a degree 2 node :
Replace the node by the largest one in its left subtree or the smallest one in its right subtree.
Note: These kinds of nodeshave degree at most 1.
Delete the replacing node from the subtree.
〖 Example 〗 Delete 60 40
50
45 55
52
60
70
20
10 30
Solution 1: reset left subtree. 55
52Solution 2: reset right subtree.
8/22
§3 Binary Search Trees
SearchTree Delete( ElementType X, SearchTree T ) { Position TmpCell; if ( T == NULL ) Error( "Element not found" ); else if ( X < T->Element ) /* Go left */
T->Left = Delete( X, T->Left ); else if ( X > T->Element ) /* Go right */
T->Right = Delete( X, T->Right ); else /* Found element to be deleted */ if ( T->Left && T->Right ) { /* Two children */ /* Replace with smallest in right subtree */ TmpCell = FindMin( T->Right ); T->Element = TmpCell->Element; T->Right = Delete( T->Element, T->Right ); } /* End if */ else { /* One or zero child */ TmpCell = T; if ( T->Left == NULL ) /* Also handles 0 child */
T = T->Right; else if ( T->Right == NULL ) T = T->Left; free( TmpCell ); } /* End else 1 or 0 child */
return T; } T( N ) = O ( h ) where h is the height of the tree
9/22
§3 Binary Search Trees
Note: If there are not many deletions, then lazy deletion may be employed: add a flag field to each node, to mark if a node is active or is deleted. Therefore we can delete a node without actually freeing the space of that node. If a deleted key is reinserted, we won’t have to call malloc again.
Note: If there are not many deletions, then lazy deletion may be employed: add a flag field to each node, to mark if a node is active or is deleted. Therefore we can delete a node without actually freeing the space of that node. If a deleted key is reinserted, we won’t have to call malloc again.
While the number of deleted nodes is the same as the number of active nodes
in the tree, will it seriously affect the efficiency of the operations?
10/22
§3 Binary Search Trees4. Average-Case Analysis
Question: Place n elements in a binary search tree. How high can this tree be?
Answer: The height depends on the order of insertion.
〖 Example 〗 Given elements 1, 2, 3, 4, 5, 6, 7. Insert them into a binary search tree in the orders:
4, 2, 1, 3, 6, 5, 7 and 1, 2, 3, 4, 5, 6, 7
4
5
6
7
2
1 3
h = 2
2
3
1
4
56
7
h = 6
11/22
§3 Binary Search Trees
【 Definition 】 The internal path length for a tree of N nodes
is defined to be: 0)1(,)()(1
DidNDN
i
where d( i ) is the depth of node i.
【 Theorem 】 Assume that all binary search trees are
equally likely. Then the average D(N) (taken over all
possible insertion sequences) is O(N log N). Thus the
expected depth of any node is O(log N).
Proof: D( N ) = D( i ) + D( N i 1 ) + N 1root
i nodes N i 1 nodes+1
Since all subtree sizes are equally likely, the average value of both D( i ) and
D( N i 1 ) is .])([1
0NjD
N
j
1])([)(1
02
NjDND
N
jN = p.212-213= O(N log N)
12/22
Home work:
p.139 4.42 Isomorphic Trees
p.140 4.45 Threaded Trees
§4 AVL Trees
Target : Speed up searching (with insertion and deletion)
Tool : Binary search trees
root
small large
Problem : Although Tp = O( height ), but the height can be as bad as O( N ).
13/22
§4 AVL Trees
〖 Example 〗 3 binary search trees obtained for the months of the year
Nov
Oct
Sept
May
Mar
June
July
Dec
Aug
Apr
Feb
Jan
July
June
Mar
May
Oct
SeptNov
Jan
Feb
Aug
Apr Dec
Entered from Jan to Dec
A balanced tree
Average search time = ?3.5
Average search time = ?3.1
What if the monthsWhat if the monthsare entered inare entered in
alphabetical order?alphabetical order?AST = 6.5AST = 6.5
14/22
§4 AVL Trees
Adelson-Velskii-Landis (AVL) Trees (1962)Adelson-Velskii-Landis (AVL) Trees (1962)【 Definition 】 An empty binary tree is height balanced. If T is a
nonempty binary tree with TL and TR as its left and right subtree
s, then T is height balanced iff
(1) TL and TR are height balanced, and
(2) | hL hR | 1 where hL and hR are the heights of TL and TR , respectively.
【 Definition 】 The balance factor BF( node ) = hL hR . In an
AVL tree, BF( node ) = 1, 0, or 1.5
82
1 4
3
7
7
82
1 4
3 5
4
5
6
3
2
1 7
The height of an empty tree is defined to be –1.
15/22
§4 AVL Trees
〖 Example 〗 Input the months Mar
Mar01
May
May0
Nov
Nov0
1
2
May01
Nov00
21
Mar00
0
The trouble maker Nov is in the right subtree’s right subtree of the trouble finder Mar. Hence it is called an RR rotation.
In general:
A1
B0
BL BR
AL
RR
InsertionRR
Rotation
A2
B1
BL BR
AL
BL
B0
A
AL
BR
0
A is not necessarily the root of the tree
Single rotation
16/22
§4 AVL TreesAug
May01
Nov00
21
Mar01
1
Aug0
Apr
Apr0
1
2
2 LL
RotationMay
01
Nov00
21
Aug01
1
1
Mar0
0
Apr0
In general:
A1
B0
BRBL
AR
LL
Insertion
A2
B1
BRBL
AR
LL
RotationB0
A
AR
BL
BR
0
17/22
§4 AVL Trees
May01
Nov00
21
Aug01
1
1
Mar0
0
Apr0
Jan
Jan0
1
1
2
LR
Rotation
Mar01
May01
21
Aug01
0
1
Jan0
0
Apr0
Nov0
In general:
A1
B0
BL
ARC0
CRCL
LR
InsertionA2
B1
BL
ARC1
CRCL
OR
LR
Rotation
BL AR
C0
A1 or 0
CR
B0 or 1
CL
OR
Double Rotation
18/22
§4 AVL TreesDec July
Mar01
May01
21
Aug01
1
1
Jan0
Apr0
Nov0
July0
Dec0
Feb
Feb0
1
1
2
2
RL
Rotation
July0
Dec01
Aug01
21
Jan01
0
1
Feb0
0
Apr0
Mar01
May01
211
Nov0
In general:
A1
B0
BR
ALC0
CL CR
RL
InsertionA2
B1
BR
ALC1
CL CR
OR
RL
Rotation
BRAL
C0
A0 or 1
CL
B1 or 0
CR
OR
19/22
§4 AVL Trees
July0
Dec01
Aug01
21
Jan01
0
1
Feb0
0
Apr0
Mar01
May01
211
Nov0
June Oct Sept
June0
1
1
1
2
Nov0
Dec01
Aug1
21
Feb0
1
July1
Apr0
Mar0
May1
June0
Jan0
Oct0
1
2
1
1
Oct0
Dec01
Aug1
21
Feb0
1
July1
Apr0
Mar0
Nov0
June0
Jan0
May0
Sept0
1
1
1
1
Note: Several bf’s might be changed even if
we don’t need to reconstructthe tree.
Another option is to keep a height field for each node.
20/22
§4 AVL TreesAvlTree Insert( ElementType X, AvlTree T ) { if ( T == NULL ) { /* Create and return a one-node tree */ T = malloc( sizeof( struct AvlNode ) ); if ( T == NULL ) FatalError( "Out of space!!!" ); else { T->Element = X; T->Height = 0; T->Left = T->Right = NULL; } } /* End creating a one-node tree */ else if ( X < T->Element ) { /* handle left insertion */
T->Left = Insert( X, T->Left ); if ( Height( T->Left ) - Height( T->Right ) == 2 ) /* not balanced */ if ( X < T->Left->Element ) /* LL Rotation */
T = SingleRotateWithLeft( T ); else /* LR Rotation */
T = DoubleRotateWithLeft( T ); } /* End left */else if( X > T->Element ) { /* handle right insertion */
T->Right = Insert( X, T->Right ); if ( Height( T->Right ) - Height( T->Left ) == 2 ) /* not balanced */ if ( X > T->Right->Element ) /* RR Rotation */
T = SingleRotateWithRight( T ); else /* RL Rotation */
T = DoubleRotateWithRight( T ); } /* End right */ /* Else X is in the tree already; we'll do nothing */
T->Height = Max( Height( T->Left ), Height( T->Right ) ) + 1; return T; }
Home work:
p.136 4.16 Create an AVL Tree
p.136 4.22
Double Rotation
21/22
§4 AVL Trees
One last question: Obviously we have Tp = O( h )
where h is the height of the tree.But h = ?
Let nh be the minimum number of nodes in a height balanced tree of height h. Then the tree must look like
A
h2 h1
A
h2h1OR nh = nh1 + nh2 + 1
Fibonacci numbers: F0 = 0, F1 = 1, Fi = Fi1 + Fi2 for i > 1
nh = Fh+2 1, for h 0
Fibonacci number theory gives thati
iF
2
51
5
1
)(ln12
51
5
12
nOhn
h
h
22/22