Thursday, 24 September 2015

Write a C Program To Traverse Binary Tree (Creation , Insertion And Traversal

1:  #include<stdio.h>  
2:  #include<conio.h>  
3:  struct tree  
4:  {  
5:  struct tree *left;  
6:  int info;  
7:  struct tree *right;  
8:  };  
9:  struct tree *root;  
10:  void insert(int element);  
11:  void display(struct tree *root,int choice);  
12:  void pd(struct tree *root,int value,int option);  
13:  void main()  
14:  {  
15:  int i,x,a,data;  
16:  char ch;  
17:  struct tree *s;  
18:  clrscr();  
19:  root=NULL;  
20:  do  
21:  {  
22:  printf("Enter your choice\n1.Insert an element\n2.Display\n");  
23:  printf("3.Find predecessor and successor in Inorder traversal\n");  
24:  scanf("%d",&x);  
25:  if(x==1)  
26:  {  
27:  printf("Enter the element\n");  
28:  scanf("%d",&i);  
29:  insert(i);  
30:  }  
31:  else if(x==2)  
32:  {  
33:  clrscr();  
34:  printf("1.Inorder notation\n2.Preorder notation\n4.Postorder notation\n");  
35:  scanf("%d",&a);  
36:  display(root,a);  
37:  }  
38:  else if(x==3)  
39:  {  
40:  clrscr();  
41:  printf("Enter the element\n");  
42:  scanf("%d",&data);  
43:  printf("1.Inorder notation\n2.Preorder notation\n3.Postorder notation\n");  
44:  scanf("%d",&a);  
45:  pd(root,data,a);  
46:  }  
47:  printf("\nDo you want to continue> y/n\n");  
48:  scanf(" %c",&ch);  
49:  clrscr();  
50:  }  
51:  while(ch=='y');  
52:  getch();  
53:  }  
54:  void insert(int element)  
55:  {  
56:  struct tree *newptr,*ptr,*parent;  
57:  newptr=(struct tree *)malloc(sizeof(struct tree));  
58:  newptr->info=element;  
59:  newptr->left=newptr->right=NULL;  
60:  if(root==NULL)  
61:  {  
62:  root=newptr;  
63:  }  
64:  else  
65:  {  
66:  parent=NULL;  
67:  ptr=root;  
68:  while(ptr!=NULL)  
69:  {  
70:  parent=ptr;  
71:  if(element<ptr->info)  
72:  {  
73:  ptr=ptr->left;  
74:  }  
75:  else  
76:  {  
77:  ptr=ptr->right;  
78:  }  
79:  }  
80:  if(element<parent->info)  
81:  {  
82:  parent->left=newptr;  
83:  }  
84:  else  
85:  {  
86:  parent->right=newptr;  
87:  }  
88:  }  
89:  }  
90:  void display(struct tree *root,int choice)  
91:  {  
92:  if(choice==1)  
93:  {  
94:  if(root!=NULL)  
95:  {  
96:  display(root->left,choice);  
97:  printf("%d\t",root->info);  
98:  display(root->right,choice);  
99:  }  
100:  }  
101:  else if(choice==2)  
102:  {  
103:  if(root!=NULL)  
104:  {  
105:  printf("%d\t",root->info);  
106:  display(root->left,choice);  
107:  display(root->right,choice);  
108:  }  
109:  }  
110:  else if(choice==3)  
111:  {  
112:  if(root!=NULL)  
113:  {  
114:  display(root->left,choice);  
115:  display(root->right,choice);  
116:  printf("%d\t",root->info);  
117:  }  
118:  }  
119:  }  
120:  void pd(struct tree *root,int data,int option)  
121:  {  
122:  int array[20];  
123:  int i=0,k=0,a=0;  
124:  if(option==1)  
125:  {  
126:  if(root!=NULL)  
127:  {  
128:  pd(root->left,data,option);  
129:  array[i]=root->info;  
130:  i++;  
131:  pd(root->right,data,option);  
132:  }  
133:  }  
134:  else if(option==2)  
135:  {  
136:  if(root!=NULL)  
137:  {  
138:  array[i]=root->info;  
139:  i++;  
140:  pd(root->left,data,option);  
141:  pd(root->right,data,option);  
142:  }  
143:  }  
144:  else if(option==3)  
145:  {  
146:  if(root!=NULL)  
147:  {  
148:  pd(root->left,data,option);  
149:  pd(root->right,data,option);  
150:  array[i]=root->info;  
151:  i++;  
152:  }  
153:  }  
154:  for(i=0;i<19;i++)  
155:  {  
156:  if(array[i]==data)  
157:  {  
158:  k=i;  
159:  }  
160:  else  
161:  i++;  
162:  }  
163:  if(k==0)  
164:  {  
165:  printf("No Predecessor :-(\n");  
166:  }  
167:  else  
168:  printf("Predecessor = %d",array[k-1]);  
169:  printf("Successor = %d",array[k+1]);  
170:  }  

No comments:

Post a Comment