diff options
Diffstat (limited to 'tree/tree.c')
-rw-r--r-- | tree/tree.c | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/tree/tree.c b/tree/tree.c new file mode 100644 index 0000000..82b4544 --- /dev/null +++ b/tree/tree.c @@ -0,0 +1,54 @@ +#include <stdio.h> +#include <stdlib.h> +#include "data_structures.h" + +void +pre_order(struct node *cur) +{ + if (cur == NULL) { + return; + } + printf("%d ", cur->val); + pre_order(cur->left); + pre_order(cur->right); +} + +void +in_order(struct node *cur) +{ + if (cur == NULL) { + return; + } + in_order(cur->left); + printf("%d ", cur->val); + in_order(cur->right); +} + +void +post_order(struct node *cur) +{ + if (cur == NULL) { + return; + } + post_order(cur->left); + post_order(cur->right); + printf("%d ", cur->val); +} + +int +main() +{ + struct node *root = NULL; + root = insert(root, 10); + int val, n = 0; + printf("How many integers do you want to add to root with val 10?\n"); + scanf("%d", &n); + for (int i = 0; i < n; ++i) { + printf("Enter a number \n"); + scanf("%d", &val); + insert(root, val); + in_order(root); + printf("\n"); + } + return 0; +} |