-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path61_Binary_Tree.cpp
More file actions
163 lines (133 loc) · 5.25 KB
/
61_Binary_Tree.cpp
File metadata and controls
163 lines (133 loc) · 5.25 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
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
156
157
158
159
160
161
162
163
// Binary Tree => Collection of nodes where each node has a left pointer, right pointer and a data element
// Height of a binary tree => Number of edges in the longest path from the root to a leaf node
// Depth of a node => Number of edges in the path from the root to the node
// Level of a node => Depth of the node + 1
// Height of a node => Number of edges in the longest path from the node to a leaf node
// Descendant of a node => All the nodes that can be reached by traversing the node
// Ancestor of a node => All the nodes that can be reached by traversing the root node
// Sibling of a node => Nodes that share the same parent
// Internal node => Node with at least one child
// Leaf node => Node with no children
// Height of a binary tree with 1 node = 0
// Height of an empty binary tree = -1
// Height of a binary tree with n nodes = n-1
// Number of nodes in a binary tree with height h = 2^(h+1) - 1
// Number of leaf nodes in a binary tree = Number of nodes with 0 children
// Number of nodes with 2 children in a binary tree = Number of internal nodes
// Number of nodes with 1 child in a binary tree = Number of internal nodes
// Number of leaf nodes in a binary tree with height h = 2^h
// Number of internal nodes in a binary tree with height h = 2^h - 1
// Number of nodes in a binary tree with n leaf nodes = 2n - 1
// Number of nodes in a binary tree with n internal nodes = 2n
// Number of nodes in a binary tree with n leaf nodes and h height = n + 2^h - 1
// Number of nodes in a binary tree with n internal nodes and h height = n + 2^h
// Maximum height of a binary tree with n nodes = n - 1 (In the case of a linear structure, i.e., every node has only one child except the leaf node)
// Minimum height of a binary tree with n nodes = floor(log2(n)) (In the case of a complete binary tree)
// Maximum number of nodes in a binary tree with height h = 2^(h+1) - 1 (This is also the total number of nodes in a perfect binary tree of height h)
// Minimum number of nodes in a binary tree with height h = h + 1 (In the case of a linear structure, i.e., every node has only one child except the leaf node)
// For a binary tree with n nodes, the minimum possible height is ceil(log2(n+1)) - This ensures the tree is as complete as possible
// For a binary tree with n nodes, the maximum possible height is n - 1, which occurs when the tree is a linear chain (each node has only one child except the last one)
// Representation of a binary tree using linked list
#include<iostream>
#include<queue>
#include<vector>
class Node {
public:
char data;
Node* left;
Node* right;
Node(int data) {
this->data = data;
this->left = nullptr;
this->right = nullptr;
}
};
void inorder(Node*& root) {
if (root == nullptr) return;
inorder(root->left);
std::cout << root->data << " ";
inorder(root->right);
}
void preorder(Node*& root) {
if (root == nullptr) return;
std::cout << root->data << " ";
preorder(root->left);
preorder(root->right);
}
void postorder(Node*& root) {
if (root == nullptr) return;
postorder(root->left);
postorder(root->right);
std::cout << root->data << " ";
}
std::vector< std::vector<char> > levelorder(Node*& root) {
std::vector< std::vector<char> > result;
if (!root) return result;
std::queue<Node*> q;
q.push(root);
while (!q.empty()) {
int levelSize = q.size();
std::vector<char> currentLevelElements;
for (int i = 0; i < levelSize; i++) {
Node* node = q.front();
q.pop();
currentLevelElements.push_back(node->data);
if (node->left) q.push(node->left);
if (node->right) q.push(node->right);
}
result.push_back(currentLevelElements);
}
return result;
}
int count(Node*& root) {
if (!root) return 0;
return count(root->left) + count(root->right) + 1;
}
int countLeafs(Node*& root) {
if (!root) return 0;
if (!root->left && !root->right) {
return 1;
}
return countLeafs(root->left) + countLeafs(root->right);
}
int countNonLeafs(Node*& root) {
if (!root) return 0;
if (!root->left && !root->right) {
return 0;
}
return countNonLeafs(root->left) + countNonLeafs(root->right) + 1;
}
int height(Node*& root) {
int x = 0, y = 0;
if (!root) return 0;
x = height(root->left);
y = height(root->right);
return x > y ? x + 1 : y + 1;
}
int main() {
Node* root = new Node('A');
root->left = new Node('B');
root->right = new Node('C');
root->left->left = new Node('D');
root->left->right = new Node('E');
root->right->left = new Node('F');
root->right->right = new Node('G');
preorder(root);
std::cout << std::endl;
inorder(root);
std::cout << std::endl;
postorder(root);
std::cout << std::endl;
std::cout << count(root) << std::endl;
std::cout << height(root) << std::endl;
std::cout << countLeafs(root) << std::endl;
std::cout << countNonLeafs(root) << std::endl;
std::vector< std::vector<char> > result = levelorder(root);
for (int i = 0; i < result.size(); i++) {
std::vector<char> level = result[i];
for (int i = 0; i < level.size(); i++) {
std::cout << level[i] << " ";
}
std::cout << std::endl;
}
}