DS 9
DS 9
Shyan-Ming Yuan
CS Department, NYCU
[email protected]
Optimal Binary Search Trees
Given a static set of records, , finding a binary search tree
(BST) that has minimal cost for all possible searches performed
in the static set of records.
, where is the key of .
is the probability for searching in the BST.
is the probability for searching keys in set , where
All keys in
, and
.
𝐾1 0.1
0.1
Cost=0.35*1+0.15*2+0.1*3
+0.15*1+0.1*2+0.1*3+0.05*3 0.05
𝐾2 =0.95 (success)+0.8 (failure) 𝐾 1< 𝐾 2< 𝐾 3
=1.75
𝐾3
Search Types
Success.
Search for a key that is in the BST.
Terminates at an internal node.
Failure.
Search for a key that is not in the BST.
Terminates at an external/failure node.
𝐾2
𝐾1 𝐾3
𝑋0 𝑋1 𝑋2 𝑋3
Internal and External Nodes in BST
𝐾2
𝐾1 𝐾3
𝑋0 𝑋1 𝑋2 𝑋3
A BST with n internal nodes has n + 1 external nodes.
Let be the internal nodes, in inorder.
, where is the key of .
Assume that there are two virtual keys and
Let be the external nodes, in inorder.
is reached iff < searched key < .
Cost Of Binary Search Tree
𝐾2
𝐾1 𝐾3
𝑋0 𝑋1 𝑋2 𝑋3
𝑇 𝑖𝑖
𝑋𝑖
For All Cases of i<j
= least cost BST for keys for all .
contains node and .
Let be the key of the root of .
𝐾𝑣
𝑇 𝑖𝑖
L R
= cost(L)+cost(R)++
= cost(L)+cost(R)+ =
Thus, .
The is the BST that is rooted by the node with the key .
Computation Of and
Initially, (zero-key trees).
Use , to compute:
(one-key trees).
Now, we can compute all 2-key trees:
(2-key trees).
Then, all 3-key trees can be computed:
(3-key trees).
Continue until and (n-key tree) have been computed
Example for Computing and
Assume that n=4 and ()=(10,15,20,25).
()=(3,3,1,1) and ()=(2,3,1,1,1).
The p’s and q’s are multiplied by 16 for convenience.
0 1 2 3 4
3 1 1 1
0-key
trees
1-key
1 3 8
trees
12
2-key 1
trees
14
3-key
trees
4-key tree
32
Time Complexity
Since ,
O(n) time to compute one
O( number of s to compute
Total time is O(
It was shown by D. E. Knuth that the optimal may be
obtained by
JUL
Y
FE MA
B Y
AU JA MA OCT
G N R
SEP
AP JUN NOV
DEC T
R E
Max # of comparisons:
4
Avg # of comparisons:
3.1
A Degenerated BST
When the input Sequence is: APR, AUG, DEC, FEB,
AP
JAN, JULY, JUNE, MAR, AUG MAY, NOV, OCT,
R AUSEPT
G
DEC FE
B JA
N JUL
Y JUN
E MA
R MA
Y NO
V
OCT
SEP
Max # of comparisons: T
12
Avg # of comparisons:
6.5
The Optimal Dynamic BST
In a dynamic situation, it is very difficult to maintain a BST
as a complete binary tree at all times.
After each insertion and deletion, the BST needs to be adjusted
in such a way that it is a complete binary tree.
However, it is possible to keep an node BST balanced
enough such that both average and worst case search time are
.
The AVL tree was proposed by Adelson-Velskii and Landis in
1962.
The AVL tree is always balanced with respect to the heights of
subtrees.
It has retrieval time of .
Keys can be inserted and deleted from an AVL tree in also.
The AVL Tree
An empty binary tree is height balanced.
A nonempty binary tree with and as its left and right subtrees
respectively, is height balanced iff
Both and are height balanced and
where and are the heights of and , respectively.
The Balance Factor , , of a node in a binary tree is defined as
, where
is the height of the left subtree of and
is the height of the right subtree of .
+1 +1
MA MA
R R
-1 -1 -1 -1
AU MA AU MA
G Y G Y
0 +1 0 0 0 0
NO NO
APR JAN APR JAN
V V
0 0 0
JUL
DEC DEC
Y
Insert “DECEMBER” Insert “JULY”
+2 +1
MA MA
R R
-2 -1 0 -1
AU MA RL MA
DEC
G Y Y
+1 0 +1 0 0
0 AU NO
NO JAN
APR JAN G V
V
-1 0 0 0 0
JUL JUL
DEC APR FEB
Y Y
0
FEB Insert “FEBRUARY”
+2 0
MA JAN
R LR
-1 -1 +1 0
MA MA
DEC DEC
Y R
-1 0 +1 0 -1 -1
+1 AU JUL MA
AU NO FEB
JAN G Y Y
G V
0 0 - 0 0 0
JUL NO
APR FEB 1 APR JUN
Y V
0
Insert “JUNE” JUN
-1 0
JA JA
N N
+1 -1 +1 0
DE MA RR DE MA
C R C R
+1 0 -1 -2 +1 0 -1 0
AU FE JUL MA AU FE JUL NO
G B Y Y G B Y V
0 0 -1 0 0 0
AP NO AP 0
JUN MA
R V R JUN Y
OCT
0
Insert “OCTOBER” OCT
-1
JA
N
+1 -1
DE MA
C R
+1 0 -1 -
AU FE JUL NO
G Y
1
B V
0 0 0 -
AP MA
JUN 1
OCT
R Y
0
Insert “SEPTEMBER” SEP
T
Rebalancing an AVL Tree on Insertion
An AVL tree may be unbalanced when inserting a new node.
After inserting a new node, retrace path towards root and adjust
balance factors (BF) as needed.
Stop at a node whose BF becomes 0, 2, or –2, or reach the root.
If the stopped node whose BF is either 2 or –2, then the new tree is not an
AVL now.
In this case, we say the tree has become unbalanced.
Let node A be the nearest ancestor of the inserted node Y,
where the balance factor of A becomes .
There are 4 kinds of rebalancing rotations can be derived.
LL rotation: Y is inserted in the left subtree of the left subtree of
A.
RR rotation: Y is inserted in the right subtree of the right subtree
The LL Rotation
A 1 A 2 B 0
B 0 B 1 0 A
h h
h+1
h h h h h
h+1
A -1 A -2 B 0
0 B -1 B A 0
h h
h+1
h h h h h
h+1
A 1 A 2 C 0
B 0 B -1 B 0 0 A
h h
C
h h h 0 h h h h
A 1 A 2 C 0
B 0 B -1 B 0 -1 A
h h
C C
h 0 h 1 h h h
h-
1
(c) When BF(C)==1, after LR1
h rotation.
h- h-1 h- 1. Move C up to A’s position
1 1 2. Set A be the right child of C
(a) Before (b) After inserting into , the height 3. Set B be the left child of C
insertion of the new () becomes h and the 4. Set be the right subtree of B
BF(C)==1. 5. Set be the left subtree of A
The LR Rotation: case 3
A 1 A 2 C 0
B 0 B -1 B 1 0 A
h h
C C
h 0 h -1 h h h
h-
1
(c) When BF(C)==-1, after LR-1
h rotation.
h- h-1 h- 1. Move C up to A’s position
1 1
2. Set A be the right child of C
(a) Before insertion(b) After inserting into , the height 3. Set B be the left child of C
of the new () becomes h and the 4. Set be the right subtree of B
BF(C)=-1. 5. Set be the left subtree of A
The RL Rotation: case 1
A -1 A -2 C 0
0 B 1 B A 0 0 B
h h C
h h 0 h h h h h
A -1 A -2 C 0
0 B 1 B A 0 -1 B
h C h C
0 h 1 h h h h
h-1
A -1 A -2 C 0
0 B 1 B A 1 0 B
h C h C
0 h -1 h h h h
h-
1
(c) When BF(C)==-1, after RL-1
h rotation.
h- h-1 h- 1. Move C up to A’s position
1 1
2. Set A be the left child of C
(a) Before insertion (b) After inserting into , the height 3. Set B be the right child of C
of the new () becomes h and the 4. Set be the right subtree of A
BF(C)==-1. 5. Set be the left subtree of B
6. Set BF(A)=1 and BF(B)=BF(C)=0
Rebalancing an AVL Tree on Deletion
An AVL tree may be unbalanced after deleting a node.
After deleting a node, retrace path towards root and adjust
balance factors (BF) as needed.
If a node whose BF is either 2 or –2, then the new tree is
unbalanced.
Let node A be the nearest ancestor of the deleted node Y, where
the balance factor of A becomes ±2.
There are 2 kinds of rebalancing rotation can be derived.
Type L: Y is deleted from left subtree of A and BF(A)==-2.
Old BF(A) is -1 and A has a right child B.
L0 Rotation: BF(B)==0; L1 Rotation: BF(B)==1; L-1 Rotation:
BF(B)==-1.
Type R: Y is deleted from right subtree of A and BF(A)==2.
The L0 Rotation
A -1 A -2 B 1
0 B 0 B A -1 h
h-
h 1
h h h h h
h-
1
A -1 A -2 B 0
-1 B -1 B A 0 h
h-
h 1
h- h h- h h-
1 1 h- 1
1
(a) Before deletion (b) after deletion (c) After L-1 rotation.
from , the height of the 1. Move B up to A’s position
new () becomes h-1. 2. Set A be the left child of B
3. Set be the right subtree of A
4. Set BF(A)=BF(B)=0
The L1 Rotation
A -1 A -2 C 0
A 1 A 2 B 0
B 0 B 0 1 A
h-
h 1
h
h h h h h
h-
1
(a) Before insertion (b) After deletion from (c) After R0 rotation.
, the height of the new 1. Move B up to A’s position
() becomes h-1. 2. Set A be the right child of B
3. Set be the left subtree of A
4. Set BF(A)=1 and BF(B)=0
The R1 Rotation
A 1 A 2 B 0
B 1 B 1 0 A
h-
h 1
h
h h- h h- h-
1 1 1 h-
1
(a) Before insertion (b) After deletion from (c) After R1 rotation.
, the height of the new 1. Move B up to A’s position
() becomes h-1. 2. Set A be the right child of B
3. Set be the left subtree of A
4. Set BF(A)=BF(B)=0
The R-1 Rotation
C 0
A 1 A 2