home *** CD-ROM | disk | FTP | other *** search
- #include "Bintree.h"
-
- #include <stdlib.h> // wiedermal NULL
- #include "log.h"
-
- BintreeNode::BintreeNode(float key, void* object){
- leftChild=NULL;
- rightChild=NULL;
- parent=NULL;
-
- this->key=key;
- this->object=object;
- }
-
- BintreeNode::~BintreeNode(){
- if(leftChild!=NULL)
- delete leftChild;
- if(rightChild!=NULL)
- delete rightChild;
-
- // delete object; // THINKABOUTME
- }
-
- void BintreeNode::buildInOrderArray(int& pos, BintreeNode** array){
- if(leftChild!=NULL)
- leftChild->buildInOrderArray(pos, array);
-
- // add
- array[pos]=this;
- pos++;
-
- if(rightChild!=NULL)
- rightChild->buildInOrderArray(pos, array);
- }
-
-
-
- Bintree::Bintree(){
- root=NULL;
- numNodes=0;
-
- inOrderArray=NULL;
- }
-
- Bintree::~Bintree(){
- if(inOrderArray!=NULL){
- delete[] inOrderArray;
- }
- if(root!=NULL)
- delete root;
- }
-
- void Bintree::insert(float key, void* object){
- BintreeNode** current=&root;
- BintreeNode* last;
-
- while(*current!=NULL){
- last=*current;
- if((*current)->key>key)
- current=&((*current)->leftChild);
- else
- current=&((*current)->rightChild);
- }
- *current=new BintreeNode(key, object);
- numNodes++;
- }
-
- void Bintree::clearTree(){
- if(root!=NULL)
- delete root;
-
- root=NULL;
- numNodes=0;
- }
-
- void Bintree::buildInOrderArray(){
- if(inOrderArray!=NULL){
- delete[] inOrderArray;
- inOrderArray=NULL;
- }
-
- if(numNodes==0)
- return;
-
- inOrderArray=new BintreeNode*[numNodes];
-
- if(root!=NULL){
- int count=0;
- root->buildInOrderArray(count, inOrderArray);
- // if(count!=numNodes)
- // warn("SCHEISSE!!!!\n");
- }
- }
-