Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Help with Binary trees in C++ I am having issues coding this assignment in C++ #include using namespace std; class TNode { public: int val;

Help with Binary trees in C++

I am having issues coding this assignment in C++

image text in transcribedimage text in transcribed

#include

using namespace std;

class TNode

{

public:

int val;

TNode(){}

TNode(int v){val = v;}

TNode * left;

TNode * right;

TNode * parent;

};

class BTree

{

public:

//constructors and destructor

BTree();

BTree(TNode *r);// initialize BTree with the root r. r is the only node.

BTree(const BTree & t); // copy constructor

BTree(const int *p, const int n);// similar to the copy constructor, but your input is of the array form.

// input is given an array that denotes a tree that uses up to n slots. The size of the tree is not given directly as input.

// the array is pointed by p and has size n, where p[0] is used to store the empty symbol.

//the function converts the array representation into a linked-list representation, i.e. BTree

~BTree();

int size;

TNode *root;

TNode * convertpos(int position);// find the pointer where the position points to

void add2left(TNode *newnode, TNode *p);

void add2right(TNode *newnode, TNode *p);

void add2left(TNode *newnode, int position);

void add2right(TNode *newnode, int position);

void add2left(BTree *newsubtree, int position);

void add2right(BTree *newsubtree, int position);

void removeleaf(int position);

void removeleaf(TNode *p);

void swapnodes(TNode *n1, TNode *n2);//swap-the-values is fine

int * Toarray(int &n);// convert the BT into the array form. Determine what n should be, and new an array of size n+1, and store the empty symbol at array[0]. Convert the BT into the array form and retrun the array pointer. The size of the array will be given to the variable n.

void print_pre_order();// print the node as you traverse according to the order.

void print_in_order();

void print_post_order();

bool isValidBST(); // return whether this BT is a valid BST

};

bool isValidBT(const int *p, const int n);

//Determine whether the array forms a valid BT.

// the input format is the same as the above

int main()

{

return 0;

}

COP 3530 Data Structure and Algorithm Analysis Homework 4 Feng-Hao Liu In this assignment, you are given several classes in the cpp file "BTree.cpp". Your task is to complete the implementation of the classes specified as below. You need to submit a single cpp file that contains everything about your source code. It must be compilable and executable. Do not submit things irrelevant (such as .exe) 1 Your Task You are given a class "TNode' that contains one integer value, and three pointers - one to the parent, one to the left child, and one to the right child. You need to complete the class "BTree" and two other functions specified in the cpp file Task 1: Write a function isValidBT that given inputs as a binary tree in the array form, and outputs whether the array forms a correct binary tree. The inputs contain a pointer p to an array, and an integer n denoting the number of slots used in the array representation. Here the array has size n, and we use p[0] to store the empty symbol for the binary tree as discussed in the class. For this representation, the tree has size n-1. Task 2: Implement the constructors (default and copy) of BTree, and the destructor. You need to make sure that the copy constructor makes a separate copy of the list In addition to the normal copy constructor, here we ask you to implement a special "copy constructor" that takes input a binary tree of the array form. The inputs have the same format as Task 1, and you need to copy the binary tree, and convert the array form/to the pointer-based construction Task 3: Implement the function convertpos that takes input an integer position, and returns the TNode pointer that points to the position-th node in the tree. We discussed about how to do this in the class Task 4: Implement the add2left, add2right functions. The functionalities are just as the names Here you need to consider how to handle different input of positions, i.e. places that you want to add the node/tree to. Task 2 can be useful in this case Task 5: Write the removeleaf function. You need to first check whether the input is a leaf or not If that is a leaf, then remove it. Otherwise do nothing COP 3530 Data Structure and Algorithm Analysis Homework 4 Feng-Hao Liu In this assignment, you are given several classes in the cpp file "BTree.cpp". Your task is to complete the implementation of the classes specified as below. You need to submit a single cpp file that contains everything about your source code. It must be compilable and executable. Do not submit things irrelevant (such as .exe) 1 Your Task You are given a class "TNode' that contains one integer value, and three pointers - one to the parent, one to the left child, and one to the right child. You need to complete the class "BTree" and two other functions specified in the cpp file Task 1: Write a function isValidBT that given inputs as a binary tree in the array form, and outputs whether the array forms a correct binary tree. The inputs contain a pointer p to an array, and an integer n denoting the number of slots used in the array representation. Here the array has size n, and we use p[0] to store the empty symbol for the binary tree as discussed in the class. For this representation, the tree has size n-1. Task 2: Implement the constructors (default and copy) of BTree, and the destructor. You need to make sure that the copy constructor makes a separate copy of the list In addition to the normal copy constructor, here we ask you to implement a special "copy constructor" that takes input a binary tree of the array form. The inputs have the same format as Task 1, and you need to copy the binary tree, and convert the array form/to the pointer-based construction Task 3: Implement the function convertpos that takes input an integer position, and returns the TNode pointer that points to the position-th node in the tree. We discussed about how to do this in the class Task 4: Implement the add2left, add2right functions. The functionalities are just as the names Here you need to consider how to handle different input of positions, i.e. places that you want to add the node/tree to. Task 2 can be useful in this case Task 5: Write the removeleaf function. You need to first check whether the input is a leaf or not If that is a leaf, then remove it. Otherwise do nothing

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_2

Step: 3

blur-text-image_3

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

Professional Microsoft SQL Server 2014 Administration

Authors: Adam Jorgensen, Bradley Ball

1st Edition

111885926X, 9781118859261

More Books

Students also viewed these Databases questions