-
Notifications
You must be signed in to change notification settings - Fork 6
/
BinarySearchTree.c
executable file
·134 lines (129 loc) · 2.21 KB
/
BinarySearchTree.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#include<stdio.h>
#include<stdlib.h>
struct node
{
struct node *lchild;
int info;
struct node *rchild;
}*root = NULL,*root1=NULL;
void insert_nrec(int ikey)
{
struct node *tmp,*par,*ptr;
ptr=root;
par=NULL;
while(ptr!=NULL)
{
par = ptr;
if(ikey<ptr->info)
ptr=ptr->lchild;
else if (ikey>ptr->info)
ptr=ptr->rchild;//ptr controls the loop;
else
{
printf("The key aleady exists\n");
}
}
tmp=(struct node *)malloc(sizeof(struct node));
tmp->info=ikey;
tmp->lchild=NULL;
tmp->rchild=NULL;
if(par==NULL)
root = tmp;
else if (ikey<par->info)
par->lchild=tmp;
else
par->rchild=tmp;//tmp is the new node. this points to it;
}
void postorder (struct node *root)
{
if(root!=NULL)
{
postorder(root->lchild);
postorder(root->rchild);
printf("%d ",root->info);
}
}
void max ()
{
struct node *par, *ptr;
ptr = root;
par = NULL;
while(ptr!=NULL)
{
par = ptr ;
ptr=ptr->rchild;
}
printf("The maximum value is : %d\n",par->info);
}
void search ()
{
printf("Enter the element you want to search for : ");
int item;
scanf("%d",&item);
struct node *ptr;
ptr = root;
//example of traversal
while(ptr!=NULL)
{
if(ptr->info>item)
ptr=ptr->lchild;
else if (ptr->info<item)
ptr=ptr->rchild;
else if (ptr->info=item)
{
printf("Teh element, has been found!\n");break;
}
}
}
void random ()
{
struct node *ptr;
ptr=root;
while(ptr!=NULL)
{
printf("%d\n",ptr->info);
ptr=ptr->rchild;
}
}
int main ()
{
//when you think there is gonna be a switch case here ..
//im sorry we dont do that here XD
int num = 0;
while (num!=6)
{
printf("Enter your choice : ");
scanf("%d",&num);
if(num== 1)
{
printf("Enter the ikey : ");
int item;
scanf("%d",&item);
insert_nrec(item);
}
else if (num==2)
{
postorder(root);
}
else if (num==3)
{
max();
}
else if (num==4)
{
search ();
}
else if (num==5)
random();
}
/*
int num1,num2,num3;
printf("Enter three values : ");
scanf("%d",&num1);
scanf("%d",&num2);
scanf("%d",&num3);
insert_nrec(num1);
insert_nrec(num2);
insert_nrec(num3);
postorder(root);*/
}