-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathBinary search tree
59 lines (49 loc) · 1.18 KB
/
Binary search tree
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
#include<stdio.h>
#include<stdlib.h>
//Implemented insert for creating a binary search tree
// Also it can traverse inorder to print the BST
struct node{
int data;
struct node *lc, *rc;
};
struct node *newNode(int data){
struct node * temp ;
temp=(struct node*)malloc(sizeof(struct node));
temp->lc=NULL;
temp->rc=NULL;
temp->data=data;
return temp;
}
struct node* insert(struct node * root, int data){
if (root==NULL) return newNode(data);
if(root->data > data) root->lc = insert( root->lc, data );
if(root->data < data) root->rc = insert( root->rc, data );
return root;
}
void inorderTraversal(struct node* root){
if(root){
inorderTraversal(root->lc);
printf("%d ",root->data);
inorderTraversal(root->rc);
}
}
int main()
{
/* Let us create following BST
50
/ \
30 70
/ \ / \
20 40 60 80 */
struct node *root=NULL;
root = insert(root, 50);
insert(root, 30);
insert(root, 20);
insert(root, 40);
insert(root, 70);
insert(root, 60);
insert(root, 80);
inorderTraversal(root);
printf("\n");
return 0;
}