summaryrefslogtreecommitdiffstats
path: root/02-data_structures/04-binary_search_trees/01-tree_orders
diff options
context:
space:
mode:
authorJérémy Zurcher <jeremy@asynk.ch>2016-11-13 22:29:48 +0100
committerJérémy Zurcher <jeremy@asynk.ch>2016-11-13 22:29:48 +0100
commitf14c9988786b578c115ba1f8a93d61f51d013e2f (patch)
tree73942a93d7f6ad87aa87aff72c428247db9a3494 /02-data_structures/04-binary_search_trees/01-tree_orders
parentb524eca827b182b0a51952557e36c945c779f848 (diff)
downloadcoursera-f14c9988786b578c115ba1f8a93d61f51d013e2f.zip
coursera-f14c9988786b578c115ba1f8a93d61f51d013e2f.tar.gz
Algorithms : add 02-data_structures 04-binary_search_trees
Diffstat (limited to '02-data_structures/04-binary_search_trees/01-tree_orders')
-rw-r--r--02-data_structures/04-binary_search_trees/01-tree_orders/tests/016
-rw-r--r--02-data_structures/04-binary_search_trees/01-tree_orders/tests/01.a3
-rw-r--r--02-data_structures/04-binary_search_trees/01-tree_orders/tests/0211
-rw-r--r--02-data_structures/04-binary_search_trees/01-tree_orders/tests/02.a3
-rw-r--r--02-data_structures/04-binary_search_trees/01-tree_orders/tree-orders.cpp71
5 files changed, 94 insertions, 0 deletions
diff --git a/02-data_structures/04-binary_search_trees/01-tree_orders/tests/01 b/02-data_structures/04-binary_search_trees/01-tree_orders/tests/01
new file mode 100644
index 0000000..69028b1
--- /dev/null
+++ b/02-data_structures/04-binary_search_trees/01-tree_orders/tests/01
@@ -0,0 +1,6 @@
+5
+4 1 2
+2 3 4
+5 -1 -1
+1 -1 -1
+3 -1 -1
diff --git a/02-data_structures/04-binary_search_trees/01-tree_orders/tests/01.a b/02-data_structures/04-binary_search_trees/01-tree_orders/tests/01.a
new file mode 100644
index 0000000..92d3d39
--- /dev/null
+++ b/02-data_structures/04-binary_search_trees/01-tree_orders/tests/01.a
@@ -0,0 +1,3 @@
+1 2 3 4 5
+4 2 1 3 5
+1 3 2 5 4
diff --git a/02-data_structures/04-binary_search_trees/01-tree_orders/tests/02 b/02-data_structures/04-binary_search_trees/01-tree_orders/tests/02
new file mode 100644
index 0000000..995d50a
--- /dev/null
+++ b/02-data_structures/04-binary_search_trees/01-tree_orders/tests/02
@@ -0,0 +1,11 @@
+10
+0 7 2
+10 -1 -1
+20 -1 6
+30 8 9
+40 3 -1
+50 -1 -1
+60 1 -1
+70 5 4
+80 -1 -1
+90 -1 -1
diff --git a/02-data_structures/04-binary_search_trees/01-tree_orders/tests/02.a b/02-data_structures/04-binary_search_trees/01-tree_orders/tests/02.a
new file mode 100644
index 0000000..98400cf
--- /dev/null
+++ b/02-data_structures/04-binary_search_trees/01-tree_orders/tests/02.a
@@ -0,0 +1,3 @@
+50 70 80 30 90 40 0 20 10 60
+0 70 50 40 30 80 90 20 60 10
+50 80 90 30 40 70 10 60 20 0
diff --git a/02-data_structures/04-binary_search_trees/01-tree_orders/tree-orders.cpp b/02-data_structures/04-binary_search_trees/01-tree_orders/tree-orders.cpp
new file mode 100644
index 0000000..5ee679c
--- /dev/null
+++ b/02-data_structures/04-binary_search_trees/01-tree_orders/tree-orders.cpp
@@ -0,0 +1,71 @@
+#include <iostream>
+#include <vector>
+#include <algorithm>
+
+using std::vector;
+using std::ios_base;
+using std::cin;
+using std::cout;
+
+class TreeOrders {
+ int n;
+ vector <int> key;
+ vector <int> left;
+ vector <int> right;
+
+public:
+ void read() {
+ cin >> n;
+ key.resize(n);
+ left.resize(n);
+ right.resize(n);
+ for (int i = 0; i < n; i++) {
+ cin >> key[i] >> left[i] >> right[i];
+ }
+ }
+
+
+ vector <int> in_order() {
+ vector<int> result;
+ // Finish the implementation
+ // You may need to add a new recursive method to do that
+
+ return result;
+ }
+
+ vector <int> pre_order() {
+ vector<int> result;
+ // Finish the implementation
+ // You may need to add a new recursive method to do that
+
+ return result;
+ }
+
+ vector <int> post_order() {
+ vector<int> result;
+ // Finish the implementation
+ // You may need to add a new recursive method to do that
+
+ return result;
+ }
+};
+
+void print(vector <int> a) {
+ for (size_t i = 0; i < a.size(); i++) {
+ if (i > 0) {
+ cout << ' ';
+ }
+ cout << a[i];
+ }
+ cout << '\n';
+}
+
+int main() {
+ ios_base::sync_with_stdio(0);
+ TreeOrders t;
+ t.read();
+ print(t.in_order());
+ print(t.pre_order());
+ print(t.post_order());
+ return 0;
+}