-
Notifications
You must be signed in to change notification settings - Fork 0
/
binary tree.cpp
53 lines (50 loc) · 1.04 KB
/
binary tree.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 <bits/stdc++.h>
#include <iostream>
using namespace std;
struct node
{
int data;
struct node *left;
struct node *right;
};
struct node *newNode(int item)
{
struct node *temp = (struct node *)malloc(sizeof(struct node));
temp->data = item;
temp->left = temp->right = NULL;
return temp;
}
struct node *insert(struct node *node, int data)
{
if (node == NULL)
return newNode(data);
if (data < node->data)
node->left = insert(node->left, data);
else if (data > node->data)
node->right = insert(node->right, data);
return node;
}
void travel(struct node *root) {}
void inorder(struct node *root)
{
if (root != NULL)
{
inorder(root->left);
cout << root->data << endl;
inorder(root->right);
}
}
int main()
{
struct node *root = NULL;
root = insert(root, 1000);
insert(root, 100);
insert(root, 1000);
insert(root, 10);
insert(root, 100);
insert(root, 5);
insert(root, 1);
insert(root, 1);
inorder(root);
return 0;
}