-
Notifications
You must be signed in to change notification settings - Fork 0
/
4_2.cpp
54 lines (49 loc) · 1.1 KB
/
4_2.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
#include <iostream>
template <class T>
class Node
{
public:
Node(T item);
Node* left_child;
Node* right_child;
T data;
};
template <class T>
Node<T>::Node(T item) : left_child(nullptr), right_child(nullptr), data(item) {};
void buildTree(int arr[], int lo, int hi, Node<int> *root)
{
if (lo > hi)
return;
int mid = ((hi-lo)/2)+lo;
root->data = arr[mid];
if (lo <= mid-1)
{
root->left_child = new Node<int>(-1);
buildTree(arr,lo,mid-1,root->left_child);
}
if (mid+1 <= hi)
{
root->right_child = new Node<int>(-1);
buildTree(arr,mid+1,hi,root->right_child);
}
}
void printTree(Node<int> *root,int tabs)
{
if (root == nullptr)
return;
for (int i = 0; i < tabs; i++)
{
std::cout << " ";
}
std::cout << root->data << std::endl;
printTree(root->left_child,tabs-1);
printTree(root->right_child,tabs+1);
}
int main(int argc, char* argv[])
{
int arr[] = {0,1,2,3,4,5,6,7};
Node<int> *root = new Node<int>(-1);
buildTree(arr,0,7,root);
printTree(root,4);
return 0;
}