Write a method that determines the key of the successor of the root node in a binary search tree.
For any input binary search tree, find the key of successor of the root node.Note: Successor is the node with the next highest key, should work for any binary search tree - not just the given example input.
#include <iostream>
using namespace std;
class Node
{
private:
int key;
string val;
Node* left;
Node* right;
friend class BinarySearchTree;
};
class BinarySearchTree
{
public:
BinarySearchTree();
void insert(int key, string val); // Recursive
int rootSuccessor(); // Returns key of the successor of the root, -1 if no
successor
private:
Node* root;
void insertHelper(Node* parent, Node* new_node);
};
int main() {
BinarySearchTree t;
t.insert(5, "Boron");
t.insert(3, "Lithium");
t.insert(7, "Nitrogen");
t.insert(2, "Helium");
t.insert(4, "Berylium");
t.insert(6, "Carbon");
t.insert(8, "Oxygen");
int succ = t.rootSuccessor();
if (succ != -1)
cout << "Successor: " << succ << endl;
else
cout << "No successor" << endl;
}
BinarySearchTree::BinarySearchTree()
{
root = nullptr;
}
void BinarySearchTree::insert(int key, string val)
{
Node* new_node = new Node;
new_node->key = key;
new_node->val = val;
new_node->left = nullptr;
new_node->right = nullptr;
if (root == nullptr) {
root = new_node;
} else {
insertHelper(root, new_node);
}
}
void BinarySearchTree::insertHelper(Node* parent, Node* new_node)
{
if (new_node->key < parent->key) {
if (parent->left == nullptr) {
parent->left = new_node;
}
else {
insertHelper(parent->left, new_node); }
}
else if (new_node->key > parent->key) {
if (parent->right == nullptr) {
parent->right = new_node;
}
else {
insertHelper(parent->right, new_node);
}
}
}
int BinarySearchTree::rootSuccessor() {
// Your code here
}
Please find the answer below for the api rootSuccessor.
Note:
In the below full program,I have modified the insert with different key values to verify whether it works all kinds of key values.
Function only:
int BinarySearchTree::rootSuccessor()
{
//assign th root node to temp and traverse right only to identify
the
//next highest key value, which is the root successor.
Node* temp = root;
if(temp != NULL)
{
while (temp->right != NULL)
temp = temp->right;
return (temp->key);
}
else
{
return -1;
}
}
Full Program:
#include <iostream>
using namespace std;
class Node
{
private:
int key;
string val;
Node* left;
Node* right;
friend class BinarySearchTree;
};
class BinarySearchTree
{
public:
BinarySearchTree();
void insert(int key, string val); // Recursive
int rootSuccessor(); /* Returns key of the successor of the root,
-1 if nosuccessor*/
private:
Node* root;
void insertHelper(Node* parent, Node* new_node);
};
int main() {
BinarySearchTree t;
//i have modified the insert to verify whether it works all kinds of key values.
t.insert(8, "Oxygen");
t.insert(5, "Boron");
t.insert(3, "Lithium");
t.insert(17, "Nitrogen");
t.insert(2, "Helium");
t.insert(4, "Berylium");
t.insert(6, "Carbon");
int succ = -1;
succ = t.rootSuccessor();
if (succ != -1)
cout << "Successor: " << succ << endl;
else
cout << "No successor" << endl;
}
BinarySearchTree::BinarySearchTree()
{
root = nullptr;
}
void BinarySearchTree::insert(int key, string val)
{
Node* new_node = new Node;
new_node->key = key;
new_node->val = val;
new_node->left = nullptr;
new_node->right = nullptr;
if (root == nullptr) {
root = new_node;
} else {
insertHelper(root, new_node);
}
}
void BinarySearchTree::insertHelper(Node* parent, Node*
new_node)
{
if (new_node->key < parent->key) {
if (parent->left == nullptr) {
parent->left = new_node;
}
else {
insertHelper(parent->left, new_node);
}
}
else if (new_node->key > parent->key)
{
if (parent->right == nullptr)
{
parent->right = new_node;
}
else {
insertHelper(parent->right, new_node);
}
}
}
int BinarySearchTree::rootSuccessor()
{
//assign th root node to temp and traverse right only to identify
the
//next highest key value, which is the root successor.
Node* temp = root;
if(temp != NULL)
{
while (temp->right != NULL)
temp = temp->right;
return (temp->key);
}
else
{
return -1;
}
}
Output:
Successor: 17
Screen Shot:

