BinarySearchTr ADTtedtoSes : unordered collection of values Iobjects - - PowerPoint PPT Presentation

binarysearchtr adttedtoses
SMART_READER_LITE
LIVE PREVIEW

BinarySearchTr ADTtedtoSes : unordered collection of values Iobjects - - PowerPoint PPT Presentation

BinarySearchTr ADTtedtoSes : unordered collection of values Iobjects - Set insert H ) ; member lx ) ; remove 4) ; empty D; sized , dear D; ) ) ( find LX - We call the values we store keys , - We assume the keys are from some ordered sets ie , for


slide-1
SLIDE 1

BinarySearchTr

slide-2
SLIDE 2

ADTtedtoSes

  • Set
: unordered collection of valuesIobjects insert H) ; memberlx) ; remove 4) ; emptyD; sized, dearD; (find LX ))
  • We call the values we store keys
,
  • We assume the keys are from some
  • rdered sets
ie , for any two keys ix.yes , we have exactly
  • ne of
day , x=y , ya x
  • Want implementations where all operations
are as efficientHast as possible
  • Compare with
  • ur list implementations
where we could have some operations fast , but others slow Q : What will count as " fast " ?
slide-3
SLIDE 3

someRelatedlontainerADTSMottiset.li

he set, but with multiplicities (aka beg) Count(x) Map : unordered collection of Lkey, take pairs , associating at most one valve with each key . put Ikey,ral ) 4in place of insert × get Ckey) Arcturus value associated with key

Dictionary

: like map , but associates a collection
  • f
values with each key .
  • Implementations of these
are simple extortions to implementations of sets , which we focus
  • n .
slide-4
SLIDE 4

searchtrees.czoat.SI

, Multi set , Mgp , . . . ADTS with (at least) insert,

find , removes

, all

efficient

  • ff¥¥im
  • by
where n= # keys in the set like binary search

( Our first attempt will not

succeed at
  • giving 0110g n) operations
.)
slide-5
SLIDE 5

BinarySearohT

A

BST

is
  • a binary tree
  • with
nodes labelled by keys
  • for every two
nodes v.v : ' if u is in the left subtree of v then key (a) a key 4)

it

III.rig

btreeoe} BST
slide-6
SLIDE 6

Ex

=

←T

  • %
⑤r

'

slide-7
SLIDE 7

i

¥6

slide-8
SLIDE 8 W

Ey : Reasoning with the

µ

  • rder invariant .

/

↳ v3

1.1

.

\v2

vi.T
  • LI
key Cut) a key Cu)

key (re) > key Cu) key Crs)

? key Cu)

Kayla)

a key ( w ) a key Cvs) ⇒ key Cnn) a key Los)
slide-9
SLIDE 9

BSTmemberkdpseudo-codef-indlt.ir

) 4 return true if t appears

in { 4 subtree rooted at it . if t a key G) ! v has a left subtree return find Lt , left Iv)) if t > key Cr ) 4 v has a right subtree return find (t, right Cr )) if key 4) = t return true return false } member(t) return true ift t is in the tree . } return findIt , root)
slide-10
SLIDE 10

BSTfindltivlpseudo-code-alternater-er.in

find Lt , r) 4 return true if t appears in { 4 subtree rooted at it . if key 4) = t return true if t a key G) ! v has a left subtree return find Lt , left Iv)) if t > key ( r ) I v has a right subtree return find (t , right Cr )) return false }
  • Oi. Which version is better ?
Since keyG) =t will almost always be false , the first version should do fewer comparisons , so be faster .
slide-11
SLIDE 11

BSTinserttxlpseudo.co#

insert Had ds t to the tree Hassomes t is not in the tree . already a ← node at which find It,root) terminates if t a key (a) give u a new left child with key t else give m a new right child with key t . }
slide-12
SLIDE 12

BSTInsertExamp

insertthy insert

2

←③

7

%

slide-13
SLIDE 13

BST

insert4) Pseudo - code
  • alternate version

f

  • insert Lt , DX insert t in the subtree rooted at v , if it is not there
. { if t a key G) ! v has a left subtree insert Lt , left Iv)) if t > key Cr ) I v has a right subtree insert Ct, right GD if t a key G) 11 here v has no left child give v a new left child with key t if key 4) at it t > key 4) 4 here r has no right child return true give r a new right child with key t . } Kit we reach here , t= keylvl , so do nothing . insert It){ Dodds t to the tree ,if it is not already there. insert It , root )

}

slide-14
SLIDE 14

Ini¥BTiExamp

1) e. start with an empty . in tae given order insert

E ? ' Ii '

2) ' start with an empty BST
  • insert
1,213,516,78 ' in the
  • rder given
  • ②-③-⑤-⑥-⑨
slide-15
SLIDE 15

BST

remove It)
  • We
consider 3 cases , of increasing difficulty .
  • Catisatal
i) find the node r wi ii) delete 'th key G) = t

E

remove4),¥⑤

F

② * "

L
slide-16
SLIDE 16

BSTremoretttfki.tisatanodewithtchild.is

find the node V with key Cut = t let u be the child of r iii) replace v with the subtree rooted at a Exampte : remove(3) , then remove4)

→ ←

¥7

1-③

¥,

slide-17
SLIDE 17

E×amp BST removeIt)

where nodeft) has 1 child remove

T

  • o ⇒ of '⑧

'

  • eCT

*

