Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Exercise 1 Use provided C++ skeleton files to insert your code. A. Implement a binary tree class MyTree using pointers (define struct BinaryNode to store

Exercise 1

Use provided C++ skeleton files to insert your code. A.

Implement a binary tree class MyTree using pointers (define struct BinaryNode to store internal nodes), where each node has a pointer to its children and to its parent, and each node holds two variable, a string myString and an integer myInt.

Write functions in MyTree class to do the following:

Insert (int,string): Insert new node into first available position (to keep the tree almost complete). E.g.,

To get full points your Insert functions should be faster than O(n), where n is # nodes in tree.

Hint: you may use an additional private variable in MyTree class.

Preorder (): Output all strings in pre-order.

FindMax (): returns a pointer to the node with maximum myInt.

MakeBST (): converts the binary tree into a binary search tree (BST) with respect to myInt. That

is, move around node values (myString and myInt) to satisfy the BST property. Do not change the structure of the tree (i.e. the pointers) but only swap myString and myInt values. (Hint: if you need to sort an array, you can use STLs sort() method)

e.g.,

B. What is the big-Oh complexity of your functions above?

Also, what is the space complexity of your functions? Are they all in-place? If not, how much extra space do they need?

C.

Test and measure the performance of your functions.

Create sequences of 100, 1000, 10000, 100000 random (int, string) pairs and insert them into MyTree (using Insert(.,.) function). Measure the times to (a) build the tree, (b) execute Preorder(), (c) execute FindMax(), (d) execute MakeBST().

Report the times for each tree size in a table.

Here is the skeleton code:

#include

#include

#include

using namespace std;

class MyTree;

class BinaryNode {

public:

BinaryNode(string s, int m) :myString(s), myInt(m) {};

~BinaryNode() {};

int getInt() const { return myInt; };

string getString() const { return myString; };

private:

string myString;

int myInt;

BinaryNode *lchild;

BinaryNode *rchild;

friend class MyTree;

};

class MyTree {

public:

MyTree();

~MyTree();

// Insert new node into first available position (to keep the tree almost complete).

void insert(int x, string s);

//Output all strings in pre-order

//all the strings will be print in one line separated by spaces

void preorder() const;

//Returns a pointer to the node with maximum myInt

BinaryNode *findMax() const;

// Converts the binary tree into a binary search tree (BST) with respect to myInt.

// That is, move around node values (myString and myInt) to satisfy the BST property.

void makeBST();

private:

MyTree *root;

};

MyTree::MyTree() {

}

MyTree::~MyTree() {

}

void MyTree::insert(int x, string s) {

}

void MyTree::preorder() const {}

BinaryNode* MyTree::findMax() const { return NULL; }

void MyTree::makeBST() {}

//Testing (you must implement yours for the perfomance studies)

int main() {

MyTree t;

//....

}

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

Machine Learning And Knowledge Discovery In Databases European Conference Ecml Pkdd 2010 Barcelona Spain September 2010 Proceedings Part 2 Lnai 6322

Authors: Jose L. Balcazar ,Francesco Bonchi ,Aristides Gionis ,Michele Sebag

2010th Edition

364215882X, 978-3642158827

More Books

Students also viewed these Databases questions

Question

explain what is meant by the terms unitarism and pluralism

Answered: 1 week ago