Question
Main.cpp #include using namespace std; // General tree node ADT ************************************************************************ template class GTNode { private: E element; GTNode *Parent; GTNode *leftChild; GTNode *rightSib; public:
Main.cpp
#include
// General tree node ADT ************************************************************************ template
public:
// Constructor GTNode(const E& v) { Parent = leftChild = rightSib = NULL; element = v; }
// Constructor GTNode(const E& v, GTNode
// Return nodes value E value() { return element; }
// True if node is a leaf bool isLeaf() { return leftChild == NULL; }
// Return node's parent GTNode* parent() { return Parent; }
// Return node's first child GTNode* leftmostChild() { return leftChild; }
// Return node's right sibling GTNode* rightSibling() { return rightSib; }
// Set node's value void setValue(E& val) { element = val; }
// Insert as the first child void insertFirst(GTNode
// Insert as the right sibling void insertNext(GTNode
// Remove first child from tree void removeFirst() { if (leftChild == NULL) return; GTNode
// Remove right sibling from tree void removeNext() { if (rightSib == NULL) return; GTNode
};
// General tree ADT ************************************************************************ template
private: GTNode
void print_postorder_Help(GTNode
public:
// Constructor GTree() { root=NULL;}
// Send all nodes to free store void clear() { root = NULL; }
// Return the root of the tree GTNode
// Combine two trees void newroot(GTNode
int depth(GTNode
// Print a tree void print_postorder() { print_postorder_Help(root); }
};
int main() {
// ********** Student code ******************
/* ======================================= This is an example ====================================================
//create a Tree GTree
//create some node GTNode
// connect nodes nA->insertFirst(nD); nA->insertFirst(nC); nA->insertFirst(nB);
// display the tree cout
// display the sequential representation cout
cout
===================================================================================================================== */ }
Assume that you have a tree T that is represented in a binary tree structure as shown by figure 1 root. Figure 1. General Tree T (represented in a binary tree structure) The depth of a node v (distance from root) number of edges from the root to v; alternatively, number of ancestors of v, except itself. (e.g depth of A is 0, depth of B is l) To Do (20 Marks) 1) Download main cpp that contains a c++ implementation of the general tree node ADT and the general tree ADT as it was introduced in class 2) Create your c++ project and add to it the main.cpp that you already downloaded. You will add your code in the source file "main.cpp" 3) Implement in the class GTree the methods Assume that you have a tree T that is represented in a binary tree structure as shown by figure 1 root. Figure 1. General Tree T (represented in a binary tree structure) The depth of a node v (distance from root) number of edges from the root to v; alternatively, number of ancestors of v, except itself. (e.g depth of A is 0, depth of B is l) To Do (20 Marks) 1) Download main cpp that contains a c++ implementation of the general tree node ADT and the general tree ADT as it was introduced in class 2) Create your c++ project and add to it the main.cpp that you already downloaded. You will add your code in the source file "main.cpp" 3) Implement in the class GTree the methods
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