-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtree.c
89 lines (68 loc) · 1.79 KB
/
tree.c
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include <stdio.h>
#include <stdlib.h>
typedef struct Leaf{
int value;
struct Leaf * left;
struct Leaf *right;
}leaf;
typedef struct Tree{
leaf *root;
}tree;
void init(tree* t){
t->root = NULL;
}
// A utility function to create a new BST leaf
leaf* create_leaf(int value){
leaf *new = (leaf *) malloc (sizeof(leaf));
if(new == NULL) return NULL;
new->value = value;
new->left = new->right = NULL;
return new;
}
// A utility function to do inorder traversal of BST
void inorder(leaf *root){
if (root != NULL){
inorder(root->left);
printf(" %d \t", root->value);
inorder(root->right);
}
}
leaf* search(leaf* root, int value){
// Base Cases: root is null or key is present at root
if (root == NULL || root->value == value)
return root;
// Key is greater than root's key
if (root->value < value)
return search(root->right, value);
// Key is smaller than root's key
return search(root->left, value);
}
// A utility function to insert a new node with given key in BST
leaf* insert(leaf *root, int value){
// If the tree is empty, return a new node
if (root == NULL) return create_leaf(value);
// Otherwise, recur down the tree
if (value < root->value)
root->left = insert(root->left, value);
else if (value > root->value)
root->right = insert(root->right, value);
// return the (unchanged) node pointer
return root;
}
// Driver Program to test above functions
int main(void){
int n;
scanf(" %d",&n);
tree *t = (tree*) malloc (sizeof(tree));
init(t);
int i, key;
scanf(" %d",&key);
t->root = insert(t->root, key);
for(i=1; i<n; i++){
scanf(" %d",&key);
insert(t->root, key);
}
// print inoder traversal of the BST
inorder(t->root);
return 0;
}