binary search - cs.hunter.cuny.edusaad/courses/alg/algorithms_lecture12.pdf · binary...

12
Binary Search Tree - Dynamic set operations such as insert , delete , search and more can be done in Ocb ) time , where he is height of tree . root ft ] - O q each node n has : f lo key GI t a he left fee ] an N " f } ! right ED within wk 'm p fee ] fo [ root CT ] ] = NIL

Upload: others

Post on 18-Oct-2020

3 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Binary Search - cs.hunter.cuny.edusaad/courses/alg/Algorithms_Lecture12.pdf · Binary SearchTree-Dynamic set operations such as insert, delete, search and more can be done in Ocb)

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

Page 2: Binary Search - cs.hunter.cuny.edusaad/courses/alg/Algorithms_Lecture12.pdf · Binary SearchTree-Dynamic set operations such as insert, delete, search and more can be done in Ocb)

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

Page 3: Binary Search - cs.hunter.cuny.edusaad/courses/alg/Algorithms_Lecture12.pdf · Binary SearchTree-Dynamic set operations such as insert, delete, search and more can be done in Ocb)

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)

Page 4: Binary Search - cs.hunter.cuny.edusaad/courses/alg/Algorithms_Lecture12.pdf · Binary SearchTree-Dynamic set operations such as insert, delete, search and more can be done in Ocb)

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¥,

Page 5: Binary Search - cs.hunter.cuny.edusaad/courses/alg/Algorithms_Lecture12.pdf · Binary SearchTree-Dynamic set operations such as insert, delete, search and more can be done in Ocb)

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)

Page 6: Binary Search - cs.hunter.cuny.edusaad/courses/alg/Algorithms_Lecture12.pdf · Binary SearchTree-Dynamic set operations such as insert, delete, search and more can be done in Ocb)

A- = [ I , 2,3 , 4, . . . , n ]

①'

Q③'04

.

#'

e.

÷

Page 7: Binary Search - cs.hunter.cuny.edusaad/courses/alg/Algorithms_Lecture12.pdf · Binary SearchTree-Dynamic set operations such as insert, delete, search and more can be done in Ocb)

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 .

Page 8: Binary Search - cs.hunter.cuny.edusaad/courses/alg/Algorithms_Lecture12.pdf · Binary SearchTree-Dynamic set operations such as insert, delete, search and more can be done in Ocb)

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)

Page 9: Binary Search - cs.hunter.cuny.edusaad/courses/alg/Algorithms_Lecture12.pdf · Binary SearchTree-Dynamic set operations such as insert, delete, search and more can be done in Ocb)

•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)

Page 10: Binary Search - cs.hunter.cuny.edusaad/courses/alg/Algorithms_Lecture12.pdf · Binary SearchTree-Dynamic set operations such as insert, delete, search and more can be done in Ocb)

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)

Page 11: Binary Search - cs.hunter.cuny.edusaad/courses/alg/Algorithms_Lecture12.pdf · Binary SearchTree-Dynamic set operations such as insert, delete, search and more can be done in Ocb)

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)

Page 12: Binary Search - cs.hunter.cuny.edusaad/courses/alg/Algorithms_Lecture12.pdf · Binary SearchTree-Dynamic set operations such as insert, delete, search and more can be done in Ocb)

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