Contains and non-working clear
This commit is contained in:
parent
9eeba598e7
commit
457cd2965e
3 changed files with 33 additions and 4 deletions
|
@ -10,5 +10,7 @@ int main(void) {
|
||||||
tree_insert(&tree, 2);
|
tree_insert(&tree, 2);
|
||||||
tree_insert(&tree, 3);
|
tree_insert(&tree, 3);
|
||||||
printf("Tree size: %d\n", tree_size(tree.root));
|
printf("Tree size: %d\n", tree_size(tree.root));
|
||||||
|
printf("Tree contains 2: %d\n", tree_contains(tree.root, 2));
|
||||||
|
// tree_clear(&tree.root); // Something is fishy here
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
29
src/tree.c
29
src/tree.c
|
@ -45,9 +45,34 @@ int tree_insert(Tree *tree, int data) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int tree_remove(Tree *tree, int data) { return 1; }
|
bool tree_contains(Node *node, int data) {
|
||||||
|
if (node == NULL)
|
||||||
|
return false;
|
||||||
|
|
||||||
int tree_clear(Tree *tree) { return 1; }
|
if (node->data == data)
|
||||||
|
return true;
|
||||||
|
|
||||||
|
return tree_contains(node->left, data) || tree_contains(node->right, data);
|
||||||
|
}
|
||||||
|
|
||||||
|
int tree_remove(Node **node, int data) {
|
||||||
|
if (node == NULL)
|
||||||
|
return false;
|
||||||
|
// TODO
|
||||||
|
}
|
||||||
|
|
||||||
|
int tree_clear(Node *node) {
|
||||||
|
if (node == NULL)
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
if (node->left != NULL)
|
||||||
|
tree_clear(node->left);
|
||||||
|
if (node->right != NULL)
|
||||||
|
tree_clear(node->right);
|
||||||
|
|
||||||
|
free(node);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
int tree_size(Node *node) {
|
int tree_size(Node *node) {
|
||||||
if (node == NULL) {
|
if (node == NULL) {
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <stdbool.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
@ -17,6 +18,7 @@ struct Tree {
|
||||||
typedef struct Tree Tree;
|
typedef struct Tree Tree;
|
||||||
|
|
||||||
int tree_insert(Tree *tree, int data);
|
int tree_insert(Tree *tree, int data);
|
||||||
int tree_remove(Tree *tree, int data);
|
int tree_remove(Node **node, int data);
|
||||||
int tree_clear(Tree *tree);
|
int tree_clear(Node *node);
|
||||||
int tree_size(Node *node);
|
int tree_size(Node *node);
|
||||||
|
bool tree_contains(Node *node, int data);
|
||||||
|
|
Loading…
Reference in a new issue