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
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:

In C++ Given a pointer to the root of a binary search tree (has left, right,...
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 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...
C++ ONLY Threaded Binary Search Tree Since a binary search tree with N nodes has N + 1 NULL pointers, half the space allocated in a binary search tree for pointer information is wasted. Suppose that if a node has a NULL left child, we make its left child pointer link to its inorder predecessor, and if a node has a NULL right child, we make its right child pointer link to its inorder successor. This is known as a...
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...
/* * 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...
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!)
You are given the root of a Binary Search Tree. Print the leaf elements of the tree starting from right to left. We have defined the following node C++ Node class for you: class Node { public: int name; Node* left = NULL; Node* right = NULL; }; Function to code: void printLeaves(Node* root); The first input in test cases are nodes of a tree which are inserted in that order. You don't need to implement insert. You have access...
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 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 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.