0% found this document useful (0 votes)
7 views24 pages

DS CH-6 (Final ?)

The document provides an overview of tree data structures in C, detailing terminology such as root, leaf nodes, edges, and forests. It explains various tree properties, traversal methods (preorder, inorder, postorder), and operations like insertion, searching, and deletion. Additionally, it includes examples and algorithms for implementing these concepts in binary trees.

Uploaded by

piyush.saini0798
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views24 pages

DS CH-6 (Final ?)

The document provides an overview of tree data structures in C, detailing terminology such as root, leaf nodes, edges, and forests. It explains various tree properties, traversal methods (preorder, inorder, postorder), and operations like insertion, searching, and deletion. Additionally, it includes examples and algorithms for implementing these concepts in binary trees.

Uploaded by

piyush.saini0798
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

DATA

STRUCTURES
USING C
REEMA THAREJA

Q
Chapter6
CHAPTER-6
Taee S

TeC Tree S a non-inean Data Stuctye


Stuctuse

9+ is dePined a8 inite Set O On o

more nodes IS (atled a TrCe.

(D
(
Teee
Terminolo9y oP tree

Root a hee
c e IS Called
The topmost node of
Roo+
(A) Root

(8

A 7 Root in tee
l e a f Node - The
The abt node oF each Pah )
extenal node.
an Called caP mode

(A
B
(
eaf node

D,E, P and all ae leaf no de.

Node A elements OP tree is Called a8


No de.

Node

Node
(D

A, 8,C, D, E , f and C ae all nodes n hee.


tree.
EdgeEdge :- Edge s the
he link 8/u two
nodes 1S Callec a8 Edge.

A Node
Edge
Node
5) Forest
A Collec tion di'soint tree
is Called a8 Fo xest

(A

Be ore Removin9 hode

D
APlo movinq node CPoves+).

Payent The node which has b&anch


bom to any o he node Is

Called Panent hode


Panent 8 and C

(D
6) Child nod, -

The hode which 1S descendant


Some node is allcd as child node

Chld ofA

Child

of B

Siblings: The childen Of Same bahent


Cale
Sibling

Sibling
Sibling

gn atee, each Step fom


8) Level:-
Called Q Level oF
top t bottom IS

tree
- Levelo
-Level

Level2
Deqec of node
The To tal Numbe of
Childhen OP hode S Ca led deqe ch
node
Deg oR 8 2
Degree oF C =2
8

Branch:
The bath ending Ith leaf
mode s Called brarch

Depth or Height of a tee


The Muximum
Numb of hodes In aa banch o f a t-ee
S Called height Of tree.

EX.
A Heigh o Tee

4 CA8, D, H)

.8,0,T)

Ancesto AIl the hodes abo ve a


12) Oncesto+S
bnhicula node Called

(13) DeS cendents


:- Al the hodes beloo a

paniculay hod iS Callec descendent


H n ee st a

(0
Degenden ts

Ancesto
Desccndent of 8IS
Ancesto oF E S B andA
D, E Qnd H

19) Path:
The Seguen ce of Nodes and edges
hom Ohc node anotheh nod u
Called as Pa th.

Path 8/ + to Z iS
A-8 0 -I

Subtrce -
Any hode oF he tet a lon g
wIth descendant 1S Calld Subthee
Bn
Chit
mode

TD

(
().
(D
b) Complete Binahy ttt-

The 8inay ee

Said to be Complee Biny tee

a he eve's ae Comple tely


Ohen
he agt level whch S
Palled xcept

om he left

Ex. (A
B
D

Peropenhes of 8inay te
No. oF hodes 1h Binahy
The maxi mum

hee of heigh h ish-1.


No. of
Level pey
Height MAX
Nodes

Lve Level 2
2-
G)Level3
2 3
3 23-
maximy m No. Of hodes at level h
The
Binay Tce OP eve is 22
No. oF
Level pehty max
Lerel Nodes at
eve
Level 2 2-1
22-
Level 3-(O)
3 23-
(

HI

HI
t HpHBEn ta hon -
Linked

In data value
LLink nRight Link Stee add css
of LeP+
Subtee
RLink Stee addxss
of Right
Subtee

Declnahon of linked tree xpesentation


ty pecle Stuct tno de

Stuct tnode child


Int data;
Stuct t nede *
nchild
Sthuct hode * oot
av Chstng a binahy trce-
TravoSing 0
binhy tec meansS
Visit all he hode

in the tree is Called 08 TaveHsins OF


Binahy ee.

Thxee ways oF T a vesin9 Q binohy tree-


D Preord
2) norde
3) Past orden

Pc ordo
a) Visit oot node
b) Vistt all the node n the }ePt
Subtrce
Vis+ all he node n the
ght
Sybtae,
Root
Ex

LePH Sub B Right Subhee

Preo&de A

Root
LePt Righ+
Subtce Subtree
hcodch> A 8, D,F, C, P,C

Algonthm PREORDER [ RooT, DATA LCHILD RCHILD


Ohoe
Root 15 a Po into Vahioble that Con taln addeg
OF oot no de
Data Contain he data In he node

LCHILD Contain qddess OF Lept chld


RCHILD Contain addrtss of Right child.

STEPI:- TFC RoOT= NULL), THEN


rCtun

End iF
STEP 2: SET PTR:=Roo T

STEP3 PRINT PTR DATA


STEP PREORDER ( PTR LCHILD)
STEP5 PREORDER (PTR RCH1LD)
STEP6: STP

