-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWeek6_1.cpp
More file actions
81 lines (69 loc) · 1.4 KB
/
Copy pathWeek6_1.cpp
File metadata and controls
81 lines (69 loc) · 1.4 KB
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/*
ทำ Binary Tree
Using recursive to insert
*/
#include <iostream>
using namespace std;
class BTree
{
private:
int data;
BTree *left;
BTree *right;
public:
BTree()
{
this->data = 0;
this->left = NULL;
this->right = NULL;
}
BTree(int data)
{
this->data = data;
left = right = NULL;
}
BTree *insert(BTree *root, int data)
{
if (!root)
{
return new BTree(data);
}
if (data > root->data)
{
root->right = insert(root->right, data);
}
else
{
root->left = insert(root->left, data);
}
return root;
}
void printTree(BTree *root, int space)
{
if (root == NULL)
return;
space += 10;
printTree(root->left, space);
cout << endl;
for (int i = 10; i < space; i++)
cout << " ";
cout << root->data;
printTree(root->right, space);
}
};
main()
{
int n;
cout << "Enter amount of element" << endl;
cin >> n;
cout << "Enter data:" << endl;
int* num = new int[n];
for(int i=0;i<n;i++)
cin >> num[i];
BTree tree,*root = NULL;
root = tree.insert(root,num[0]);
for(int i=1;i<n;i++)
tree.insert(root,num[i]);
cout << "Binary Tree:" << endl;
tree.printTree(root,0);
}