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

Binary Tree

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)
14 views

Binary Tree

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/ 15

File Edit Search Run Conmpile Debug Pro,ject Options Window Help

DDFS\BINARYTR.C =1-[$]
include<stdio.h>
include <conio.h>
tinclude <stdlib.h>
/binars search tree

x Input : 50, 30, 20,40,?0,60,80

Let us create fol lowing BST


50

30 70

20 40 60 80 */

struct node
{
struct node *1eft, right:
int data:

struct node *root, *temp ;


1:1
F1 Help Alt-F8 Next Msg Alt-F7 Prev Msg Alt-F9 Compile F9 Make F10 Menu
E File Edit Search Run Compi le Debug Project Options Window Help
DDFS\B INARYTR.C -1-[]
struct node *root, *temp

struct node xcreate(int);


oid insert (int) ;
struct node *del(struct node *, imt);
void preorder ();
void inorder (struct node *);
uoid postorder 0:
struct node *largestnode (struct node *): ********************

struct node *Sma llestnode (struct node *) ;


int getdata();
int ma in()

int data, choice:


struct node *result:

clrscr(0:

= 40:1
F1 Help Alt-F8 Next Msg Alt-F7 Prey Msu Alt-F9 Compile F9 Make F1O Menu
E File Edit Search Run Compile Debug Pro ject Dptions WindoW Help
DDFS\BINARYTR.C -1-[$]

clrscr0:

while1)

printfC-8inary Search Tree Dperntions


printf (Cl, nsert');
printf ( 2 . Delete');
printfC3. Largest Node ):
printf t. Smllnsi Nude");
printf( 5 . RreDrdbr');
printf(%n6. Inorder");
printf ( ' .Postordexr');
printfn8. Exit");
printf 'nEnter Cho ice "):
Scanf( a " , &cho ice);
pr intf(n"):

= 57:1
F1 Help Alt-F8 Next Msg Alt-F? Prey Msg Alt F9 Comp i le F9 Make F10 Menu
EFile Edit Search Run Compile Debug Project Options Window Help
DDFS\B INARYTR.C =1=[t]
printf(Enter Cho ice
Scanf ( , &cho ice);
printf();

switch(cho ice)
{
Case 1:
data=getdata(0:
insert (data) ;
break:

Case 2:
data=getdata():
root=del(root, data) ;
break;

Case 3:
result =largestnode (root );
if (result != NULL )
{
printf ('nLargest Node : zd" , result->data):
74:1
F1 Help Alt-F8 Next Msg Alt-F Prev Msg Al
Alt-F9 Compi le F9Make F10 Menu
File Edit Search Run Comp ile Debug Project Options WindoJ Help
-[u]= DDFS\BINARYTR.C =1=[$]
Case 3:
result = largestnode(root);
if (result != NULL )

printf((Largest Node : 2", result->data);


break:

Case 4:
result =smal lestmode (root):
if (result != NULL)
{
printf Smallest Node : xa", result->data);
break;

Case 5:
preorder (root):
break;

Case 6:
90:1
F1 Help Alt-F8 Next Msg Alt-F? Prev tsg Alt-F9 Compi le F9 Make F10 Menu
Edit Search Run Comp i le Debug Project Options Window Help
-[u]: DDFS\BINARYTR.C
=1-[$]=
Case 6:
inorder (root ) ;
break:

Case ?:
postorder (root);
break:

Case 8:
exit(0);

def aul t:
printf Clnwalia Choice"):
break:
}

struct node *create (int data) //create node


110:1
F1 Help Alt-F8 Next Msg Alt-F7 Preu Msg Alt
IL F9 Compile F9 Make M0 Menu
E File Edit Search Run Compile Debug Project Üptions Window Help
DDFS\BINARYTR.C 1-[$]
struct node *Create(int data) //Create node
{
struct node *nemode= (struct node *) mal loc(s izeof (struct node) ):

newnode->data=data:
newmode->left=NULL:
nemode->r ight=NULL:

return nemode:
****************8

void isert (data)


{
struct node *mewnodecreate(data);
struct node *temp=root:
struct node *preu:
if (neunode ! =NULL)

if (root=-NULL)
{
129:1
F1 Help Alt-F8 Next Msg Alt-F? Prev MsgAlt F9 Compile F9 Make F10 Men
EFile Edit Search Run Comp i le Debug Pro,ject Options Windo Help
-[u]= DDFS\B INARYTR.C =1=[$]
if (root==NULL)

root=nemode:
printfCoda having 2lanent Las iDserted", data);
return:

else
***************

temp=root;

while(temp!=NULL)
{
prev=temp;
if (data>temp->data)
{
temp=temp->r ight:
}
else
147:1 =
Make F10 Menu
= File Edit Search Run Comp i le Debug Pro,ject Optios Window Help
-[u]= DDFS\BINARYTR.C
else
-1-[t]T
{
temp=temp-> left;
}
}

if (data>pre->data)
pre->right=neunode;
}
else

prev-> left-mewnode;

printf ('node having 2 eleent ans inserted", data) :


}
}

struct node *del(struct node *root, int key)


16?:1
F1 Help Alt-F8 Next Msg Alt-F? Prev Msg Alt-F9
Alt-F9 Compi le F9 Make F10 Menu
File
EFile Edit Search Run Compi le Debug Pro,ject Options Windou Help
DDFS\BINARYTR.C 1-[tl
struct node del(struct node *root, int key)
if (root=-NULL)
f
return root:
}

if (key<root->data)
{ ***************
root-> left=del(root-> left, key):
}
else if (key>root->data)
{
root->right=del(root->right, key):
else

if (root-> leftz=NULL)
temp=root->right:
free(root);
=186:1 H
F1 Help Alt-F8 Next Msg Alt-F? Prev Msg Alt-F9 Comp i le F9 Make F10 Menu
E File Edit Scarch Run Comp i le Debug Pro ject Options Window Help
{
DDFS\BINARYTR.C -1-[$]
if (root-> lef t=-NULL )

temp=root->r ight :
free(root);
returm temp:
}
else if (root->right==NULL )
{
temp=root-> left:
free(root);
returm temp;
}
temp=sma llestode(root->right):
root->data=temp->data:
root->right=de 1(root->right, temp->data): ***
printf( Node having zd elomant as8 dreleated",key);
return root;

200:1
F1 Help AltF8 Next Msg Alt-F? Prey Msg Alt-F9 Comp i le F9 Make F10 Menu
E File Edit Run
Search Run Comp ile Debug Pro,ject Options Window Help
-[u] DDFS\BINARYTR.C =1-[$1
struct node *largestnode (struct node *root)
struct node *curr=root:

while(curr!=NULL && curr->right !-NULL)


{
Curr=curr->right:

return c r r ;

struct node *Smallestnode(struct node *root)


struct node *CurrFroot:

while(Curr!=NULL && curr-> left!=NULL)


curr=curr->left ;

223:1
F1 Help Ált-F8 Next Msg Alt-F? Prev Msg Alt-F9 Comp i le F9 Make F10 Menu
E File Edit Search Run Comp i le Debug Pro ject Uptions Windou Help
-[u]= DDFS\B INARYTR.C =1=[ $]
return curr;

void preorder (struct node *root )//root left right


if (root=-NULL)

returm:

printf
( ,root->data);
preorder (root->left):
preorder (root->right) :

void inorder (struct mode *root) //1eft root right


if(root=-NULL)
return:

243:1
F1 Help Alt-F8 Next Msg Alt-F? Prev Msg AIt F9 Compile
Alt F9 Make F10 Menu
E File Edit Scarch Run Compile Debug Pro ject Üptions Window Help
DDFS\B INARYTR.C =1-[$]
return:

inorder (root->left);
printf
( ,root->data);
inorder (root->r ight):

oid postorder (struct node *root)/left right root


if (root=-NULL)
{
return:
}
postorder (root->left);
postorder(root->right);
printf
( ,root->data);

int getdata ()
int data;
262:1
F1 Help Alt-F8 Next Msg Alt-F? Prev Msg Alt-F9 Compile F9 Make F10 Menu
E File Edit Search Run Comp ile Debug Pro ject Options Window Help
DDFS\BINARYTR.C 1-[$]
inorder (root->right);

void postorder (struct node xroot) /left right root


if (root=-NULL)

returm:

postorder (root-> left); R*****************80****

postorder (root->right);
printf , root->data);

int getdata()
int data:
printf(Enter Data:"):
Scanf ('", &data) :
returm data:

266:1
F1 Help Alt-F8 Next Msg Alt-F? Prev Msg Alt F9 Compile F9
F9 Make
Make F10 Menu

You might also like