Question: binSTree.h #ifndef BINSTREE_H #define BINSTREE_H #include binTree.h #include binTreeNode.h template class binSTree : public binTree { public: void insert ( const T& x ); //
| binSTree.h
#ifndef BINSTREE_H | |
| #define BINSTREE_H | |
| #include "binTree.h" | |
| #include "binTreeNode.h" | |
| template | |
| class binSTree : public binTree | |
| { | |
| public: | |
| void insert ( const T& x ); // insert node with value x | |
| bool search ( const T& x ) const; // searches leaf with value x | |
| bool remove ( const T& x ); // removes leaf with value x | |
| private: | |
| void insert ( binTreeNode | |
| bool search ( binTreeNode | |
| binTreeNode | |
| bool leaf ( binTreeNode | |
| }; | |
| // insert node with value x | |
| template | |
| void binSTree | |
| { | |
| insert( this->root, x ); | |
| } | |
| // searches leaf with value x | |
| template | |
| bool binSTree | |
| { | |
| return search( this->root, x ); | |
| } | |
| // removes leaf with value x | |
| template | |
| bool binSTree | |
| { | |
| if( this->size() == 1 ) | |
| { | |
| this->root = NULL; | |
| return false; | |
| } | |
| if( this->size() > 1 ) | |
| { | |
| if( search( x ) ) this->root = remove( this->root, x ); | |
| return true; | |
| } | |
| else if( this->size() == 1 ) | |
| { | |
| return false; | |
| } | |
| else | |
| { | |
| return false; | |
| } | |
| } | |
| // private version of insert | |
| template | |
| void binSTree | |
| { | |
| if(p == NULL) | |
| { | |
| p = new binTreeNode | |
| } | |
| else if( v < p->data) | |
| { | |
| insert( p->left, v ); | |
| } | |
| else if( v > p->data ) | |
| { | |
| insert( p->right, v ); | |
| } | |
| else | |
| { | |
| return; // duplicate | |
| } | |
| } | |
| // private version of search | |
| template | |
| bool binSTree | |
| { | |
| if( p == NULL ) return false; | |
| else | |
| { | |
| if( v == p->data ) | |
| { | |
| if( leaf( p ) ) return true; | |
| else return false; | |
| } | |
| else if( v < p->data ) | |
| return search( p->left, v ); | |
| else | |
| return search( p->right, v ); | |
| } | |
| } | |
| // private version of remove | |
| template | |
| binTreeNode | |
| { | |
| binTreeNode | |
| binTreeNode | |
| curr = p; | |
| while( curr != NULL ) | |
| { | |
| if( curr->data == v ) | |
| { | |
| break; | |
| } | |
| else | |
| { | |
| parent = curr; | |
| if( v > curr->data ) curr = curr->right; | |
| else curr = curr->left; | |
| } | |
| } | |
| if( curr != NULL ) | |
| { | |
| if( parent->right == curr ) parent->right = NULL; | |
| else parent->left = NULL; | |
| delete curr; | |
| curr = NULL; | |
| } | |
| if( this->size() >= 1 ) | |
| { | |
| return this->root; | |
| } | |
| return this->root; | |
| } | |
| // checks if node is leaf | |
| template | |
| bool binSTree | |
| { | |
| if( p->left == NULL && p->right == NULL ) | |
| { | |
| return true; // node is a leaf | |
| } | |
| else | |
| { | |
| return false; // node is not a leaf | |
| } | |
| } | |
| Getting these errors please help | prog7.cc: In instantiation of void print_vals(binSTree
|
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
