C++ Help with Test #1 (at bottom) to work properly. May adjust code to work or add any additional code. Do not use global variables.
#include <iostream>
#include <string>
using std::endl;
using std::cout;
using std::cin;
using std::string;
void pressAnyKeyToContinue() {
printf("Press any key to continue\n");
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 <iostream>
#include <memory>
using std::cout;
using std::cin;
using std::endl;
using std::unique_ptr;
using std::make_unique;
template <typename T>
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<Node> left;
unique_ptr<Node> right;
~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<Node> root;
};
template <typename T>
Tree<T>::~Tree() {
root.reset();
}
template <typename T>
void Tree<T>::insert(const T& item) {
// Specific: It's empty
if (!root) {
root = make_unique<Node>();
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<Node>();
curr->left->data = item;
break;
}
}
else {
if (curr->right) {
curr = curr->right.get();
}
else {
curr->right = make_unique<Node>();
curr->right->data = item;
break;
}
}
} while (true);
}
template <typename T>
void Tree<T>::preOrder() {
preOrder(root.get());
cout << endl;
}
template <typename T>
void Tree<T>::preOrder(Node * curr) {
if (curr) {
cout << curr->data << " ";
preOrder(curr->left.get());
preOrder(curr->right.get());
}
}
template <typename T>
void Tree<T>::inOrder() {
inOrder(root.get());
cout << endl;
}
template <typename T>
void Tree<T>::inOrder(Node * curr) {
if (curr) {
inOrder(curr->left.get());
cout << curr->data << " ";
inOrder(curr->right.get());
}
}
template <typename T>
void Tree<T>::postOrder() {
postOrder(root.get());
cout << endl;
}
template <typename T>
void Tree<T>::postOrder(Node * curr) {
if (curr) {
postOrder(curr->left.get()); //may have to adjust this
postOrder(curr->right.get());
cout << curr->data << " ";
}
}
int main() {
Tree<int> myTree;
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 #1, number of nodes", 35, myTree.nodeCount()); //this is what I need help with to work
pressAnyKeyToContinue();
return 0;
}
#include <iostream>
#include <iostream>
#include <string>
using std::endl;
using std::cout;
using std::cin;
using std::string;
void pressAnyKeyToContinue() {
printf("Press any key to continue\n");
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 <iostream>
#include <memory>
using std::cout;
using std::cin;
using std::endl;
using std::unique_ptr;
using std::make_unique;
template <typename T>
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<Node> left;
unique_ptr<Node> right;
~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<Node> root;
};
template <typename T>
Tree<T>::~Tree() {
root.reset();
}
template <typename T>
void Tree<T>::insert(const T& item) {
// Specific: It's empty
if (!root) {
root = make_unique<Node>();
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<Node>();
curr->left->data = item;
break;
}
}
else {
if (curr->right) {
curr = curr->right.get();
}
else {
curr->right = make_unique<Node>();
curr->right->data = item;
break;
}
}
} while (true);
}
template <typename T>
int Tree<T>::nodeCount() {
return nodeCount(root.get());
cout << endl;
}
template <typename T>
int Tree<T>::nodeCount(Node * curr) {
if (curr== NULL)
return 0;
return 1 + nodeCount(curr->left.get()) + nodeCount(curr->right.get());
}
template <typename T>
void Tree<T>::preOrder() {
preOrder(root.get());
cout << endl;
}
template <typename T>
void Tree<T>::preOrder(Node * curr) {
if (curr) {
cout << curr->data << " ";
preOrder(curr->left.get());
preOrder(curr->right.get());
}
}
template <typename T>
void Tree<T>::inOrder() {
inOrder(root.get());
cout << endl;
}
template <typename T>
void Tree<T>::inOrder(Node * curr) {
if (curr) {
inOrder(curr->left.get());
cout << curr->data << " ";
inOrder(curr->right.get());
}
}
template <typename T>
void Tree<T>::postOrder() {
postOrder(root.get());
cout << endl;
}
template <typename T>
void Tree<T>::postOrder(Node * curr) {
if (curr) {
postOrder(curr->left.get()); //may have to adjust this
postOrder(curr->right.get());
cout << curr->data << " ";
}
}
int main() {
Tree<int> myTree;
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 #1, number of nodes", 35, myTree.nodeCount()); //this is what I need help with to work
pressAnyKeyToContinue();
return 0;
}
======================
See Output

