GlobeEngine
BinarySearchTree.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_BinarySearchTree_h
6 #define VMML_BinarySearchTree_h
7 
8 #include "Bintree.h"
9 #include "BinarySearchTreeNode.h"
10 
11 namespace geData {
12  template <class T>
13  class BinarySearchTree : public Bintree<T>
14  {
15  public:
16  //constructors and destructor
18  BinarySearchTree(T data):Bintree<T>(new BinarySearchTreeNode<T>(data)){}
20 
21  //search
23  {
24  return 0L;
25  }
26 
27  //insert in a BST
28  bool insert(T data)
29  {
30  if(this->root != 0L)
31  {
32  return this->root->insert(data);
33  }else{
34  this->root = new BinarySearchTreeNode<T>(data);
35  return true;
36  }
37  return false;
38  }
39 
40  bool remove(T _data){
41  if(this->root != 0L)
42  {
43  if(this->root->getData() == _data)
44  {
45  BinarySearchTreeNode<T>* nodeToDelete = dynamic_cast<BinarySearchTreeNode<T>*>(this->root);
46  if(nodeToDelete->getLeft() == 0L && nodeToDelete->getRight() == 0L)
47  {
48  // case 1: node to delete has no children
49  }else{
50  // case 2: node to delete has one child
51  if(nodeToDelete->getLeft() == 0L)
52  {
53  this->root = nodeToDelete->getRight();
54  }else if ( nodeToDelete->getRight() == 0L){
55  this->root = nodeToDelete->getLeft();
56  }else{
57  // case 3: node to delete is internal node
58  this->root = dynamic_cast<BinarySearchTreeNode<T>*>(nodeToDelete->getRight()->searchLeftElement());
59  if(this->root != nodeToDelete->getRight())
60  {
61  this->root->getParent()->setLeft(0L);
62  // falls es ein teilbaum ist der raufgenommen wird, muss am ende noch der
63  // erste teilbaum hinugefuegt werden
64  if(this->root->getRight() != 0L){
65  BinarySearchTreeNode<T>* newRight = dynamic_cast<BinarySearchTreeNode<T>*>(this->root->searchRightElement());
66  newRight->setRight(nodeToDelete->getRight());
67  }
68  }
69 
70  BinarySearchTreeNode<T>* newLeft = dynamic_cast<BinarySearchTreeNode<T>*>(nodeToDelete->getLeft());
71  this->root->setLeft(newLeft);
72  }
73  this->root->setParent(0L);
74  }
75  nodeToDelete->setLeft(0L);
76  nodeToDelete->setRight(0L);
77  delete nodeToDelete;
78  }else{
79  return this->root->remove(_data);
80  }
81  }else{ // no tree exists
82  return true;
83  }
84  return false;
85 
86  }
87 
88  };
89 }
90 #endif
void setLeft(BinarySearchTreeNode< T > *node)
Definition: BinarySearchTreeNode.h:162
Definition: Bintree.h:12
Definition: BinarySearchTreeNode.h:13
void setRight(BinarySearchTreeNode< T > *node)
Definition: BinarySearchTreeNode.h:171
Definition: AvalancheTrainingSimulationEngine.h:39
BinarySearchTreeNode< T > * search(T data)
Definition: BinarySearchTree.h:22
BinarySearchTree(T data)
Definition: BinarySearchTree.h:18
~BinarySearchTree()
Definition: BinarySearchTree.h:19
BintreeNode< T > * root
Definition: Bintree.h:15
BinarySearchTreeNode< T > * getLeft()
Definition: BinarySearchTreeNode.h:155
Definition: BinarySearchTree.h:13
BinarySearchTreeNode< T > * getRight()
Definition: BinarySearchTreeNode.h:158
bool insert(T data)
Definition: BinarySearchTree.h:28
BinarySearchTree()
Definition: BinarySearchTree.h:17