Binary Search Tree: Prof. Prateek Vishnoi
Binary Search Tree: Prof. Prateek Vishnoi
September 5, 2024
Some Lemmas
By mathematical induction
By mathematical induction
Base Case : For n = r, rCr = r+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
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
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
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
Lemma 2
r
Cr + r+1Cr + r+2Cr . . . nCr = n+1Cr+1
Jensen’s Inequality
Jensen’s Inequality
When we apply a convex function f (x) to a random variable X, then
Jensen’s inequality gives us
Jensen’s Inequality
When we apply a convex function f (x) to a random variable X, then
Jensen’s inequality gives us
Jensen’s Inequality
When we apply a convex function f (x) to a random variable X, then
Jensen’s inequality gives us
Definition
Definition
BST formed by inserting the keys in random order into an initially empty
tree.
Confusion
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?
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.
Proof
Proof
Xn : Denotes height of randomly built BST on n keys.
Proof
Xn : Denotes height of randomly built BST on n keys.
Yn = 2Xn , Y1 = 1, Y0 = 0
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.
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.
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}
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}
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
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
Proof
Proof
n
X
Yn = Zn,i (2.max(Yi−1 , Yn−i ))
i=1
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
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
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
Proof
n
X
E[Yn ] = E[Zn,i ] E[2.max(Yi−1 , Yn−i )]
i=1
Proof
n
X
E[Yn ] = E[Zn,i ] E[2.max(Yi−1 , Yn−i )]
i=1
Proof
n
X
E[Yn ] = E[Zn,i ] E[2.max(Yi−1 , Yn−i )]
i=1
Proof
n
X
E[Yn ] = E[Zn,i ] E[2.max(Yi−1 , Yn−i )]
i=1
1 = Y1 = E[Y1 ] ≤ 1/44C3
Proof
Proof
n−1
4X 1 n+3
E[Yn ] ≤ E[Yi ] ≤ C3
n 4
i=0
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
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,
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
Proof
1 n+3
E[Yn ] ≤ C4
n
Proof
1 n+3
E[Yn ] ≤ C4
n
1 (n + 3)!
E[Yn ] ≤
n 4!(n − 1)!
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!
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
1 n+3
E[Yn ] ≤ C3
4
1 n+3
E[Yn ] ≤ C3
4
2E[Xn ] ≤ E[2Xn ]
1 n+3
E[Yn ] ≤ C3
4
2E[Xn ] ≤ E[2Xn ]
1
2E[Xn ] ≤ n+3
C3
4
1 n+3
E[Yn ] ≤ C3
4
2E[Xn ] ≤ E[2Xn ]
1
2E[Xn ] ≤ n+3
C3
4
n3 + 6n2 + 11n + 6
2E[Xn ] ≤
24
1 n+3
E[Yn ] ≤ C3
4
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