-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathin,post,pre_order_tree_node.c
128 lines (128 loc) · 1.85 KB
/
in,post,pre_order_tree_node.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#include<stdio.h>
#include<conio.h>
#include<malloc.h>
struct node
{
int info;
struct node *lptr;
struct node *rptr;
}*root,*p,*q;
void insert(int item)
{
struct node *newnode;
newnode=(struct node*)malloc(sizeof(struct node));
newnode->info=item;
newnode->lptr=NULL;
newnode->rptr=NULL;
if(root==NULL)
{
root=newnode;
return;
}
else
{
p=root;
q=root;
while(q!=NULL)
{
if(q->info==item)
{
printf("\nData Already Exits.");
return;
}
else if(item>q->info)
{
p=q;
q=q->lptr;
}
else
{
p=q;
q=q->lptr;
}
}
if(item>p->info)
{
p->rptr=newnode;
}
else
{
p->lptr=newnode;
}
}
}
void in_order(struct node *temp)
{
if(root==NULL)
{
printf("\nTree is Empty");
return;
}
if(temp==NULL)
return;
else
{
in_order(temp->lptr);
printf("\t%d",temp->info);
in_order(temp->rptr);
}
}
void pre_order(struct node *temp)
{
if(root==NULL)
{
printf("\nTree is Empty");
return;
}
if(temp==NULL)
return;
else
{
printf("\t%d",temp->info);
pre_order(temp->lptr);
pre_order(temp->rptr);
}
}
void post_order(struct node *temp)
{
if(root==NULL)
{
printf("\nTree is Empty");
return;
}
if(temp==NULL)
return;
else
{
printf("\t%d",temp->info);
post_order(temp->lptr);
post_order(temp->rptr);
}
}
void main()
{
int item,choice;
do
{
printf("\n(1)Insert \n(2)In-Order \n(3)Pre-Order \n(4)Post-Order \n(5)Exit");
printf("\nEnter Your Choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("Enter Number:");
scanf("%d",&item);
insert(item);
break;
case 2:
in_order(root);
break;
case 3:
pre_order(root);
break;
case 4:
post_order(root);
break;
}
}while(choice>0&&choice<5);
}