Implement the simplified heap class for integers

Assignment Help C/C++ Programming
Reference no: EM132136223

1. Implement the Binary Search Tree (BST) in C++, using the Node class template provided below. Please read the provided helper methods in class BST, especially for deleteValue(), make sure you get a fully understanding of the recursive algorithm, by comparing it with Algorithms 3.4.15 and 3.4.16 from page 128 to 130. Are they equivalent?

template <class T>
class Node {
public:
T value;
Node *left;
Node *right;

Node(T val) {
value = val;
left = NULL;
right = NULL;
}

Node(T val, Node<T> left, Node<T> right) {
value = val;
left = left;
right = right;
}
};

template <class T>
class BST {

private:
Node<T> *root;
intheightHelper(Node<T> *root) {
if (!root) return 0;
else return 1 + max(heightHelper(root->left), heightHelper(root->right));
}
bool deleteValueHelper(Node<T>* parent, Node<T>* current, T value) {
if (!current) return false;
if (current->value == value) {
if (current->left == NULL || current->right == NULL) {
Node<T>* temp = current->left;
if (current->right) temp = current->right;
if (parent) {
if (parent->left == current) {
parent->left = temp;
} else {
parent->right = temp;
}
} else {
this->root = temp;
}
} else {
Node<T>* validSubs = current->right;
while (validSubs->left) {
validSubs = validSubs->left;
}
T temp = current->value;
current->value = validSubs->value;
validSubs->value = temp;
return deleteValueHelper(current, current->right, temp);
}
delete current;
return true;
}
return deleteValueHelper(current, current->left, value) ||
deleteValueHelper(current, current->right, value);
}


public:
void add(T val) {
}

// print items in ascending order
void print() {
}

intnodesCount() {

}

intheight() {
return heightHelper(this->root);
}

bool deleteValue(T value) {
return this->deleteValueHelper(NULL, this->root, value);
}
};

Then test it with the following list of last names in our class:
intmain() {
constsize_tarraySize{17};
array <string, arraySize>names{"Taylor", "Tian", "Wheeler",
"Wilmot", "Hoffmann", "Fall", "Kline", "Aparicio",
"Li", "Hess", "Stenseng", "Weiss", "Hood", "Christiansen",
"Dale", "Seward", "Han"};

BST<string> *bst = new BST<string>();

for(size_ti{0}; i<names.size(); ++i)
bst->add(names[i]);
bst->print();
cout<<endl;
cout<<"Nodes count: "<<bst->nodesCount();
cout<<endl;
cout<<"Height: "<<bst->height();
cout<<endl;

bst->deleteValue("Tian");
cout<<"Tian removed: ";
bst->print();
cout<<endl;

bst->deleteValue("Kline");
cout<<"Kline removed: ";
bst->print();
cout<<endl;

return 0;
}

2. Implement the simplified Heap class for integers, in C++. Refer to the algorithms in the textbook.

The required methods are:
- Siftdown (algorithm 3.5.7)
- heapify (algorithm 3.5.12)
- print (to print the array of integers)
Test your heap's heapify method,using the following sequence of integers as initial status of the array.Display the before and after status.
42, 58, 11, 33, 81, 65, 32, 19, 24, 57, 13, 47, 66, 41, 95

Verified Expert

The solution file is prepared in ms word and implementation done it in dev c++ . This solution has twos files are bst.cpp and heap.cpp. The bst file implemented binary search tree algorithm using recursive method in delete node operation and insert node. The insert node can be vector string and performed delete node , compared 2 algorithms for node do not have child and node have two children. The second file implemented heap sort with heapify and sift algorithm to sort array. The output of the programs are attached in the solution.

Reference no: EM132136223

Questions Cloud

Explaining employee vs. independent contractor : You will write a paper explaining employee vs. independent contractor classifications using the Madrid and Berne case scenario provided below.
Staff cutbacks because of declining profits : A paper manufacturer is forced to make staff cutbacks because of declining profits. It decides to cut back each employee's hours and pay by one-half day
Evaluate google diversification into new products : Evaluate Google's diversification into new products and businesses. With particular reference to (a) video sharing (You Tube), (b) browsers (Chrome)
Perceptions to match objective reality : Leaders who recognize perceptual distortions can adjust their perceptions to match objective reality?
Implement the simplified heap class for integers : Implement the simplified Heap class for integers, in C++. Refer to the algorithms in the textbook - Implement the Binary Search Tree
Formulate a linear programming model : Introduction to Math Programming - Formulate a linear programming model for the following description. What do you observe about the solution
Successful or not in the organization : What factors determine whether teams are successful or not in the organization?
Do you agree or disagree with his conclusions : Do you agree or disagree with his conclusions? Can goals of a business sometimes conflict with the goals of society and/or a community?
Implications of generational differences in the workforce : What are the implications of generational differences in the workforce? What strategies should companies consider from a training

Reviews

inf2136223

11/26/2018 11:35:42 PM

I got a perfect essay that made me score 9/10 in the assessment exam and it will surely enhance my grades that I had degraded in the previous exam due to incorrect and late submission of the assignment. But this time since it was done by your perfect tutors I was expecting some good marks and hurray I got it. Thanks to you dear team.

Write a Review

C/C++ Programming Questions & Answers

  Design and code an ansi standard c program

Design and code an ANSI Standard C program which enhances the code below only so as to make it a modular program. (each of the program four tasks of ask, get, alter and display are now separate functions).

  Provide appropriate accessor methods to set and get the data

Provide the appropriate accessor methods to set and get the data for each of these class variables. For example setYear(int year) and getYear().

  A minivan has two sliding doors

A minivan has two sliding doors.  Each door can be opened by either a dashboard switch, its inside handle, or its outside handle. However, the inside handles do not work if a child lock switch is activated.  In order for the sliding doors to open, th..

  Prepare a bus reservation system

You will be asked to demonstrate your program to your tutor - You should be prepared to answer questions concerning your design, code and test plan.

  What is the value of the expression

following are the simple question.What is the value of the expression  1000 /10 /10 + 100 %1.What is the output printf("%c",65)What is the output {int i=10;printf("%d",10/i++);}.

  Create a program that forces one data type to be converted

Create a program in C++ or C# that forces one data type to be converted into another (using explicit type conversion). Show the Source code.

  Explain the importance of pointers

Assuming an array has to be sorted, inserting a new element required shifting, in the worst case, n elements, where n is the number of elements in the array, i.e.:

  Using virtual methods, organize the animals

Using virtual methods, organize the animals in a small zoo, having as guests lions, parrots, alligators, penguins, elephants, cobras, zebras, hawks, turkeys and rabbits.

  Create class that allows you to generate number of plants

In your driver, you should create at least 2 different plants, after you instantiate a plant, you should print out the information about the plant, then water the plant at least three times and then print out the plant again.

  Write the equation for the logic-hazard free function

Write the equation for the minimum Boolean function first. Now, eliminate all logic hazards that can result from the 0's

  Write a for loop that adds the integers

Write a for loop that adds the integers between lo and hi (inclusive), and stores the result in result.

  Creating virtual computer using c programming language

I need help with creating virtual computer using c programming language, the program should exactly work like a real computer

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd