forked from SunilPant1/C_Programs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bst_DSA.c
57 lines (44 loc) · 985 Bytes
/
bst_DSA.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
#include<stdio.h>
#include<stdlib.h>
struct BST{
struct BST *left;
int data;
struct BST *right;
};
void append(struct BST **,int );
int main(){
struct BST *root = NULL;
append(&root,45);
append(&root,96);
append(&root,213);
append(&root,78);
return 0;
}
void append(struct BST ** pr,int num){
struct BST *p,*temp,*prev;
p = (struct BST *)malloc(sizeof(struct BST));
//if memory is not allocated to the new node
if(p == NULL){
printf("Insufficient memory \n");
return;
}
p->data = num;
p->left = p->right = NULL;
//for inserting first data in a node
if(*pr == NULL){
*pr = p;
return;
}
temp = *pr;
while(temp != NULL){
prev = temp;
if(temp->data > num)
temp = temp->left;
else
temp= temp->right;
}
if(prev->data>num)
prev->left = p;
else
prev->right = p;
}