Question

Convert the TreeArray C++ source code(posted below) into a BinaryTree, using this TreeNode definition: class TreeNode<T>...

Convert the TreeArray C++ source code(posted below) into a BinaryTree, using this TreeNode definition:

class TreeNode<T>

      T data

      TreeNode<T> left

      TreeNode<T> 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 will likely require writing a compare function or operator.

Hint: Think LEFT if the index is calculate (2n+1) and RIGHT if index is (2n+2).

Source code:

#include<iostream>

using namespace std;

class BinarySearchTree

{

   int size;

   int* array;

public:

   BinarySearchTree(int s) : size(s)

   {

       size = reSize(size);

       array = new int[size];

       for (int x = 0; x < size; x++)

           array[x] = NULL;

   }

   int reSize(int x)

   {

       int value = pow(2, x);

       return value;

   }

   void insert(int x)

   {

       int currentIndex = 0;

       cout << "insert: " << x;

       while (true)

       {

           if (array[currentIndex] == NULL)

           {

               array[currentIndex] = x;

               cout << " Inserted at index: " << currentIndex << endl;

               break;

           }

           else if (x < array[currentIndex])

           {

               cout << " <<< Left ";

               currentIndex = (2 * currentIndex + 1);

           }

           else

           {

               cout << " Right >>> ";

               currentIndex = (2 * currentIndex + 2);

           }

       }

   }

   void search(int x)

   {

       cout << "Search: " << x << endl;

       int currentIndex = 0;

       while (true)

       {

           if (array[currentIndex] == NULL)

           {

               cout << "Not Found" << endl;

               break;

           }

           if (array[currentIndex] == x)

           {

               cout << "Found at index: " << currentIndex << endl;

               break;

           }

           else if (x < array[currentIndex])

           {

               cout << " <<< Left " << endl;

               currentIndex = (2 * currentIndex + 1);

           }

           else

           {

               cout << " >>> Right " << endl;

               currentIndex = (2 * currentIndex + 2);

           }

       }

   }

   void parent(int x)

   {

       while (x != 0)

       {

           x = (x - 1) / 2;

           cout << "---|";

       }

   }

   void inOrder(int currentIndex)

   {

       if (array[currentIndex] != NULL)

       {

           inOrder(2 * currentIndex + 1);

           parent(currentIndex);

           cout << array[currentIndex] << endl;

           inOrder(2 * currentIndex + 2);

       }

   }

   void preOrder(int currentIndex)

   {

       if (array[currentIndex] != NULL)

       {

           parent(currentIndex);

           cout << array[currentIndex] << " " << endl;

           preOrder(2 * currentIndex + 1);

           preOrder(2 * currentIndex + 2);

       }

   }

   void postOrder(int currentIndex)

   {

       if (array[currentIndex] != NULL)

       {

           postOrder(2 * currentIndex + 1);

           postOrder(2 * currentIndex + 2);

           parent(currentIndex);

           cout << array[currentIndex] << " " << endl;

       }

   }

   void printArray()

   {

       for (int i = 0; i < size; i++)

           if (array[i])

               cout << array[i] << ' ';

           else

               cout << '.';

       cout << endl;

   }

};

int main()

{

   int array[] = { 42, 68, 35, 1, 70, 25, 79, 59, 63, 65 };

   int size = sizeof(array) / sizeof(array[0]);

   BinarySearchTree bst(size);

   for (int i = 0; i < size; i++)

       bst.insert(array[i]);

   cout << "Inorder" << endl;

   bst.inOrder(0);

   cout << "Preorder" << endl;

   bst.preOrder(0);

   cout << "Postorder" << endl;

   bst.postOrder(0);

   cout << "Search" << endl;

   bst.search(65);

   cout << "In memory" << endl;

   bst.printArray();

};

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Solution ::