Write a method that determines the key of the successor of the root node in a...
C++, implement the bst.cpp file without adding any additional methods or functions to it ======================================================================== // bst.cpp #include <iostream> #include "bst.h" using namespace std; BinarySearchTree::BinarySearchTree() { root = nullptr; } void BinarySearchTree::insert(int key, string val) { Node* new_node = new Node; new_node->key = key; new_node->val = val; new_node->left = nullptr; new_node->right = nullptr; if (root == nullptr) { root = new_node; } else { insertHelper(root, new_node); } } void BinarySearchTree::insertHelper(Node* parent, Node* new_node) { if (new_node->key < parent->key) { if...
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>...
Write a function, swapSubtrees, that swaps all of the left and right subtrees of a binary tree. Add this function to the class binaryTreeType and create a program to test this function. #include <iostream> using namespace std; //Definition of the Node template <class elemType> struct nodeType { elemType info; nodeType<elemType> *lLink; nodeType<elemType> *rLink; }; //Definition of the class template <class elemType> class binaryTreeType { public: //Overload the assignment operator. const binaryTreeType<elemType>& operator=(const binaryTreeType<elemType>&) { if (this != &otherTree) //avoid self-copy...
Using the following implementation of Tree class Node { public int iData; // data item (key) public double dData; // data item public Node leftChild; // this node's left child public Node rightChild; // this node's right child public void displayNode() // display ourself { System.out.print('{'); System.out.print(iData); System.out.print(", "); System.out.print(dData); System.out.print("} "); } } // end class Node //------------------------------------------------------------------ import java.io.IOException; import java.util.Stack; public class Tree { private Node root; // first node of tree // ------------------------------------------------------------- public Tree() // constructor { root = null; }...
Having code issues wth my C++ program. My program checks if two binary trees are similar and if they're not they return false. My program is return true with different binary trees. Could use some help thanks #include <iostream> #include <string> using namespace std; //Struct of Nodes struct BinarySearchTree { int data; BinarySearchTree *left; BinarySearchTree *right; }; // Inserting nodes into BST BinarySearchTree* insert( BinarySearchTree* node, int val) { if (node == NULL) { BinarySearchTree *newNode = new BinarySearchTree(); newNode->data...
Java binary search tree Add the following print method to the binary search tree class created in class (on D2L). This method should print all the nodes in the tree in level order (root first, then all children of root, then all children of those). Ensure your method runs in O(N), include comments to show how it conforms to this rule. Method header: public void printInLevelOrder() public class BinarySearchTree<E extends Comparable<? super E>> { private Node root; public BinarySearchTree() {...
C++: PLEASE HELP~!!! ~~~~~~~~ Implement bool AVLTree::deleteNode(string ss) method. Function deleteNode() tries to delete the node containing value ss. If there is no such node, it returns false. Otherwise, it deletes the node, check the balance of the tree, rebalance the tree if it is necessary. When you delete a node, consider three different scenarios: -The node is a leaf -The node has only ONE child subtree -The node has two child subtrees Important: When you implement this project, do...
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;...
Here you'll write the inorder traversal function in the header file "bst.h". Notice that the public inorder function calls a private recursive function _ inorder to do the actual traversal. This public-private strategy is the correct way to implement recursive functions, where the public function kicks off the recursion and the private function does the actual work. The public function is written for you, your job is to implement the private _ inorder function. he main program has been written...
In C++ I need the printRange function, and the main.cpp program. Thanks. (Binary search tree) Write a function printRange that takes as input a binary search tree t and two keys, k1 and k2, which are ordered so that k1 < k2, and print all elements x in the tree such that k1 <= x <= k2. You can add this function in BinarySearchTree.h (click the link) that we used in the lecture and lab 7. public: void printRange(int k1,...