forked from amanpro30/Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbst.c
72 lines (64 loc) · 1.68 KB
/
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
########################## BST ######################
#include<stdio.h>
#include<stdlib.h>
struct node{
int data;
struct node *left, *right;
};
void InOrder(struct node* head){
if(head){
InOrder(head -> left);
printf("%d ", head -> data);
InOrder(head -> right);
}
}
struct node* NewNode(int num){
struct node* temp = (struct node*)malloc(sizeof(struct node));
temp -> data = num;
temp -> left = NULL;
temp -> right = NULL;
return temp;
}
struct node* Insert(struct node* head, int num){
struct node* temp = head;
if (!temp){
temp = NewNode(num);
head = temp;
}
else if (num < temp -> data)
temp -> left = Insert(head -> left, num);
else
temp -> right = Insert(head -> right, num);
return head;
}
strcut node* Remove(struct node* head, int num){
l
}
struct node* SearchNode(struct node* head, int num){
struct node* temp = head;
if(!temp)
return NULL;
else if(num < temp -> data)
SearchNode(temp -> left, num);
else if(num > temp -> data)
SearchNode(temp -> right, num);
else if(num == temp ->data)
return temp;
}
int main(int argc, char const *argv[])
{
int arr[15] = {3, 2, 22, 32, 41, 1, 21, 89, 8, 92, 10, 99, 123, 68, 55};
struct node* head = (struct node *)malloc(sizeof(struct node));
for(int i = 0; i < 15; i++)
head = Insert(head, arr[i]);
InOrder(head);
printf("\n");
int num_search;
scanf("%d", &num_search);
struct node* poin_search = SearchNode(head, num_search);
if(!poin_search)
printf("No Such element\n");
else
printf("Element exists\n");
return 0;
}