diff options
author | zedarider <ymherklotz@gmail.com> | 2016-11-27 13:14:43 +0000 |
---|---|---|
committer | zedarider <ymherklotz@gmail.com> | 2016-11-27 13:14:43 +0000 |
commit | 5973d6a630ccf93e580ed01bd347c2ffa3fb7b39 (patch) | |
tree | 0ac4690b62107bfa241d850fa3ff4f331ebbefe9 | |
parent | 1f1936bfe813983a6fb1bfa59e2f5e2aa3d7cd40 (diff) | |
download | BinaryTree-5973d6a630ccf93e580ed01bd347c2ffa3fb7b39.tar.gz BinaryTree-5973d6a630ccf93e580ed01bd347c2ffa3fb7b39.zip |
finished the basics of the tree
-rwxr-xr-x | bin/binary_tree_test | bin | 19940 -> 28852 bytes | |||
-rw-r--r-- | include/binary_tree.hpp | 23 | ||||
-rw-r--r-- | src/main.cpp | 18 |
3 files changed, 37 insertions, 4 deletions
diff --git a/bin/binary_tree_test b/bin/binary_tree_test Binary files differindex daf75eb..8d0ef45 100755 --- a/bin/binary_tree_test +++ b/bin/binary_tree_test diff --git a/include/binary_tree.hpp b/include/binary_tree.hpp index 72977f0..1927a16 100644 --- a/include/binary_tree.hpp +++ b/include/binary_tree.hpp @@ -121,7 +121,7 @@ public: BinaryTree() : root_node(NULL), tree_height(0) {} ~BinaryTree() { std::cout << "This is the Binary Tree destructor" << std::endl; - delete root_node; + delete_nodes(root_node); } // adds an element to the tree @@ -157,6 +157,27 @@ public: } } + void delete_nodes(node_ptr node) { + if(node != NULL) { + this->delete_nodes(node->next_left); + this->delete_nodes(node->next_right); + std::cout << "deleting node: " << *node << std::endl; + delete node; + } + } + + void print_tree() { + print_tree(root_node); + } + + void print_tree(node_ptr node) { + if(node != NULL) { + print_tree(node->next_left); + std::cout << *node << std::endl; + print_tree(node->next_right); + } + } + Node<T> get_root() { return *root_node; } diff --git a/src/main.cpp b/src/main.cpp index e093a98..41909a1 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,14 +1,26 @@ #include "binary_tree.hpp" #include <iostream> +#include <cstdlib> +#include <ctime> using namespace std; int main(int argc, char* argv[]) { + srand(time(NULL)); BinaryTree<int> int_bt; - int_bt.push_back(20); - Node<int> node(int_bt.get_root()); + BinaryTree<double> double_bt; + BinaryTree<char> char_bt; + + for(unsigned int i = 0; i < 50; ++i) { + int_bt.push_back(rand() % 0xFFFFFFFF); + double_bt.push_back(double((rand() % 0xFFFFFFFF)) / double((rand() % 0xFFFFFFFF))); + char_bt.push_back(rand() % 94 + 33); + } + + int_bt.print_tree(); + double_bt.print_tree(); + char_bt.print_tree(); - cout << "root node int: " << node.get_item() << endl; return 0; } |