top of page
Search
  • jr229931hh1

Binary Search Trees Part 2

This post contains the program that tests the functions developed in https://jr229931hh1.wixsite.com/website/post/binary_search_tree



City cFromInput(){
 City ci;
 string n, c; int p;
 cout<<"Insert the name, country, and population. ";
 cin>>n>>c>>p;
 ci.setValues(p,c,n);
 return ci;
}
void printCity(City const & c){
 cout<<c.name()<<" ("<<c.country()<<","<<c.population()<<")";
}
void printTree(BSTNode* root){
 if(root!=nullptr){
  printTree(root->left);cout<<" ";
  printCity(root->storage);cout<<" ";
  printTree(root->right);cout<<" ";
 }
}
void printTreeBFS(BSTNode* root){
 if(root!=nullptr){
  printCity(root->storage);cout<<" ";
  printTreeBFS(root->left);cout<<" ";
  printTreeBFS(root->right);cout<<" ";
 }
}
int main(){
 City c;
 int option;
 cin>>option;
 BSTNode* root=nullptr;
 while(option!=0){
 if(option==1){
  c=cFromInput();
  root=insert(root,c);
 }
 if(option==2){
  c=cFromInput();
  root=delNode(root,c);
 }
 if(option==3){
  printTree(root);
  cout<<endl;
 }
 if(option==4){
  printTreeBFS(root);
  cout<<endl;
 }
 if(option==5){
  root=deleteMaximum(root);
 }
 if(option==6){
  root=deleteMinimum(root);
 }
 cin>>option;
 }
 while(root!=nullptr){
  cout<<"Deleting ";
  printCity(root->storage);cout<<endl;
  root=delNode(root,root->storage);
 }
 return 0;
}


4 views0 comments

Recent Posts

See All

Evaluating formulas using stack

A stack can be used to evaluate formulas with brackets and operations +, -, and * on integers. The formula must have brackets around each of the operation. For example, the formulas "8" and "(1+7)" ar

Interior Stack Expansion for Integer Stacks

The following simple problem is an excellent for practicing working with stacks. Your goal is to create the function that takes two stacks st1 and st2 and one integer z. The function should expand the

Binary Search Tree

The building block of the binary search tree is its node. template<typename T> struct BSTNode{ T storage; BSTNode* left; BSTNode* right; }; The component storage contains the relevant data. T

bottom of page