-
Notifications
You must be signed in to change notification settings - Fork 4
/
bst_insertion.C
53 lines (49 loc) · 901 Bytes
/
bst_insertion.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
//author @ Ayush Aggarwal
#include <bits/stdc++.h>
using namespace std;
struct node
{
int data;
struct node *left;
struct node *right;
};
struct node *newNode(int data)
{
struct node *temp = (struct node*)malloc(sizeof(struct node));
temp->data = data;
temp->left = NULL;
temp->right = NULL;
return temp;
}
struct node *insert(struct node* root,int data)
{
if(root == NULL)
return(newNode(data));
if(data > root->data)
root->right = insert(root->right,data);
else
root->left = insert(root->left,data);
return root;
}
void INORDER(struct node *temp)
{
if(temp != NULL)
{
INORDER(temp->left);
cout<<temp->data<<" ";
INORDER(temp->right);
}
}
int main()
{
struct node *root = NULL;
root = insert(root, 50);
insert(root, 30);
insert(root, 20);
insert(root, 40);
insert(root, 70);
insert(root, 60);
insert(root, 80);
INORDER(root);
return 0;
}