-
Notifications
You must be signed in to change notification settings - Fork 0
/
76_insertion_in_bst.c
98 lines (88 loc) · 2.33 KB
/
76_insertion_in_bst.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
90
91
92
93
94
95
96
97
98
#include<stdio.h>
#include<stdlib.h>
//inserting a key in th e best
//first one needs to check if its already present
//as bst dont have duplicate entries
//and if not found insert beneath the previous node
struct node{
int data;
struct node* right;
struct node* left;
};
struct node* createNode(int data){ // to create a node (the node is not linked to any other node for now)
struct node * n;
n = (struct node *)malloc(sizeof(struct node));
n->data = data;
n->left = NULL;
n->right = NULL;
return n;
}
void inorder(struct node* root){ //left -> root-> right
if (root!=NULL){
inorder(root->left); //recursive function
printf("the element is %d \n",root->data);
inorder(root->right);
}
}
void insert_bst(struct node *root,int key,int *level1){
struct node *ptr = createNode(key);
int flag =0;// if the element is found if cant insert it in the bst
struct node * prev;
prev =NULL;
while(root!=NULL){
prev = root;
*level1 = *level1+1;
if (key == root->data){
// return root;
flag++;
break;
}
else if (key< root->data){
root = root->left;
}
else if (key> root->data){
root = root->right;
}
}
if (flag == 0){
if (key < prev->data){
prev->left = ptr;
}
else if (key > (prev->data)){
prev->right = ptr ;
}
}
else{
printf("key ' %d ' already present\n",key);
}
}
int main(){
//constructing the root node - using function
//binary tree building
struct node *p = createNode(5);
struct node *p1 = createNode(3);
struct node *p2 = createNode(6);
struct node *p3 = createNode(2);
struct node *p4 = createNode(4);
p->left = p1;
p->right =p2;
p1->left = p3;
p1->right = p4;
int level1 =0;
int *ptr ;
ptr = &level1;
//binary search tree:-
// 5
// / \
// 3 6
// / \
// 2 4
// finished building the binary tree
inorder(p);//just an traversal through the elements
insert_bst(p,1,ptr);//second entry is the key to be inserted //passing level throu call by reference
insert_bst(p,4,ptr); //passing level throu call by reference
insert_bst(p,12,ptr);
printf("After insertion op :- \n");
inorder(p);
return 0;
}