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 array;

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 alist = { 42, 68, 35, 1, 70, 25, 79, 59, 63, 65 };

//array alist = { 1, 3, 5, 7, 9, 11, 13, 15, 17 }; //

degenerate

BinarySearchTree bst(512);

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);

}

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

#include #include #include #include #include "cons oleColor.h" using namespace stdi template class BinarySearchTree vectorT array: int size - 0; int lastUsed = 0; public: BinarySearchTree (int s) size (s) size = resize (size); array.resize (size) for (int x 0; x insert: " > " >> Right " array[0]) cout array[0]) cout array[0]) cout array[0]) cout alist- 42, 68, 35, 1, 70, 25, 79, 59, 63, 65; //arrayKint, 10> alist-1, 3, 5, 7, 9, 11, 13, 15, 17 i 1I degenerate BinarySearchTree bst (512) for (int = 0; 1

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!