slide-18
SLIDE 18

BST remove : Case 3 Preparation

  • In an ordered set
f =L . . . Si- I , Si , Siti , sit 2 . . . 7 Sin is the predecessor of si Sit , is the successor
  • f
Si
  • Let s
be the set of keys in the Best , and t be
  • ur target
K .
  • We will want to locate the predecessor/
successor of Tins.
slide-19
SLIDE 19

BST remove : Case 3 Preparation

  • 0bse An
in- order traversal of a BST visits nodes in the order of their keys .
  • Let 4=4 , vz .
. . . un) be the set of nodes in the BST in the order visited by an in - order traversal
  • For an
  • rdered set
X , and xtX , write succxtx) for the successor of × in X.
  • Then we have
a) if key (a) =k then key ( succubi)) = succslk) b) If nodeck) =u then node(sudsCKD = such
slide-20
SLIDE 20

BSTremoveicasezpreparation.IT

node v has a right child , it is easy to find its successor : 4.↳ ← succor) is the first

\

node visited by an
  • in - order traversal
  • f the right subtree
  • f v.

E%f

→ such

§

µ

Succor)

4.

v ⇒

F.

I

Do

T

  • such#↳
to skew)
slide-21
SLIDE 21

smallest key

in a B5T .

¥884

¥0

slide-22
SLIDE 22

BSTremoveilase3preparation.TO

find the successor of node v (assuming v has a right child) use: suck) { u⇐ right G) while ( left Cm ) exists) { } U * left Cns return a } The next node after r contains the next key after keyG)
  • i.e. the
smallest key that is larger than keyG) .
slide-23
SLIDE 23

Bs Trema#

Casey

: t is at a node with 2 children i) find the node v with key G) =t ii ) find the successor of V
  • call it
u . iii) key f) ← key G) iv) delete re : a) if u is a leaf , delete it b) if u is not a leaf , it has
  • ne child
w replace u with the subtree rooted at w

(as

in Case 2)
slide-24
SLIDE 24

BSTremovelhwhennodelldhas2chddreurem.ve

(5)7¥

  • µ⑤

,

€ T

  • ← ¥

µ? '

¥10

slide-25
SLIDE 25

BSeCk)whenhdren.

removed

,②T⑧D

"

A

④#

"I

⇒ A

,④,

A

A

  • 40 ✓

replace

⑤ ④

with

A

slide-26
SLIDE 26

Complexity

  • f B5T
Operations # keys
  • Measure
as a function of : height 1h)
  • r site Ln) :
  • All operations essentially
involve traversing a path from the root to a node v , where v in the worst case it is a leaf of maximum depth
  • so :
member :O (h) , 0 (n)

/ T

  • ÷:::÷:÷:
remove :O ( h) , old 'h= ? eg.tt) his small relative to n .
  • For
"tall skinny " trees leg Tbh is proportional to n .

⇐ %.IS?I3sBshIEe

' r ' 90
slide-27
SLIDE 27
  • A perfeutbinarytree of height
h is a binary tree
  • f height h with the
Max . number of nodes :
  • I
1ha

T

  • .
✓ I

r

an £5

DAH ha

X

I

×

slide-28
SLIDE 28
slide-29
SLIDE 29

Notice :

Because a perfect binary tree of height h is like this: 2h -I

÷:/

2h leaves 2" t 2h - I = 2. 2h
  • I
= 2 htt . I
slide-30
SLIDE 30 Claim : For every set S of n keys , there exists a BST for S with height at most It Logan Pro¥ Let h be the smallest integer st . 2h > n , and m=2h . So : 2h In > 2h
  • I
log, 2" slogan > log,2h" h =

logznyh-lha.lt

Logan
  • let T be the perfect binary tree of
height h= login . label the first n nodes of T ( as visited by an in - order traversal) with the keys of 5 , and delete the remaining nodes (to get T ' ) .
  • T
' is a B5T for S with height h = log. m < It login . So , there is always a B5T with height
  • log n
slide-31
SLIDE 31

0ptimalBSTTnsertionorder-G.ir

en a set
  • f
keys , we can insert them so as to get a minimum height BST : Consider : 0 ← What can we say about
  • / \
the key here?

( It

is the

← to

6 to

median ← to 6 to 610

68

key ) Observe : The first key inserted into a BST is at the root forever

( unless

we remove it from the BST)
slide-32
SLIDE 32

Optimal BST Insertion Order .

¥→

I

D

* AI1II

seethe

median key " principle
slide-33
SLIDE 33
  • So , there
is always a B5T with height
  • log n
  • Can
we maintain min . height with Ollogn) insert 4 remove ? . Consider : insert I
  • ← TO

6

66

⑤ T

' B is the only min height B5T for 1 . . 7.
  • A → B required
" moving every node "
  • To get 0( log
n)
  • perations , we need another
kind of search tree , other than plain BSTS .
slide-34
SLIDE 34

To get efficient

search trees , give up at least
  • ne of :
  • binary
  • min height

.to/logn)operatims-

slide-35
SLIDE 35

Ends

slide-36
SLIDE 36

Testt

  • Coverage
: lectures up to last friday t At. . Short answer
  • r exercises
Focus : data structures - properties %
  • perations
  • Note:
use versions from lectures . F⇒ . BST remove cqn use pied OR a .

D.

D

pied Sufc
  • Usetheouefromclas-webp.ge
  • Slides coming