binary search - cs.hunter.cuny.edusaad/courses/alg/algorithms_lecture12.pdf · binary...
TRANSCRIPT
Binary Search Tree-Dynamic set operations such as insert , delete , searchand more can be done in Ocb) time , where he
is height of tree .
root ft]-O q each node n has :
f lo key GIt a he
left fee]an N"f} ! rightEDwithin wk 'mp fee]
fo[rootCT]] = NIL
BinarySeavkTeepopatgyy E left subtree of a ⇒ key [y] s key fee]
y e right tab tree of a ⇒ key GI Z key
Examineyo\ (It's ok to have duplicates too)
④
⑦'⑤ to
Searching and Inserting-
Tree - Search (n, k)
if u= NIL or k= Key [k]
then return se
if K L key Costhen return Tree- Search ( leftfed , K)else return Tree -Search ( rightCod, K)
Initial call is for Thee -Search ( rootETI , K)
Tree-InsertCT, x)
- similar code
-Insert se in place of Nih (where it should have been)- use trailing pointer to keep track where you came from(see book)
Sorting with BST-
First consider the iuorder tree walk,
In order - Tree - Walk (a) ① §if n# NILthen in order -tree
-
walk ( leftCad) visit left [ se]print key [a] In order { process seinorder -Tree-walk (righted) visit right fr]
In order -Tree -Walk (rootkit) will print keys in order
⑦ 4
2€ 5 f
'af 103¥,
BST- sort (A, n)for i ← I to n
do a← node such that key fee] = A [i]Tree -Insert ( T, se)
Inorder - Tree - Walk ( rootCT))
what can we say about this algorithm ?-O Cny [ mainly due to inserts when tree is bad
,h -- Oln ))
-
I Cnlogn) [It's comparison based]
- It's Ocnkgn) on average , similar to Amicksort .
'
: BST- sort and a stable partition Quicksort withfirstelement as pirot , make exactly the same comparisons( but in different order)
A- = [ I , 2,3 , 4, . . . , n ]
①'
Q③'04
.
#'
e.
÷
Example :- Chicksort comparisons :3 I 8 2 6 7 5-
/ \- Everyone with 3
I 2 8 6 7 5
\ / - 2 to 1
2 6 7 5
/ \- {617,53 to 8
5 7- { 7,53 to 6
BST Comparisons : 113 ; 8,3 ; 2,3 ; 2,1 ; 6,3 ; 6,8
7,3 ; 7,8 ; 7,6 ; 5,3 ; 5,8 ; 5,6
So BST- sort has an Ocnlogn) time on random input .
So what did we conclude ?
• A randomly built BST has expected sum of depths
equal to Ocnlogn) , so expected average depth is 0 Closer)-
•This does not mean expected height is 0Clog n),height is the maximum depth .
-
Example :O g
- Glo n = 2h" - I + l hneoclogn)do Ao h
Hobb Don t Height- htt
µ sum of depth a O @n + e ')b- o (n) 6
(of average depth = Och + E)6 Choose l# rn ⇒ average depth is A (b) = AClog n)
Height ai a Crn)
•It would be great if expected height of randomly builtBST is 0 Clog n) , then all operations on tree run in 0Clog n) .
•In fact, it is !
Let Xu = height of random BST 10Xn = It Max 4h , Xn- K)
To make this look like recurrence we have seen before,let Yn = 2X" , then
yn = 21+ Max (Xk- t ) kn - K)= 2
.2.max (xx-I, Xn - K)
= 2. max (2 "" , 2X"- t) = 2 max ( Yai , Yn- K)
Eun] =L SEE Imax (Ye. . , Yn - r. )) (left subtree equally"
⇐t* f- Max LE fluid , Eun-KI) likely to have
£ I 7¥ EL Ya, + Yn -k) Qb - --in - I nodes)
E[Yn] f In E [ Ya ] Guess EGG] Sdn 3
Eun ] s 4dg K's 4¥ !a'da = tend 7,11! = tan .
Now E [ Yn ] f dnf
By Jensen 's Inequality E ( Xn] -- EC log Yn ) s log Eff]
Efxn] f log dn3 = log d + 3 log n -
- O(log n)
Jensen 's Inequality (Not hard to show)
If ffa) is convex :.
f-(ELM) s Effy,] µ¥µGwexf
inylog is concave ⇒ anti-by
log ELXI z Ellogx] f- (art#g) Gaffe) tf-a) fly)
So BST operations run in 0 (log n) on a randomly builtBST
.See book for
Tree - Insert
Tree - Delete
Tree - Successor
Thee - Predecessor
Tree - Minimum
Tree- Maximum