-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTreeAlgo.cpp
More file actions
149 lines (120 loc) · 4.88 KB
/
Copy pathTreeAlgo.cpp
File metadata and controls
149 lines (120 loc) · 4.88 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
//
// Created by ppx on 2022/1/16.
//
#include <array>
#include<fmt/core.h>
#include "TreeAlgo.h"
#include <optional>
void TreeAlgo::cal_node_distance() {
// ????????????
const std::unique_ptr<TreeNode> &tree = create_tree();
auto impl = [](TreeNode *root, auto &&self) -> std::pair<int, int> {
// ??????? root ??????
if (root == nullptr) {
return {0, 0};//??????????
} else {
std::pair<int, int> left = self(root->left.get(), self);
std::pair<int, int> right = self(root->right.get(), self);
int cur_height = std::max(left.first, right.first) + 1;
int result = std::max(std::max(left.second, right.second), cur_height);
return {cur_height, result};
}
};
fmt::print("impl(tree.get(), impl) {}", impl(tree.get(), impl).second);
}
std::unique_ptr<TreeNode> TreeAlgo::create_tree() {
using std::make_unique;
std::unique_ptr<TreeNode> root = make_unique<TreeNode>(0);
root->left = make_unique<TreeNode>(1);
root->left->left = make_unique<TreeNode>(3);
root->right = make_unique<TreeNode>(2);
root->right->left = make_unique<TreeNode>(4);
root->right->right = make_unique<TreeNode>(5);
root->right->right->right = make_unique<TreeNode>(6);
return root;
}
void TreeAlgo::find_non_exist_num() {
// ?????????? ???????? 2^16 ?????? 2^15
// ???????????????д????count???
// constexpr int length = 1 << 16;
// std::array<int, length> data{};
}
void TreeAlgo::run() {
TreeAlgo::cal_node_distance();
}
void TreeAlgo::find_most_search_subtree() { // ????????????? ????????????
std::unique_ptr<TreeNode> root = TreeAlgo::create_tree();
auto fn = [](TreeNode *cur) {
auto count = [](TreeNode *subTree, auto &&count) -> int {
if (subTree != nullptr) {
int left = count(subTree->left.get(), count);
int right = count(subTree->right.get(), count);
return left + right + 1;
} else { return 0; }
};
auto search_sub_tree = [](TreeNode *data, auto &&search_sub_tree) -> bool {
if (data == nullptr)return true;
if (data->left != nullptr and (data->left->value > data->value))
return false;
if (data->right != nullptr and (data->right->value < data->value))
return false;
return search_sub_tree(data->left.get(), search_sub_tree) and
search_sub_tree(data->right.get(), search_sub_tree);
};
auto impl = [&](TreeNode *data, auto &&impl) -> int {
if (search_sub_tree(data, search_sub_tree)) {
return count(data, count);
}
if (data != nullptr) {
int left = 0, right = 0;
if (search_sub_tree(data->left.get(), search_sub_tree)) {
left = count(data->left.get(), count);
}
if (search_sub_tree(data->right.get(), search_sub_tree)) {
right = count(data->right.get(), count);
}
return std::max(left, right);
}
};
return impl(cur, impl);
};
struct BstNodeResult {
int min; // ???? ????С?? ?
int max; // ???? ?????? ?
Node *bstNodeHead;
int height;
bool isBst; //???????????? ???????????ж?
BstNodeResult(int mi, int ma, Node *bst, int he, int isb) : min(mi),
max(ma), bstNodeHead(bst), height(he), isBst(isb) {}
};
auto searchImpl = [](Node *root, auto &&searchImpl) -> std::optional<BstNodeResult> {
if (root == nullptr) {
return {};
}
std::optional<BstNodeResult> leftValue = searchImpl(root->left, searchImpl);
std::optional<BstNodeResult> rightValue = searchImpl(root->right, searchImpl);
BstNodeResult bstNodeResult = BstNodeResult(INT_MAX, INT_MIN, nullptr, 1, false);
if (leftValue) {
auto &left = leftValue.value();
bstNodeResult.min = std::min(left.min, bstNodeResult.min);
bstNodeResult.max = std::max(left.max, bstNodeResult.max);
}
if (rightValue) {
auto &right = rightValue.value();
bstNodeResult.min = std::min(right.min, bstNodeResult.min);
bstNodeResult.max = std::max(right.max, bstNodeResult.max);
}
if (leftValue and rightValue) {
auto &left = leftValue.value();
auto &right = rightValue.value();
if (left.isBst and right.isBst) {
if (left.max <= right.min and root->value >= left.max
and root->value <= right.min) {
bstNodeResult.bstNodeHead = root; // ?????????????????????????
}
} else {
}
}
// return;
};
}