Attachment 'BINARYTR.C'
Download
Toggle line numbers
1 #include<stdio.h>
2 #include<stdlib.h>
3 #include<conio.h>
4 #include<malloc.h>
5 typedef struct treenode *position;
6 typedef struct treenode *searchtree;
7 position findpos(int ,searchtree);
8 position findmin(searchtree);
9 position findmax(searchtree);
10 searchtree insert(int ,searchtree );
11 searchtree del(int,searchtree);
12 void display( searchtree);
13 struct treenode
14 {
15 int ele;
16 searchtree right;
17 searchtree left;
18 }*t,*temp;
19
20 void main()
21 {
22 int x,ch,*tp;
23 clrscr();
24
25 do
26 {
27 printf("\n1.Insert\n2.Delete\n3.Findposition\n4.Findmin");
28 printf("\n5.Findmax\n6.Display\n7.exit ");
29 printf("\nEnter ur choice:");
30 scanf("%d",&ch);
31 switch(ch)
32 {
33 case 1:printf( "Enter the data");
34 scanf("%d",&x);
35 t=insert(x,t);
36 break;
37
38 case 2:printf("Enter the data to be deleted");
39 scanf("%d",&x);
40 t= del(x,t);
41 break;
42
43 case 3:printf("\nEnter the element to find its position");
44 scanf("%d",&x);
45 temp=findpos(x,t);
46 if(x==temp->ele)
47 printf("\nthe element is in the tree");
48 else
49 printf("the element is not in the tree");
50 break;
51
52 case 4:temp=findmin(t);
53 printf("the min element is :%d",temp->ele);
54 break;
55
56 case 5:temp=findmax(t);
57 printf("\nthe max element is :%d ",temp->ele);
58 break;
59
60 case 6:display(t);
61 break;
62
63 case 7:exit(0);
64 break;
65 }
66
67 }while(ch!=7);
68
69 getch();
70
71 }
72
73 searchtree insert(int x,searchtree t)
74 {
75 if(t==NULL)
76 {
77 t=(struct treenode*)malloc(sizeof(struct treenode));
78 if(t==NULL)
79 printf("\nout of space");
80 else
81 {
82 t->ele=x;
83 t->left=NULL;
84 t->right=NULL;
85 }
86 }
87
88 if(x<t->ele)
89 t->left=insert(x,t->left);
90 else if(x>t->ele)
91 t->right=insert(x,t->right);
92 return t;
93 }
94
95 searchtree del(int x,searchtree t)
96 {
97 position tmpcell;
98 if(t==NULL)
99 printf("Element not found");
100 else if(x<t->ele)
101 t->left=del(x,t->left);
102 else if(x>t->ele)
103 t->right=del(x,t->right);
104 else if(t->left&&t->right)
105 {
106 tmpcell=t;
107 tmpcell=findmin(t->right);
108 t->ele=tmpcell->ele;
109 t->right=del(t->ele,t->right);
110 }
111 else
112 {
113 tmpcell=t;
114 if(t->left==NULL)
115 t=t->right;
116 else if(t->right==NULL)
117 t=t->left;
118 free(tmpcell);
119 }
120 return t;
121 }
122 position findpos(int x,searchtree t)
123 {
124 if(t==NULL)
125 return NULL;
126 else if(x<t->ele)
127 return findpos(x,t->left);
128 else if(x>t->ele)
129 return findpos(x,t->right);
130 else
131 return t;
132 }
133 position findmin(searchtree t)
134 {
135 if(t==NULL)
136 return NULL;
137 else if(t->left==NULL)
138 return t;
139 else
140 return findmin(t->left);
141 }
142 position findmax(searchtree t)
143 {
144 if(t==NULL)
145 return NULL;
146 else if(t->right==NULL)
147 return t;
148 else
149 return findmax(t->right);
150 }
151 void display(searchtree t)
152 {
153
154 if(t!=NULL)
155 {
156 display(t->left);
157 printf("\n%d",t->ele);
158 display(t->right);
159 }
160 }
Attached Files
To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.You are not allowed to attach a file to this page.