Thanks, PLEASE UPVOTE if helpful
C++ Help with Test #1 (at bottom) to work properly. May adjust code to work or...
C++ Binary tree, help pls #pragma once // include this library to use NULL, otherwise use nullptr instead #include <cstddef> #include <iostream> #include "node.hpp" template<class T> class BST{ public: // Constructor for the BST class, creates an empty tree BST(void); // Destructor for the BST class, destroys the tree ~BST(void); // Inserts data into the tree // param: The data to be inserted into the tree void insert(T); // Removes data from the tree // param: The data to be...
C++ EXERCISE (DATA STRUCTURES). I just need a code for some functions that are missing. Please help me figure out. Thanks. C++ BST implementation (using a struct) Enter the code below, and then compile and run the program. After the program runs successfully, add the following functions: postorder() This function is similar to the inorder() and preorder() functions, but demonstrates postorder tree traversal. displayParentsWithTwo() This function is similar to the displayParents WithOne() function, but displays nodes having only two children....
Write a function, swapSubtrees, that swaps all of the left and right subtrees of a binary tree. Add this function to the class binaryTreeType and create a program to test this function. #include <iostream> using namespace std; //Definition of the Node template <class elemType> struct nodeType { elemType info; nodeType<elemType> *lLink; nodeType<elemType> *rLink; }; //Definition of the class template <class elemType> class binaryTreeType { public: //Overload the assignment operator. const binaryTreeType<elemType>& operator=(const binaryTreeType<elemType>&) { if (this != &otherTree) //avoid self-copy...
Convert the TreeArray C++ source code(posted below) into a BinaryTree, using this TreeNode definition: class TreeNode<T> T data TreeNode<T> left TreeNode<T> right Since this TreeNode is a generic Template, use any data file we've used this quarter to store the data in the BinaryTree. To do this will likely require writing a compare function or operator. Hint: Think LEFT if the index is calculate (2n+1) and RIGHT if index is (2n+2). Source code: #include<iostream> using namespace std;...
Q. write the algorithm for each function in this code: void insert(int x, node*&p) { //cheak if the pointer is pointing to null. if (p==NULL) { p = new node; p->key=x; p->left=NULL; p->right=NULL; } else { //Cheak if the element to be inserted is smaller than the root. if (x < p->key) { //call the function itself with new parameters. insert(x,p->left); } //cheak if the alement to be inserted...
Question - modify the code below so that for a node, the value of every node of its right subtree is less the node, and the value of each node of its left subtree is greater than the node. - create such a binary tree in the Main method, and call the following method: InOrder(Node theRoot), PreOrder(Node theRoot), PostOrder(Node theRoot), FindMin(), FindMax(), Find(int key) using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;...
I need help with this lab assignment. I use C++, thank you. Derive a MinHeap class from the BST class of your Lab 4 code.Define/Override only the Search/Insert/Delete methods in the new class.Write a new main for this lab which:Will only be a test program for the MinHeap.Declares and creates a MinHeap object as necessary.Declares and creates the Dollar objects as necessary.Inserts the 20 Dollar objects specified in Lab 4 in the same order.Performs all 4 traversals after the inserting...
Can you take a look at my code that why the maxDepth function is not working? public class BinaryTree { class Node{ int key; Node left,right; public Node(int item) { key = item; left = right = null; } } Node root; public void BinaryTree(){ root = null; } void...
Hi there, I am working on a binary search tree code in c++. The program must store and update students' academic records, each node includes the student name, credits attempted, credits earned and GPA. I have made some progress with the code and written most of the functions in the .cpp file (already did the .h file) but i am struggling with what to put in the main and how to put an update part in the insert function. I...
Using the following implementation of Tree class Node { public int iData; // data item (key) public double dData; // data item public Node leftChild; // this node's left child public Node rightChild; // this node's right child public void displayNode() // display ourself { System.out.print('{'); System.out.print(iData); System.out.print(", "); System.out.print(dData); System.out.print("} "); } } // end class Node //------------------------------------------------------------------ import java.io.IOException; import java.util.Stack; public class Tree { private Node root; // first node of tree // ------------------------------------------------------------- public Tree() // constructor { root = null; }...