-
Notifications
You must be signed in to change notification settings - Fork 0
/
bst.cpp
48 lines (42 loc) · 767 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
#include<iostream>
using namespace std;
struct Node
{
int data;
Node *left, *right;
Node(int val)
{
data = val;
left = NULL;
right = NULL;
}
};
Node* insertBST(Node* root, int val)
{
if(root == NULL)
return new Node(val);
if(val < root->data)
root->left = insertBST(root->left, val);
else
root->right = insertBST(root->right, val);
return root;
}
void inorder(Node* root)
{
if(root == NULL)
return;
inorder(root->left);
cout << root->data << " ";
inorder(root->right);
}
int main()
{
int arr[] = {5,1,3,4,2,7};
Node* root = NULL;
for(int i=0; i<6; i++)
{
root = insertBST(root, arr[i]);
}
inorder(root);
return 0;
}