Skip to content

Commit 296f3d0

Browse files
authored
[bug+docs] add docs + fix error in getMax (#579)
* add docs + fix error in getMax * fix clang-tidy alerts and errors * rearrange comments * allow subfolders in data_structure * set pointer to NULL after purge
1 parent 83a8239 commit 296f3d0

File tree

2 files changed

+119
-54
lines changed

2 files changed

+119
-54
lines changed

.github/workflows/awesome_workflow.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ jobs:
142142
print(f"{len(space_files)} files contain space or dash characters:")
143143
print("\n".join(space_files) + "\n")
144144
145-
nodir_files = [file for file in cpp_files if file.count(os.sep) != 1 and "project_euler" not in file]
145+
nodir_files = [file for file in cpp_files if file.count(os.sep) != 1 and "project_euler" not in file and "data_structure" not in file]
146146
if nodir_files:
147147
print(f"{len(nodir_files)} files are not in one and only one directory:")
148148
print("\n".join(nodir_files) + "\n")

data_structures/binary_trees/binary_search_tree.c

+118-53
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,30 @@
1+
/**
2+
* @file
3+
* @brief A basic unbalanced binary search tree implementation in C.
4+
* @details The implementation has the following functionalities implemented:
5+
* - Insertion
6+
* - Deletion
7+
* - Search by key value
8+
* - Listing of node keys in order of value (from left to right)
9+
*/
110
#include <stdio.h>
211
#include <stdlib.h>
312

4-
/* A basic unbalanced binary search tree implementation in C, with the following
5-
functionalities implemented:
6-
- Insertion
7-
- Deletion
8-
- Search by key value
9-
- Listing of node keys in order of value (from left to right)
10-
*/
11-
12-
// Node, the basic data structure in the tree
13+
/** Node, the basic data structure in the tree */
1314
typedef struct node
1415
{
15-
// left child
16-
struct node *left;
17-
18-
// right child
19-
struct node *right;
20-
21-
// data of the node
22-
int data;
16+
struct node *left; /**< left child */
17+
struct node *right; /**< right child */
18+
int data; /**< data of the node */
2319
} node;
2420

25-
// The node constructor, which receives the key value input and returns a node
26-
// pointer
21+
/** The node constructor, which receives the key value input and returns a node
22+
* pointer
23+
* @param data data to store in a new node
24+
* @returns new node with the provided data
25+
* @note the node must be deleted before program terminates to avoid memory
26+
* leaks
27+
*/
2728
node *newNode(int data)
2829
{
2930
// creates a slug
@@ -37,61 +38,82 @@ node *newNode(int data)
3738
return tmp;
3839
}
3940

40-
// Insertion procedure, which inserts the input key in a new node in the tree
41+
/** Insertion procedure, which inserts the input key in a new node in the tree
42+
* @param root pointer to parent node
43+
* @param data value to store int he new node
44+
* @returns pointer to parent node
45+
*/
4146
node *insert(node *root, int data)
4247
{
4348
// If the root of the subtree is null, insert key here
4449
if (root == NULL)
50+
{
4551
root = newNode(data);
46-
// If it isn't null and the input key is greater than the root key, insert
47-
// in the right leaf
52+
}
4853
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
4957
root->right = insert(root->right, data);
50-
// If it isn't null and the input key is lower than the root key, insert in
51-
// the left leaf
58+
}
5259
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
5362
root->left = insert(root->left, data);
63+
}
5464
// Returns the modified tree
5565
return root;
5666
}
5767

58-
// Utilitary procedure to find the greatest key in the left subtree
68+
/** Utilitary procedure to find the greatest key in the left subtree
69+
* @param root pointer to parent node
70+
* @returns pointer to parent node
71+
*/
5972
node *getMax(node *root)
6073
{
6174
// If there's no leaf to the right, then this is the maximum key value
62-
if (root->right == NULL)
63-
return root;
64-
else
65-
root->right = getMax(root->right);
75+
if (root->right != NULL)
76+
{
77+
return getMax(root->right);
78+
}
79+
return root;
6680
}
6781

