From 1f701429ba9714d65662178ee15e96f50c86d292 Mon Sep 17 00:00:00 2001 From: name Date: Thu, 2 Nov 2023 07:20:20 +0800 Subject: [PATCH 1/2] init --- algorithm/tree_algorithms.cpp | 79 +++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 algorithm/tree_algorithms.cpp diff --git a/algorithm/tree_algorithms.cpp b/algorithm/tree_algorithms.cpp new file mode 100644 index 0000000..2683257 --- /dev/null +++ b/algorithm/tree_algorithms.cpp @@ -0,0 +1,79 @@ +#include +#include +#include + +template +struct node { +public: + node():left(nullptr), right(nullptr) {} + T value; + node * left; + node * right; +} + +void DLR(node * root) { + std::stack stack; + stack.push(root); + + node * top; + while(!stack.empty()) { + top = stack.top(); + std::cout << top->value << std::endl; + stack.pop() + if (top->right != nullptr) stack.push(top->right); + if (top->left != nullptr) stack.push(top->left); + } + return; +} + +void LDR(node * root) { + std::stack stack; + while(root != nullptr || !stack.empty()) { + while(root != nullptr) { + stack.push(root); + root = root->left; + } + if(!stack.empty()){ + root = stack.top(); + std::cout << root->val << std::endl; + stack.pop(); + root = root->right; + } + } + return; +} + +void LRD(node * root) { + if (root == nullptr) return; + std::stack stack; + stack.push(root); + + node * last_pop = nullptr; + while(!stack.empty()) { + while(root->left != nullptr) { + root = root->left; + stack.push(root); + } + while(!stack.empty()) { + if (root->right == nullptr || root->right == last_pop) { + std::cout << root->value << std::endl; + stack.pop(); + last_pop = root; + root = stack.top(); + } else { + root = root->right; + stack.push(right); + break; + } + } + } + return; +} + + +void topological_sorting() { + +} + +int main() { +} From eb7c5d44e6f0fc26281db85ca261052ee86b0324 Mon Sep 17 00:00:00 2001 From: name Date: Fri, 3 Nov 2023 01:44:48 +0800 Subject: [PATCH 2/2] update --- algorithm/graph_algorithm.cpp | 48 ++++++++++++++++++++++++++ algorithm/tree_algorithms.cpp | 64 ++++++++++++++++++++++++----------- 2 files changed, 93 insertions(+), 19 deletions(-) create mode 100644 algorithm/graph_algorithm.cpp diff --git a/algorithm/graph_algorithm.cpp b/algorithm/graph_algorithm.cpp new file mode 100644 index 0000000..6630fd2 --- /dev/null +++ b/algorithm/graph_algorithm.cpp @@ -0,0 +1,48 @@ +#include +#include +#include +#include + +template +struct graph { +public: + graph(int v_num):v(v_num) { + edge = std::vector>(v_num); + indegree = std::vector(v_num, 0); + } + + void addEdge(int from, int to) { + edge[in].push_back(to); + indegree[to]++; + } + + int v; + vector indegree; + std::vector> edge; + +} + +template +void topological_sorting(graph g) { + std::queue q; + + for (int i = 0; i < g.indegree.size(); ++i ) { + if (g.indegree[i] == 0) { + q.push[i]; + } + } + + while(!q.empty()) { + int v = q.top() + std::cout << v << std::endl; + q.pop(); + for (auto i = g.edge[v].begin(); i != g.edge[v].end(), ++i) { + g.indegree[*i]--; + if (g.indegree[*i] == 0) q.push(*i); + } + } + return; +} + +int main() { +} diff --git a/algorithm/tree_algorithms.cpp b/algorithm/tree_algorithms.cpp index 2683257..d9dac5c 100644 --- a/algorithm/tree_algorithms.cpp +++ b/algorithm/tree_algorithms.cpp @@ -9,25 +9,27 @@ struct node { T value; node * left; node * right; -} +}; -void DLR(node * root) { - std::stack stack; +template +void DLR(node * root) { + std::stack *> stack; stack.push(root); - node * top; + node * top; while(!stack.empty()) { top = stack.top(); std::cout << top->value << std::endl; - stack.pop() + stack.pop(); if (top->right != nullptr) stack.push(top->right); if (top->left != nullptr) stack.push(top->left); } return; -} +}; -void LDR(node * root) { - std::stack stack; +template +void LDR(node * root) { + std::stack *> stack; while(root != nullptr || !stack.empty()) { while(root != nullptr) { stack.push(root); @@ -35,20 +37,21 @@ void LDR(node * root) { } if(!stack.empty()){ root = stack.top(); - std::cout << root->val << std::endl; + std::cout << root->value << std::endl; stack.pop(); root = root->right; } } return; -} +}; -void LRD(node * root) { +template +void LRD(node * root) { if (root == nullptr) return; - std::stack stack; + std::stack *> stack; stack.push(root); - node * last_pop = nullptr; + node * last_pop = nullptr; while(!stack.empty()) { while(root->left != nullptr) { root = root->left; @@ -59,21 +62,44 @@ void LRD(node * root) { std::cout << root->value << std::endl; stack.pop(); last_pop = root; - root = stack.top(); + if (!stack.empty()){ + root = stack.top(); + } } else { root = root->right; - stack.push(right); + stack.push(root); break; } } } return; -} +}; +/* + 1 + / \ + 2 3 + \ + 4 + / + 5 +*/ -void topological_sorting() { +int main() { + node * root = new node(); + root->value = 1; + root->left = new node(); + root->left->value = 2; + root->right = new node(); + root->right->value = 3; + root->left->right = new node(); + root->left->right->value = 4; + root->left->right->left = new node(); + root->left->right->left->value = 5; -} + DLR(root); + LDR(root); + LRD(root); -int main() { + return 0; }