-
Notifications
You must be signed in to change notification settings - Fork 0
/
Binary_tree.cpp
56 lines (50 loc) · 1.01 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
54
55
56
#include<iostream>
using namespace std;
class Node{
public:
int data;
Node* right=NULL;
Node* left=NULL;
Node(int item){
data=item;
}
};
class Binary_Tree{
public:
Node* root;
Binary_Tree(int root_data){
root=new Node(root_data);
}
void insert(Node* root){
int left;
int right;
cin>>left>>right;
if(left!=-1){
Node* left_data= new Node(left);
root->left=left_data;
insert(left_data);
}
if(right!=-1){
Node* right_data= new Node(right);
root->right= right_data;
insert(right_data);
}
return;
}
};
void traversal(Node* node){
if(node==NULL){
return;
}
cout<<node->data<<" ";
traversal(node->left);
traversal(node->right);
}
int main()
{
Binary_Tree binary_tree_1(13);
Node* r_node = binary_tree_1.root;
binary_tree_1.insert(r_node);
traversal(r_node);
return 0;
}