0% found this document useful (0 votes)
19 views41 pages

DS 9

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views41 pages

DS 9

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 41

Efficient Binary Search Trees

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
.

The cost for performing a search in a BST is defined as the


comparisons needed to either finding the record or determining
not in the BST.
It is equal to the level of the key in the BST.
Example of BSTs
Key probabilit
𝐾2 Cost=0.35*2+0.15*1+0.1*2
+0.15*2+0.1*2+0.1*2+0.05*2
y
0.15
=1.05 (success)+0.8 (failure)
=1.85 0.35
𝐾1 𝐾3 0.1
0.15

𝐾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

 Let = probability for searching , .


 Let = probability for searching a key and reaching , .
Sum of all ps and all qs must be 1.
 Cost of a BST =
How to Determine the Optimal BST?
The Brute force Method
Generate all possible binary trees
Compute the cost of each tree
Determine the tree with minimum cost
Time complexity
O(nN(n)) where N(n) is the number of distinct BSTs with n
nodes
N(n)=O()
This method is impractical for large n.
Dynamic Programming
Let keys are .
Let = least cost BST for keys for all .
Then = least cost BST for keys .
 contains internal nodes and external nodes , therefore the
probability of being reached is the sum of and .
Let be the weight of .
 is then the cost of .
Let be the root of the BST .
For All Cases of i=j
 contains only the external node with probability of .
Thus, is a zero key BST containing .
, and.

𝑇 𝑖𝑖
𝑋𝑖
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

L contains nodes and .


R contains nodes and .
The Cost of , L and R
L contains nodes and .
R contains nodes and .
Since L and R are subtrees of , the level of each node in L is 1 more
than when L is viewed as a stand alone tree and the level of each
node in R is 1 more than when R is viewed as a stand alone tree.
 and

= 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

Total time then can be reduced to O(


Finding an Optimal BST
void Obst (double *p, double *q, int n) {
for (int i=0; i<n; i++) {
w [i][i] = q [i]; r [i][i] = c [i][i] = 0; // initialization
w [i][i+1] = q [i]+q [i+1]+p [i+1]; // optimal 1-key trees
r [i][i+1] = i+1;
c [i][i+1] = w [i][i+1];
}
w [n][n] = q [n]; r [n][n] = c [n][n] = 0;
for (int m=2; m<=n; m++) // find optimal m-key trees
for (i=0; i<=n-m; i++) {
int j = i+m;
w [i][j] = w [i][j-1]+p [j]+q [j];
int k = KnuthMin (i, j); // or int k = cMin (i, j); O(
// KnuthMin returns a value k in the range [r[i][j-1], r[i+1][j]]. O(
// minimizing c[i][k-1]+c[k][j]
c [i][j] = w [i][j] + c [i][k-1] + c [k][j];
r [i][j] = k;
}
}
Dynamic BSTs
In general, the records maintained by a BST may be
dynamically inserted and deleted.
Thus, the structure of a BST will be changed dynamically
and the resulting BST depends on the sequence of insertions
and deletions.
A BST for the months of a year, when the input sequence
is: JA
MA N
JAN, FEB, MAR, APR,
FE MAY, JUNE, JULY, AUG, SEPT,
B R
OCT, NOV, DEC.
AP JUN MA
R E Y
AU JUL SEP
G Y T
Max # of comparisons:
6 OCT
DEC
Avg # of comparisons:
3.5 NOV
A Balanced BST for Months of a Year
When the input sequence is: JULY, FEB, MAY, AUG,
DEC, MAR, OCT, APR, JAN, JUNE, SEPT, NOV.

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 .

An AVL tree is a binary search tree, where


 node in the AVL, =-1,0, or1.
The AVL Trees for Months of a Year
BF=0 BF=-1 BF=-2 0
MA MA MA RR MA
R R R Y
BF=0 BF=-1 0 0
Insert “MARCH” MA MA MA NO
Y Y R V
Insert “MAY” Insert “NOV” NOBF=0
V
+2 +1
+1 MA MA
MA Y LL Y
Y +2 0 0 0
+1 0 MA NO AU NO
MA NO R V G V
R V +1 0 0
0 AU MA
AU APR
G R
G 0
Insert “AUGUST” APR Insert “APRIL”
+2 0
MA MA
Y LR R
-1 0 -1
AU NO 0
AU MA
G V G Y
0 +1 0 0 0
MA NO
APR APR JAN
R V
0
JAN Insert “JANUARY”

+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) Before insertion (c) After LL rotation.


