-
Notifications
You must be signed in to change notification settings - Fork 2
/
create_bst.c
181 lines (148 loc) · 3.62 KB
/
create_bst.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
/****************************************************************************
File name: create_bst.c
Author: babajr
*****************************************************************************/
/*
Binary Search Tree (BST).
File contains the implementation of BST.
*/
#include <stdio.h>
#include <stdlib.h>
struct BstNode
{
struct BstNode *left;
int data;
struct BstNode *right;
};
typedef struct BstNode Node;
// create global root pointer
Node *root = NULL;
/*
API to create new node.
*/
Node *getNewNode(int value)
{
// create new node.
Node *newNode = (Node *)malloc(sizeof(Node));
newNode->left = NULL;
newNode->data = value;
newNode->right = NULL;
return newNode;
}
/*
API to insert the nodes in order to create the BST.
Recursive Insert function is used.
Assumption: BST do not contain the duplicate values, so if value
is already present, it will not be added.
*/
Node *insert(Node *ptr, int value)
{
// If BST is empty, create the new node and insert it as root node.
if(ptr == NULL)
ptr = getNewNode(value);
// If nodes are present in the tree, then check for appropriate position to
// enter the node.
else if(value < ptr->data) // If duplicate is allowed else if(value <= ptr->data)
{
ptr->left = insert(ptr->left, value);
}
else
{
ptr->right = insert(ptr->right, value);
}
return ptr;
}
/*
Iterative Method of inserting element in the array.
Idea is to check is value is already present. If yes, do not insert.
Otherwise insert at appropriate position.
*/
void insertIterative(Node *ptr, int value)
{
// Tail pointer to keep track of root, when ptr becomes NULL.
Node *tail = NULL;
Node *newNode;
// If tree is empty.
if(ptr == NULL)
{
// Create new node and point root to that node.
ptr = getNewNode(value);
root = ptr;
return;
}
// Firstly search if node with value is already present.
while(ptr != NULL)
{
tail = ptr;
if(value == ptr->data)
{
printf("Element is already present, will not insert\n");
return;
}
else if(value < ptr->data)
ptr = ptr->left;
else
ptr = ptr->right;
}
// Node with value is not present, we can create new node and insert it.
newNode = getNewNode(value);
if(newNode->data < tail->data)
tail->left = newNode;
else
tail->right = newNode;
}
/*
API to display the tree in INORDER fashion.
*/
void printInorder(Node *ptr)
{
// Tree is empty.
if(ptr == NULL)
return;
printInorder(ptr->left);
printf("%d -> ", ptr->data);
printInorder(ptr->right);
}
/*
API to display the tree in PREORDER fashion.
*/
void printPreorder(Node *ptr)
{
// Tree is empty.
if(ptr == NULL)
return;
printf("%d -> ", ptr->data);
printPreorder(ptr->left);
printPreorder(ptr->right);
}
/*
API to display the tree in POSTORDER fashion.
*/
void printPostorder(Node *ptr)
{
// Tree is empty.
if(ptr == NULL)
return;
printPostorder(ptr->left);
printPostorder(ptr->right);
printf("%d -> ", ptr->data);
}
int main(void)
{
root = insert(root, 30); // create the root node.
root = insert(root, 20);
root = insert(root, 40);
root = insert(root, 50);
root = insert(root, 10);
root = insert(root, 25);
root = insert(root, 35);
// insertIterative(root, 30);
// insertIterative(root, 20);
// insertIterative(root, 40);
printInorder(root);
printf("\n");
printPreorder(root);
printf("\n");
printPostorder(root);
return 0;
}