forked from paranlee/ludtm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbtree.c
More file actions
62 lines (50 loc) · 1.52 KB
/
Copy pathbtree.c
File metadata and controls
62 lines (50 loc) · 1.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <btree.h>
int main() {
struct bt_root mytree = BT_ROOT;
int values[] = {5, 3, 7, 2, 4, 6, 8, 1, 9, 10};
int num_values = sizeof(values) / sizeof(values[0]);
int i;
// Insert values (manipulation)
printf("Inserting values into BST: ");
for (i = 0; i < num_values; i++) {
bt_insert(&mytree, values[i]);
printf("%d ", values[i]);
}
printf("\n");
// Traversals
printf("Inorder traversal: ");
bt_inorder(mytree.node);
printf("\n");
printf("Preorder traversal: ");
bt_preorder(mytree.node);
printf("\n");
printf("Postorder traversal: ");
bt_postorder(mytree.node);
printf("\n");
// Search
int search_val = 4;
struct bt_node *found = bt_search(mytree.node, search_val);
if (found) {
printf("Searched for %d: found %d\n", search_val, found->data);
} else {
printf("Searched for %d: not found\n", search_val);
}
// Delete (manipulation)
int delete_val = 5;
printf("Deleting %d\n", delete_val);
mytree.node = bt_delete(mytree.node, delete_val);
// Traversals after delete
printf("Inorder after delete: ");
bt_inorder(mytree.node);
printf("\n");
// Validation
printf("Tree height: %d\n", bt_height(mytree.node));
printf("Is balanced: %s\n", bt_is_balanced(mytree.node) ? "Yes" : "No");
printf("Is BST: %s\n", bt_is_bst(mytree.node) ? "Yes" : "No");
// Clean up
bt_free(mytree.node);
return 0;
}