summaryrefslogtreecommitdiff
path: root/tree/node.c
diff options
context:
space:
mode:
Diffstat (limited to 'tree/node.c')
-rw-r--r--tree/node.c29
1 files changed, 29 insertions, 0 deletions
diff --git a/tree/node.c b/tree/node.c
new file mode 100644
index 0000000..c0d7738
--- /dev/null
+++ b/tree/node.c
@@ -0,0 +1,29 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include "data_structures.h"
+
+struct node *root = NULL;
+
+struct node
+*create(int val)
+{
+ struct node *tmp = malloc(sizeof(struct node));
+ tmp->val = val;
+ tmp->left = NULL;
+ tmp->right = NULL;
+ return tmp;
+}
+
+struct node
+*insert(struct node *cur, int val)
+{
+ if (cur == NULL) {
+ return create(val);
+ }
+ if (val < cur->val) {
+ cur->left = insert(cur->left, val);
+ } else if (val > cur->val) {
+ cur->right = insert(cur->right, val);
+ }
+ return cur;
+}