2024-10-26 ADS with JAVA - Day 8 (2)
2024-10-26 ADS with JAVA - Day 8 (2)
light tree
notim a a
=
Wort
I
leage embe
↓ height unt
⑨ O
=
Cedge
= T +
0 ③
+
+ ① + 0
rot
=
4
edges
height = o
S
O ifsort is empty
.
,
leaf mode
O, if rot is
height (root) =
Offerwise
AVL Tree
-
Balance factor (B .
F .
) =
An hr
h > height of left subtree
-
2
,2 >
-
Dr
Balance
Insertepts
o
as factor
=>
to
①
unti
①
L
①new Node difference · newhich
Ihrhr/
path
turrent)
track nodes in
Y noches Stack
Left
↑
Rotation
g ↑ - ·
⑩ new Node Rimbalance
I
.
g >
-
nearest parent of new Node ,
having
incorrect balance factor.
not-doesn't matter) .
reach new Nod ,
we might ·
Record L ,
if followed left child .
Record R
,
if followed right child .
child mode
Right rotation p
is
For Left or ,
Node .
of g ,
in the path from g to new
Rotation
-eff
Ozg 8
Ot
0
at
+
T
xP
in
&
s
S
a It
·
Xero
D
~
terrent
↑ Left
8
>
-
I
MonewNode Rotation
l
(15)
Insert
s
at
current
modestuc
=>
·
⑮new Node
(20) port
Insert
E
-
2 nochs stack
- RR imbalance
& &
-
current
new Node II
,
⑳ Left Rotation
S
-
& &
>
-
current
new Node
⑳
Relation
↓ Left
·
(30)
-usert RR imbalance => Left Rotation
8 wi + rat
Erat
·
je
je
Left
u
u
>
-
25
Rotation g
ent
· + 2
current y o
new Node T
Node
sort
E
new
Sx
⑩
·
#
NodesStuck
·
Rotation
Right >
-
Mirror
image of
Left Rotation .
g g
M↑
↓
p
y
-
Rotation
5
Insert : 30 2015 10
(20not
(3bly
ineut
sout
ne ·
=>
de
·
BonewNode
-
-Insert
(I let
>
- nochsStuck
Right Rotation
imbalance>
currents p L
-
is
s
a
new Node
-=>⑮ -
modestuck
new Node
⑩
are
-
#
Rotation
Y(
Right
nochstuck Xi
~ Ap
new Node
d
Y(
set
·re
,⑮
us
Current
Xi ⑤
~ Ap -
artBus
-
> Right Rotation
Right
-
Rotations
nochsStuck I 8 -
< Noc
s
new
P
a ·
ument
I
k
· new Noch
insert (18)
G
E
Enterto 15 10 5
-
-
wirort
⑳
⑳
rort
was nodes Stack
i ·
visrecurrent
12
· P
O
⑱new Node
LR imbalance
new Node .
LR imbalance >
= Left Rotation
Right Rotation
Left Rotation
LR imbalance => Left Rotation
Right Rotation
Wort -
was
ro
vis Left
2
-I
⑳
Wort
170
P ⑮
>
-
Rotation 1P ·
-
·
ri
↓Right
Rotation
L
aut
i
-
F
25
Insert
H
Insert :
T5 10 15 20 wort
· · I E
Ap current
B
nodes a
· new Node
& L imbalance
H
Right Left
right
I
/Xp
Rotation
·
·
↑
⑩
·
art
Yest
wesout
Rotatin
·
·
·
Given a list of sorted elements ,
create a
balanced BST .
25 30]
[
15 20
-
right subtree
sort
⑮25
⑤ ⑮
so
⑳
-
Given two traversal of a
BINARY ,
TREE
15 8 10 15 20 in order
- --
left subfree most right subtree
Preorder
5L
S
~
15 20
not
left zijnubtree
subtree
-
rativeinordertar
a
#
nodes Stack
current - *
30
5*
will 3 well
well null
%: 1 581015 20
Flerative Inorder
-
I
>
-
While (current := Mull
I
current Nodes Stuck
>
-
Push on
>
-
current = current · left child
current-pop a noc
X a b
space te
O(logh)
a
=
taken
InOrder(root)
- if (root is empty) then by recursive
- Stop. call on
- If (root node’s left child exists) then
to store additional
- InOrder(root’s left child). system
- Process root node’s data. stack
.
data .
.
Co-length of a word
⑬
⑮ complet a
time
search
⑯
BST
Using
ab a be ad db
Trie
-sing
%v
·
ora iswor
I
rain
↓ %..
Z
.
, , , 09 //
iscope
a b
..
ab,& /
Z
iscope ..
/
timecomplete
search
m-way
Search Tree >
-
B-Tree family
-
at mostm children .
#
⑮
4
A Bt-Tree with m = .
sort
↓,
#11]
j
Hdl] H11]
11 12
)]
Fast F
IHFts1)J
Searching >
-
In linear data
structures.
↓ >
linear search Binary Search
possity
My ↓
find element by find elements
checking every in ordered set
value , one by one .
of data
.
find elements
in unordered
set of data .
by a
Linearsearch :Wecheck
element
each
one
-
boolean
unit value)
i length ; ++ i)
for (int i = 0
;
< an
if (aurtil ==
value)3 linear
return true
; ↓
3
Time
0 (n)
3
=
>
-
Require
data to be
sorted/arranged in a well
defined order
0123456789
#8/10/12/15/20130/35/40)n = 10
-
↑
low > 0 mid Search (8)
binary
-
high
-
>
9
[mid] then
if element < an
mid element .
be found before
element can
/10/12/15/20/30/35/40m
= 1
high
>
-
mid
>1
-
then
if an [mid] < element
mid element
element can be found after
#8/10/12/15/20130/35/40)n = 10
low >
-
2 #
mid
Value
high
->
> 3 an [mid] =
mid-2 #,
FOUND
narysearchists
mid
#110/11/20100135
n = 10
mich
low >
-
05 6 mid
high -> &5 -
mid >
-
4 756 W
NOT FOUND
Search (aw value
nar y ,
low = 0
high = au .
length -
1 low + short
While (low <= high) -
high)/2
I
>
-
mid = (low +
value)
if (aw [mid]
=
>
-
return true
OClogu)
+ 1
space
return false . complexity = O(I)