2929#include "bst.h"
3030#include "memory.h"
3131
32- #define MAX (a , b ) a > b ? a : b
33- #define HEIGHT (n ) !n ? 0 : n->height
32+ #define MAX (a , b ) a > b ? a : b
33+ #define HEIGHT (n ) !n ? 0 : n->height
3434#define BALANCE (n ) !n ? 0 : (HEIGHT(n->left)) - (HEIGHT(n->right))
3535
36- struct bst_node * bst_new (unsigned char key , const void * data ) {
36+ struct bst_node * bst_new (unsigned char key , const void * data )
37+ {
3738 struct bst_node * node = try_alloc (sizeof (* node ));
38- node -> key = key ;
39- node -> height = 1 ;
40- node -> left = NULL ;
41- node -> right = NULL ;
42- node -> data = (void * ) data ;
39+ node -> key = key ;
40+ node -> height = 1 ;
41+ node -> left = NULL ;
42+ node -> right = NULL ;
43+ node -> data = (void * )data ;
4344 return node ;
4445}
4546
46- static struct bst_node * bst_rotate_right (struct bst_node * y ) {
47- struct bst_node * x = y -> left ;
47+ static struct bst_node * bst_rotate_right (struct bst_node * y )
48+ {
49+ struct bst_node * x = y -> left ;
4850 struct bst_node * t2 = x -> right ;
4951
50- x -> right = y ;
51- y -> left = t2 ;
52+ x -> right = y ;
53+ y -> left = t2 ;
5254
53- y -> height = MAX (HEIGHT (y -> left ), HEIGHT (y -> right )) + 1 ;
54- x -> height = MAX (HEIGHT (x -> left ), HEIGHT (x -> right )) + 1 ;
55+ y -> height = MAX (HEIGHT (y -> left ), HEIGHT (y -> right )) + 1 ;
56+ x -> height = MAX (HEIGHT (x -> left ), HEIGHT (x -> right )) + 1 ;
5557
5658 return x ;
5759}
5860
59- static struct bst_node * bst_rotate_left (struct bst_node * x ) {
60- struct bst_node * y = x -> left ;
61+ static struct bst_node * bst_rotate_left (struct bst_node * x )
62+ {
63+ struct bst_node * y = x -> left ;
6164 struct bst_node * t2 = y -> right ;
6265
63- y -> right = x ;
64- x -> left = t2 ;
66+ y -> right = x ;
67+ x -> left = t2 ;
6568
66- x -> height = MAX (HEIGHT (x -> left ), HEIGHT (x -> right )) + 1 ;
67- y -> height = MAX (HEIGHT (y -> left ), HEIGHT (y -> right )) + 1 ;
69+ x -> height = MAX (HEIGHT (x -> left ), HEIGHT (x -> right )) + 1 ;
70+ y -> height = MAX (HEIGHT (y -> left ), HEIGHT (y -> right )) + 1 ;
6871
6972 return y ;
7073}
7174
72- static struct bst_node * bst_min (const struct bst_node * node ) {
75+ static struct bst_node * bst_min (const struct bst_node * node )
76+ {
7377 const struct bst_node * curr = node ;
7478 while (curr -> left )
7579 curr = curr -> left ;
76- return (struct bst_node * ) curr ;
80+ return (struct bst_node * )curr ;
7781}
7882
7983/*
8084 * Insert a new node into the AVL tree, paying attention to maintain the tree
8185 * balanced in order to freeze the time-complexity to O(logN) for search
8286 */
83- struct bst_node * bst_insert (struct bst_node * node ,
84- unsigned char key , const void * data ) {
87+ struct bst_node * bst_insert (struct bst_node * node , unsigned char key ,
88+ const void * data )
89+ {
8590
8691 // Base case, an empty tree, just add the key to the root
8792 if (!node )
@@ -92,7 +97,7 @@ struct bst_node *bst_insert(struct bst_node *node,
9297 * to be inserted on the left subtree
9398 */
9499 if (key < node -> key )
95- node -> left = bst_insert (node -> left ,key ,data );
100+ node -> left = bst_insert (node -> left , key , data );
96101
97102 /*
98103 * Recursive call: Same reasoning as the step before, but now the key is
@@ -122,7 +127,7 @@ struct bst_node *bst_insert(struct bst_node *node,
122127 * a negative value means the opposite, the tree is unbalanced on the right
123128 * subtree
124129 */
125- int balance = BALANCE (node );
130+ int balance = BALANCE (node );
126131
127132 /*
128133 * Recursive calls done before assinged to nodes the result of the
@@ -148,15 +153,16 @@ struct bst_node *bst_insert(struct bst_node *node,
148153}
149154
150155/* Return a node from the tree if present, otherwise return NULL. O(logN). */
151- struct bst_node * bst_search (const struct bst_node * node , unsigned char key ) {
156+ struct bst_node * bst_search (const struct bst_node * node , unsigned char key )
157+ {
152158
153159 // Base: No nodes in the tree
154160 if (!node )
155161 return NULL ;
156162
157163 // Base: We found the node, just return it
158164 if (key == node -> key )
159- return (struct bst_node * ) node ;
165+ return (struct bst_node * )node ;
160166
161167 /*
162168 * Recursive call: The key is smaller than the current node, we have to
@@ -173,7 +179,8 @@ struct bst_node *bst_search(const struct bst_node *node, unsigned char key) {
173179 return bst_search (node -> right , key );
174180}
175181
176- struct bst_node * bst_delete (struct bst_node * node , unsigned char key ) {
182+ struct bst_node * bst_delete (struct bst_node * node , unsigned char key )
183+ {
177184 if (!node )
178185 return node ;
179186 if (key < node -> key )
@@ -184,28 +191,28 @@ struct bst_node *bst_delete(struct bst_node *node, unsigned char key) {
184191 if (!node -> left || !node -> right ) {
185192 struct bst_node * tmp = node -> left ? node -> left : node -> right ;
186193 if (!tmp ) {
187- tmp = node ;
194+ tmp = node ;
188195 node = NULL ;
189196 } else
190197 * node = * tmp ;
191198 free_memory (tmp );
192199 } else {
193200 struct bst_node * tmp = bst_min (node -> right );
194- node -> key = tmp -> key ;
195- node -> right = bst_delete (node -> right , tmp -> key );
201+ node -> key = tmp -> key ;
202+ node -> right = bst_delete (node -> right , tmp -> key );
196203 }
197204 }
198205
199206 // If the tree had only one node then return
200207 if (!node )
201- return node ;
208+ return node ;
202209
203210 // STEP 2: UPDATE HEIGHT OF THE CURRENT NODE
204211 node -> height = 1 + MAX ((HEIGHT (node -> left )), (HEIGHT (node -> right )));
205212
206213 // STEP 3: GET THE BALANCE FACTOR OF THIS NODE (to
207214 // check whether this node became unbalanced)
208- int balance = BALANCE (node );
215+ int balance = BALANCE (node );
209216
210217 // If this node becomes unbalanced, then there are 4 cases
211218
@@ -215,7 +222,7 @@ struct bst_node *bst_delete(struct bst_node *node, unsigned char key) {
215222
216223 // Left Right Case
217224 if (balance > 1 && BALANCE (node -> left ) < 0 ) {
218- node -> left = bst_rotate_left (node -> left );
225+ node -> left = bst_rotate_left (node -> left );
219226 return bst_rotate_right (node );
220227 }
221228
0 commit comments