Fixing clear
This commit is contained in:
parent
26bba55a3c
commit
f3a32c4c79
3 changed files with 60 additions and 9 deletions
20
src/main.c
20
src/main.c
|
@ -5,12 +5,28 @@
|
||||||
|
|
||||||
int main(void) {
|
int main(void) {
|
||||||
Tree tree = {NULL};
|
Tree tree = {NULL};
|
||||||
printf("Tree size: %d\n", tree_size(tree.root));
|
|
||||||
|
// Some test insertions
|
||||||
tree_insert(&tree, 1);
|
tree_insert(&tree, 1);
|
||||||
tree_insert(&tree, 2);
|
tree_insert(&tree, 2);
|
||||||
tree_insert(&tree, 3);
|
tree_insert(&tree, 3);
|
||||||
|
|
||||||
|
// Check size and print inorder
|
||||||
printf("Tree size: %d\n", tree_size(tree.root));
|
printf("Tree size: %d\n", tree_size(tree.root));
|
||||||
|
tree_inorder(tree.root);
|
||||||
|
|
||||||
printf("Tree contains 2: %d\n", tree_contains(tree.root, 2));
|
printf("Tree contains 2: %d\n", tree_contains(tree.root, 2));
|
||||||
// tree_clear(&tree.root); // Something is fishy here
|
|
||||||
|
// Clear the tree
|
||||||
|
tree_clear(&tree);
|
||||||
|
|
||||||
|
// Check if the tree is really empty
|
||||||
|
if (tree.root == NULL) printf("Tree is empty\n");
|
||||||
|
else printf("Tree is not empty: %d\n", tree.root->data);
|
||||||
|
|
||||||
|
// Check size and print inorder
|
||||||
|
printf("Tree size: %d\n", tree_size(tree.root));
|
||||||
|
tree_inorder(tree.root);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
27
src/tree.c
27
src/tree.c
|
@ -65,17 +65,34 @@ int tree_remove(Node **node, int data) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int tree_clear(Node *node) {
|
void tree_clear(Tree *tree) {
|
||||||
|
if (tree->root == NULL)
|
||||||
|
return;
|
||||||
|
|
||||||
|
node_clear(tree->root);
|
||||||
|
tree->root = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
void node_clear(Node *node) {
|
||||||
if (node == NULL)
|
if (node == NULL)
|
||||||
return 0;
|
return;
|
||||||
|
|
||||||
if (node->left != NULL)
|
if (node->left != NULL)
|
||||||
tree_clear(node->left);
|
node_clear(node->left);
|
||||||
|
|
||||||
if (node->right != NULL)
|
if (node->right != NULL)
|
||||||
tree_clear(node->right);
|
node_clear(node->right);
|
||||||
|
|
||||||
free(node);
|
free(node);
|
||||||
return 0;
|
}
|
||||||
|
|
||||||
|
void tree_inorder(Node *node) {
|
||||||
|
if (node == NULL)
|
||||||
|
return;
|
||||||
|
|
||||||
|
tree_inorder(node->left);
|
||||||
|
printf("%d ", node->data);
|
||||||
|
tree_inorder(node->right);
|
||||||
}
|
}
|
||||||
|
|
||||||
int tree_size(Node *node) {
|
int tree_size(Node *node) {
|
||||||
|
|
22
src/tree.h
22
src/tree.h
|
@ -1,4 +1,5 @@
|
||||||
#pragma once
|
#ifndef TREE_H
|
||||||
|
#define TREE_H
|
||||||
|
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
@ -17,8 +18,25 @@ struct Tree {
|
||||||
};
|
};
|
||||||
typedef struct Tree Tree;
|
typedef struct Tree Tree;
|
||||||
|
|
||||||
|
// Insert some data into the tree
|
||||||
int tree_insert(Tree *tree, int data);
|
int tree_insert(Tree *tree, int data);
|
||||||
|
|
||||||
|
// Remove some data from the tree
|
||||||
int tree_remove(Node **node, int data);
|
int tree_remove(Node **node, int data);
|
||||||
int tree_clear(Node *node);
|
|
||||||
|
// Clear the tree (this wraps node_clear)
|
||||||
|
void tree_clear(Tree *tree);
|
||||||
|
|
||||||
|
// Recursively clear from a certain node
|
||||||
|
void node_clear(Node *node);
|
||||||
|
|
||||||
|
// Get the size of the tree
|
||||||
int tree_size(Node *node);
|
int tree_size(Node *node);
|
||||||
|
|
||||||
|
// Check if the tree contains some data
|
||||||
bool tree_contains(Node *node, int data);
|
bool tree_contains(Node *node, int data);
|
||||||
|
|
||||||
|
// Print the tree in order
|
||||||
|
void tree_inorder(Node *node);
|
||||||
|
|
||||||
|
#endif // TREE_H
|
Loading…
Reference in a new issue