aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorzedarider <ymherklotz@gmail.com>2016-12-03 14:59:34 +0000
committerzedarider <ymherklotz@gmail.com>2016-12-03 14:59:34 +0000
commit4add89d72f328e511882466d2bbce4c1f48616b8 (patch)
tree266dbb4e1598aa63437b3a80d7bd445c324f557a
parentaaf3a8e6af453b0db1ed7d5faef6367d7a613b3a (diff)
downloadBinaryTree-4add89d72f328e511882466d2bbce4c1f48616b8.tar.gz
BinaryTree-4add89d72f328e511882466d2bbce4c1f48616b8.zip
fixing indentation
-rwxr-xr-xbin/binary_tree_testbin28852 -> 28884 bytes
-rw-r--r--include/binary_tree.hpp253
-rw-r--r--src/main.cpp28
3 files changed, 141 insertions, 140 deletions
diff --git a/bin/binary_tree_test b/bin/binary_tree_test
index 242bd7c..a6957c1 100755
--- a/bin/binary_tree_test
+++ b/bin/binary_tree_test
Binary files differ
diff --git a/include/binary_tree.hpp b/include/binary_tree.hpp
index e0428f1..6ffad81 100644
--- a/include/binary_tree.hpp
+++ b/include/binary_tree.hpp
@@ -13,143 +13,144 @@ class Node;
// class that descibes a node in the tree
template<typename T>
class Node {
- friend BinaryTree<T>;
+ friend BinaryTree<T>;
public:
- // create an empty node with no next and only adding the item
- Node(const T& it, const T& height) : item(it), next_left(NULL),
- next_right(NULL), height(height), depth(0), bf(0) {}
-
- // create a new node with next nodes
- Node(const T& it, Node<T>* n_left, Node<T> n_right)
- : item(it), next_left(n_left), next_right(n_right) {}
-
- // nothing needed in destructor
- ~Node() {
- std::cout << "This is the Node destructor" << std::endl;
- }
-
- // overloading the operators for ease of use
- friend bool operator==(const Node<T>& n1, const Node<T>& n2) {
- return n1.item == n2.item;
- }
-
- friend bool operator!=(const Node<T>& n1, const Node<T>& n2) {
- return !(n1 == n2);
- }
-
- friend bool operator<(const Node<T>& n1, const Node<T>& n2) {
- return n1.item < n2.item;
- }
-
- friend bool operator>(const Node<T>& n1, const Node<T>& n2) {
- return n1.item > n2.item;
- }
-
- friend bool operator<=(const Node<T>& n1, const Node<T>& n2) {
- return !(n1.item > n2.item);
- }
-
- friend bool operator>=(const Node<T>& n1, const Node<T>& n2) {
- return !(n1.item < n2.item);
- }
-
- friend std::ostream& operator<<(std::ostream& out, const Node<T>& n) {
- out << n.item;
- return out;
- }
+ // create an empty node with no next and only adding the item
+ Node(const T& it, const T& height) : item(it), next_left(NULL),
+ next_right(NULL), height(height), depth(0), bf(0) {
+ }
+
+ // create a new node with next nodes
+ Node(const T& it, Node<T> *n_left, Node<T> n_right)
+ : item(it), next_left(n_left), next_right(n_right) {
+ }
+
+ // nothing needed in destructor
+ ~Node() {
+ std::cout << "This is the Node destructor" << std::endl;
+ }
+
+ // overloading the operators for ease of use
+ friend bool operator==(const Node<T>& n1, const Node<T>& n2) {
+ return n1.item == n2.item;
+ }
+
+ friend bool operator!=(const Node<T>& n1, const Node<T>& n2) {
+ return !(n1 == n2);
+ }
+
+ friend bool operator<(const Node<T>& n1, const Node<T>& n2) {
+ return n1.item < n2.item;
+ }
+
+ friend bool operator>(const Node<T>& n1, const Node<T>& n2) {
+ return n1.item > n2.item;
+ }
+
+ friend bool operator<=(const Node<T>& n1, const Node<T>& n2) {
+ return !(n1.item > n2.item);
+ }
+
+ friend bool operator>=(const Node<T>& n1, const Node<T>& n2) {
+ return !(n1.item < n2.item);
+ }
+
+ friend std::ostream& operator<<(std::ostream& out, const Node<T>& n) {
+ out << n.item;
+ return out;
+ }
protected:
private:
- // item stored in the tree
- T item;
- // pointers to the next elements in the tree
- Node<T> *next_left, *next_right;
-
- // distance from node to leaf
- unsigned int height;
- // distance from node to root
- unsigned int depth;
- // height of the right subtree - height of the left subtree
- signed int bf;
+ // item stored in the tree
+ T item;
+ // pointers to the next elements in the tree
+ Node<T> *next_left, *next_right;
+
+ // distance from node to leaf
+ unsigned int height;
+ // distance from node to root
+ unsigned int depth;
+ // height of the right subtree - height of the left subtree
+ signed int bf;
};
// tree of all the nodes
template<typename T>
class BinaryTree {
- typedef Node<T>* node_ptr;
+ typedef Node<T> *node_ptr;
public:
- // empty tree node which will then be created when adding elements
- BinaryTree() : root_node(NULL), tree_height(0) {}
- ~BinaryTree() {
- std::cout << "This is the Binary Tree destructor" << std::endl;
- delete_nodes(root_node);
- }
-
- // adds an element to the tree
- void push_back(const T& element) {
- // create new node with element
- node_ptr new_node = new Node<T>(element, tree_height);
-
- if(root_node == NULL) {
- root_node = new_node;
- } else {
- // make current node
- node_ptr curr_node = root_node;
- bool found = false;
-
- // go to node that has to be added
- while(!found) {
- if(*new_node < *curr_node) {
- if(curr_node->next_left != NULL) {
- curr_node = curr_node->next_left;
- } else {
- found = true;
- curr_node->next_left = new_node;
- }
- } else {
- if(curr_node->next_right != NULL) {
- curr_node = curr_node->next_right;
- } else {
- found = true;
- curr_node->next_right = new_node;
- }
- }
- }
- }
- }
-
- void set_height_depth(node_ptr node) {
- if(node != NULL) {
- set_height_depth(node->next_left);
- set_height_depth(node->next_right);
- }
- }
-
- 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);
- }
- }
+ // empty tree node which will then be created when adding elements
+ BinaryTree() : root_node(NULL), tree_height(0) {
+ }
+ ~BinaryTree() {
+ std::cout << "This is the Binary Tree destructor" << std::endl;
+ delete_nodes(root_node);
+ }
+
+ // adds an element to the tree
+ void push_back(const T& element) {
+ // create new node with element
+ node_ptr new_node = new Node<T>(element, tree_height);
+
+ if(root_node == NULL) {
+ root_node = new_node;
+ } else {
+ // make current node
+ node_ptr curr_node = root_node;
+ bool found = false;
+
+ // go to node that has to be added
+ while(!found) {
+ if(*new_node < *curr_node) {
+ if(curr_node->next_left != NULL) {
+ curr_node = curr_node->next_left;
+ } else {
+ found = true;
+ curr_node->next_left = new_node;
+ }
+ } else {
+ if(curr_node->next_right != NULL) {
+ curr_node = curr_node->next_right;
+ } else {
+ found = true;
+ curr_node->next_right = new_node;
+ }
+ }
+ }
+ }
+ }
+
+ void set_height_depth(node_ptr node) {
+ if(node != NULL) {
+ }
+ }
+
+ 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);
+ }
+ }
private:
- // root of the tree
- node_ptr root_node;
- // height of the whole tree
- unsigned int tree_height;
+ // root of the tree
+ node_ptr root_node;
+ // height of the whole tree
+ unsigned int tree_height;
};
#endif
diff --git a/src/main.cpp b/src/main.cpp
index 41909a1..698fe9f 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -6,21 +6,21 @@
using namespace std;
-int main(int argc, char* argv[]) {
- srand(time(NULL));
- BinaryTree<int> int_bt;
- BinaryTree<double> double_bt;
- BinaryTree<char> char_bt;
+int main(int argc, char *argv[]) {
+ srand(time(NULL));
+ BinaryTree<int> int_bt;
+ 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);
- }
+ for(unsigned int i = 0; i < 1000; ++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();
+ int_bt.print_tree();
+ double_bt.print_tree();
+ char_bt.print_tree();
- return 0;
+ return 0;
}