(b) After inserting into 1. Move B up to A’s position
, the height of the new 2. Set A be the right child of B
() becomes h+1. 3. Set be the left subtree of A
4. Set BF(A)=BF(B)=0
The RR Rotation

A -1 A -2 B 0

0 B -1 B A 0

h h
h+1
h h h h h
h+1

(a) Before insertion (c) After RR rotation.


(b) After inserting into 1. Move B up to A’s position
, the height of the new 2. Set A be the left child of B
() becomes h+1. 3. Set be the right subtree of A
4. Set BF(A)=BF(B)=0
The LR Rotation: case 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

(c) When BF(C)==0, after LR0


(a) Before insertion rotation.
h h
1. Move C up to A’s position
2. Set A be the right child of C
(b) After inserting into , the height 3. Set B be the left child of C
of the new () becomes h+1 and the 4. Set be the right subtree of B
BF(C)==0. 5. Set be the left subtree of A
The LR Rotation: case 2

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

(c) When BF(C)==0, after RL0


(a) Before insertion rotation.
h h
1. Move C up to A’s position
2. Set A be the left child of C
(b) After inserting into , the height 3. Set B be the right child of C
of the new () becomes h+1 and the 4. Set be the right subtree of A
BF(C)=0. 5. Set be the left subtree of B
6. Set BF(A)=BF(B)=0
The RL Rotation: case 2

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

(c) When BF(C)==1, after RL1


h rotation.
h- h-1 h-1 1. Move C up to A’s position
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(B)=-1 and
The RL Rotation: case 3

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) Before deletion (b) after deletion (c) After L0 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)=-1 and BF(B)=1
The L-1 Rotation

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

b==0 => 0 b==0 => 0


1 B 1 B b==1 => 0 A B b==1 => -1
b==-1 =>1 b==-1 => 0
h-
h 1
C C
b h- b h- h-
𝐶𝐿 𝐶𝑅 h-
1
1 1 1

(c) After L1 rotation.


𝐶𝐿 𝐶𝑅 𝐶𝐿 𝐶𝑅 1. Move C up to A’s position
2. Set A be the left child of C
b==0, h-1, h-1 (b) after deletion 3. Set B be the right child of C
b==1, h-1, h-
b==-1, h-2, h- from , the height of the 4. Set be the right subtree of A
2
1 new () becomes h-1. 5. Set be the left subtree of B
(a) Before deletion. 6. Set BF(C)=0
The R0 Rotation

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

b==0 => 0 b==0 => 0


b==1 => 0 B A b==1 => -1
B -1 B -1
b==-1 =>1 b==-1 => 0
h-
h 1
C C
b b
𝐶𝐿 𝐶𝑅 h-
h- h- h-
1 1
1 1
(c) After R-1 rotation.
𝐶𝐿 𝐶𝑅 𝐶𝐿 𝐶𝑅 1. Move C up to A’s position
2. Set A be the right child of C
b==0, h-1, h-1 3. Set B be the left child of C
b==1, h-1, h- (b) After deletion from
b==-1,
2 h-2, h- , the height of the new 4. Set be the right subtree of B
1(a) Before deletion. () becomes h-1. 5. Set be the left subtree of B
6. Set BF(C)=0
Comparison of Various Structures

operation Sorted array Sorted Link lists AVL tree


Search for x O() O(N) O()
Search for jth O(1) O(j) O()
item
Delete x O(N) O(1) O()
Delete jth item O(N-j) O(j) O()
Insert x O(N) O(1) O()
Output in order O(N) O(N) O(N)

Assume that the positions of insert x and delete x are known in


advance.

You might also like