Skip to content
Open

Tree #10

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions algorithm/graph_algorithm.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#include <iostream>
#include <list>
#include <vector>
#include <queue>

template<class T>
struct graph {
public:
graph(int v_num):v(v_num) {
edge = std::vector<std::list<T>>(v_num);
indegree = std::vector<int>(v_num, 0);
}

void addEdge(int from, int to) {
edge[in].push_back(to);
indegree[to]++;
}

int v;
vector<int> indegree;
std::vector<list<T>> edge;

}

template<class T>
void topological_sorting(graph<T> g) {
std::queue<int> 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() {
}
105 changes: 105 additions & 0 deletions algorithm/tree_algorithms.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
#include <iostream>
#include <vector>
#include <stack>

template<class T>
struct node {
public:
node():left(nullptr), right(nullptr) {}
T value;
node * left;
node * right;
};

template<class T>
void DLR(node<T> * root) {
std::stack<node<T> *> stack;
stack.push(root);

node<T> * 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;
};

template<class T>
void LDR(node<T> * root) {
std::stack<node<T> *> stack;
while(root != nullptr || !stack.empty()) {
while(root != nullptr) {
stack.push(root);
root = root->left;
}
if(!stack.empty()){
root = stack.top();
std::cout << root->value << std::endl;
stack.pop();
root = root->right;
}
}
return;
};

template<class T>
void LRD(node<T> * root) {
if (root == nullptr) return;
std::stack<node<T> *> stack;
stack.push(root);

node<T> * 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;
if (!stack.empty()){
root = stack.top();
}
} else {
root = root->right;
stack.push(root);
break;
}
}
}
return;
};

/*
1
/ \
2 3
\
4
/
5
*/

int main() {
node<int> * root = new node<int>();
root->value = 1;
root->left = new node<int>();
root->left->value = 2;
root->right = new node<int>();
root->right->value = 3;
root->left->right = new node<int>();
root->left->right->value = 4;
root->left->right->left = new node<int>();
root->left->right->left->value = 5;

DLR(root);
LDR(root);
LRD(root);

return 0;
}