0% found this document useful (0 votes)
48 views

Dynamic Programming 3

The document discusses binary search tree deletion operations. It describes 4 cases for deleting a node x from a binary search tree: 1) x is a leaf node, 2) x has one child and is not the root, 3) x has one child and is the root, 4) x has two children. For case 4, the algorithm finds the smallest element s in x's right subtree and replaces x with s. The document also covers average case analysis of insertion, showing insertions take O(log n) time on average, and describes an optimal binary search tree problem.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views

Dynamic Programming 3

The document discusses binary search tree deletion operations. It describes 4 cases for deleting a node x from a binary search tree: 1) x is a leaf node, 2) x has one child and is not the root, 3) x has one child and is the root, 4) x has two children. For case 4, the algorithm finds the smallest element s in x's right subtree and replaces x with s. The document also covers average case analysis of insertion, showing insertions take O(log n) time on average, and describes an optimal binary search tree problem.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

IT-307

Algorithm Notes

Dynamic Programming-3

By
Dr. K. V. Arya
ABV-IIITM, Gwalior, India
Binary Search Tree
The Deletion Operation
To delete x from the BST:
Procedure search can be modified to return
the node v that has value x (instead of a
boolean value) . Once this has been found,
there are four cases.
1. v is a leaf.
this just remove v from the tree. 2
The deletion operation
2. v has exactly one child w, and v is not the root.

Then make the parent of v the parent of w.

v w
X

3
The deletion operation
3. v has exactly one child w, and v is the root.

Then make w the new root.

v
X
w

4
The deletion operation
4. v has 2 children.
This is the interesting case . First, find the smallest element s in
the right sub tree of v (using procedure min). Delete s (note that
this uses case 1 or 2 above). Replace the value in node v with s.
v
10 12

5 15 5 15

s
2 7 12 2 7 13

5
13
Binary search tree
• Note : could have used largest in left sub tree for s.

Correctness: obvious for cases 1, 2 ,3. in case 4, s is


larger than all of the values in the left subtree of v, and
smaller than the other values in the right subtree of v.
therefore, s can replace the value in v.

Analysis: running time O(d) – dominated by search for


node v.
6
The insert operation
procedure insert (x,v) ;
comment insert x in BST with root v
if v is the empty tree
then create a root node v with l(v) = x
else
if x < l(v) then
if v has a left child w
then insert (x,w)
else
create a new left child w of v
l(w) := x
7
Correctness: once again, an easy induction
Analysis: Once again O(d)
Analysis
• All operations run in time O(d).
But how big is d?
The worst case for an n node BST is O(n) and Ω(log n ).

8
The average case is O(log n)
Average Case Analysis
What is the average case running time for n insertions into
an empty BST ? Suppose we insert x1, ….xn, where x1 < x2 <
…xn (not necessarily inserted in that order). Then

• Run time is proportional to number of comparisons.

• Measure number of comparisons, T(n).

• The root is equally likely to be xj for 1≤j≤n (whichever is


inserted first).

Suppose, the root is xj. List the values to be inserted in 9

ascending order,
Average Case Analysis
• x1,…xj-1 : these go to the left of the root; needs j -1
comparisons to root, T(j-1) comparisons to each other.

• xj: this is the root, needs no comparisons.

• xj+1 ,…,xn : these go to the right of the root; needs n – j


comparisons to the root, T(n – j) comparisons to each
other.

Therefore, when the root is xj the total number of


comparisons is T(j – 1 ) + T( n – j) + n – 1
10
Average Case Analysis
Therefore, T(0) = 0 , and for n > 0

This is just like the quicksort analysis ! It can be shown


similarly , that T(n) ≤ knlog n where k = loge4 ≈ 1.39.

Therefore, each insert operation takes O(log n) time on


11
average.
Optimal BST
Given:

• S = {x1, …xn} , xi < xi+1 for 1 ≤ i < n.

• For all 1 ≤ i ≤ n , the probability pi that we will be asked search


(xi,S).

• For all 0 ≤i ≤n, the probability qi that we will be asked search ( x,


S) for some xi < x <xi+1 ( where x0 = -∞ xn+1 = ∞).

The problem : construct a BST that has the minimum number of


expected comparisons. 12
Fictitious Nodes
Add fictitious nodes labelled 0,1….,n to the BST . Node i is where
we would fall off the tree on a query search (x, S) where xi < x <
xi+1 .
Example:

13
Depth of a Node
• Definition: The Depth of a node v is
• 0 if v is the root.
• d + 1 if v’s parent has depth d.
0 15

1 5

2 10
2
3
7 12
14
Cost of a Node
Let depth (xi ) be the depth of the node v with l(v)= xi.
Number of comparisons for search (xi,S) is
depth(xi) + 1.

This happens with probability pi.


Number of comparisons for search(x, S) for some xi < x < xi+1 is

depth(i).

This happens with probability qi.


15
Cost of a BST
The expected number of comparisons is therefore.
n n

∑ ph(depth(xh) + 1) + ∑ qh depth(h).
h=1 h=0

real fictitious
Given the probabilities , we want to find the BST that
minimizes the value.

Call it the cost of the BST.


16
Weight of a BST

nodes
xi+1,……..,xj

T i,j

i j

17
Constructing Ti,j
• To find the best tree Ti,j :

Choose a root xk. Construct Ti,k-1 and Tk,j.

xk

Ti,k-1 Tk,j
18
Computing Ci, j
ci , j  (ci, k  1  wi , k  1)  (ck , j  wk , j )  pk

19
Dynamic Programming
Algorithm
To compute the cost of the minimum cost BST , store c i, j in a
table c[i,j] , and store wi, j in a table w[i , j].
for i:= 0 to n do
W[i,i]:= qi
C[i,i]:= 0
for l:=1 to n do
for i:=0 to n-l do
j:= I + l
w[i , j]:= w[I,j-1] = p j + q j
c[i ,j] := min i<k≤j (c[i,k-1] + c[k,j] + w[I,j])
Correctness : similar to earlier examples. 20
Analysis: O(n3)

You might also like