Algorithms_in_C 1.0.0
Set of algorithms implemented in C.
Loading...
Searching...
No Matches
binary_search_tree.c File Reference

A basic unbalanced binary search tree implementation in C. More...

#include <stdio.h>
#include <stdlib.h>
Include dependency graph for binary_search_tree.c:

Data Structures

struct  node
 Node, the basic data structure in the tree. More...
 

Typedefs

typedef struct node node
 Node, the basic data structure in the tree.
 

Functions

nodenewNode (int data)
 The node constructor, which receives the key value input and returns a node pointer.
 
nodeinsert (node *root, int data)
 Insertion procedure, which inserts the input key in a new node in the tree.
 
nodegetMax (node *root)
 Utilitary procedure to find the greatest key in the left subtree.
 
nodedelete (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.
 

Detailed Description

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)

Function Documentation

◆ delete()

node * delete ( node root,
int  data 
)

Deletion procedure, which searches for the input key in the tree and removes it if present.

Parameters
rootpointer to parent node
datavalue to search for int the node
Returns
pointer to parent node
89{
90 // If the root is null, nothing to be done
91 if (root == NULL)
92 {
93 return root;
94 }
95 else if (data > root->data)
96 { // If the input key is greater than the root's, search in the right
97 // subtree
98 root->right = delete (root->right, data);
99 }
100 else if (data < root->data)
101 { // If the input key is lower than the root's, search in the left subtree
102 root->left = delete (root->left, data);
103 }
104 else if (data == root->data)
105 {
106 // If the input key matches the root's, check the following cases
107 // termination condition
108 if ((root->left == NULL) && (root->right == NULL))
109 { // Case 1: the root has no leaves, remove the node
110 free(root);
111 return NULL;
112 }
113 else if (root->left == NULL)
114 { // Case 2: the root has one leaf, make the leaf the new root and
115 // remove
116 // the old root
117 node *tmp = root;
118 root = root->right;
119 free(tmp);
120 return root;
121 }
122 else if (root->right == NULL)
123 {
124 node *tmp = root;
125 root = root->left;
126 free(tmp);
127 return root;
128 }
129 else
130 { // Case 3: the root has 2 leaves, find the greatest key in the left
131 // subtree and switch with the root's
132
133 // finds the biggest node in the left branch.
134 node *tmp = getMax(root->left);
135
136 // sets the data of this node equal to the data of the biggest node
137 // (lefts)
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
Here is the call graph for this function:

◆ 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
rootpointer to parent node
datavalue to store int he new node
Returns
0 if value not found in the nodes
1 if value was found
153{
154 // If the root is null, the key's not present
155 if (root == NULL)
156 {
157 return 0;
158 }
159 else if (data > root->data)
160 {
161 // If the input key is greater than the root's, search in the right
162 // subtree
163 return find(root->right, data);
164 }
165 else if (data < root->data)
166 {
167 // If the input key is lower than the root's, search in the left subtree
168 return find(root->left, data);
169 }
170 else if (data == root->data)
171 {
172 // If the input and the root key match, return 1
173 return 1;
174 }
175 else
176 { // unknown result!!
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
Here is the call graph for this function:

◆ getMax()

node * getMax ( node root)

Utilitary procedure to find the greatest key in the left subtree.

Parameters
rootpointer to parent node
Returns
pointer to parent node
73{
74 // If there's no leaf to the right, then this is the maximum key value
75 if (root->right != NULL)
76 {
77 return getMax(root->right);
78 }
79 return root;
80}
Here is the call graph for this function:

◆ height()

int height ( node root)

Utilitary procedure to measure the height of the binary tree.

Parameters
rootpointer to parent node
datavalue 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 // If the root is null, this is the bottom of the tree (height 0)
190 if (root == NULL)
191 {
192 return 0;
193 }
194 else
195 {
196 // Get the height from both left and right subtrees to check which is
197 // the greatest
198 int right_h = height(root->right);
199 int left_h = height(root->left);
200
201 // The final height is the height of the greatest subtree(left or right)
202 // plus 1(which is the root's level)
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
Here is the call graph for this function:

◆ 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
rootpointer to parent node
239{
240 if (root != NULL)
241 {
242 inOrder(root->left);
243 printf("\t[ %d ]\t", root->data);
244 inOrder(root->right);
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
Here is the call graph for this function:

◆ insert()

node * insert ( node root,
int  data 
)

Insertion procedure, which inserts the input key in a new node in the tree.

Parameters
rootpointer to parent node
datavalue to store int he new node
Returns
pointer to parent node
47{
48 // If the root of the subtree is null, insert key here
49 if (root == NULL)
50 {
51 root = newNode(data);
52 }
53 else if (data > root->data)
54 {
55 // If it isn't null and the input key is greater than the root key,
56 // insert in the right leaf
57 root->right = insert(root->right, data);
58 }
59 else if (data < root->data)
60 { // If it isn't null and the input key is lower than the root key, insert
61 // in the left leaf
62 root->left = insert(root->left, data);
63 }
64 // Returns the modified tree
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
Here is the call graph for this function:

◆ main()

int main ( void  )

Main funcion.

250{
251 // this reference don't change.
252 // only the tree changes.
253 node *root = NULL;
254 int opt = -1;
255 int data = 0;
256
257 // event-loop.
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); // reads the choice of the user
264
265 // processes the choice
266 switch (opt)
267 {
268 case 1:
269 printf("Enter the new node's value:\n");
270 scanf("%d", &data);
271 root = insert(root, data);
272 break;
273
274 case 2:
275 printf("Enter the value to be removed:\n");
276 if (root != NULL)
277 {
278 scanf("%d", &data);
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");
289 scanf("%d", &data);
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:
299 inOrder(root);
300 break;
301 }
302 }
303
304 // deletes the tree from the heap.
305 purge(root);
306
307 return 0;
308}
void purge(node *root)
Utilitary procedure to free all nodes in a tree.
Definition: binary_search_tree.c:217
Here is the call graph for this function:

◆ newNode()

node * newNode ( int  data)

The node constructor, which receives the key value input and returns a node pointer.

Parameters
datadata 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 // creates a slug
31 node *tmp = (node *)malloc(sizeof(node));
32
33 // initializes the slug
34 tmp->data = data;
35 tmp->left = NULL;
36 tmp->right = NULL;
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
rootpointer to parent node
218{
219 if (root != NULL)
220 {
221 if (root->left != NULL)
222 {
223 purge(root->left);
224 }
225 if (root->right != NULL)
226 {
227 purge(root->right);
228 }
229 free(root);
230 root = NULL; // reset pointer
231 }
232}
Here is the call graph for this function: