Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

IN C++ PLEASE AND PLEASE PRINT THE DESIRED TREE. I have provided the header file and the main.cpp file. Implement your own Tree ADT. Please

IN C++ PLEASE AND PLEASE PRINT THE DESIRED TREE. I have provided the header file and the main.cpp file.

Implement your own Tree ADT.

Please download the tree.h file which has all the methods for you to implement and Do not modify the tree.h file. I have also provided tree_main.cpp file to test the program functionality.

----THE HEADER FILE called tree.h ------

#ifndef _TREE_H

#define _TREE_H

#include // necessary in order to use NULL

class TreeNode

{

public:

TreeNode() : left(NULL), right(NULL) {}

TreeNode* left;

TreeNode* right;

int value;

};

class Tree

{

public:

// Default constructor

Tree();

// Copy constructor

Tree(const Tree& other);

//Destructor

~Tree();

// overloaded Assignment Operator

Tree& operator=(const Tree& other);

// Similar to insert function we discussed earlier

// creates node and inserts it at appropriate position.

void push(int value);

// Returns the address of the node containing the value.

TreeNode* find(int value) const;

// Print the tree data

void print() const;

// Deletes the node with value in the tree and deallocates its memory.

void deleteNode(int value);

private:

// Root of the tree.

TreeNode* start;

//copyOther

// you should implement and use this helper function inside your

// copy constructor and overloadedAssignment operator.

void copyOther(const Tree& other);

// clear

// you should implement and use this function inside your

// destructor to delete all the nodes and free memory

void clear();

// pushFrom

// Recursively push a single element into a tree.

// Use it in your push function.

void pushFrom(TreeNode* startingPoint, TreeNode* nodeToPush);

// findFrom

// Recursively find a single element in a tree.

TreeNode* findFrom(TreeNode* startingPoint, int value) const;

// printFrom

//

// Recursively print the values in a tree. Use

// pre-order traversal.

//

// If a tree looks like this:

//

// 6

// / \

// / \

// 5 8

// / / \

// / / \

// 0 7 9

//

// then it should be printed like this:

//

// 6

// 5

// 0

// 8

// 7

// 9

//

// Helper function that you should use inside your print function

void printFrom(TreeNode* startintPoint, int numSpaces) const;

// copyFrom

// Recursively copy another tree's nodes. Use

// pre-order traversal. Use this in CopyOther function.

void copyFrom(TreeNode* startintPoint);

// deleteFrom

// should implement and use in the delete function.

// Deletes the node with the value specified in the below function.

void deleteFrom(TreeNode* startintPoint, int value);

// clearFrom

// Recursively delete nodes. Use post-order traversal.

// Use it in clear function.

void clearFrom(TreeNode* startingPoint);

};

#endif

---------THE MAIN.CPP FILE called tree_main.cpp -------------------

#include "tree.h"

#include

using namespace std;

int main()

{

Tree t;

t.push(4);

t.push(2);

t.push(1);

t.push(3);

t.push(6);

t.push(5);

cout<<"t is : "<

t.print();

Tree t3(t);

t3.push(7);

cout<<"t is : "<

t.print();

cout<<"t3 is : "<

t3.print();

Tree t2;

t2.push(2);

t2.push(1);

t2.push(3);

t2 = t;

t2.push(8);

t2.push(9);

t2.push(11);

cout<<"t2 is : "<

t2.print();

cout<<"t is : "<

t.print();

t2.deleteNode(1);

t2.deleteNode(5);

cout<<"t2 is : "<

t2.print();

TreeNode *node = t.find(5);

cout << "found: " << node->value << endl;

node = t.find(100000);

cout << "t.find(100000): " << node << endl;

}

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

Database Driven Web Sites

Authors: Mike Morrison, Joline Morrison

1st Edition

061901556X, 978-0619015565

More Books

Students also viewed these Databases questions

Question

How do Excel Pivot Tables handle data from non OLAP databases?

Answered: 1 week ago