OnSnorda
the node Ih LeP Subtee
a) VISI+ all
b) VISI+oot nede

visit at he node in Right SubtKe


( Ruut

Left Subte
(B
) ght
(D Sub ter

gnorden B (A

Root
Lept Rght
Subhee Subtce

gntoad th D.BE, A,P.C,G

ALGnORITHM gNORDER CRooT, DATA, LCHILD, RCHILD)

STEPI- JF RooT = NuLL) THEN


Retum
TEND IF]
STEP2:- SET PTR: =Roo7
STEP3: 9NORDER PTR LCHILO0)
STEPY:- Pint PTR DATA
STEP 5:-NORDE R PTR
RCHILD)
STEP 6: CND
Postordo):
VISIt all he hode n Left Sube
a) Subr
b) yisit
Vist a he hode
R19ht Subte
c) ViSH root Ndde

A Root

LeP Ryht Subhe

Sub

Past oden
RoOt

LePt Su bhe
L Rght
Sub tae
Post 0deh D,E, B, F, Cn, C, A

Al90ithm PoSTORDER [Ro0T, DATA, LCHILD, RCHILD

STEP:- 9F CRo0T =
NULL,THEN
RETURN
tEND IP]
STEP 2:- SET PTR: RooT =

STEP3:
PoSTORDER ( PTR -9 LCHILD)
STEP 4 -
poS T ORDER PTR>RCHILO)
STEP5: PRINT PTR DATA
STEP6: END
nor der, posto>d Ond P)cosdeh
tayOnSal Of h Rllouolh9 thee:

7Root
SoM (A
Let
Right Subhee
Subhee

a) Pre ordn-

Root
Lept Right
Subhee Subtrtc

Prcoyde A,B,EECe
b) Ino do -

Root
LePt Right
Sube Subhce

noxder> D,8E, A,GP


OPostojden> (A)
C

LePt Root
Riyht
Subthec Syhhe

Post Posto)> DEB, F.C,A

Qu Con8ts uc+ abihany trte whose node ce a


Un de

Preorde: ABD GTHCER IKJ


hode: - BaHD A E CIRPJ
Ory

(8

(D
(

b) eorde: FA,E, C,D, H,Ca,8


gnordn - E,AC K, F H, D,8,
inany Seahch rce -
The Value oF JeFt
no de mugt be Smalle han brent hodt
Ond Va lue Of Right node must b
ga-t than pahent node IS (alled os
Bihahy hee
Seahch hee

(go
95

pehtes
Al ho de he leP+ Subtree
esse
2) hode of the
Rght Subtrce ae

geatty
LePt 2 Righ+ Subtree ae also Binay
Seahch

u) Thene ae no duplicate hodes


9+ IS type of Brnahy tree
pongtion on Binany Seahch trce

Jnsehon
delehon
Seahchin9
Insehon:- The Yo cess n which 9nsert 0
node 1h Bina y Seanch 7ee S Caled
Jnsenhon
Syppose 55 Ins ert n ee

(5o

(22 ( 60

1 3
STEP1- Compahe he 55 with oot 50
S5 >5o
Move Rht chilc of oot.
STEP2:-Compase 55 h 6o
55 60 Ce
Move Let chil Df pot

STEP3: ComPane 55 oh 3 o

55>3 o
Move Right child of oot.

(60
gon h m: INS 8ST (RooT, ITEn)
ohene Roo Is boin tu Vaiable

hat Contain adden oF o t nede


ITEM IS yalye to be In8 Cnt In Bs

STEP - gP CRo0T =NOLL) THEN


RooT LCHILD ;= NOLL
RoOT INFo= DATA
RooT RCHILD: =NULL
ENO IF.
STEP 2 SET PTR: = Roo T
STEP3:- TF C TEM< P TR DATA)
NS-3ST LPTR LCHI LD , ITEM )
LSE
INS BST C PTR RCHILD ITEM)
S TEPy: SToP

Seanching:- The Process n which


Seanch a ode In Binay Secs+ch
Tee is Called Scanchihg
Supp ose 25 Seach In tree
Ex.
STEP 25 o t h oof hode So
Compohe
255o
Move LePt chile oF root

STEP2: Compane 25 wth 22


25> 22

Mo ve Right child oF doot.

STEP3: - (ompant 25 wth 25


25 2 5
25 IS Seanched.
ALG Seanch CRoot, Hem

9P L TEM = oot> data) o (oot =


NuLL)
STEP1-
etun oot

ELSE IF ITE M <root- data )

ttun Seanch ( oot lePt tem)

ELSE
xetun Scanch Coot> Right, Item)

END IP

STEP2 END
peletion: a Pocess which
Deletion S
Scanch tee
ddeh the node n Bi ncy
is Called delehion.

COse i: Deleing a node wlth no childen

(s) (15
Delete (18 (
(1o (20
Case 8 (12
8

APt
Befoe Delek
delee
hode hod

Case 2: Dele ting a hod wlth On Child h

delek (25)
Ca_e 2

(19 (3

Befon dtlet Aftn delete


hode
node
3 Casc 3: a nede uwc chldn
Delcting9

(15)
Dekte (2

Beon dekt delek ho de,


aflen
hode

Applicaton of tree.
Set >cptsenta tion
9+ S used fo
2) Tee is In decision ma Hing
gntensting applicohon oF e S playing

gomes
games ke chess, chec Ken S, hc-4ac-toe ete

You might also like