Skip to content

Commit 88b9d66

Browse files
committed
added non recurive BST destruction/delete function
1 parent 398e662 commit 88b9d66

File tree

1 file changed

+24
-1
lines changed

1 file changed

+24
-1
lines changed

Binary-Trees/utilityFunctions.c

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,6 @@ struct node* insert_nonRecursive(struct node* node, int data) {
8989
else {
9090
// 2. Otherwise, recur down the tree
9191
struct node* it = node;
92-
struct jsw_node *it = tree->root;
9392
int dir;
9493
for ( ; ; ) {
9594
if ( it->data == data ) return (node); // duplicate case
@@ -199,3 +198,27 @@ void deleteTree(struct node** node_ref)
199198
_deleteTree(*node_ref);
200199
*node_ref = NULL;
201200
}
201+
202+
/*
203+
we delete a node when there's no left link and do a right rotation
204+
when there is, we can be sure that we'll see and delete every node in the tree.
205+
206+
**** see http://imgur.com/a/U2sA4 ***
207+
*/
208+
void deleteTree_nonRecursive ( struct node* tree ) {
209+
struct node* it = tree;
210+
struct node* save;
211+
while ( it != NULL ) {
212+
if ( it->left != NULL ) {
213+
/* Right rotation */
214+
save = it->left;
215+
it->left = save->right;
216+
save->right = it;
217+
}
218+
else {
219+
save = it->right;
220+
free ( it );
221+
}
222+
it = save;
223+
}
224+
}

0 commit comments

Comments
 (0)