-
Notifications
You must be signed in to change notification settings - Fork 0
/
createbst.cpp
69 lines (59 loc) · 867 Bytes
/
createbst.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include<iostream>
using namespace std;
struct node
{
int data;
node* left;
node* right;
};
void createbst(node** root,int v)
{
node* temp = new node;
temp->data = v;
temp->left = temp->right = NULL;
if((*root)==NULL)
{
(*root) = temp;
return ;
}
else
{
if(v > (*root)->data)
{
createbst(&((*root)->right),v);
}
else
{
createbst(&((*root)->left),v);
}
}
return ;
}
void inorder(node* root)
{
if(root==NULL)
{
return ;
}
inorder(root->left);
cout<<root->data<<" ";
inorder(root->right);
return ;
}
int main()
{
node* root = new node;
root = NULL;
int n,i,v;
cout<<"\nEnter how many values to be updated to BST:";
cin>>n;
for(i=0;i<n;i++)
{
cout<<"\nEnter a value:";
cin>>v;
createbst(&root,v);
}
cout<<"\nThe sorted list using BST(using inorder traversal) is:\n";
inorder(root);
return 0;
}