Professional Documents
Culture Documents
Binary Tree
Binary Tree
DDFS\BINARYTR.C =1-[$]
include<stdio.h>
include <conio.h>
tinclude <stdlib.h>
/binars search tree
30 70
20 40 60 80 */
struct node
{
struct node *1eft, right:
int data:
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)
= 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 )
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:
}
newnode->data=data:
newmode->left=NULL:
nemode->r ight=NULL:
return nemode:
****************8
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;
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:
return c r r ;
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;
returm:
printf
( ,root->data);
preorder (root->left):
preorder (root->right) :
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):
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);
returm:
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