68-
// Deletion procedure, which searches for the input key in the tree and removes
69-
// it if present
82+
/** Deletion procedure, which searches for the input key in the tree and removes
83+
* it if present
84+
* @param root pointer to parent node
85+
* @param data value to search for int the node
86+
* @returns pointer to parent node
87+
*/
7088
node *delete (node *root, int data)
7189
{
7290
// If the root is null, nothing to be done
7391
if (root == NULL)
92+
{
7493
return root;
75-
// If the input key is greater than the root's, search in the right subtree
94+
}
7695
else if (data > root->data)
96+
{ // If the input key is greater than the root's, search in the right
97+
// subtree
7798
root->right = delete (root->right, data);
78-
// If the input key is lower than the root's, search in the left subtree
99+
}
79100
else if (data < root->data)
101+
{ // If the input key is lower than the root's, search in the left subtree
80102
root->left = delete (root->left, data);
81-
// If the input key matches the root's, check the following cases
82-
// termination condition
103+
}
83104
else if (data == root->data)
84105
{
85-
// Case 1: the root has no leaves, remove the node
106+
// If the input key matches the root's, check the following cases
107+
// termination condition
86108
if ((root->left == NULL) && (root->right == NULL))
87-
{
109+
{ // Case 1: the root has no leaves, remove the node
88110
free(root);
89111
return NULL;
90112
}
91-
// Case 2: the root has one leaf, make the leaf the new root and remove
92-
// the old root
93113
else if (root->left == NULL)
94-
{
114+
{ // Case 2: the root has one leaf, make the leaf the new root and
115+
// remove
116+
// the old root
95117
node *tmp = root;
96118
root = root->right;
97119
free(tmp);
@@ -104,10 +126,10 @@ node *delete (node *root, int data)
104126
free(tmp);
105127
return root;
106128
}
107-
// Case 3: the root has 2 leaves, find the greatest key in the left
108-
// subtree and switch with the root's
109129
else
110-
{
130+
{ // Case 3: the root has 2 leaves, find the greatest key in the left
131+
// subtree and switch with the root's
132+
111133
// finds the biggest node in the left branch.
112134
node *tmp = getMax(root->left);
113135

@@ -120,30 +142,55 @@ node *delete (node *root, int data)
120142
return root;
121143
}
122144

123-
// Search procedure, which looks for the input key in the tree and returns 1 if
124-
// it's present or 0 if it's not in the tree
145+
/** Search procedure, which looks for the input key in the tree and returns 1 if
146+
* it's present or 0 if it's not in the tree
147+
* @param root pointer to parent node
148+
* @param data value to store int he new node
149+
* @returns 0 if value not found in the nodes
150+
* @returns 1 if value was found
151+
*/
125152
int find(node *root, int data)
126153
{
127154
// If the root is null, the key's not present
128155
if (root == NULL)
156+
{
129157
return 0;
130-
// If the input key is greater than the root's, search in the right subtree
158+
}
131159
else if (data > root->data)
160+
{
161+
// If the input key is greater than the root's, search in the right
162+
// subtree
132163
return find(root->right, data);
133-
// If the input key is lower than the root's, search in the left subtree
164+
}
134165
else if (data < root->data)
166+
{
167+
// If the input key is lower than the root's, search in the left subtree
135168
return find(root->left, data);
136-
// If the input and the root key match, return 1
169+
}
137170
else if (data == root->data)
171+
{
172+
// If the input and the root key match, return 1
138173
return 1;
174+
}
175+
else
176+
{ // unknown result!!
177+
return 0;
178+
}
139179
}
140180

141-
// Utilitary procedure to measure the height of the binary tree
181+
/** Utilitary procedure to measure the height of the binary tree
182+
* @param root pointer to parent node
183+
* @param data value to store int he new node
184+
* @returns 0 if value not found in the nodes
185+
* @returns height of nodes to get to data from parent node
186+
*/
142187
int height(node *root)
143188
{
144189
// If the root is null, this is the bottom of the tree (height 0)
145190
if (root == NULL)
191+
{
146192
return 0;
193+
}
147194
else
148195
{
149196
// Get the height from both left and right subtrees to check which is
@@ -154,27 +201,40 @@ int height(node *root)
154201
// The final height is the height of the greatest subtree(left or right)
155202
// plus 1(which is the root's level)
156203
if (right_h > left_h)
204+
{
157205
return (right_h + 1);
206+
}
158207
else
208+
{
159209
return (left_h + 1);
210+
}
160211
}
161212
}
162213

163-
// Utilitary procedure to free all nodes in a tree
214+
/** Utilitary procedure to free all nodes in a tree
215+
* @param root pointer to parent node
216+
*/
164217
void purge(node *root)
165218
{
166219
if (root != NULL)
167220
{
168221
if (root->left != NULL)
222+
{
169223
purge(root->left);
224+
}
170225
if (root->right != NULL)
226+
{
171227
purge(root->right);
228+
}
172229
free(root);
230+
root = NULL; // reset pointer
173231
}
174232
}
175233

176-
// Traversal procedure to list the current keys in the tree in order of value
177-
// (from the left to the right)
234+
/** Traversal procedure to list the current keys in the tree in order of value
235+
* (from the left to the right)
236+
* @param root pointer to parent node
237+
*/
178238
void inOrder(node *root)
179239
{
180240
if (root != NULL)
@@ -185,7 +245,8 @@ void inOrder(node *root)
185245
}
186246
}
187247

188-
void main()
248+
/** Main funcion */
249+
int main()
189250
{
190251
// this reference don't change.
191252
// only the tree changes.
@@ -218,7 +279,9 @@ void main()
218279
root = delete (root, data);
219280
}
220281
else
282+
{
221283
printf("Tree is already empty!\n");
284+
}
222285
break;
223286

224287
case 3:
@@ -240,4 +303,6 @@ void main()
240303

241304
// deletes the tree from the heap.
242305
purge(root);
306+
307+
return 0;
243308
}

0 commit comments

Comments
 (0)