home *** CD-ROM | disk | FTP | other *** search
/ Enter 2005 March / ENTER.ISO / files / fwp-0.0.6-win32-installer.exe / Bintree.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2004-12-06  |  1.6 KB  |  94 lines

  1. #include "Bintree.h"
  2.  
  3. #include <stdlib.h>        // wiedermal NULL
  4. #include "log.h"
  5.  
  6. BintreeNode::BintreeNode(float key, void* object){
  7.     leftChild=NULL;
  8.     rightChild=NULL;
  9.     parent=NULL;
  10.  
  11.     this->key=key;
  12.     this->object=object;
  13. }
  14.  
  15. BintreeNode::~BintreeNode(){
  16.     if(leftChild!=NULL)
  17.         delete leftChild;
  18.     if(rightChild!=NULL)
  19.         delete rightChild;
  20.  
  21. //    delete object;    // THINKABOUTME
  22. }
  23.  
  24. void BintreeNode::buildInOrderArray(int& pos, BintreeNode** array){
  25.     if(leftChild!=NULL)
  26.         leftChild->buildInOrderArray(pos, array);
  27.  
  28.     // add
  29.     array[pos]=this;
  30.     pos++;
  31.  
  32.     if(rightChild!=NULL)
  33.         rightChild->buildInOrderArray(pos, array);
  34. }
  35.  
  36.  
  37.  
  38. Bintree::Bintree(){
  39.     root=NULL;
  40.     numNodes=0;
  41.  
  42.     inOrderArray=NULL;
  43. }
  44.  
  45. Bintree::~Bintree(){
  46.     if(inOrderArray!=NULL){
  47.         delete[] inOrderArray;
  48.     }
  49.     if(root!=NULL)
  50.         delete root;
  51. }
  52.  
  53. void Bintree::insert(float key, void* object){
  54.     BintreeNode** current=&root;
  55.     BintreeNode* last;
  56.  
  57.     while(*current!=NULL){
  58.         last=*current;
  59.         if((*current)->key>key)
  60.             current=&((*current)->leftChild);
  61.         else
  62.             current=&((*current)->rightChild);
  63.     }
  64.     *current=new BintreeNode(key, object);
  65.     numNodes++;
  66. }
  67.  
  68. void Bintree::clearTree(){
  69.     if(root!=NULL)
  70.         delete root;
  71.  
  72.     root=NULL;
  73.     numNodes=0;
  74. }
  75.  
  76. void Bintree::buildInOrderArray(){
  77.     if(inOrderArray!=NULL){
  78.         delete[] inOrderArray;
  79.         inOrderArray=NULL;
  80.     }
  81.  
  82.     if(numNodes==0)
  83.         return;
  84.  
  85.     inOrderArray=new BintreeNode*[numNodes];
  86.  
  87.     if(root!=NULL){
  88.         int count=0;
  89.         root->buildInOrderArray(count, inOrderArray);
  90. //        if(count!=numNodes)
  91. //            warn("SCHEISSE!!!!\n");
  92.     }
  93. }
  94.