-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbst.cpp
53 lines (53 loc) · 1022 Bytes
/
bst.cpp
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
#include<stdio.h> //binary search tree
#include<stdlib.h>
struct node // creating a node
{
int data;
struct node *left;
struct node *right;
};
//creation of new node struct node *newnode (int x)
struct node *newnode(int x)
{
struct node *temp;
temp=(struct node*)malloc(sizeof(struct node));
temp->data=x;
temp->left=NULL;
temp->right=NULL;
return temp;
}
//insertion
struct node *insert(struct node *root,int x)
{
if(root==NULL){
return newnode (x);
}
else if(x>root->data)
{
root->right=insert(root->right,x);
}
else
{
root->left=insert(root->left,x);
return root;
}
}
void inorder(struct node*root)
{
if(root!=NULL)
{
inorder(root->left);
printf("%d\n",root->data);
inorder(root->right);
}
}
int main()
{
struct node *root;
root=newnode(20);
insert(root,5);
insert(root,1);
insert(root,15);
inorder(root);
return 0;
}