Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Please refer to the example of C++ codes.: // code from // Data Structures and Algorithms in C++, Goodrich, Tamassia, and Mount, 2nd Ed., 2011.
Please refer to the example of C++ codes.:
// code from | |
// Data Structures and Algorithms in C++, Goodrich, Tamassia, and Mount, 2nd Ed., 2011. | |
// | |
#pragma once | |
#include "LinkedBinaryTree.h" | |
template | |
class Entry { | |
public: | |
typedef K Key; | |
typedef V Value; | |
Entry(const Key &k = Key(), const Value &v = Value()) | |
: _key(k), _value(v) {} | |
const Key& key() const { return _key; } | |
const Value& value() const { return _value; } | |
void setKey(const Key& k) { _key = k; } | |
void setValue(const Value& v) { _value = v; } | |
private: | |
Key _key; | |
Value _value; | |
}; | |
template | |
class SearchTree { // a binary search tree | |
public: // public types | |
typedef typename E::Key K; // a key | |
typedef typename E::Value V; // a value | |
// class Iterator; // an iterator/position | |
public: // public functions | |
SearchTree(); // constructor | |
int size() const; // number of entries | |
bool empty() const; // is the tree empty? | |
Position | |
Position | |
void erase(const K& k); // remove key k entry | |
//void erase(const Iterator& p); // remove entry at p | |
// Iterator begin(); // iterator to first entry | |
//Iterator end(); // iterator to end entry | |
void printInorder() const; | |
protected: // local utilities | |
// BinaryTree | |
//typedef typename BinaryTree::Position TPos; // position in the tree | |
Position | |
Position | |
Position | |
void inorder(const Position | |
Position | |
// Position restructure(const TPos& v); // restructure | |
private: // member data | |
LinkedBinaryTree | |
int n; // number of entries | |
public: | |
// ...insert Iterator class declaration here | |
}; | |
template | |
SearchTree | |
{ | |
T.addRoot(); | |
} | |
template | |
Position | |
{ | |
return T.root(); | |
} | |
template | |
int SearchTree | |
{ | |
return n; | |
} | |
template | |
bool SearchTree | |
{ | |
return size() == 0; | |
} | |
template | |
void SearchTree | |
{ | |
cout << "Keys in order: "; | |
inorder(root()); | |
cout << endl; | |
} | |
template | |
void SearchTree | |
{ | |
if (v.isExternal()) return; | |
inorder(v.left()); | |
cout << (*v).key() << ", "; | |
inorder(v.right()); | |
} | |
template | |
Position | |
if (v.isExternal()) return v; // key not found | |
if (k < (*v).key()) return finder(k, v.left()); // search left subtree | |
else if ((*v).key() < k) return finder(k, v.right()); // search right subtree | |
else return v; // found it here | |
} | |
template | |
Position | |
return finder(k, root()); // search from root | |
} | |
template | |
Position | |
Position | |
if (!v.isExternal()) { // key already exists? | |
(*v).setValue(x); // update value | |
} | |
else { | |
T.expandExternal(v); // add new internal node | |
(*v).setKey(k); | |
(*v).setValue(x); // set entry | |
n++; // one more entry | |
} | |
return v; // return insert position | |
} | |
template | |
Position | |
{ | |
return inserter(k, x); | |
} | |
template | |
Position | |
Position | |
if (v.left().isExternal()) | |
w = v.left(); // remove from left | |
else if (v.right().isExternal()) | |
w = v.right(); // remove from right | |
else { // both internal? | |
w = v.right(); // go to right subtree | |
do { w = w.left(); } while (!w.isExternal()); // get leftmost node | |
Position | |
(*v).setKey((*u).key()); | |
(*v).setValue((*u).value()); // copy w's parent to v | |
} | |
n--; // one less entry | |
return T.removeAboveExternal(w); // remove w and parent | |
} | |
template | |
void SearchTree | |
Position | |
if (v.isExternal()) // not found? | |
throw out_of_range("Erase of nonexistent key"); | |
eraser(v); // remove it | |
} |
Question:
Add a new function to print in reverse (descending) order.:
template
void SearchTree::printReverseorder() const
{ // add your codes here
}
Only show the codes for the function:
void SearchTree::printReverseorder() const
and provide any required helper functions.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started