Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Main.cpp #include using namespace std; // General tree node ADT ************************************************************************ template class GTNode { private: E element; GTNode *Parent; GTNode *leftChild; GTNode *rightSib; public:

image text in transcribed

image text in transcribed

image text in transcribed

Main.cpp

#include using namespace std;

// General tree node ADT ************************************************************************ template class GTNode { private: E element; GTNode *Parent; GTNode *leftChild; GTNode *rightSib;

public:

// Constructor GTNode(const E& v) { Parent = leftChild = rightSib = NULL; element = v; }

// Constructor GTNode(const E& v, GTNode* par) { element=v, leftChild=rightSib=NULL, Parent=par;}

// 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* n) { n->rightSib = leftChild; n->Parent = this; leftChild = n; }

// Insert as the right sibling void insertNext(GTNode* n) { n->rightSib = rightSib; n->Parent = Parent; rightSib = n; }

// Remove first child from tree void removeFirst() { if (leftChild == NULL) return; GTNode* temp = leftChild; leftChild = leftChild->rightSib; delete temp; }

// Remove right sibling from tree void removeNext() { if (rightSib == NULL) return; GTNode* temp = rightSib; rightSib = rightSib->rightSib; delete temp; }

};

// General tree ADT ************************************************************************ template class GTree {

private: GTNode *root;

void print_postorder_Help(GTNode *rt) { // ********** Student code ****************** }

public:

// Constructor GTree() { root=NULL;}

// Send all nodes to free store void clear() { root = NULL; }

// Return the root of the tree GTNode* Root() { return root; }

// Combine two trees void newroot(GTNode *rt) { clear(); root=rt; }

int depth(GTNode * t) { // ********** Student code ****************** }

// Print a tree void print_postorder() { print_postorder_Help(root); }

};

int main() {

// ********** Student code ******************

/* ======================================= This is an example ====================================================

//create a Tree GTree gt;

//create some node GTNode *nA, *nB, *nC, *nD; nA= new GTNode('A', NULL); nB= new GTNode('B', NULL); nC= new GTNode('C', NULL);nD= new GTNode('D', NULL); gt.newroot(nA);

// 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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

LO5 Describe job analysis and the stages in the process.

Answered: 1 week ago