#include "bst.h" #include using namespace std; BST::BST(){ theroot = NULL; thesize = 0; } // basic tree operators position BST::root(){ return(theroot); } position BST::parent(position p){ if (p != NULL) { return(p->parent); } else { return(NULL); } } bool BST::isInternal(position p ){ if (p != NULL) { return ((p->lchild != NULL ) || (p->rchild != NULL)); } else { return (false); } } bool BST::isExternal(position p ){ if (p != NULL) { return ((p->lchild == NULL ) && (p->rchild == NULL)); } else { return (false); } } bool BST::isRoot(position p ){ if (p!= NULL) { return(p->parent == NULL); } else { return(false); } } int BST::size(){ return (thesize); } itemType BST::element(position p) { if (p != NULL) { return(p->data); } } void BST::replaceElement(position p, itemType i) { } // binary tree operators position BST::leftChild(position p ){ if (p!= NULL) { return(p->lchild); } else { return (NULL); } } position BST::rightChild(position p ){ if (p!= NULL) { return(p->rchild); } else { return (NULL); } } position BST::sibling(position p ){ if (p!= NULL) { if (p->parent != NULL) { if (p->parent->lchild == p) { return(p->parent->rchild); } else { return(p->parent->lchild); } } else { return(NULL); } } else { return (NULL); } } // my operations bool BST::hasLeftChild(position p ){ if(p!= NULL) { return(p->lchild != NULL); } else { return(false); } } bool BST::hasRightChild(position p ){ if(p!= NULL) { return(p->rchild != NULL); } else { return(false); } } bool BST::isNode(position p) { return(p!= NULL) ; } // BST operators position find(position p, itemType i) { if (p == NULL) { return(p); } else { if (p->data == i) { return(p); } else if (p->data < i) { return(find(p->rchild,i)); } else { return(find(p->lchild,i)); } } } position BST::findElement(itemType i ){ return(find(theroot,i)); } position newnode(position parent, itemType i) { position rv; rv = new node; rv-> data = i; rv->parent = parent; rv->lchild = NULL; rv->rchild = NULL; return (rv); } void insert(position & v, itemType i) { if (i < v->data) { if (v->lchild == NULL) { v->lchild = newnode(v,i); } else { insert(v->lchild,i); } } else { if (v->rchild == NULL) { v->rchild = newnode(v,i); } else { insert(v->rchild,i); } } return; } void BST::insertItem(itemType i){ thesize ++; if (theroot == NULL) { theroot = newnode(NULL,i); } else { insert(theroot,i); } return; } void killNode(position p, position child, position & r , int & size) { // reparent the child if(child != NULL) { child->parent = p->parent; } // rechild the parent if (p->parent != NULL) { if(p->parent->lchild == p) { p->parent->lchild = child; } else { p->parent->rchild = child; } } else { // or change the root r = child; } delete(p); size--; return; } itemType deleteMin(position p) { itemType tmp; position foo; // save the return data if (p->lchild == NULL) { tmp = p->data; // changing the child of the parent if (p->parent->lchild != p) { (p->parent->rchild) = p->rchild; } else { (p->parent->lchild) = p->rchild; } // changing the parent of the child if (p->rchild != NULL) { p->rchild->parent = p->parent; } delete p; return(tmp); } else { return(deleteMin(p->lchild)); } } void deleteitem(position & p, itemType i, position & r, int & size) { position t; if (p != NULL) { if (i < p->data) { deleteitem(p->lchild,i,r,size); } else if (i > p->data) { deleteitem(p->rchild,i,r,size); } else if ((p->lchild == NULL) && (p->rchild == NULL)) { killNode(p,NULL,r,size); } else if (p->lchild == NULL) { killNode(p,p->rchild,r,size); } else if (p->rchild == NULL) { killNode(p,p->lchild,r,size); } else { p->data= deleteMin(p->rchild); size--; } } return; } void BST::removeElement(itemType i ){ deleteitem(theroot, i , theroot, thesize); return; } void iop(position p, ostream & s) { if (p != NULL) { iop(p->lchild, s); s << p->data << endl; iop(p->rchild, s); } return; } void BST::inorderPrint(ostream & s) { iop(theroot,s); return; }