Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program to store a tree as an array. In your test program, be sure to pr

ID: 3839593 • Letter: W

Question

Write a program to store a tree as an array. In your test program, be sure to print the array values, and to print the values in tree "format". Write a function to add an element to an existing tree. Write a function to delete an element from an existing tree. Write a function to perform an "in-order" traversal on an existing tree. Write a function to perform an "pre-order" traversal on an existing tree. Write a function to perform an "post-order' traversal on an existing tree. Write a function to perform an "level-order" traversal on an existing tree. Write a function to find the children of a specified node. Write a function to find the parent of a specified node.

Explanation / Answer

BST (int sz) {

        this -> sz = extendSz(sz);

        this -> arr = new int[this -> sz];

        for(int a = 0; a < this -> sz; a++){

            arr[a] = NULL;

        }

    }

-------------------------------------------------------------------

void BST::insert(int a) {

    int crrtInd = 0;

    cout << "Inserting: " << a;

    while(true) {

        if(arr[crrtInd] == NULL){

            arr[crrtInd] = a;

            cout << " Element inserted at Index: " << crrtInd << endl;

            break;

        }else if(arr[crrtInd] <= a) {

            if(arr[crrtInd] == a){

                cout << "ERROR!-- Repeating element" << endl;

                break;

            }else

            cout << " Right ";

            crrtInd = (2 * crrtInd + 2);

        }else if(arr[crrtInd] >= a) {

             if(arr[crrtInd] == a){

                cout << "Error!!! Element repeating…" << endl;

                break;

            }else

            cout << " Left ";

            crrtInd = (2 * crrtInd + 1);

        }

    }

}

-------------------------------------------------------------

void BST::inOrder(int crrtInd){

    if(arr[crrtInd] != NULL) {

            inOrder(2 * crrtInd + 1);

            prt(crrtInd);

            cout << arr[crrtInd] << endl;

            inOrder(2 * crrtInd + 2);

    }

}

--------------------------------------------------------------------------

void BST::postOrder(int crrtInd) {

    if(arr[crrtInd] != NULL){

        postOrder(2 * crrtInd + 1);

        postOrder(2 * crrtInd + 2);

        prt(crrtInd);

        cout << arr[crrtInd] << " " << endl;

    }

}

----------------------------------------------------------------------------

void BST::preOrder(int crrtInd) {

    if(arr[crrtInd] != NULL) {

        preOrder(2 * crrtInd + 1);

        prt(crrtInd);

        cout << arr[crrtInd] << " " << endl;

        preOrder(2 * crrtInd + 2);

    }

}

---------------------------------------------------------------------------------------

void BST::search(int a){

    int crrtInd = 0;

    while (true) {

            if (arr[crrtInd] == NULL) {

            cout << "Element not found" << endl;

            break;

            }

            if (arr[crrtInd] == a) {

            cout << "Element found at index: " << crrtInd << endl;

            break;

            }

            else if(arr[crrtInd] < a) {

            crrtInd = (2 * crrtInd + 2);

        }

            else if(arr[crrtInd] > a) {

            crrtInd = (2 * crrtInd + 1);

        }

    }

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote