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

Binary Search Tree: Prof. Prateek Vishnoi

Uploaded by

Sake Anila
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)
19 views52 pages

Binary Search Tree: Prof. Prateek Vishnoi

Uploaded by

Sake Anila
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/ 52

Binary Search Tree

Prof. Prateek Vishnoi

September 5, 2024
Some Lemmas

Prof. Prateek Vishnoi Binary Search Tree September 5, 2024 2 / 12


Some Lemmas
Lemma 1
r
Cr + r+1Cr + r+2Cr . . . nCr = n+1Cr+1

By mathematical induction

Prof. Prateek Vishnoi Binary Search Tree September 5, 2024 2 / 12


Some Lemmas
Lemma 1
r
Cr + r+1Cr + r+2Cr . . . nCr = n+1Cr+1

By mathematical induction
Base Case : For n = r, rCr = r+1Cr+1

Prof. Prateek Vishnoi Binary Search Tree September 5, 2024 2 / 12


Some Lemmas
Lemma 1
r
Cr + r+1Cr + r+2Cr . . . nCr = n+1Cr+1

By mathematical induction
Base Case : For n = r, rCr = r+1Cr+1
Inductive Step : Assume that, for k,
r
Cr + r+1Cr + r+2Cr . . . kCr = k+1Cr+1

Prof. Prateek Vishnoi Binary Search Tree September 5, 2024 2 / 12


Some Lemmas
Lemma 1
r
Cr + r+1Cr + r+2Cr . . . nCr = n+1Cr+1

By mathematical induction
Base Case : For n = r, rCr = r+1Cr+1
Inductive Step : Assume that, for k,
r
Cr + r+1Cr + r+2Cr . . . kCr = k+1Cr+1

We need to prove that,


r
Cr + r+1Cr + r+2Cr . . . kCr + k+1Cr = k+2Cr+1

Prof. Prateek Vishnoi Binary Search Tree September 5, 2024 2 / 12


Some Lemmas
Lemma 1
r
Cr + r+1Cr + r+2Cr . . . nCr = n+1Cr+1

By mathematical induction
Base Case : For n = r, rCr = r+1Cr+1
Inductive Step : Assume that, for k,
r
Cr + r+1Cr + r+2Cr . . . kCr = k+1Cr+1

We need to prove that,


r
Cr + r+1Cr + r+2Cr . . . kCr + k+1Cr = k+2Cr+1

k+1
Cr+1 + k+1Cr = k+2Cr+1 (From Pascals identity)
Prof. Prateek Vishnoi Binary Search Tree September 5, 2024 2 / 12
Some Lemmas

Lemma 2
r
Cr + r+1Cr + r+2Cr . . . nCr = n+1Cr+1

Prof. Prateek Vishnoi Binary Search Tree September 5, 2024 3 / 12


Some Lemmas

Lemma 2
r
Cr + r+1Cr + r+2Cr . . . nCr = n+1Cr+1

We now replace the r = 3 and n = n + 2, we get


n−1
X
i+3
C3 = n+3C4
i=0

Prof. Prateek Vishnoi Binary Search Tree September 5, 2024 3 / 12


Lemma 2

Jensen’s Inequality

Prof. Prateek Vishnoi Binary Search Tree September 5, 2024 4 / 12


Lemma 2

Jensen’s Inequality
When we apply a convex function f (x) to a random variable X, then
Jensen’s inequality gives us

E[f (X)] ≥ f (E[X])

Prof. Prateek Vishnoi Binary Search Tree September 5, 2024 4 / 12


Lemma 2

Jensen’s Inequality
When we apply a convex function f (x) to a random variable X, then
Jensen’s inequality gives us

E[f (X)] ≥ f (E[X])

Convex Function: A function f is convex if the line segment between


any two points on the graph of the function lies above the graph itself.

Prof. Prateek Vishnoi Binary Search Tree September 5, 2024 4 / 12


Lemma 2

Jensen’s Inequality
When we apply a convex function f (x) to a random variable X, then
Jensen’s inequality gives us

E[f (X)] ≥ f (E[X])

Convex Function: A function f is convex if the line segment between


any two points on the graph of the function lies above the graph itself.
f (x) = 2x which implies that E[2[ X]] ≥ 2E[X] where X is any
random variable.

Prof. Prateek Vishnoi Binary Search Tree September 5, 2024 4 / 12


Randomly built Binary Search Tree(BST)

Definition

Prof. Prateek Vishnoi Binary Search Tree September 5, 2024 5 / 12


Randomly built Binary Search Tree(BST)

Definition
BST formed by inserting the keys in random order into an initially empty
tree.

Confusion

