0% found this document useful (0 votes)
5 views

2024-10-26 ADS with JAVA - Day 8 (2)

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

2024-10-26 ADS with JAVA - Day 8 (2)

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 36

of

light tree
notim a a

the longest path from


Number of edges on

that mode to a leaf mode.

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

(wort's left child),


It MAX
(height
height (wort's right child)
,

Offerwise
AVL Tree
-

Height balanced binary search tree .

Each mode has a balance factor between -1 and +

Balance factor (B .
F .

) =
An hr
h > height of left subtree
-

2
,2 >
-

height ofright subsee

Dr
Balance

Insertepts
o
as factor
=>
to

unti

L
①new Node difference · newhich
Ihrhr/
path

turrent)
track nodes in

sout fill new Node.


I

Y noches Stack
Left

Rotation

g ↑ - ·
⑩ new Node Rimbalance
I
.

g >
-
nearest parent of new Node ,
having
incorrect balance factor.

towards Node (we


a way
new
Take two steps from

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

rotations performed around p


.

Left and Right are

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

insert (5) g ↓L imbalance

are
-

#
Rotation
Y(
Right
nochstuck Xi
~ Ap
new Node
d
Y(
set

·re
,⑮
us
Current

Xi ⑤
~ Ap -

new Node Right


Rotation
rut(i is
↓L imbalance
g >
X rort
H/
-

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

For LR imbalance and LR imbalance ,


we perform
two rotations around p.
child of in path from g towards
p grand g ,

new Node .
LR imbalance >
= Left Rotation

Right Rotation

RL imbalance => 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

create the original tree back .

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
-

> current = roof


emple)
-

- While (current != null) 11 mode stack.

I
>
-
While (current := Mull

I
current Nodes Stuck
>
-

Push on

>
-
current = current · left child

from mode Stack


>
-

current-pop a noc

Proces current node .

current= current night child


Time
complexity =
0(2) modes stuck.
Used be
space complexity =
0
(log n) *
Time complexity = 0 (w) Face
complexity
space complexity =

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 .

- If (root node’s right child exists) then


- InOrder(root’s right child).
- Stop.
ofwords db ab ad
Pctionary abe
:

.
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
-

B-Tree o- order m each mode has


,

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
=

return fabe ; space


=
O(1)

3
constant
Search
Binary
-

>
-

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


2
=
(9) = 4

[mid] then
if element < an

mid element .

be found before
element can

I set high end of


range
to before mid)
0123456789

/10/12/15/20/30/35/40m
= 1

high
>
-

mid
>1
-

then
if an [mid] < element
mid element
element can be found after

(set low end a


range
to after mid)
0123456789

#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 -

> high low

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

if (value < an Emid]) Time


high=mid-1 complexity
ebe
low-mid
=

OClogu)
+ 1
space
return false . complexity = O(I)

You might also like