some programs
This commit is contained in:
@@ -0,0 +1,64 @@
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <cassert>
|
||||
|
||||
using namespace std;
|
||||
|
||||
class BinaryTree {
|
||||
public:
|
||||
struct Node {
|
||||
int left = -1, right = -1, father = -1;
|
||||
};
|
||||
|
||||
vector<Node> nodes;
|
||||
|
||||
int depth = -1, breadth = -1;
|
||||
|
||||
void connect(int father, int son) {
|
||||
assert(father < nodes.size() && son < nodes.size());
|
||||
if (nodes[son].father != -1 && nodes[father].father == -1) { swap(father, son); }
|
||||
nodes[son].father = father;
|
||||
if (nodes[father].left == -1) {
|
||||
nodes[father].left = son;
|
||||
} else {
|
||||
nodes[father].right = son;
|
||||
}
|
||||
}
|
||||
|
||||
void dfs() {
|
||||
depth = -1, breadth = -1;
|
||||
vector<int> depths_map;
|
||||
dfs(0, 0, depths_map);
|
||||
for (const int d : depths_map) {
|
||||
breadth = max(breadth, d);
|
||||
}
|
||||
}
|
||||
|
||||
void dfs(int node_idx, int _depth, vector<int>& depths_map) {
|
||||
depth = max(depth, _depth);
|
||||
if (_depth >= depths_map.size()) {
|
||||
depths_map.push_back(0);
|
||||
}
|
||||
depths_map[_depth]++;
|
||||
if (nodes[node_idx].left != -1) {
|
||||
dfs(nodes[node_idx].left, _depth + 1, depths_map);
|
||||
}
|
||||
if (nodes[node_idx].right != -1) {
|
||||
dfs(nodes[node_idx].right, _depth + 1, depths_map);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
BinaryTree tree;
|
||||
int n;
|
||||
|
||||
int main() {
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
cin >> n;
|
||||
tree.nodes.resize(n);
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user