All pastes #477668 Raw Edit

Stuff

public text v1 · immutable
·published 2007-05-08 19:53 UTC
rendered paste body
///////////////////////////////////////////BSTREENODE.H////////////////////////////////////////
class BSTreeNode{
      public:
             BSTreeNode();
             void setKey(double);
             double getKey();
             void setLeft(BSTreeNode*);
             BSTreeNode* getLeft();
             void setRight(BSTreeNode*);
             BSTreeNode* getRight();
             BSTreeNode* getParent();
             void setParent(BSTreeNode*);
      private:
              BSTreeNode* left;
              BSTreeNode* right;
              BSTreeNode* parent;
              double key;
};

////////////////////////////////////////////////////////////////////BSTREENODE.CPP//////////////////////////////////////
#include "BSTreeNode.h"

BSTreeNode::BSTreeNode(){
    left = 0;
    right = 0;
    parent = 0;
    key = 0;
}
void BSTreeNode::setKey(double d){key = d; }
double BSTreeNode::getKey(){ return key; }
void  BSTreeNode::setLeft(BSTreeNode* l){ left = l; }
BSTreeNode* BSTreeNode::getLeft(){ return left; }
void BSTreeNode::setRight(BSTreeNode* r){ right = r; }
BSTreeNode* BSTreeNode::getRight(){ return right; }
void BSTreeNode::setParent(BSTreeNode* p){ parent = p; }
BSTreeNode* BSTreeNode::getParent(){ return parent; }


/////////////////////////////////BSTREE.H/////////////////////////////////////////////////
#include "BSTreeNode.h"

class BSTree{
    public:
        BSTree();
        ~BSTree();
        void insert(double); //Wrapper for insert(BSTreNode*)
        double min();  //Wrapper for min(BSTreeNode*);
    private:
        void insert(BSTreeNode*);
        BSTreeNode* min(BSTreeNode*);
        BSTreeNode* root;
};
///////////////////////////////////BSTREE.CPP/////////////////////////////////////////////////
#include "BSTree.h"
#include <iostream>

BSTree::BSTree() { root = new BSTreeNode; root->setKey(2); }
BSTree::~BSTree(){ delete root; }

void BSTree::insert(double n){
     BSTreeNode* newNode = new BSTreeNode;
     newNode->setKey(n);
     insert(newNode);
}

void BSTree::insert(BSTreeNode* n){
     BSTreeNode* t = root; //Tree cursor
     BSTreeNode* p = 0; //Keep track of parent node 
     std::cout<<n->getKey()<<" "<<root->getKey();
     while(t!=0){ //As long as t points to a node.
         p = t;
         if(n->getKey() < p->getKey())//Traverse Left Branch
             {t = t->getLeft();}
         else{t = t->getRight(); } //Traverse Right Branch
     }
     std::cout<<"wassabi!";
     n->setParent(p); //Set n's parent
     if(p == 0){
          root = n;
          return;
     }
     if(n->getKey() < p->getKey()) //Set p's child appropriately
         p->setLeft(n);
     else{ p->setRight(n); }
}

BSTreeNode* BSTree::min(BSTreeNode* x){
    while(x->getLeft() != 0)
        x = x->getLeft();
    return x;
}

double BSTree::min(){
    if(min(root) != 0)
        return (min(root))->getKey();
    else{return 0;}
}