Question
C++ Help with Test #2 (at bottom) to work properly. May adjust code to work or add any additional code. Do not use global variables.
C++ Help with Test #2 (at bottom) to work properly. May adjust code to work or add any additional code. Do not use global variables.
#include
#include
#include
using std::endl;
using std::cout;
using std::cin;
using std::string;
void pressAnyKeyToContinue() {
printf("Press any key to continue ");
cin.get();
}
//This helps with testing, do not modify.
bool checkTest(string testName, int whatItShouldBe, int whatItIs) {
if (whatItShouldBe == whatItIs) {
cout << "Passed " << testName << endl;
return true;
}
else {
cout << "***Failed test " << testName << " *** " << endl << " Output was " << whatItIs << endl << " Output should have been " << whatItShouldBe << endl;
return false;
}
}
#include
#include
using std::cout;
using std::cin;
using std::endl;
using std::unique_ptr;
using std::make_unique;
template
class Tree {
public:
~Tree();
void insert(const T& item);
void preOrder();
void inOrder();
void postOrder();
int nodeCount(); //maybe change this?
private:
struct Node {
T data{};
unique_ptr
unique_ptr
~Node() { cout << "I'm a node with value " << data << " and I'm destruting" << endl; }
};
void preOrder(Node * curr);
void inOrder(Node * curr);
void postOrder(Node * curr);
int nodeCount(Node * curr); //maybe change this?
unique_ptr
};
template
Tree
root.reset();
}
template
void Tree
// Specific: It's empty
if (!root) {
root = make_unique
root->data = item;
return;
}
// Generalized: Not empty
Node* curr = root.get();
do {
if (item < curr->data) {
if (curr->left) {
curr = curr->left.get();
}
else {
curr->left = make_unique
curr->left->data = item;
break;
}
}
else {
if (curr->right) {
curr = curr->right.get();
}
else {
curr->right = make_unique
curr->right->data = item;
break;
}
}
} while (true);
}
template
int Tree
return nodeCount(root.get());
cout << endl;
}
template
int Tree
if (curr== NULL)
return 0;
return 1 + nodeCount(curr->left.get()) + nodeCount(curr->right.get());
}
template
void Tree
preOrder(root.get());
cout << endl;
}
template
void Tree
if (curr) {
cout << curr->data << " ";
preOrder(curr->left.get());
preOrder(curr->right.get());
}
}
template
void Tree
inOrder(root.get());
cout << endl;
}
template
void Tree
if (curr) {
inOrder(curr->left.get());
cout << curr->data << " ";
inOrder(curr->right.get());
}
}
template
void Tree
postOrder(root.get());
cout << endl;
}
template
void Tree
if (curr) {
postOrder(curr->left.get()); //may have to adjust this
postOrder(curr->right.get());
cout << curr->data << " ";
}
}
int main() {
Tree
myTree.insert(37);
myTree.insert(32);
myTree.insert(73);
myTree.insert(95);
myTree.insert(42);
myTree.insert(12);
myTree.insert(00);
myTree.insert(49);
myTree.insert(98);
myTree.insert(7);
myTree.insert(27);
myTree.insert(17);
myTree.insert(47);
myTree.insert(87);
myTree.insert(77);
myTree.insert(97);
myTree.insert(67);
myTree.insert(85);
myTree.insert(15);
myTree.insert(5);
myTree.insert(35);
myTree.insert(55);
myTree.insert(65);
myTree.insert(75);
myTree.insert(25);
myTree.insert(45);
myTree.insert(3);
myTree.insert(93);
myTree.insert(83);
myTree.insert(53);
myTree.insert(63);
myTree.insert(23);
myTree.insert(13);
myTree.insert(43);
myTree.insert(33);
myTree.preOrder();
checkTest("Test #2, number of leaves, (i.e. nodes with no children)", 11, myTree.leavesCount()); //this is what I need help with to work
pressAnyKeyToContinue();
return 0;
}
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