-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharr_bt_levelorder.cpp
79 lines (64 loc) · 1.74 KB
/
arr_bt_levelorder.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
// Binary tree from array, via level order readings
//
// Created by @altanai on 31/03/21.
//
#include<iostream>
using namespace std;
struct node{
int data;
struct node* left;
struct node* right;
node(int val){
data = val;
left = NULL;
right = NULL;
}
};
node* insertarr_bt(int arr[],node* root, int i, int n){
if(i<n){
root = new node(arr[i]);
root->left = insertarr_bt(arr, root->left, 2*i+1, n);
root->right = insertarr_bt(arr, root->right, 2*i+2, n);
}
return root;
}
void inorder(struct node* root){
if(!root) return;
inorder(root->left);
cout<<root->data<<" ";
inorder(root->right);
}
// ----------------------- utility function
void printBT(const std::string& prefix, const struct node* root, bool isLeft){
if( root != nullptr ){
std::cout << prefix;
std::cout << (isLeft ? "├──" : "└──" );
std::cout << root->data << std::endl;
printBT( prefix + (isLeft ? "│ " : " "), root->left, true);
printBT( prefix + (isLeft ? "│ " : " "), root->right, false);
}
}
void printBT(const struct node* root){
printBT("", root, false);
}
int main(){
// int arr[] = { 5,1,7 };
int arr[] = { 1,2,3,4,5,-1,6 }; // assume -1 is null
// int arr[] = { 1, 2, 3, 4, 5, 6, 6, 6, 6 };
int n = sizeof(arr)/sizeof(arr[0]);
struct node* root = insertarr_bt(arr, root ,0, n);
printBT(root);
inorder(root);
return 0;
}
// g++ arr_bt.cpp -o arr_bt.out
// ./arr_bt.out
// output for array [ 1,2,3,4,5,-1,6]
// └──1
// ├──2
// │ ├──4
// │ └──5
// └──3
// ├──-1
// └──6
// 4 2 5 1 -1 3 6 %