-
Notifications
You must be signed in to change notification settings - Fork 0
/
BST.cpp
44 lines (43 loc) · 814 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
#include <iostream>
#include <cstdlib>
using namespace std;
struct Node
{
int value;
Node *right;
Node *left;
};
Node *insert(Node *root, int target)
{
Node *temp = (Node *)malloc(sizeof(Node));
temp->value = target;
temp->left=temp->right=NULL;
if (!root)
return temp;
if (target < root->value)
root->left = insert(root->left, target);
else
{
root->right = insert(root->right, target);
}
return root;
}
void inorder(Node *root)
{
if (root == NULL)
return;
inorder(root->left);
cout << root->value << " ";
inorder(root->right);
}
int main()
{
int arr[] = {3, 7, 4, 1, 6, 8};
Node *root = NULL;
for (int i = 0; i < 6; i++)
{
root = insert(root, arr[i]);
}
inorder(root);
return 0;
}