BinarySearchTr
BinarySearchTr ADTtedtoSes : unordered collection of values Iobjects - - PowerPoint PPT Presentation
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
ADTtedtoSes
- Set
- We call the values we store keys
- We assume the keys are from some
- rdered sets
- ne of
- Want implementations where all operations
- Compare with
- ur list implementations
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 keyDictionary
: like map , but associates a collection- f
- Implementations of these
- n .
searchtrees.czoat.SI
, Multi set , Mgp , . . . ADTS with (at least) insert,find , removes
, allefficient
- ff¥¥im
- by
( Our first attempt will not
succeed at- giving 0110g n) operations
BinarySearohT
ABST
is- a binary tree
- with
- for every two
it
III.rig
btreeoe} BSTEx
= ⑤- ⑤
←T
- %
'
③
⇐
i
¥6
④
Ey : Reasoning with the
µ
- rder invariant .
/
↳ v3
1.1
.\v2
vi.T- LI
key (re) > key Cu) key Crs)
? key Cu)Kayla)
a key ( w ) a key Cvs) ⇒ key Cnn) a key Los)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)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 ?
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 . }BSTInsertExamp
insertthy insert2
←③
7
%
BST
insert4) Pseudo - code- alternate version
f
- insert Lt , DX insert t in the subtree rooted at v , if it is not there
}
Ini¥BTiExamp
1) e. start with an empty . in tae given order insertE ? ' Ii '
←
2) ' start with an empty BST- insert
- rder given
- ②-③-⑤-⑥-⑨
- ⑧
BST
remove It)- We
- Catisatal
E
remove4),¥⑤⑤
F
② * "
⑨
LBSTremoretttfki.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-③¥,
⑤
⑧
E×amp BST removeIt)
where nodeft) has 1 child remove③
T
- o ⇒ of '⑧
'
⑥- eCT
*
BST remove : Case 3 Preparation
- In an ordered set
- f
- Let s
- ur target
- We will want to locate the predecessor/
BST remove : Case 3 Preparation
- 0bse An
- Let 4=4 , vz .
- For an
- rdered set
- Then we have
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#↳
smallest key
in a B5T .→
¥884
¥0
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
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
- ne child
(as
in Case 2)BSTremovelhwhennodelldhas2chddreurem.ve
(5)7¥
- µ⑤
⑤
,€ T
- ← ¥
µ? '
①
¥10
BSeCk)whenhdren.
removed,②T⑧D
"
A
④#
"I⇒ A
,④,
A
A
- 40 ✓
replace
⑤ ④
with ⑨A
Complexity
- f B5T
- Measure
- r site Ln) :
- All operations essentially
- so :
/ T
- ÷:::÷:÷:
- For
⇐ %.IS?I3sBshIEe
' r ' 90- A perfeutbinarytree of height
- f height h with the
- I
T
- .
r
an £5
✓DAH ha
X
I
↳
→
×
Notice :
Because a perfect binary tree of height h is like this: 2h -I÷:/
2h leaves 2" t 2h - I = 2. 2h- I
- I
logznyh-lha.lt
Logan- let T be the perfect binary tree of
- T
- log n
0ptimalBSTTnsertionorder-G.ir
en a set- f
- / \
( It
is the← to
6 to
median ← to 6 to 61068
key ) Observe : The first key inserted into a BST is at the root forever( unless
we remove it from the BST) ⇒Optimal BST Insertion Order .
¥→
I
D
* AI1IIseethe
median key " principle- So , there
- log n
- Can
- ← TO
←
6
66
⑤ T
- ⑦
④
' B is the only min height B5T for 1 . . 7.- A → B required
- To get 0( log
- perations , we need another
To get efficient
search trees , give up at least- ne of :
- binary
- min height
.to/logn)operatims-
Ends
Testt
- Coverage
- r exercises
- perations
- Note:
D.
D
pied Sufc- Usetheouefromclas-webp.ge
- Slides coming