You are on page 1of 17

Binary Search Tree

Distinct

Nodes
EIt.at
a
B STX
Y
H Y
est
Gx
T
3
1 76 8 118
µ
Ex 15
I BSTX
I
g 13
12
1
of
516
7

hordes of BST left node right

1
G 7,811118
3 11 12,3
I 76 8118
z

He Inorder traversal of BST sorted

Given a BST int K Search K is present ios no

Distinct numbers
groot
12 9 4
ans true
0 110
Y 3
y is

6 Ig
12 9 groot

I
Y 3
I is

g
12 5 groot
ans False

0 Je
T 3
I is

s
a
int K
bool search node not

Nole
while C tmp
If Ctmp data K
Emp Emp Isbt
else if Emp data K
Emp EMP test
else
setum the
setum False

IgworstO CN
Tc Oct
Sc Oct

the False based


If K is there in the tree

y
If root NULL Setum False

If C not data K setum True


else if not data k setum Search root right K

else setum search root lest K

TC OCH
SC OCH
recursion stack
Q Given a BST Insert k in correct position

so that it is still BST


alreadythere

12 5 root

if fit

7
Insert K and
return

Node Insert node soot int K


If not NULL
Base condition
Tetum new age a

If soot data LK
f
root right Insert root right K

else If root data K


Not left Insert soot hest K

setum soot
TC OCH
Sc O CH
recursion stack

4
TI
ie s

dy
K
d
B g g

Q Ana CJ Construct BST

I one by one add each element

4,5 3 2,7 4.5 62

3 s

21 45 17 TC OCN
BST Still BST
Delete give number from

1273 6
1.5 10

181
Delete 0
2
Ceca
6
I 8510
17.5

Delete

181
12
one child
3
777.585110
4

Delete
I I
62 children 6
Ig
I 8.310
which you are trying to delete

Nolde

aia
b bz bz by

in sight
smallest child

Node delete node soot int K

If root NOLL SAM NULL

It soot data K
soot right dude root right R
else if not data K
soot test delete not lest K
setum Noll

else If root.fyt NULL 11 soot2Gt NOL

f
If soot test NULL
setum soot right
else
gym soot list

Node Emp not test


i
while Emp right NULL

type Emp right

1
data t data
not deft date root test tmpdata

schism soot

TC O CH
Sc Oc m T recursion stack

A GbsC LST RST II

1
balahced
Q Given a sorted array Construct a BST

I I 111213
23W

2,3
3 x 3 x

arm 1,2 9,506 7,8


1,2 a

15 BBST
BBST
q
construct a B BST from an Ct 2
return soot
y int d int 2 not
Node BBST Carr CJ

Iffy 2 oetym null


f J

t
node soot new mode arr Cmid

soot test BBST anic I mid D


wot aight BB ST Cara CJ mid 11,2
TC OC N OCN
Sc OCH Ocean
Node maine 7
Recursion

setum BBST Carr O arreen D

I
mid 5
4 5

I
Can S 47 arr 6,67

2
IN
N z
N 2 27
t

rely
holy Nf nosy 22

1 11 11 23
I
aaaaa
1 1 1 1 1 I l
I 1 1 1 11
E loan
21081 N
Q Given a Tree Find If it is BST or not

10 se

5 is

18,11
7 It sorted
1 In order
If not sorted X

TC O CN
Sc Ocn th D O CH
r tstack Icursion
incl

nodes LST I node date RST

H
A nodes smallest in RST
largestLsts node data
BST X

5 is

Ii't
M e
soot
given

I
of
bool globalans

I o o

go.s.cn into 1 BST not lest


into 2 BST not sight

iii

f globalans

into ans
False

ans.mn min l.mn t.mn root data


ans my Max C t.mx 2 mx not data
scrim ans

bool main C
global ans True

g BST not

i
atum ans
the
gondans kill
S
y y
is
10 a 1 BST
48.8
s

pygmy
BST NOLL

To Do of nodes
go
Q Find size
max of sobbee which is a BST

ans 3
37

mr

ingine
int mx
no of nodes
intent
bool is BST
X x x

BST t nodes
C RST
LST C not date

Search BST
Add an element BST

Delete an element o child


i child
a child
B BST from array
IS BST

To DO Max subtree BST


Doubts

N 2 Wiz 2

1 My
holy f nosy 22
11 1 A 2

A 11 hahaa
t t t t
t t t t t t t t t t

Som 2 21 224 2

a 1

Sum 2kt I
d
som 22k I
D
2 I

OCN
T
I I
13
o
on 8.5 10
7S

T
I
in I
1 s io

J C 1 I 7

76
Coe a

NULL
NOU

You might also like