Write a program to evaluate empirically the following strategies for removing nodes with two children:
a) Replace with the largest node, X, in TL and recursively remove X.
b) Alternately replace with the largest node in TL and the smallest node in TR, and recursively remove the appropriate node.
c) Replace with either the largest node in TL or the smallest node in TR (recursively removing the appropriate node), making the choice randomly.
Which strategy seems to give the most balance? Which takes the least CPU time to process the entire sequence?
NOTE: please submit atleast 3 test cases in C++.
binary.h
#ifndef BINARY_H
#define BINARY_H
class Node
{
private:
int data;
Node *parentNode;
Node *leftChild;
Node *rightChild;
public:
Node(int);
Node(int, Node*);
void setParent(Node*);
void setLeftChild(Node*);
void setRightChild(Node*);
Node* getParent();
Node* getLeftChild();
Node* getRightChild();
int getValue();
void setValue(int);
};
// https://www.youtube.com/watch?v=gcULXE7ViZw
// Todo
//
// find/search for node
// insert node
// delete node
// edit node
Node* deleteNode_A(Node* root, int data);
Node* deleteNode_B(Node* root, int data);
Node* deleteNode_C(Node* root, int data);
/**
* To delete a (leaf) node from bst
* 1) remove refeference of node from its parent so it will be detatched
* from the tree
* 2) wipe the node object from memory
*/
/**
* To delete a non-leaf (one child) node from bst
* 1) link this.parent to this.child
*/
/**
* To delete a non-leaf (two child) node from bst
* 1) Find minimum in right subtree
* 2) copy the value in targetted node
* 3) delete duplicate from right subtree
*/
Node* findMin(Node*);
Node* findMax(Node*);
int insertNode(Node* , Node*);
#endif
main.cpp
#include <iostream>
#include "binary.h"
void printTree(Node* node);
int main(int argc, char const *argv[])
{
std::cout << "Hello World" << std::endl;
Node *rootA = new Node(12);
Node *rootB = new Node(12);
Node *rootC = new Node(12);
// populating the binary tree
int input[13] = {5,15,3,7,13,17, 20,1,9,14,18,8,11};
for (int i = 0; i < 13; i++)
{
insertNode(new Node(input[i]), rootA);
insertNode(new Node(input[i]), rootB);
insertNode(new Node(input[i]), rootC);
}
std::cout << "\n\nMETHOD A" << std::endl;
std::cout << "\nBefore deletion:" << std::endl;
printTree(rootA);
std::cout << "\nDeleting..." << std::endl;
deleteNode_A(rootA, 9);
std::cout << "\nAfter deletion:" << std::endl;
printTree(rootA);
std::cout << "\n\nMETHOD B" << std::endl;
std::cout << "\nBefore deletion:" << std::endl;
printTree(rootB);
std::cout << "\nDeleting..." << std::endl;
deleteNode_B(rootB, 9);
std::cout << "\nAfter deletion:" << std::endl;
printTree(rootB);
std::cout << "\n\nMETHOD C" << std::endl;
std::cout << "\nCefore deletion:" << std::endl;
printTree(rootC);
std::cout << "\nDeleting..." << std::endl;
deleteNode_C(rootC, 9);
std::cout << "\nAfter deletion:" << std::endl;
printTree(rootC);
return 0;
}
void printTree(Node* node)
{
std::cout << "Subtree " << node->getValue() << " => ";
if (node->getLeftChild() == NULL)
{
std::cout << "(NULL, ";
}
else
{
std::cout << "(" << node->getLeftChild()->getValue() << ", ";
}
if (node->getRightChild() == NULL)
{
std::cout << "NULL)" << std::endl;
}
else
{
std::cout << node->getRightChild()->getValue() << ")" << std::endl;
}
if (node->getLeftChild() != NULL)
{
printTree(node->getLeftChild());
}
if (node->getRightChild() != NULL)
{
printTree(node->getRightChild());
}
}
GENERATED FROM PROGRAM:
> Running program...
METHOD A
Before deletion:
Subtree 12 => (5, 15)
Subtree 5 => (3, 7)
Subtree 3 => (1, NULL)
Subtree 1 => (NULL, NULL)
Subtree 7 => (NULL, 9)
Subtree 9 => (8, 11)
Subtree 8 => (NULL, NULL)
Subtree 11 => (NULL, NULL)
Subtree 15 => (13, 17)
Subtree 13 => (NULL, 14)
Subtree 14 => (NULL, NULL)
Subtree 17 => (NULL, 20)
Subtree 20 => (18, NULL)
Subtree 18 => (NULL, NULL)
Deleting...
After deletion:
Subtree 13 => (5, 17)
Subtree 5 => (3, 7)
Subtree 3 => (1, NULL)
Subtree 1 => (NULL, NULL)
Subtree 7 => (NULL, 11)
Subtree 11 => (8, NULL)
Subtree 8 => (NULL, NULL)
Subtree 17 => (14, 20)
Subtree 14 => (NULL, NULL)
Subtree 20 => (18, NULL)
Subtree 18 => (NULL, NULL)
Deleting 9, 15, and 12 ran for 0.002 ms (2 clicks)
METHOD B
Before deletion:
Subtree 12 => (5, 15)
Subtree 5 => (3, 7)
Subtree 3 => (1, NULL)
Subtree 1 => (NULL, NULL)
Subtree 7 => (NULL, 9)
Subtree 9 => (8, 11)
Subtree 8 => (NULL, NULL)
Subtree 11 => (NULL, NULL)
Subtree 15 => (13, 17)
Subtree 13 => (NULL, 14)
Subtree 14 => (NULL, NULL)
Subtree 17 => (NULL, 20)
Subtree 20 => (18, NULL)
Subtree 18 => (NULL, NULL)
Deleting...
After deletion:
Subtree 11 => (5, 14)
Subtree 5 => (3, 7)
Subtree 3 => (1, NULL)
Subtree 1 => (NULL, NULL)
Subtree 7 => (NULL, 8)
Subtree 8 => (NULL, NULL)
Subtree 14 => (13, 17)
Subtree 13 => (NULL, NULL)
Subtree 17 => (NULL, 20)
Subtree 20 => (18, NULL)
Subtree 18 => (NULL, NULL)
Deleting 9, 15, and 12 ran for 0.002 ms (2 clicks)
METHOD C
Before deletion:
Subtree 12 => (5, 15)
Subtree 5 => (3, 7)
Subtree 3 => (1, NULL)
Subtree 1 => (NULL, NULL)
Subtree 7 => (NULL, 9)
Subtree 9 => (8, 11)
Subtree 8 => (NULL, NULL)
Subtree 11 => (NULL, NULL)
Subtree 15 => (13, 17)
Subtree 13 => (NULL, 14)
Subtree 14 => (NULL, NULL)
Subtree 17 => (NULL, 20)
Subtree 20 => (18, NULL)
Subtree 18 => (NULL, NULL)
Deleting...
After deletion:
Subtree 13 => (5, 17)
Subtree 5 => (3, 7)
Subtree 3 => (1, NULL)
Subtree 1 => (NULL, NULL)
Subtree 7 => (NULL, 11)
Subtree 11 => (8, NULL)
Subtree 8 => (NULL, NULL)
Subtree 17 => (14, 20)
Subtree 14 => (NULL, NULL)
Subtree 20 => (18, NULL)
Subtree 18 => (NULL, NULL)
Deleting 9, 15, and 12 ran for 0.006 ms (6 clicks)
> Finished run.
Write a program to evaluate empirically the following strategies for removing nodes with two children: a)...
For this assignment, you will write a program to work with Huffman encoding. Huffman code is an optimal prefix code, which means no code is the prefix of another code. Most of the code is included. You will need to extend the code to complete three additional methods. In particular, code to actually build the Huffman tree is provided. It uses a data file containing the frequency of occurrence of characters. You will write the following three methods in the...
JAVA 3 PLEASE ANSWER AS MANY QUESTIONS AS POSSIBLE! ONLY 2 QUESTIONS LEFT THIS MONTH!!! Question 12 pts Which is a valid constructor for Thread? Thread ( Runnable r, int priority ); Thread ( Runnable r, String name ); Thread ( int priority ); Thread ( Runnable r, ThreadGroup g ); Flag this Question Question 22 pts What method in the Thread class is responsible for pausing a thread for a specific amount of milliseconds? pause(). sleep(). hang(). kill(). Flag...