forked from HarshCasper/NeoAlgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Iterative_preorder.c
194 lines (192 loc) · 4.58 KB
/
Iterative_preorder.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/*
The traversal operation is a frequently used operation on a binary tree. This operation is used to visit each node in the tree exactly once
A full traversal on a binary tree gives a linear ordering of data in the tree . This is the iterative preorder tree traversal algorithms
*/
#include <stdio.h>
#include <stdlib.h>
//structure for the binary tree node
typedef struct Tree
{
char data;
struct Tree *left;
struct Tree *right;
} TreeNode;
//structure for stack which is used for the iterative traversal algorithms
//stack is implemented using linked list
typedef struct Stack
{
TreeNode *node;
struct Stack *next;
} Stack;
//push operation for the stack
void push(Stack **s, TreeNode *node)
{
Stack *New = (Stack *)malloc(sizeof(Stack));
New->node = node;
New->next = (*s);
(*s) = New;
}
// pop operation for the stack
TreeNode *pop(Stack **s)
{
if ((*s) != NULL)
{
TreeNode *ptr = (*s)->node;
(*s) = (*s)->next;
return ptr;
}
}
//checks whether the stack is empty or not
int isEmpty(Stack **s)
{
if ((*s) == NULL)
{
return 1;
}
return 0;
}
//iterative preorder traversal
void preorder(TreeNode *root)
{
TreeNode *ptr = root;
Stack *s = NULL;
push(&s, root);
while (!isEmpty(&s))
{
ptr = pop(&s);
if (ptr != NULL)
{
printf("%c ", ptr->data);
push(&s, ptr->right);
push(&s, ptr->left);
}
}
}
//Search_Link returns the pointer to the node which contains the value key , it is used in the insertion operation
TreeNode *Search_Link(TreeNode *root, char key)
{
TreeNode *ptr = root;
if (ptr->data != key)
{
if (ptr->left != NULL)
{
Search_Link(ptr->left, key);
}
if (ptr->right != NULL)
{
Search_Link(ptr->right, key);
}
if (ptr->right == NULL && ptr->left == NULL)
{
return NULL;
}
}
else
{
return ptr;
}
}
// Insertion operation of the binary tree
void insert(TreeNode *root, char key, char c)
{
TreeNode *ptr = Search_Link(root, key);
if (ptr == NULL)
{
printf("Search Unsuccessfull No Insertion \n");
return;
}
char option;
if (ptr->left == NULL || ptr->right == NULL)
{
printf("Enter Where to insert left or right (L/R)\n");
scanf(" %c", &option);
if (option == 'L' || option == 'l')
{
if (ptr->left == NULL)
{
TreeNode *New = (TreeNode *)malloc(sizeof(TreeNode));
ptr->left = New;
New->data = c;
printf("Inserting %c as the left\n", c);
New->left = NULL;
New->right = NULL;
}
else
{
printf("Insertion not Possible as Left child \n");
}
}
else
{
if (ptr->right == NULL)
{
TreeNode *New = (TreeNode *)malloc(sizeof(TreeNode));
ptr->right = New;
New->data = c;
printf("Inserting %c as the right\n", c);
New->left = NULL;
New->right = NULL;
}
else
{
printf("Insertion not possible as the right child \n");
}
}
}
else
{
printf("The Node Already has child");
}
}
int main()
{
printf("Enter the value for root node\n");
TreeNode *root = (TreeNode *)malloc(sizeof(TreeNode));
char c;
scanf("%c", &c);
root->data = c;
root->right = NULL;
root->left = NULL;
char choice = 'y';
char data;
do
{
printf("Enter the Node to be Inserted \n");
scanf(" %c", &data);
printf("Enter the Parent node of the node to be inserted\n");
scanf(" %c", &c);
insert(root, c, data);
printf("Do you want to continue (y/n)\n");
scanf(" %c", &choice);
} while (choice == 'y' || choice == 'Y');
printf("Preorder Traversal of the Tree \n");
preorder(root);
return 0;
}
/*
Sample I/O:
Enter the value for root node
A
Enter the Node to be Inserted
B
Enter the Parent node of the node to be inserted
A
Enter Where to insert left or right (L/R)
L
Inserting B as the left
Do you want to continue (y/n)
y
Enter the Node to be Inserted
C
Enter the Parent node of the node to be inserted
A
Enter Where to insert left or right (L/R)
R
Inserting C as the right
Do you want to continue (y/n)
n
Preorder Traversal of the Tree
A B C
Time Complexity = O( n )
Space Complexity = O( n )
*/