-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBinaryTree.cpp
155 lines (148 loc) · 4.69 KB
/
BinaryTree.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#include <iostream>
using namespace std;
const int MAXN = 1e5;
class Node
{
public:
int data;
Node *left_child;
Node *right_child;
Node()
{
this->data = data;
this->left_child = NULL;
this->right_child = NULL;
}
Node(int data)
{
this->data = data;
this->left_child = NULL;
this->right_child = NULL;
}
};
class BinaryTree
{
private:
Node *binary_tree;
public:
BinaryTree ()
{
this->binary_tree = NULL;
}
BinaryTree (Node *bbinary_tree)
{
this->binary_tree = bbinary_tree;
}
void print_preorder (Node *bbinary_tree)
{
if (bbinary_tree)
{
printf("%d ", bbinary_tree->data);
print_preorder(bbinary_tree->left_child);
print_preorder(bbinary_tree->right_child);
}
}
void print_inorder (Node *bbinary_tree)
{
if (bbinary_tree)
{
print_inorder(bbinary_tree->left_child);
printf("%d ", bbinary_tree->data);
print_inorder(bbinary_tree->right_child);
}
}
void print_postorder (Node *bbinary_tree)
{
if (bbinary_tree)
{
print_postorder(bbinary_tree->left_child);
print_postorder(bbinary_tree->right_child);
printf("%d ", bbinary_tree->data);
}
}
int height (Node* bbinary_tree)
{ // Recursive function to calculate the height of a given binary tree
// base case: empty tree has a height of 0
if (bbinary_tree == nullptr) {
return 0;
}
// recur for the left and right subtree and consider maximum depth
return 1 + max(height(bbinary_tree->left_child), height(bbinary_tree->right_child));
}
void print_levelorder (Node *bbinary_tree)
{
/* Function to line by line print level order traversal a tree*/
int h = height(bbinary_tree);
int i;
for (i = 1; i <= h; i++)
{
printGivenLevel(bbinary_tree, i);
//printf("\n");
}
}
void printGivenLevel (Node *bbinary_tree, int level)
{
/* Print Nodes at a given level */
if (bbinary_tree == NULL)
return;
if (level == 1)
printf("%d ", bbinary_tree->data);
else if (level > 1)
{
printGivenLevel(bbinary_tree->left_child, level - 1);
printGivenLevel(bbinary_tree->right_child, level - 1);
}
}
int numberofNodes (Node *bbinary_tree){
if (!bbinary_tree) return 0;
return numberofNodes(bbinary_tree->left_child)+numberofNodes(bbinary_tree->right_child)+1;
}
int numberofleafs (Node *bbinary_tree){
if(!bbinary_tree) return 0;
if (!bbinary_tree->left_child && !bbinary_tree->right_child) return 1;
return numberofleafs (bbinary_tree->left_child)+numberofleafs (bbinary_tree->right_child);
}
int numberofnonleafs (Node *bbinary_tree){
if((!bbinary_tree) || (!bbinary_tree->left_child && !bbinary_tree->right_child) )return 0;
return numberofnonleafs (bbinary_tree->left_child)+numberofnonleafs (bbinary_tree->right_child)+1;
}
int numberoffullNode (Node *bbinary_tree){
if(!bbinary_tree) return 0;
if(!bbinary_tree->left_child && !bbinary_tree->right_child ) return 0;
return numberoffullNode(bbinary_tree->left_child)+numberoffullNode(bbinary_tree->right_child)+ (bbinary_tree->left_child && bbinary_tree->right_child)?1:0;
}
Node* BuildTreePreIn (int in[], int pre[], int inStrt, int inEnd)
{
static int preIndex = 0;
if (inStrt > inEnd) return NULL;
Node *tNode = new Node(pre[preIndex++]);
if (inStrt == inEnd) return tNode;
int inIndex = -1;
for (int i = inStrt; i <= inEnd; i++) if (in[i] == tNode->data) inIndex = i;
tNode->left_child = BuildTreePreIn(in, pre, inStrt, inIndex - 1);
tNode->right_child = BuildTreePreIn(in, pre, inIndex + 1, inEnd);
return tNode;
}
void BuildTree (int in[], int pre[], int inStrt, int inEnd)
{
this->binary_tree = BuildTreePreIn(in,pre, inStrt, inEnd);
}
Node* Returntreehead ()
{
return binary_tree;
}
};
int main()
{
int n, in[MAXN],pre[MAXN];
cin>>n;
for(int i=0; i<n; i++) scanf("%d",&pre[i]);
for(int j=0; j<n; j++) scanf("%d",&in[j]);
int len = n;
BinaryTree t1 = BinaryTree();
t1.BuildTree(in, pre, 0, len - 1);
t1.print_postorder(t1.Returntreehead());
printf("\n");
t1.print_levelorder(t1.Returntreehead());
printf("\n");
}