-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBST.c
More file actions
66 lines (50 loc) · 982 Bytes
/
BST.c
File metadata and controls
66 lines (50 loc) · 982 Bytes
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
/* incertion of nodes in binary search tree */
#include<stdio.h>
#include<stdlib.h>
struct node{
int data;
struct node *left;
struct node *right;
};
struct node *HEAD = NULL;
void incert(int val)
{
struct node *temp;
struct node *parent;
struct node *current;
temp =(struct node*)malloc(sizeof(struct node));
temp->data = val;
temp->right = NULL;
temp->left = NULL;
parent = HEAD;
if(HEAD == NULL){
HEAD = temp;
}
else{
current = HEAD;
while(current) /*Findimg path to know where we have to add new node*/
{
parent = current;
if(temp->data > current->data){
current = current->right;
}
else{
current = current->left;
}
}
if(temp->data > parent->data){ /* to know where we have to add newnode left or right */
parent->right = temp;
}
else{
parent->left = temp;
}
}
}
int main()
{
struct node *s1 = NULL;
s1 =(struct node*)malloc(sizeof(struct node));
incert(50);
incert(45);
incert(70);
}