top of page
Search

Binary Search Trees Part 2

  • jr229931hh1
  • Sep 17, 2020
  • 1 min read

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;
}


 
 
 

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...

 
 
 
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;...

 
 
 

Comments


  • Facebook
  • Twitter
  • LinkedIn

©2020 by Combinatorics and computer science blog. Proudly created with Wix.com

bottom of page