Question

In C++ Given a pointer to the root of a binary search tree (has left, right,...

In C++ Given a pointer to the root of a binary search tree (has left, right, and parent pointers as well as a data section ) write a function (or functions) which will return an STL list (you should not define this class, it’s already included) with all of the values from the tree in sorted order. Your code should run in theta(N) time.

for the second part,.given a pointer to the first node of a linked list, you are asked to reverse the list. Explain, in English, not code, how you would complete such a task if you had a limited amount of memory (not enough to store the whole list twice!)

for the third part, given a binary search tree of ints, in which each node contains a size parameter (also an int), explain, in English, not code, how you would find the median element of the tree in theta(log N) time

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

Part 1:

C++ code:

// Header fies section
#include <iostream>
#include <string>
#include <list> // for STL list
using namespace std;

// TreeNode structure including left, right, and parent pointers as well as a data
struct TreeNode
{
    int data;
    TreeNode* left;
    TreeNode* right;
    TreeNode* parent;
};

// BinarySearchTree class declaration
class BinarySearchTree
{
public:
    // constructor
    BinarySearchTree();

    // function to insert the given key
    void insert(int key);

    // functions to print the tree
    void printPreorder();
    void printInorder();  
    void printPostorder();

    // function to make an ordered list from the tree
    list<int> makeOrderedList();

private:  
    // data field for the root of the tree
    TreeNode *root;

    // helper functions as private
    TreeNode* insertHelper(TreeNode *node, int key);
    void printPreorderHelper(TreeNode* node);
    void printInorderHelper(TreeNode* node);
    void printPostorderHelper(TreeNode *node);
    void makeOrderedListHelper(TreeNode* node, list<int> &result);
};

// default constructor sets the root to NULL
BinarySearchTree::BinarySearchTree()
{
    root = NULL;
}

// insert function inserts a new TreeNode in the tree
void BinarySearchTree::insert(int key)
{
    root = insertHelper(root, key);
}

// insertHelper function implementation
TreeNode* BinarySearchTree::insertHelper(TreeNode* node, int key)
{
    // if the node is NULL, create and return a new node
    if (node == NULL)
    {
        TreeNode* newNode = new TreeNode;
        newNode->data = key;
        newNode->left = NULL;
        newNode->right = NULL;
        newNode->parent = NULL;

        return newNode;
    }

    if (key < node->data)
    {
        // if the key is less than the node's data
        // insert the new node as the left node
        TreeNode* leftChild = insertHelper(node->left, key);
        node->left = leftChild;

        // set the parent of the left node
        leftChild->parent = node;
    }
    else if (key > node->data)
    {
        // if the key is greater than the node's data
        // insert the new node as the right node
        TreeNode* rightChild = insertHelper(node->right, key);
        node->right = rightChild;

        // set the parent of the right node
        rightChild->parent = node;
    }
    else
    {
        // do not allow the duplicates
    }

    // return the current node pointer
    return node;
}


// printPreorder function prints the tree elements using preorder traversal
void BinarySearchTree::printPreorder()
{
    printPreorderHelper(root);
}

// printPreorderHelper function implementation
void BinarySearchTree::printPreorderHelper(TreeNode* node)
{
    if (node != NULL)
    {
        // print the data of current node
        cout << node->data << " ";

        // go to left node
        printPreorderHelper(node->left);

        // go to right node
        printPreorderHelper(node->right);
    }
}

// printInorder function prints the tree elements using inorder traversal
void BinarySearchTree::printInorder()
{
    printInorderHelper(root);
}

// printInorderHelper function implementation
void BinarySearchTree::printInorderHelper(TreeNode* node)
{
    if (node != NULL)
    {
        // go to left node
        printInorderHelper(node->left);

        // print the data of current node
        cout << node->data << " ";

        // go to right node
        printInorderHelper(node->right);
    }
}

// printPostorder function prints the tree elements using inorder traversal
void BinarySearchTree::printPostorder()
{
    printPostorderHelper(root);
}

// printPostorderHelper function implementation
void BinarySearchTree::printPostorderHelper(TreeNode* node)
{
    if (node != NULL)
    {
        // go to left node
        printPostorderHelper(node->left);      

        // go to right node
        printPostorderHelper(node->right);

        // print the data of current node
        cout << node->data << " ";
    }
}

// makeOrderedList function returns an STL list with all of the values from the tree in sorted order
list<int> BinarySearchTree::makeOrderedList()
{
    list<int> result;

    makeOrderedListHelper(root, result);

    return result;
}

// makeOrderedListHelper function implementation
void BinarySearchTree::makeOrderedListHelper(TreeNode* node, list<int> &result)
{
    if (node != NULL)
    {
        // go to left node
        makeOrderedListHelper(node->left, result);

        result.push_back(node->data);

        // go to right node
        makeOrderedListHelper(node->right, result);
    }
}

// start main function
int main()
{
    // delcare a variable for the root of TreeNode
    BinarySearchTree bst;

    bst.insert(55);
    bst.insert(33);
    bst.insert(77);
    bst.insert(22);
    bst.insert(88);
    bst.insert(44);
    bst.insert(66);

    // print the tree using preorder traversal
    cout << "Preorder traversal of BST: ";
    bst.printPreorder();

    // print the tree using inorder traversal
    cout << "\nInorder traversal of BST:   ";
    bst.printInorder();

    // print the tree using postorder traversal
    cout << "\nPostorder traversal of BST: ";
    bst.printPostorder();

    // call the makeOrderedList function
    list<int> result = bst.makeOrderedList();
   
    // print the elements stored in the list
    cout << "\n\nElements stored in the list: ";
    for (auto itr = result.begin(); itr != result.end(); itr++)
    {
        cout << *itr << " ";
    }
    cout << endl;

    return 0;
} // end of main function

Sample Output:

Part 2:

Part 3:

Add a comment
Know the answer?
Add Answer to:
In C++ Given a pointer to the root of a binary search tree (has left, right,...
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
  • CODE IN JAVA** V. Given a pointer to the root of a binary tree and a...

    CODE IN JAVA** V. Given a pointer to the root of a binary tree and a pointer ‘p’ to a given node in the tree and a second pointer ‘q’ to another node in the tree write a routine which will return the total number of nodes in the tree that are on level ‘p’ and ‘q’. If they are on the same level you should multiply the answer by 3.

  • A collection of nodes is arranged as a binary search tree ordered on the field INFO which contain...

    A collection of nodes is arranged as a binary search tree ordered on the field INFO which contains distinct positive integers for each of the nodes. In addition to INFO, LLINK and RLINK, each node has three other fields CLASS SUCC and PRED CLASS is an information field containing a single letter that denotes the class to which the node belongs (there being up to 26 classes). The nodes in each class are arranged as a doubly-linked circular list with...

  • /* * struct for a single node in a binary tree. data contains the int *...

    /* * struct for a single node in a binary tree. data contains the int * stored in this node. left and right contain pointers to the left and * right subtrees respectively. * * All of the ints stored in the left subtree is smaller than data. * All of the ints stored in the right subtree is larger than data. */ struct node { int data; struct node *left; struct node *right; }; typedef struct node node; Write...

  • Recall that in a binary search tree, at every node, all elements to the left of...

    Recall that in a binary search tree, at every node, all elements to the left of the node have a smaller key, and all elements to the right of a node have a larger key. Write a program called that takes two parameters: a pointer to a binary search tree node, and an int parameter called min which will print all the elements bigger than the specified value, min. Your program should allow the user to enter data (integer) from...

  • given a pointer to the first node of a linked list, you are asked to reverse...

    given a pointer to the first node of a linked list, you are asked to reverse the list. Explain, in English, not code, how you would complete such a task if you had a limited amount of memory (not enough to store the whole list twice!)

  • SHOW HOW WE VISUALIZE THE BINARY SEARCH TREE WITH ROOT REFERENCED BY ROOT A. AFTER EACH...

    SHOW HOW WE VISUALIZE THE BINARY SEARCH TREE WITH ROOT REFERENCED BY ROOT A. AFTER EACH OF THE FOLLOWING CHANGES, ALSO LIST THE SEQUENCE OF BINARY SEARCH TREE METHOD CALLS, BOTH PUBLIC AND PRIVATE, THAT WOULD BE MADE WHEN EXECUTING THE CHANGE. USE THE ORIGINAL TREE TO ANSWER EACH PART OF THIS QUESTION. A) ADD NODE Q. B) REMOVE NODE R. DO THE QUESTION ABOVE WITH REFERENCE TO ROOT A GIVEN ABOVE. AND ALSO LIST THE SEQUENCE OF BINARY SEARCHTREE...

  • Need this in C++ Goals: Your task is to implement a binary search tree of linked...

    Need this in C++ Goals: Your task is to implement a binary search tree of linked lists of movies. Tree nodes will contain a letter of the alphabet and a linked list. The linked list will be an alphabetically sorted list of movies which start with that letter. MovieTree() ➔ Constructor: Initialize any member variables of the class to default ~MovieTree() ➔ Destructor: Free all memory that was allocated void printMovieInventory() ➔ Print every movie in the data structure in...

  • in javascrift please Given a pointer to the root of a binary tree write a routine...

    in javascrift please Given a pointer to the root of a binary tree write a routine that will delete every node in the tree that is currently a leaf (has no sons), and when finished tell you how many nodes were deleted as well as how many nodes are left in the tree.

  • 1. What is the worst case time complexity of insertion into a binary search tree with...

    1. What is the worst case time complexity of insertion into a binary search tree with n elements? You should use the most accurate asymptotic notation for your answer. 2. A binary search tree is given in the following. Draw the resulting binary search tree (to the right of the given tree) after deleting the node with key value 8. 10 3. You have a sorted array B with n elements, where n is very large. Array C is obtained...

  • C++ (Using Binary Search Trees) other methods will result in downvote Implement the binary search tree...

    C++ (Using Binary Search Trees) other methods will result in downvote Implement the binary search tree methods (bst.cpp) for the binary search tree provided in the header file. Test your implementation with the included test. bst.h bst_test.cpp Note: Your implementation must correspond to declarations in the header file, and pass the test. Do not modify these two. I will compile your code against these. If the compilation fails, you will get down vote. bst.h #ifndef BINARY_SEARCH_TREE_H #define BINARY_SEARCH_TREE_H #include <string>...

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