GlobeEngine
BinarySearchTreeNode.h
Go to the documentation of this file.
1 //
2 // Created by Matthias Thöny on 1/11/12.
3 // Copyright (c) 2012 University of Zuerich. All rights reserved.
4 //
5 #ifndef VMML_BinarySearchTreeNode_h
6 #define VMML_BinarySearchTreeNode_h
7 
8 #include "BintreeNode.h"
9 #include <iostream>
10 
11 namespace geData {
12  template <class T>
14  {
15  public:
16  //constructors and destructor
17  BinarySearchTreeNode(T _data): BintreeNode<T>(_data) {}
18 
19  bool insert(T data)
20  {
21  if(this->data == 0L)
22  {
23  this->setData(data);
24  return 1;
25  }else{
26  if(data < this->data)
27  {
28  if(this->left == 0L)
29  {
30  this->left = new BinarySearchTreeNode<T>(data);
31  this->left->setParent(this);
32  return 1;
33  }else{
34  return this->left->insert(data);
35  }
36  }else{ // data >= this->data
37  if(this->right == 0L)
38  {
39  this->right = new BinarySearchTreeNode<T>(data);
40  this->right->setParent(this);
41  return 1;
42  }else{
43  return this->right->insert(data);
44  }
45  }
46  }
47  }
48 
49  bool remove(T data)
50  {
51  if(this->data == 0L)
52  { // data not in tree
53  std::cout << "data not in tree" << std::endl;
54  }else if(this->data == data)
55  {
56  return 1;
57  }else{
58  if(data < this->data)
59  {
60  if(this->left->remove(data))
61  {
62  BinarySearchTreeNode<T>* nodeToDelete = dynamic_cast<BinarySearchTreeNode<T>*>(this->getLeft());
63  if(nodeToDelete->getLeft() == 0L && nodeToDelete->getRight() == 0L)
64  {
65  // case 1: node to delete has no children
66  }else{
67  // case 2: node to delete has one child
68  if(nodeToDelete->getLeft() == 0L)
69  {
70  this->left = nodeToDelete->getRight();
71  }else if ( nodeToDelete->getRight() == 0L){
72  this->left = nodeToDelete->getLeft();
73  }else{
74  // case 3: node to delete is internal node
75  this->left = dynamic_cast<BinarySearchTreeNode<T>*>(nodeToDelete->getRight()->searchLeftElement());
76  if(this->left != nodeToDelete->getRight())
77  {
78  this->left->getParent()->setLeft(0L);
79  // falls es ein teilbaum ist der raufgenommen wird, muss am ende noch der
80  // erste teilbaum hinugefuegt werden
81  if(this->left->getRight() != 0L){
82  BinarySearchTreeNode<T>* newRight = dynamic_cast<BinarySearchTreeNode<T>*>(this->left->searchRightElement());
83  newRight->setRight(nodeToDelete->getRight());
84  }
85  }
86  this->left->setLeft(nodeToDelete->getLeft());
87 
88  }
89  this->left->setParent(this);
90  }
91  nodeToDelete->setLeft(0L);
92  nodeToDelete->setRight(0L);
93  delete nodeToDelete;
94  }
95  }else{
96  if(this->right->remove(data))
97  {
98  BinarySearchTreeNode<T>* nodeToDelete = dynamic_cast<BinarySearchTreeNode<T>*>(this->getRight());
99  if(nodeToDelete->getLeft() == 0L && nodeToDelete->getRight() == 0L)
100  {
101  // case 1: node to delete has no children
102  }else{
103  // case 2: node to delete has one child
104  if(nodeToDelete->getLeft() == 0L)
105  {
106  this->right = nodeToDelete->getRight();
107  }else if ( nodeToDelete->getRight() == 0L){
108  this->right = nodeToDelete->getLeft();
109  }else{
110  // case 3: node to delete is internal node
111  this->right = dynamic_cast<BinarySearchTreeNode<T>*>(nodeToDelete->getRight()->searchLeftElement());
112  if(this->right != nodeToDelete->getRight())
113  {
114  this->right->getParent()->setLeft(0L);
115  // falls es ein teilbaum ist der raufgenommen wird, muss am ende noch der
116  // erste teilbaum hinugefuegt werden
117  if(this->right->getRight() != 0L){
118  BinarySearchTreeNode<T>* newRight = dynamic_cast<BinarySearchTreeNode<T>*>(this->right->searchRightElement());
119  newRight->setRight(nodeToDelete->getRight());
120  }
121  }
122  this->right->setLeft(nodeToDelete->getLeft());
123  }
124  this->right->setParent(this);
125  }
126  nodeToDelete->setLeft(0L);
127  nodeToDelete->setRight(0L);
128  delete nodeToDelete;
129  }
130  }
131  }
132  return 0;
133  }
134 
135  virtual void printValue(){
136  //T::print_value();
137  std::cout << this->data << " ";
138  std::cout << &this->data << std::endl;
139  }
140 
141  //comparison operators
142  virtual bool operator<(const BinarySearchTreeNode<T>& node) const {
143  return this->data < node.getData();
144  }
145 
146  virtual bool operator>(const BinarySearchTreeNode<T>& node) const {
147  return this->data > node.getData();
148  }
149 
150  virtual bool operator==(const BinarySearchTreeNode<T>& node) const {
151  return this->data == node.getData();
152  }
153 
154  //set and get left and right branches
156  return dynamic_cast<BinarySearchTreeNode<T>*>(this->left);
157  }
159  return dynamic_cast<BinarySearchTreeNode<T>*>(this->right);
160  }
161 
163  if(node != 0L){
164  node->setParent(this);
165  this->left = node;
166  }else{
167  this->left = 0L;
168  }
169  }
170 
172  if(node != 0L){
173  node->setParent(this);
174  this->right = node;
175  }else{
176  this->right = 0L;
177  }
178  }
179  };
180 }
181 #endif
182 
void setLeft(BinarySearchTreeNode< T > *node)
Definition: BinarySearchTreeNode.h:162
Definition: BinarySearchTreeNode.h:13
virtual bool operator>(const BinarySearchTreeNode< T > &node) const
Definition: BinarySearchTreeNode.h:146
void setRight(BinarySearchTreeNode< T > *node)
Definition: BinarySearchTreeNode.h:171
T data
Definition: BintreeNode.h:15
Definition: AvalancheTrainingSimulationEngine.h:39
BintreeNode< T > * right
Definition: BintreeNode.h:16
Definition: BintreeNode.h:12
void setParent(BintreeNode< T > *node)
Definition: BintreeNode.h:40
void setData(T data)
Definition: BintreeNode.h:36
virtual void printValue()
Definition: BinarySearchTreeNode.h:135
bool insert(T data)
Definition: BinarySearchTreeNode.h:19
virtual bool operator==(const BinarySearchTreeNode< T > &node) const
Definition: BinarySearchTreeNode.h:150
BinarySearchTreeNode< T > * getLeft()
Definition: BinarySearchTreeNode.h:155
BintreeNode< T > * left
Definition: BintreeNode.h:16
const T getData() const
Definition: BintreeNode.h:35
BinarySearchTreeNode(T _data)
Definition: BinarySearchTreeNode.h:17
BinarySearchTreeNode< T > * getRight()
Definition: BinarySearchTreeNode.h:158