snippets 0.1.0
Loading...
Searching...
No Matches
binary_tree.hpp
Go to the documentation of this file.
1#ifndef BINARY_TREE_H
2#define BINARY_TREE_H
3
4#include <functional>
5#include <vector>
6#include <optional>
7#include <queue>
8#include <cassert>
9#include <unordered_map>
10
11namespace hsc_snippets {
12 struct TreeNode {
13 int val;
16
17 TreeNode() : val(0), left(nullptr), right(nullptr) {}
18
19 TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
20
22 };
23
32 static TreeNode *new_binary_tree(const std::vector<std::optional<int>> v) {
33 if (v.empty()) {
34 return nullptr;
35 }
36
37 const auto n = v.size();
38
39 assert(v[0].has_value());
40
41 auto root = new TreeNode(v[0].value());
42 auto q = std::queue<TreeNode *>{};
43 q.push(root);
44 int i = 1;
45
46 while (i < n) {
47 auto node = q.front();
48 q.pop();
49 if (v[i].has_value()) {
50 node->left = new TreeNode(v[i].value());
51 q.push(node->left);
52 }
53 i += 1;
54 if (i >= n) {
55 break;
56 }
57 if (v[i].has_value()) {
58 node->right = new TreeNode(v[i].value());
59 q.push(node->right);
60 }
61 i += 1;
62 }
63
64 return root;
65 }
66
74 static std::vector<std::optional<int>> binary_tree_to_vector(TreeNode *root) {
75 if (!root) {
76 return {};
77 }
78
79 std::vector<std::optional<int>> result;
80 std::queue<TreeNode *> q;
81 q.push(root);
82
83 while (!q.empty()) {
84 TreeNode *node = q.front();
85 q.pop();
86
87 if (node) {
88 result.emplace_back(node->val);
89 q.push(node->left);
90 q.push(node->right);
91 } else {
92 result.emplace_back(std::nullopt);
93 }
94 }
95
96 // Remove trailing nullopts for a more compact representation
97 while (!result.empty() && !result.back().has_value()) {
98 result.pop_back();
99 }
100
101 return result;
102 }
103
111 static void delete_binary_tree(TreeNode *root) {
112 if (root == nullptr)
113 return;
116 delete root;
117 }
118
126 static int get_binary_tree_depth(TreeNode *root) {
127 if (root == nullptr)
128 return 0;
129 int depth = 1;
130 depth = std::max(depth, get_binary_tree_depth(root->left) + 1);
131 depth = std::max(depth, get_binary_tree_depth(root->right) + 1);
132 return depth;
133 }
134
140 static void inorder(TreeNode *root, std::function<void(int)> func) {
141 if (root == nullptr)
142 return;
143 inorder(root->left, func);
144 func(root->val);
145 inorder(root->right, func);
146 }
147
153 static void preorder(TreeNode *root, std::function<void(int)> func) {
154 if (root == nullptr)
155 return;
156 func(root->val);
157 preorder(root->left, func);
158 preorder(root->right, func);
159 }
160
166 static void postorder(TreeNode *root, std::function<void(int)> func) {
167 if (root == nullptr)
168 return;
169
170 postorder(root->left, func);
171 postorder(root->right, func);
172 func(root->val);
173 }
174
185 static std::unordered_map<int, std::vector<int>> binary_tree_to_adjacency_list(TreeNode *root) {
186 auto adjacencyList = std::unordered_map<int, std::vector<int>>{};
187 if (root == nullptr) {
188 return adjacencyList;
189 }
190 auto q = std::queue<TreeNode *>{};
191 q.push(root);
192 while (!q.empty()) {
193 auto node = q.front();
194 q.pop();
195 if (node->left != nullptr) {
196 adjacencyList[node->left->val].push_back(node->val);
197 adjacencyList[node->val].push_back(node->left->val);
198 q.push(node->left);
199 }
200 if (node->right != nullptr) {
201 adjacencyList[node->right->val].push_back(node->val);
202 adjacencyList[node->val].push_back(node->right->val);
203 q.push(node->right);
204 }
205 }
206
207 return adjacencyList;
208 }
209
210}
211
212#endif // BINARY_TREE_H
Definition big_integer.hpp:14
static void delete_binary_tree(TreeNode *root)
Definition binary_tree.hpp:111
static TreeNode * new_binary_tree(const std::vector< std::optional< int > > v)
Definition binary_tree.hpp:32
static int get_binary_tree_depth(TreeNode *root)
Definition binary_tree.hpp:126
static void preorder(TreeNode *root, std::function< void(int)> func)
Definition binary_tree.hpp:153
static void postorder(TreeNode *root, std::function< void(int)> func)
Definition binary_tree.hpp:166
static void inorder(TreeNode *root, std::function< void(int)> func)
Definition binary_tree.hpp:140
static std::unordered_map< int, std::vector< int > > binary_tree_to_adjacency_list(TreeNode *root)
Definition binary_tree.hpp:185
static std::vector< std::optional< int > > binary_tree_to_vector(TreeNode *root)
Definition binary_tree.hpp:74
Definition binary_tree.hpp:12
TreeNode * right
Definition binary_tree.hpp:15
TreeNode()
Definition binary_tree.hpp:17
TreeNode * left
Definition binary_tree.hpp:14
TreeNode(int x)
Definition binary_tree.hpp:19
int val
Definition binary_tree.hpp:13
TreeNode(int x, TreeNode *left, TreeNode *right)
Definition binary_tree.hpp:21