GlobeEngine
BintreeNode.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_BintreeNode_h
6 #define VMML_BintreeNode_h
7 
8 #include <iostream>
9 
10 namespace geData {
11  template <class T>
13  {
14  protected:
15  T data;
17  public:
18  //constructors and destructor
19  BintreeNode(T _data): data(_data), left(0L), right(0L), parent(0L){}
21  if(this->left != NULL)
22  {
23  delete this->left;
24  }
25  if(this->right != NULL)
26  {
27  delete this->right;
28  }
29  this->left = 0L;
30  this->right = 0L;
31  this->parent = 0L;
32  }
33 
34  //set and get data member
35  const T getData() const { return data; }
36  void setData(T data) { this->data = data; }
37 
38  //set and get parent
39  BintreeNode<T>* getParent() { return this->parent; }
40  void setParent(BintreeNode<T> * node) { this->parent = node; }
41 
42  //set and get left and right branches
43  BintreeNode<T>* getLeft() { return left; }
44  BintreeNode<T>* getRight() { return right; }
45 
46  void setLeft(BintreeNode<T>* node){
47  if(node != 0L){
48  node->setParent(this);
49  left = node;
50  }else{
51  left = 0L;
52  }
53  }
54 
55  void setRight(BintreeNode<T> * node){
56  if(node != 0L){
57  node->setParent(this);
58  right = node;
59  }else{
60  right = 0L;
61  }
62  }
63 
65  {
66  // Daten nicht vorhanden
67  if(this->data == 0L)
68  {
69  return 0L;
70  }else if(this->data == data)
71  {
72  return this;
73  }else if(data < this->data)
74  {
75  return this->left->search(data);
76  }else{
77  return this->right->search(data);
78  }
79  }
80 
82  if(this->left == 0L)
83  {
84  return this;
85  }else{
86  return this->getLeft()->searchLeftElement();
87  }
88  }
89 
91  if(this->right == 0L)
92  {
93  return this;
94  }else{
95  return this->getRight()->searchRightElement();
96  }
97  }
98 
100  if(this->right != 0L) {
101  return this->right->searchLeftElement();
102  }else{
103  return 0L;
104  }
105  }
106 
108  if(this->left != 0L) {
109  return this->left->searchRightElement();
110  }else{
111  return 0L;
112  }
113  }
114 
115  virtual void printValue(){
116  //std::cout << &this->data << std::endl;
117  std::cout << this->data << " ";
118  }
119 
120  virtual void printInorder() {
121  this->printValue();
122  if(this->left != 0L) {
123  this->left->printInorder();
124  }
125  if(this->right != 0L) {
126  this->right->printInorder();
127  }
128  }
129 
130  virtual void printPostorder() {
131  if(left != 0L) {
132  left->printPostorder();
133  }
134  if(right != 0L) {
135  right->printPostorder();
136  }
137  this->printValue();
138  }
139 
140  virtual void printEuler() {
141  this->printValue();
142  if(left != 0L) {
143  left->printEuler();
144  }
145  this->printValue();
146  if(right != 0L) {
147  right->printEuler();
148  }
149  this->printValue();
150  }
151 
152  virtual bool insert(T data)
153  {
154  if(left == 0L)
155  {
156  left = new BintreeNode<T>(data);
157  left->setData(data);
158  return true;
159  }
160  if(right == 0L)
161  {
162  right = new BintreeNode<T>(data);
163  right->setData(data);
164  return true;
165  }
166  if(left->insert(data))
167  {
168  return true;
169  }else{
170  if(right->insert(data))
171  {
172  return true;
173  }else{
174  std::cout << "Binary tree reaches max capacity" << std::endl;
175  return false;
176  }
177  }
178  }
179 
180  virtual bool remove(T data){
181  std::cout << "TODO";
182  return 0;
183  }
184 
185  //comparison operators
186  virtual bool operator<(const BintreeNode<T>& node) const {
187  return this->data < node.data;
188  }
189 
190  bool virtual operator>(const BintreeNode<T>& node) const {
191  return this->data > node.data;
192  }
193 
194  bool virtual operator==(const BintreeNode<T>& node) const {
195  return this->data == node.data;
196  }
197  };
198 }
199 #endif
200 
virtual void printInorder()
Definition: BintreeNode.h:120
BintreeNode< T > * parent
Definition: BintreeNode.h:16
T data
Definition: BintreeNode.h:15
virtual void printValue()
Definition: BintreeNode.h:115
virtual void printEuler()
Definition: BintreeNode.h:140
BintreeNode< T > * findInorderPredeccessor()
Definition: BintreeNode.h:107
Definition: AvalancheTrainingSimulationEngine.h:39
BintreeNode< T > * right
Definition: BintreeNode.h:16
BintreeNode< T > * getParent()
Definition: BintreeNode.h:39
BintreeNode< T > * getRight()
Definition: BintreeNode.h:44
Definition: BintreeNode.h:12
BintreeNode< T > * search(T data)
Definition: BintreeNode.h:64
BintreeNode(T _data)
Definition: BintreeNode.h:19
void setParent(BintreeNode< T > *node)
Definition: BintreeNode.h:40
BintreeNode< T > * searchRightElement()
Definition: BintreeNode.h:90
void setData(T data)
Definition: BintreeNode.h:36
virtual bool insert(T data)
Definition: BintreeNode.h:152
virtual bool operator==(const BintreeNode< T > &node) const
Definition: BintreeNode.h:194
BintreeNode< T > * left
Definition: BintreeNode.h:16
void setRight(BintreeNode< T > *node)
Definition: BintreeNode.h:55
~BintreeNode()
Definition: BintreeNode.h:20
const T getData() const
Definition: BintreeNode.h:35
void setLeft(BintreeNode< T > *node)
Definition: BintreeNode.h:46
BintreeNode< T > * getLeft()
Definition: BintreeNode.h:43
virtual bool operator>(const BintreeNode< T > &node) const
Definition: BintreeNode.h:190
BintreeNode< T > * findInorderSuccessor()
Definition: BintreeNode.h:99
BintreeNode< T > * searchLeftElement()
Definition: BintreeNode.h:81
virtual void printPostorder()
Definition: BintreeNode.h:130