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

Trees4 LCA

1. The document discusses algorithms for finding the maximum subtree in a binary search tree (BST), finding the largest subtree, and calculating the lowest common ancestor (LCA) node in a tree. 2. It provides pseudocode for traversing a tree in-order, pre-order, and post-order to find node pairs where the order is different in the in-order traversal. This can identify swapped nodes in a BST. 3. Algorithms for finding the path from the root node to any other node in a tree and calculating the LCA of two nodes in both a BST and a general tree are presented, with time and space complexities of O(N) for both problems.

Uploaded by

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

Trees4 LCA

1. The document discusses algorithms for finding the maximum subtree in a binary search tree (BST), finding the largest subtree, and calculating the lowest common ancestor (LCA) node in a tree. 2. It provides pseudocode for traversing a tree in-order, pre-order, and post-order to find node pairs where the order is different in the in-order traversal. This can identify swapped nodes in a BST. 3. Algorithms for finding the path from the root node to any other node in a tree and calculating the LCA of two nodes in both a BST and a general tree are presented, with time and space complexities of O(N) for both problems.

Uploaded by

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

Answer for last class To Do

Topo Max subtree BST


A
globalans soot
node
info largestsubtree

root sight
info s largestsubtree

info ans
ans Cnt front 2 ont th
ans.mn min l.mn t.mn rootdata
ans.mx Max C t.mx 2 mx soot date
Omm
If C d BST IT 44 v BST T 44 Not data
48 not data t.mx
ans bst True
910am maxcgloans ans.cat
epee
I ans bs False

Recursion TC Oc
In order Sc Oct
In order iterative TC OCN
SC OCH

non isinsdy
wndsy.gg
to In order
eat isnt

In
f 29

5
f's I
2 7,575
Pj pg
s

69819110.19117119120.23125129130132.33 3539

to

Is

I 25 3 9
S 33
y Ig
10
0422 root
l

Y print
422
422 data
422 sis bt

else

Cuz 2 left
Node tmp
Noll
while tmp right cuz
Emp right
Emp right
Emp

I F C EMP right NULL

an
up.name
cu 22 Cy 22 fest

else
tmp right null

point Carr data


422 Cy 22 night
I TC Ocn och

GI
at
sci OCD

6,8120123
25129,30 fifth can
23,2
25
th droit

Morn's Preorder

Node test sight


data

Morris Postondes

tix
node

Q A BST mistakely 2 node got swapped

Find those 2 nodes

is 1544
T s y
213
is
is
t 18
L
a

5
110 2,3 is oT
Sg
2 g g

First noo first pair


2,3 45,5 0,4 second no of Ind pair

2 3 9 9,10 11 20

r f

A
2 8 9 15 20 25
2 8 15 9 20 25
1 Do Inordin traversal I

2 Find pairs where in Cigs In Ci i

If 2 pairs
ans 1stof P 2ndof Da
else
ans entire pair

910 20
2 3 9 11 8
d d

2 3 9 8110 9 Cl 20

99

Lowest Common ancestor


CA

3
i g

LCACN.IQ
g
20
14 8
I
3
i g
LCAC I

14
1 12

3
7

61
14
1
y
I 2
18

10

5 is

3 17 12
I

I o f a 25

6,252 no
5 CCA
LCA C 2,63 2,52 5
LCA C
LCA C H M z z
LCAC IS 147 15
Tree
Q Given a node Find path from soot to node

iterate soot dist nodes


void
NULL SAM
IF CHOE
Mody
A PathCAD
If not data

J
Mody
B PathCAD
If not data
iterate f soot left nods
iterate soot sight nodes
nodes delete end C
CUT
9
43
Pays
Cass f
dpa
s9 V C Ccac A B
a
r
4,512
path
A path CB

10

5 is

3 ng

o 8 Yu
LCA 11,25 9 5 15

CID 10,151121
path Te OC Nt 11
91h12m Sc OCH

H TH
OCH
I
7 an 5 5
Path 177 10,5
3 z
Path C 27

a D y
g 3
chis
2 7,4
4,5 1,7 4,3
a
M
43,9
4 5,2
LCA in BT TC OCM
Sc och

BST TC OC M
CA in
se Oct

LCA 11,25
10

g
I I IS
ans 15

3 17 ng
I I 125
2 6 1 y

LCA 17,14
I
10
am

3 17
125
I f y
tmp soot

tap non
while
B
data A 44 tmp data
If C tmp
lest
tm pi trop
A 44 Emp
dataCB
tmp data C

4
else it
tm p tm proight

else

TC O CH
Sc O d

BT
CA in
f 0,17
7 0
1
5,167
3
2,37
GM 10,15
g
7,83
611,12 613,143
7
In time 4 outtime
In Ntl
Out Cny
7 0

Node not
void travel
NULL return
f 200 t
soot T
In time
Ttt
I travel soot lest
travel root sight
Croot T
out time
Ttt
L

0,17
0,471 5,167
2 3
2,37
G
GM 10,15
g
7,83
611,12 613,143
7

x Y
X anscentor of y
in Coo Cincy
out Cx out Cy
422 root

while Carr n

If Curr left is ancestor of A 4 B


Curt cars left

else if Curr right is ancestor of


A 4 B
Curt Cura tight
else

Tc O CN H
Sc O CN
r
Inc oatC
see
game
are there for LCA a
It lots of queries

Path method TC AXN

in out method To Nt OH

X
X
i
A
pea
poor

2 Swaps in BST

LC A

LC A in BT as path
LCA in BST

You might also like