iven Series is Y2)68, 35,01,-70,25)79丿59, 63,(5 59 2 S 2 ol 6 8 63 Cenvert te tree into mãc effici usina notationsin fiqure (

Inoidevs o! as 3542 S963 65 68 70 9 poslovder: 01 3s as S96543干9.70 68 42 /// *** Thank You *** ///

Add a comment
Know the answer?
Add Answer to:
Convert the TreeArray C++ source code(posted below) into a BinaryTree, using this TreeNode definition: class TreeNode<T>...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Question - modify the code below so that for a node, the value of every node...

    Question - modify the code below so that for a node, the value of every node of its right subtree is less the node, and the value of each node of its left subtree is greater than the node. - create such a binary tree in the Main method, and call the following method:  InOrder(Node theRoot),  PreOrder(Node theRoot),  PostOrder(Node theRoot),  FindMin(),  FindMax(),  Find(int key) using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;...

  • I need to make it so this program outputs to an output.txt, the program works fine,...

    I need to make it so this program outputs to an output.txt, the program works fine, just need it to fprintf to output.txt #include <stdio.h> #include <string.h> #include <malloc.h> #define MAX 30 struct treeNode { char names[MAX];    struct treeNode *right; struct treeNode *left; }*node; void searchName(char names[], struct treeNode ** parent, struct treeNode ** location) { struct treeNode * ptr, * tempPtr; if(node == NULL)    { *location = NULL; *parent = NULL; return; } if(strcmp(names, node->names) == 0)...

  • C++ EXERCISE (DATA STRUCTURES). I just need a code for some functions that are missing. Please...

    C++ EXERCISE (DATA STRUCTURES). I just need a code for some functions that are missing. Please help me figure out. Thanks. C++ BST implementation (using a struct) Enter the code below, and then compile and run the program. After the program runs successfully, add the following functions: postorder() This function is similar to the inorder() and preorder() functions, but demonstrates postorder tree traversal. displayParentsWithTwo() This function is similar to the displayParents WithOne() function, but displays nodes having only two children....

  • Please I need help ASAP Java Programing: Binary Search Tree Fully implement the BST class in Listing 25.4 (on page 961 of the 11th Edition of the text). Design and write a (main) driver program to com...

    Please I need help ASAP Java Programing: Binary Search Tree Fully implement the BST class in Listing 25.4 (on page 961 of the 11th Edition of the text). Design and write a (main) driver program to completely test every method in the BST class to ensure the class meets all its requirements. You should read the Listing 25.5: TestBST.java for an idea of what your program should look like. Listing 25.4 BST.java public class BST> extends AbstractTree { protected TreeNode...

  • write a new test program called RemoveDuplicates.java. The program reads text input from keyboard or a text file and adds the words to a BST. The program then traverses the BST and prints out the word...

    write a new test program called RemoveDuplicates.java. The program reads text input from keyboard or a text file and adds the words to a BST. The program then traverses the BST and prints out the words in order (based on ASCII/UNICODE order) on the screen (or to output text file). Note that you may need to make some changes to BST.java. Sample test: ----jGRASP exec: java -ea removeDuplicates Original Text: a B 2 n w C q K l 0...

  • TreeNode.java public class TreeNode {    public int key;    public TreeNode p;    public TreeNode...

    TreeNode.java public class TreeNode {    public int key;    public TreeNode p;    public TreeNode left;    public TreeNode right;       public TreeNode () {        p = left = right = null;    }       public TreeNode (int k) {        key = k;        p = left = right = null;    } } BinarySearchTree.java public class BinarySearchTree {    public TreeNode root;       public BinarySearchTree () {        root...

  • C++ Help with Test #1 (at bottom) to work properly. May adjust code to work or...

    C++ Help with Test #1 (at bottom) to work properly. May adjust code to work or add any additional code. Do not use global variables. #include <iostream> #include <string> using std::endl; using std::cout; using std::cin; using std::string; void pressAnyKeyToContinue() { printf("Press any key to continue\n"); cin.get(); } //This helps with testing, do not modify. bool checkTest(string testName, int whatItShouldBe, int whatItIs) {    if (whatItShouldBe == whatItIs) { cout << "Passed " << testName << endl; return true; } else...

  • Q. write the algorithm for each function in this code: void insert(int x, node*&p) { //cheak...

    Q. write the algorithm for each function in this code: void insert(int x, node*&p) { //cheak if the pointer is pointing to null. if (p==NULL) {     p = new node;     p->key=x;     p->left=NULL;     p->right=NULL; } else {     //Cheak if the element to be inserted is smaller than the root.     if (x < p->key)     {       //call the function itself with new parameters.       insert(x,p->left);     }     //cheak if the alement to be inserted...

  • 3. (Gaddis Exercises 20.4) Tree Height Write a recursive member function for the BinaryTree class that...

    3. (Gaddis Exercises 20.4) Tree Height Write a recursive member function for the BinaryTree class that returns the height of the tree. The height of the tree is the number of levels it contains. Demonstrate the function in a driver program. CPP FILE CODE: #include "BinaryTree.h" #include <iostream> using namespace std; BinaryTree::BinaryTree() { root = NULL; } BinaryTree::~BinaryTree() { destroy(root); } bool BinaryTree::search(int data) { return search(data, root); } void BinaryTree::insert(int data) { insert(data, root); } void BinaryTree::traverseInOrder() { traverseInOrder(root);...

  • I need to do a tree sort method but the treesortMethod is not working /****Binarytree class****\...

    I need to do a tree sort method but the treesortMethod is not working /****Binarytree class****\ package Tree; public class BinaryTree { private TreeNode root; // head of the list    //constructor - create an empty binary tree public BinaryTree() { root = null;    }    //isEmpty() - return true if tree is empty, false otherwise public boolean isEmpty() { return (root == null); }    //deleteTree() - remove all items from tree public void deleteList() { root =...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT