Question
C++ Convert the TreeArray C++ source code into a BinaryTree, using this TreeNode definition: class TreeNode T data TreeNode left TreeNode right Since this TreeNode
C++
Convert the TreeArray C++ source code into a BinaryTree, using this TreeNode definition:
class TreeNode T data TreeNode left TreeNode right
Since this TreeNode is a generic Template, use any data file we've used this quarter to store the data in the BinaryTree. To do this in will likely require writing a compare function or operator.
Hint: Think LEFT node if the index is calculate (2n+1) and RIGHT node if index is (2n+2)
Add functionality to your tree source to determine if a tree is "complete" or not.
This is TreeArray
#include
#include
#include
#include
#include "ConsoleColor.h"
using namespace std;
template
class BinarySearchTree
{
vector
int size = 0;
int lastUsed = 0;
public:
BinarySearchTree(int s) : size(s)
{
size = reSize(size);
array.resize(size);
for (int x = 0; x
array[x] = NULL;
}
int reSize(int x)
{
int value = x;
return value;
}
void insert(T x)
{
int currentIndex = 0;
cout insert: "
while (true)
{
if (array[currentIndex] == NULL)
{
array[currentIndex] = x;
cout
cout
yellow);
cout
lastUsed = currentIndex;
break;
}
else if (x
{
currentIndex = (2 * currentIndex + 1);
cout
currentIndex
}
else
{
currentIndex = (2 * currentIndex + 2);
cout >> "
currentIndex
}
}
}
void search(T x)
{
cout
int currentIndex = 0;
while (true)
{
if (array[currentIndex] == NULL)
{
cout
break;
}
if (array[currentIndex] == x)
{
cout
yellow
break;
}
else if (x
{
cout
currentIndex = (2 * currentIndex + 1);
}
else
{
cout >> Right "
endl;
currentIndex = (2 * currentIndex + 2);
}
}
}
void printHeight(int x)
{
while (x != 0)
{
x = (x - 1) / 2;
cout
}
}
void inOrder(int currentIndex)
{
if (array[currentIndex] != NULL)
{
inOrder(2 * currentIndex + 1);
printHeight(currentIndex);
if (array[currentIndex] > array[0])
cout
else
cout
"]:"
inOrder(2 * currentIndex + 2);
}
}
void preOrder(int currentIndex)
{
if (array[currentIndex] != NULL)
{
printHeight(currentIndex);
if (array[currentIndex] > array[0])
cout
else
cout
"]:"
preOrder(2 * currentIndex + 1);
preOrder(2 * currentIndex + 2);
}
}
void postOrder(int currentIndex)
{
if (array[currentIndex] != NULL)
{
postOrder(2 * currentIndex + 1);
postOrder(2 * currentIndex + 2);
printHeight(currentIndex);
if (array[currentIndex] > array[0])
cout
else
cout
"]:"
}
}
void reverseOrder(int currentIndex)
{
if (array[currentIndex] != NULL)
{
reverseOrder(2 * currentIndex + 2);
printHeight(currentIndex);
if (array[currentIndex] > array[0])
cout
else
cout
"]:"
reverseOrder(2 * currentIndex + 1);
}
}
void printArray()
{
int exp = 0;
int sum = int(pow(2, exp));
int power = sum;
for (int i = 0; i
{
if (i == sum)
{
cout
exp++;
power = int(pow(2, exp));
sum += power;
}
if (array[i])
(i % 2) ? cout
' ' : cout
else
cout
}
for (int j = lastUsed; j
cout
cout
}
};
int main()
{
array
//array
degenerate
BinarySearchTree
for (int i = 0; i
bst.insert(alist[i]);
cout
cout
bst.inOrder(0);
cout
bst.preOrder(0);
cout
bst.postOrder(0);
cout
bst.search(65);
cout
bst.printArray();
cout
bst.reverseOrder(0);
}
#include
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started