-
Notifications
You must be signed in to change notification settings - Fork 0
/
binary_search_tree.c
58 lines (46 loc) · 1.02 KB
/
binary_search_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
#include<stdio.h>
#include<stdlib.h>
struct node{
int key;
struct node *left;
struct node *right;
};
//newnode
struct node *getNewNode(int val){
struct node *newNode = malloc(sizeof(struct node));
newNode->key = val;
newNode->right = NULL;
newNode->left = NULL;
return newNode;
}
//insert function
struct node *insert(struct node *root, int val){
if(root == NULL)
return getNewNode(val);
if(val < root->key)
root->left = insert(root->left, val);
if(val > root->key)
root->right = insert(root->right, val);
return root;
}
//inorder traversal function
void inorder(struct node *root){
if(root == NULL)
return;
inorder(root->left);
printf("%d -> ", root->key);
inorder(root->right);
}
int main(){
struct node *root = NULL;
root = insert(root,100);
insert(root, 120);
insert(root, 50);
insert(root, 20);
insert(root, 12);
insert(root, 70);
insert(root, 200);
insert(root, 110);
inorder(root);
return 0;
}