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 main()
13: {
14: int i,x,a;
15: char ch;
16: clrscr();
17: root=NULL;
18: do
19: {
20: printf("Enter your choice\n1.Insert an element\n2.Display\n");
21: scanf("%d",&x);
22: if(x==1)
23: {
24: printf("Enter the element\n");
25: scanf("%d",&i);
26: insert(i);
27: }
28: else if(x==2)
29: {
30: clrscr();
31: printf("1.Infix notation\n2.Prefix notation\n4.Postfix notation\n");
32: scanf("%d",&a);
33: display(root,a);
34: }
35: printf("\nDo you want to continue> y/n\n");
36: scanf(" %c",&ch);
37: clrscr();
38: }
39: while(ch=='y');
40: getch();
41: }
42: void insert(int element)
43: {
44: struct tree *newptr,*ptr,*parent;
45: newptr=(struct tree *)malloc(sizeof(struct tree));
46: newptr->info=element;
47: newptr->left=newptr->right=NULL;
48: if(root==NULL)
49: {
50: root=newptr;
51: }
52: else
53: {
54: parent=NULL;
55: ptr=root;
56: while(ptr!=NULL)
57: {
58: parent=ptr;
59: if(element<ptr->info)
60: {
61: ptr=ptr->left;
62: }
63: else
64: {
65: ptr=ptr->right;
66: }
67: }
68: if(element<parent->info)
69: {
70: parent->left=newptr;
71: }
72: else
73: {
74: parent->right=newptr;
75: }
76: }
77: }
78: void display(struct tree *root,int choice)
79: {
80: if(choice==1)
81: {
82: if(root!=NULL)
83: {
84: display(root->left,choice);
85: printf("%d\t",root->info);
86: display(root->right,choice);
87: }
88: }
89: else if(choice==2)
90: {
91: if(root!=NULL)
92: {
93: printf("%d\t",root->info);
94: display(root->left,choice);
95: display(root->right,choice);
96: }
97: }
98: else if(choice==3)
99: {
100: if(root!=NULL)
101: {
102: display(root->left,choice);
103: display(root->right,choice);
104: printf("%d\t",root->info);
105: }
106: }
107: }
getsprogramming Tutorials , C Programming, CPP Programming and JAVA Programming , Python Programming , Javascript Programming Welcome to getsprogramming Blog Here You Find Different Programs in Easier way to Explain and gain Knowledge and Try Your Self..Thanks
Thursday, 24 September 2015
Write a C Program To Traverse Binary Search Tree (INORDER , PREORDER and POSTORDER)
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment