-
Notifications
You must be signed in to change notification settings - Fork 254
/
Copy path02 Traversal of Tree.c
128 lines (122 loc) · 2.66 KB
/
02 Traversal of Tree.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 <stdlib.h>
struct Node // this is tree node
{
struct Node* lchild; // lefe child as node in tree
int data; // data element
struct Node* rchild; // right child as node in tree
};
// we are implementing tree with the help of queue
struct Queue
{
int size; // size of queue
int front; // front of queue // using for del
int rear; // last of queue used for insertion
struct Node** Q; // this should be an aray of nodes
};
void create(struct Queue* q, int size) // creating a queue // passing pointer and the size of queue
{
q->size = size; // size of queue
q->front = 0; // make front and rear as null intially
q->rear = 0;
q->Q = (struct Node**)malloc(q->size * sizeof(struct Node*)); // allocating space in heap
}
void enqueue(struct Queue* q, struct Node* x) // passing queue as pointer
{
if ((q->rear + 1) % q->size == q->front) // checking wheather queue is full
printf("Queue if FULL");
else
{
q->rear = (q->rear + 1) % q->size;
q->Q[q->rear] = x; // push the data
}
}
struct Node* dequeue(struct Queue* q)
{
struct Node* x = NULL;
if (q->front == q->rear)
return NULL;
else
{
q->front = (q->front + 1) % q->size;
x = q->Q[q->front];
return x;
}
}
int isEmpty(struct Queue q)
{
return q.front == q.rear;
}
struct Node* root = NULL;
void TreeCreate()
{
struct Node* t, * p;
int x;// val of root
struct Queue q;
create(&q, 20); // size of a queue should be bigger
root = (struct Node*)malloc(sizeof(struct Node));
printf("Enter a Root Value :\n");
scanf_s("%d", &x);
root->data = x;
root->lchild = NULL;
root->rchild = NULL;
enqueue(&q, root);
while (!isEmpty(q))
{
p = dequeue(&q);
printf("Enter Left Child of %d :", p->data);
scanf_s("%d", &x);
if (x != -1)
{
t = (struct Node*)malloc(sizeof(struct Node));
t->data = x;
t->lchild = t->rchild = NULL;
p->lchild = t;
enqueue(&q, t);
}
printf("Enter Right Child of %d:", p->data);
scanf_s("%d", &x);
if (x != -1)
{
t = (struct Node*)malloc(sizeof(struct Node));
t->data = x;
t->lchild = t->rchild = NULL;
p->rchild = t;
enqueue(&q, t);
}
}
}
void preorder1(struct Node* p)
{
if (p != NULL)
{
printf("%d\t", p->data);
preorder1(p->lchild);
preorder1(p->rchild);
}
}
void Inorder(struct Node* p)
{
if (p)
{
Inorder(p->lchild);
printf("%d\t", p->data);
Inorder(p->rchild);
}
}
void Postorder(struct Node* p)
{
if (p)
{
Postorder(p->lchild);
Postorder(p->rchild);
printf("%d\t", p->data);
}
}
int main()
{
TreeCreate();
preorder1(root);
Inorder(root);
Postorder(root);
}