Prof. Prateek Vishnoi Binary Search Tree September 5, 2024 5 / 12


Randomly built Binary Search Tree(BST)

Definition
BST formed by inserting the keys in random order into an initially empty
tree.

Confusion
Does every permutation of a set of numbers represent a different BST?
In other words, is there a 1-1 correspondence in BST and a permutation?

Prof. Prateek Vishnoi Binary Search Tree September 5, 2024 5 / 12


Randomly built Binary Search Tree(BST)

Definition
BST formed by inserting the keys in random order into an initially empty
tree.

Confusion
Does every permutation of a set of numbers represent a different BST?
In other words, is there a 1-1 correspondence in BST and a permutation?

NO!
Assume we have a set of permutations on set {1, 2, 3}
[2,1,3] and [2,3,1] represent the same BST.

Prof. Prateek Vishnoi Binary Search Tree September 5, 2024 5 / 12


Theorem
Expected height of randomly built BST on n-distinct keys is O(log n)

Proof

Prof. Prateek Vishnoi Binary Search Tree September 5, 2024 6 / 12


Theorem
Expected height of randomly built BST on n-distinct keys is O(log n)

Proof
Xn : Denotes height of randomly built BST on n keys.

Prof. Prateek Vishnoi Binary Search Tree September 5, 2024 6 / 12


Theorem
Expected height of randomly built BST on n-distinct keys is O(log n)

Proof
Xn : Denotes height of randomly built BST on n keys.
Yn = 2Xn , Y1 = 1, Y0 = 0

Prof. Prateek Vishnoi Binary Search Tree September 5, 2024 6 / 12


Theorem
Expected height of randomly built BST on n-distinct keys is O(log n)

Proof
Xn : Denotes height of randomly built BST on n keys.
Yn = 2Xn , Y1 = 1, Y0 = 0
Rn : Denotes the rank(position that this key would occupy if the set
of keys are sorted) of the key that is used as root node.

Prof. Prateek Vishnoi Binary Search Tree September 5, 2024 6 / 12


Theorem
Expected height of randomly built BST on n-distinct keys is O(log n)

Proof
Xn : Denotes height of randomly built BST on n keys.
Yn = 2Xn , Y1 = 1, Y0 = 0
Rn : Denotes the rank(position that this key would occupy if the set
of keys are sorted) of the key that is used as root node.
If Rn = i, then left subtree is randomly built on {i − 1} keys and
right subtree is randomly built on {n − i} keys.

Prof. Prateek Vishnoi Binary Search Tree September 5, 2024 6 / 12


Theorem
Expected height of randomly built BST on n-distinct keys is O(log n)

Proof
Xn : Denotes height of randomly built BST on n keys.
Yn = 2Xn , Y1 = 1, Y0 = 0
Rn : Denotes the rank(position that this key would occupy if the set
of keys are sorted) of the key that is used as root node.
If Rn = i, then left subtree is randomly built on {i − 1} keys and
right subtree is randomly built on {n − i} keys.
Zn,i = I{Rn = i}

Prof. Prateek Vishnoi Binary Search Tree September 5, 2024 6 / 12


Theorem
Expected height of randomly built BST on n-distinct keys is O(log n)

Proof
Xn : Denotes height of randomly built BST on n keys.
Yn = 2Xn , Y1 = 1, Y0 = 0
Rn : Denotes the rank(position that this key would occupy if the set
of keys are sorted) of the key that is used as root node.
If Rn = i, then left subtree is randomly built on {i − 1} keys and
right subtree is randomly built on {n − i} keys.
Zn,i = I{Rn = i}
Rn is equally likely to be any element from {1, 2, 3 . . . n}

Prof. Prateek Vishnoi Binary Search Tree September 5, 2024 6 / 12


Theorem
Expected height of randomly built BST on n-distinct keys is O(log n)

Proof
Xn : Denotes height of randomly built BST on n keys.
Yn = 2Xn , Y1 = 1, Y0 = 0
Rn : Denotes the rank(position that this key would occupy if the set
of keys are sorted) of the key that is used as root node.
If Rn = i, then left subtree is randomly built on {i − 1} keys and
right subtree is randomly built on {n − i} keys.
Zn,i = I{Rn = i}
Rn is equally likely to be any element from {1, 2, 3 . . . n}
P {Rn = i} = 1/n for i = 1, 2, . . . n

Prof. Prateek Vishnoi Binary Search Tree September 5, 2024 6 / 12


Theorem
Expected height of randomly built BST on n-distinct keys is O(log n)

Proof
Xn : Denotes height of randomly built BST on n keys.
Yn = 2Xn , Y1 = 1, Y0 = 0
Rn : Denotes the rank(position that this key would occupy if the set
of keys are sorted) of the key that is used as root node.
If Rn = i, then left subtree is randomly built on {i − 1} keys and
right subtree is randomly built on {n − i} keys.
Zn,i = I{Rn = i}
Rn is equally likely to be any element from {1, 2, 3 . . . n}
P {Rn = i} = 1/n for i = 1, 2, . . . n
E[Zn,i ] = 1/n

Prof. Prateek Vishnoi Binary Search Tree September 5, 2024 6 / 12


Continued...

Proof

Prof. Prateek Vishnoi Binary Search Tree September 5, 2024 7 / 12


Continued...

Proof
n
X
Yn = Zn,i (2.max(Yi−1 , Yn−i ))
i=1

Prof. Prateek Vishnoi Binary Search Tree September 5, 2024 7 / 12


Continued...

Proof
n
X
Yn = Zn,i (2.max(Yi−1 , Yn−i ))
i=1
n
" #
X
E[Yn ] = E Zn,i (2.max(Yi−1 , Yn−i ))
i=1

Prof. Prateek Vishnoi Binary Search Tree September 5, 2024 7 / 12


Continued...

Proof
n
X
Yn = Zn,i (2.max(Yi−1 , Yn−i ))
i=1
n
" #
X
E[Yn ] = E Zn,i (2.max(Yi−1 , Yn−i ))
i=1
n
X
E[Yn ] = E[Zn,i (2.max(Yi−1 , Yn−i ))] (by linearity of expectation)
i=1

Prof. Prateek Vishnoi Binary Search Tree September 5, 2024 7 / 12


Continued...

Proof
n
X
Yn = Zn,i (2.max(Yi−1 , Yn−i ))
i=1
n
" #
X
E[Yn ] = E Zn,i (2.max(Yi−1 , Yn−i ))
i=1
n
X
E[Yn ] = E[Zn,i (2.max(Yi−1 , Yn−i ))] (by linearity of expectation)
i=1
n
X
E[Yn ] = E[Zn,i ] E[2.max(Yi−1 , Yn−i )] by independence
i=1

Prof. Prateek Vishnoi Binary Search Tree September 5, 2024 7 / 12


Continued...

Proof
n
X
E[Yn ] = E[Zn,i ] E[2.max(Yi−1 , Yn−i )]
i=1

Prof. Prateek Vishnoi Binary Search Tree September 5, 2024 8 / 12


Continued...

Proof
n
X
E[Yn ] = E[Zn,i ] E[2.max(Yi−1 , Yn−i )]
i=1

Replace the value of E[Zn,i ] = 1/n


n
2X
E[Yn ] = E[max(Yi−1 , Yn−i )]
n
i=1

Prof. Prateek Vishnoi Binary Search Tree September 5, 2024 8 / 12


Continued...

Proof
n
X
E[Yn ] = E[Zn,i ] E[2.max(Yi−1 , Yn−i )]
i=1

Replace the value of E[Zn,i ] = 1/n


n
2X
E[Yn ] = E[max(Yi−1 , Yn−i )]
n
i=1
n
2X
E[Yn ] ≤ E[Yi−1 ] + E[Yn−i ]
n
i=1

Prof. Prateek Vishnoi Binary Search Tree September 5, 2024 8 / 12


Continued...

Proof
n
X
E[Yn ] = E[Zn,i ] E[2.max(Yi−1 , Yn−i )]
i=1

Replace the value of E[Zn,i ] = 1/n


n
2X
E[Yn ] = E[max(Yi−1 , Yn−i )]
n
i=1
n
2X
E[Yn ] ≤ E[Yi−1 ] + E[Yn−i ]
n
i=1
n−1
4X
E[Yn ] ≤ E[Yi ]
n
i=0

Prof. Prateek Vishnoi Binary Search Tree September 5, 2024 8 / 12


n−1
4X 1 n+3
E[Yn ] ≤ E[Yi ] ≤ C3
n i=0 4

Prof. Prateek Vishnoi Binary Search Tree September 5, 2024 9 / 12


n−1
4X 1 n+3
E[Yn ] ≤ E[Yi ] ≤ C3
n i=0 4

Claim by strong induction


1 n+3
E[Yn ] ≤ C3
4

Prof. Prateek Vishnoi Binary Search Tree September 5, 2024 9 / 12


n−1
4X 1 n+3
E[Yn ] ≤ E[Yi ] ≤ C3
n i=0 4

Claim by strong induction


1 n+3
E[Yn ] ≤ C3
4

Base Case : 0 = Y0 = E[Y0 ] ≤ 1/4 3C3

1 = Y1 = E[Y1 ] ≤ 1/44C3

Prof. Prateek Vishnoi Binary Search Tree September 5, 2024 9 / 12


Continued...

Proof

Prof. Prateek Vishnoi Binary Search Tree September 5, 2024 10 / 12


Continued...

Proof
n−1
4X 1 n+3
E[Yn ] ≤ E[Yi ] ≤ C3
n 4
i=0

Prof. Prateek Vishnoi Binary Search Tree September 5, 2024 10 / 12


Continued...

Proof
n−1
4X 1 n+3
E[Yn ] ≤ E[Yi ] ≤ C3
n 4
i=0

By inductive hypothesis,
n−1
4X1 i+3
E[Yn ] ≤ C3
n 4
i=0

Prof. Prateek Vishnoi Binary Search Tree September 5, 2024 10 / 12


Continued...

Proof
n−1
4X 1 n+3
E[Yn ] ≤ E[Yi ] ≤ C3
n 4
i=0

By inductive hypothesis,
n−1
4X1 i+3
E[Yn ] ≤ C3
n 4
i=0

n−1
1 X i+3
E[Yn ] ≤ C3
n
i=0

From lemma 1,

Prof. Prateek Vishnoi Binary Search Tree September 5, 2024 10 / 12


Continued...

Proof
n−1
4X 1 n+3
E[Yn ] ≤ E[Yi ] ≤ C3
n 4
i=0

By inductive hypothesis,
n−1
4X1 i+3
E[Yn ] ≤ C3
n 4
i=0

n−1
1 X i+3
E[Yn ] ≤ C3
n
i=0

From lemma 1,
1 n+3
E[Yn ] ≤ C4
n

Prof. Prateek Vishnoi Binary Search Tree September 5, 2024 10 / 12


Continued...

Proof
1 n+3
E[Yn ] ≤ C4
n

Prof. Prateek Vishnoi Binary Search Tree September 5, 2024 11 / 12


Continued...

Proof
1 n+3
E[Yn ] ≤ C4
n
1 (n + 3)!
E[Yn ] ≤
n 4!(n − 1)!

Prof. Prateek Vishnoi Binary Search Tree September 5, 2024 11 / 12


Continued...

Proof
1 n+3
E[Yn ] ≤ C4
n
1 (n + 3)!
E[Yn ] ≤
n 4!(n − 1)!
1 (n + 3)!
E[Yn ] ≤
4 3!n!

Prof. Prateek Vishnoi Binary Search Tree September 5, 2024 11 / 12


Continued...

Proof
1 n+3
E[Yn ] ≤ C4
n
1 (n + 3)!
E[Yn ] ≤
n 4!(n − 1)!
1 (n + 3)!
E[Yn ] ≤
4 3!n!
1 n+3
E[Yn ] ≤ C3
4

Prof. Prateek Vishnoi Binary Search Tree September 5, 2024 11 / 12


Proof

1 n+3
E[Yn ] ≤ C3
4

Prof. Prateek Vishnoi Binary Search Tree September 5, 2024 12 / 12


Proof

1 n+3
E[Yn ] ≤ C3
4

But our goal is to bound Xn , Since 2x is a convex function, we can


say from Jensens inequality that,

2E[Xn ] ≤ E[2Xn ]

Prof. Prateek Vishnoi Binary Search Tree September 5, 2024 12 / 12


Proof

1 n+3
E[Yn ] ≤ C3
4

But our goal is to bound Xn , Since 2x is a convex function, we can


say from Jensens inequality that,

2E[Xn ] ≤ E[2Xn ]

1
2E[Xn ] ≤ n+3
C3
4

Prof. Prateek Vishnoi Binary Search Tree September 5, 2024 12 / 12


Proof

1 n+3
E[Yn ] ≤ C3
4

But our goal is to bound Xn , Since 2x is a convex function, we can


say from Jensens inequality that,

2E[Xn ] ≤ E[2Xn ]

1
2E[Xn ] ≤ n+3
C3
4

n3 + 6n2 + 11n + 6
2E[Xn ] ≤
24

Prof. Prateek Vishnoi Binary Search Tree September 5, 2024 12 / 12


Proof

1 n+3
E[Yn ] ≤ C3
4

But our goal is to bound Xn , Since 2x is a convex function, we can


say from Jensens inequality that,

2E[Xn ] ≤ E[2Xn ]

1
2E[Xn ] ≤ n+3
C3
4

n3 + 6n2 + 11n + 6
2E[Xn ] ≤
24
By taking logarithm,
E[Xn ] = O(log n)
Prof. Prateek Vishnoi Binary Search Tree September 5, 2024 12 / 12

You might also like