creating tree containers: one that uses a vector to hold your trees (class VectorContainer). Each of these container classes should be able to hold any amount of different expressions each of which can be of any size.see example CODE GIVEN class Container { protected: Sort* sort_function; public: /* Constructors */ Container() : sort_function(nullptr) { }; Container(Sort* function) : sort_function(function) { }; /* Non Virtual Functions */ void set_sort_function(Sort* sort_function); // set the type of sorting algorithm /* Pure Virtual Functions */ // push the top pointer of the tree into container virtual void add_element(Base* element) = 0; // iterate through trees and output the expressions (use stringify()) virtual void print() = 0; // calls on the previously set sorting-algorithm. Checks if sort_function is not null, throw exception if otherwise virtual void sort() = 0; /* Essentially the only functions needed to sort */ //switch tree locations virtual void swap(int i, int j) = 0; // get top ptr of tree at index i virtual Base* at(int i) = 0; // return container size virtual int size() = 0; }; #endif //__CONTAINER_HPP__   Example for list as reference #ifndef __LISTCONTAINER_HPP__ #define __LISTCONTAINER_HPP__ #include "container.hpp" #include #include #include class Sort; class ListContainer: public Container{ public: std::list baseList; //Container() : sort_function(nullptr){}; //Container(Sort* function) : sort_Function(function){}; void set_sort_funtion(Sort* sort_function){ this -> sort_function = sort_function; } void add_element(Base* element){ baseList.push_back(element); } void print(){ for(std::list::iterator i = baseList.begin(); i != baseList.end(); ++i){ std::cout << ' ' << (*i) -> stringify(); } } void sort(){ try{ if(sort_function != nullptr){ sort_function -> sort(this); } else{ throw std::logic_error("invalid sort_function"); } } catch(std::exception &exp){ std::cout << "ERROR : " << exp.what() << "\n"; } } //sorting functions void swap(int i, int j){ std::list::iterator first = baseList.begin(); for(int f = 0; f < i; f++){ first++; } Base* temp = *first; std::list::iterator second = baseList.begin(); for(int s = 0; s < j; s++){ second++; } *first = *second; *second = temp; } Base* at(int i){ std::list::iterator x = baseList.begin(); for(int a = 0; a < i; a++){ x++; } return *x; } int size(){ return baseList.size(); } }; #endif //__LISTCONTAINER_HPP__

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

creating tree containers: one that uses a vector to hold your trees (class VectorContainer). Each of these container classes should be able to hold any amount of different expressions each of which can be of any size.see example

CODE GIVEN

class Container {
protected:
Sort* sort_function;

public:
/* Constructors */
Container() : sort_function(nullptr) { };
Container(Sort* function) : sort_function(function) { };

/* Non Virtual Functions */
void set_sort_function(Sort* sort_function); // set the type of sorting algorithm

/* Pure Virtual Functions */
// push the top pointer of the tree into container
virtual void add_element(Base* element) = 0;
// iterate through trees and output the expressions (use stringify())
virtual void print() = 0;
// calls on the previously set sorting-algorithm. Checks if sort_function is not null, throw exception if otherwise
virtual void sort() = 0;

/* Essentially the only functions needed to sort */
//switch tree locations
virtual void swap(int i, int j) = 0;
// get top ptr of tree at index i
virtual Base* at(int i) = 0;
// return container size
virtual int size() = 0;
};

#endif //__CONTAINER_HPP__

 

Example for list as reference

#ifndef __LISTCONTAINER_HPP__ #define __LISTCONTAINER_HPP__ #include "container.hpp" #include <list> #include <iostream> #include <stdexcept> class Sort; class ListContainer: public Container{ public: std::list<Base*> baseList; //Container() : sort_function(nullptr){}; //Container(Sort* function) : sort_Function(function){}; void set_sort_funtion(Sort* sort_function){ this -> sort_function = sort_function; } void add_element(Base* element){ baseList.push_back(element); } void print(){ for(std::list<Base*>::iterator i = baseList.begin(); i != baseList.end(); ++i){ std::cout << ' ' << (*i) -> stringify(); } } void sort(){ try{ if(sort_function != nullptr){ sort_function -> sort(this); } else{ throw std::logic_error("invalid sort_function"); } } catch(std::exception &exp){ std::cout << "ERROR : " << exp.what() << "\n"; } } //sorting functions void swap(int i, int j){ std::list<Base*>::iterator first = baseList.begin(); for(int f = 0; f < i; f++){ first++; } Base* temp = *first; std::list<Base*>::iterator second = baseList.begin(); for(int s = 0; s < j; s++){ second++; } *first = *second; *second = temp; } Base* at(int i){ std::list<Base*>::iterator x = baseList.begin(); for(int a = 0; a < i; a++){ x++; } return *x; } int size(){ return baseList.size(); } }; #endif //__LISTCONTAINER_HPP__

Expert Solution
steps

Step by step

Solved in 3 steps

Blurred answer
Knowledge Booster
Concept of memory addresses in pointers
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education