A basic unbalanced binary search tree implementation in C.
More...
#include <stdio.h>
#include <stdlib.h>
|
typedef struct node | node |
| Node, the basic data structure in the tree.
|
|
|
node * | newNode (int data) |
| The node constructor, which receives the key value input and returns a node pointer.
|
|
node * | insert (node *root, int data) |
| Insertion procedure, which inserts the input key in a new node in the tree.
|
|
node * | getMax (node *root) |
| Utilitary procedure to find the greatest key in the left subtree.
|
|
node * | delete (node *root, int data) |
| Deletion procedure, which searches for the input key in the tree and removes it if present.
|
|
int | find (node *root, int data) |
| Search procedure, which looks for the input key in the tree and returns 1 if it's present or 0 if it's not in the tree.
|
|
int | height (node *root) |
| Utilitary procedure to measure the height of the binary tree.
|
|
void | purge (node *root) |
| Utilitary procedure to free all nodes in a tree.
|
|
void | inOrder (node *root) |
| Traversal procedure to list the current keys in the tree in order of value (from the left to the right)
|
|
int | main () |
| Main funcion.
|
|
A basic unbalanced binary search tree implementation in C.
The implementation has the following functionalities implemented:
- Insertion
- Deletion
- Search by key value
- Listing of node keys in order of value (from left to right)
◆ delete()
Deletion procedure, which searches for the input key in the tree and removes it if present.
- Parameters
-
root | pointer to parent node |
data | value to search for int the node |
- Returns
- pointer to parent node
89{
90
91 if (root == NULL)
92 {
93 return root;
94 }
95 else if (
data > root->data)
96 {
97
98 root->right =
delete (root->right,
data);
99 }
100 else if (data < root->
data)
101 {
102 root->left =
delete (root->left,
data);
103 }
104 else if (
data == root->data)
105 {
106
107
108 if ((root->left == NULL) && (root->right == NULL))
109 {
111 return NULL;
112 }
113 else if (root->left == NULL)
114 {
115
116
118 root = root->right;
120 return root;
121 }
122 else if (root->right == NULL)
123 {
125 root = root->left;
127 return root;
128 }
129 else
130 {
131
132
133
135
136
137
138 root->data = tmp->
data;
139 root->left =
delete (root->left, tmp->
data);
140 }
141 }
142 return root;
143}
node * getMax(node *root)
Utilitary procedure to find the greatest key in the left subtree.
Definition binary_search_tree.c:72
#define free(ptr)
This macro replace the standard free function with free_dbg.
Definition malloc_dbg.h:26
Definition prime_factoriziation.c:25
Node, the basic data structure in the tree.
Definition binary_search_tree.c:15
int data
data of the node
Definition binary_search_tree.c:18
◆ find()
int find |
( |
node * |
root, |
|
|
int |
data |
|
) |
| |
Search procedure, which looks for the input key in the tree and returns 1 if it's present or 0 if it's not in the tree.
- Parameters
-
root | pointer to parent node |
data | value to store int he new node |
- Returns
- 0 if value not found in the nodes
-
1 if value was found
153{
154
155 if (root == NULL)
156 {
157 return 0;
158 }
159 else if (
data > root->data)
160 {
161
162
164 }
165 else if (data < root->
data)
166 {
167
169 }
170 else if (
data == root->data)
171 {
172
173 return 1;
174 }
175 else
176 {
177 return 0;
178 }
179}
int find(node *root, int data)
Search procedure, which looks for the input key in the tree and returns 1 if it's present or 0 if it'...
Definition binary_search_tree.c:152
◆ getMax()
Utilitary procedure to find the greatest key in the left subtree.
- Parameters
-
root | pointer to parent node |
- Returns
- pointer to parent node
73{
74
75 if (root->right != NULL)
76 {
77 return getMax(root->right);
78 }
79 return root;
80}
◆ height()
int height |
( |
node * |
root | ) |
|
Utilitary procedure to measure the height of the binary tree.
- Parameters
-
root | pointer to parent node |
data | value to store int he new node |
- Returns
- 0 if value not found in the nodes
-
height of nodes to get to data from parent node
188{
189
190 if (root == NULL)
191 {
192 return 0;
193 }
194 else
195 {
196
197
198 int right_h =
height(root->right);
199 int left_h =
height(root->left);
200
201
202
203 if (right_h > left_h)
204 {
205 return (right_h + 1);
206 }
207 else
208 {
209 return (left_h + 1);
210 }
211 }
212}
int height(node *root)
Utilitary procedure to measure the height of the binary tree.
Definition binary_search_tree.c:187
◆ inOrder()
void inOrder |
( |
node * |
root | ) |
|
Traversal procedure to list the current keys in the tree in order of value (from the left to the right)
- Parameters
-
root | pointer to parent node |
239{
240 if (root != NULL)
241 {
243 printf("\t[ %d ]\t", root->data);
245 }
246}
void inOrder(node *root)
Traversal procedure to list the current keys in the tree in order of value (from the left to the righ...
Definition binary_search_tree.c:238
◆ insert()
Insertion procedure, which inserts the input key in a new node in the tree.
- Parameters
-
root | pointer to parent node |
data | value to store int he new node |
- Returns
- pointer to parent node
47{
48
49 if (root == NULL)
50 {
52 }
53 else if (
data > root->data)
54 {
55
56
58 }
59 else if (data < root->
data)
60 {
61
63 }
64
65 return root;
66}
node * insert(node *root, int data)
Insertion procedure, which inserts the input key in a new node in the tree.
Definition binary_search_tree.c:46
node * newNode(int data)
The node constructor, which receives the key value input and returns a node pointer.
Definition binary_search_tree.c:28
◆ main()
Main funcion.
250{
251
252
254 int opt = -1;
256
257
258 while (opt != 0)
259 {
260 printf(
261 "\n\n[1] Insert Node\n[2] Delete Node\n[3] Find a Node\n[4] Get "
262 "current Height\n[5] Print Tree in Crescent Order\n[0] Quit\n");
263 scanf("%d", &opt);
264
265
266 switch (opt)
267 {
268 case 1:
269 printf("Enter the new node's value:\n");
272 break;
273
274 case 2:
275 printf("Enter the value to be removed:\n");
276 if (root != NULL)
277 {
279 root =
delete (root,
data);
280 }
281 else
282 {
283 printf("Tree is already empty!\n");
284 }
285 break;
286
287 case 3:
288 printf("Enter the searched value:\n");
290 find(root,
data) ? printf(
"The value is in the tree.\n")
291 : printf("The value is not in the tree.\n");
292 break;
293
294 case 4:
295 printf(
"Current height of the tree is: %d\n",
height(root));
296 break;
297
298 case 5:
300 break;
301 }
302 }
303
304
306
307 return 0;
308}
void purge(node *root)
Utilitary procedure to free all nodes in a tree.
Definition binary_search_tree.c:217
◆ newNode()
node * newNode |
( |
int |
data | ) |
|
The node constructor, which receives the key value input and returns a node pointer.
- Parameters
-
data | data to store in a new node |
- Returns
- new node with the provided data
- Note
- the node must be deleted before program terminates to avoid memory leaks
29{
30
32
33
37
38 return tmp;
39}
#define malloc(bytes)
This macro replace the standard malloc function with malloc_dbg.
Definition malloc_dbg.h:18
struct node * right
right child
Definition binary_search_tree.c:17
struct node * left
left child
Definition binary_search_tree.c:16
◆ purge()
void purge |
( |
node * |
root | ) |
|
Utilitary procedure to free all nodes in a tree.
- Parameters
-
root | pointer to parent node |
218{
219 if (root != NULL)
220 {
221 if (root->left != NULL)
222 {
224 }
225 if (root->right != NULL)
226 {
228 }
230 root = NULL;
231 }
232}