Question
In this assignment you will write 2 methods to rotate the binary search tree (BST), both left and right given a pivot node. Tree rotations
In this assignment you will write 2 methods to rotate the binary search tree (BST), both left and right given a pivot node. Tree rotations are an important operation used in balancing binary search trees such as AVL, Red-Black, and Splay. Additionally, you will need to write a private helper method for finding the parent node. BRIDGES is used to display the tree. The final output is a web link that displays the tree after all nodes are inserted, after right, and after left rotations.
// BST implementation with nodes as BRIDGES BSTElement objects. insert,
add_node(), getNode(), getNode() methods provided. Students to code
rotateRight() and rotateLeft() methods.
#include
#include
#include "Bridges.h"
#include "BSTElement.h"
using namespace std;
using namespace bridges;
std::string getEnvVar(string const & key);
struct pivotFamily {
BSTElement
BSTElement
BSTElement
BSTElement
BSTElement
BSTElement
BSTElement
};
class BST {
public:
BST();
int insert(int key, string value);
BSTElement
void rotateRight(BSTElement
void rotateLeft(BSTElement
void display();
private:
BSTElement
// Private methods
void add_node(BSTElement
BSTElement
BSTElement string> *node) const; pivotFamily findFamily(BSTElement }; int main() { BST my_tree; my_tree.insert(2, "two"); my_tree.insert(1, "one"); my_tree.insert(3, "three"); my_tree.insert(7, "seven"); my_tree.insert(10, "ten"); my_tree.insert(2, "two"); my_tree.insert(5, "five"); my_tree.insert(8, "eight"); my_tree.insert(6, "six"); my_tree.insert(4, "four"); my_tree.display(); BSTElement if (n != nullptr) { cout << "Value of key is " << n->getValue() << endl; my_tree.rotateRight(n); my_tree.display(); // Check if right rotation was correctly implemented n = my_tree.getNode(7); my_tree.rotateLeft(n); my_tree.display(); } else { cout << "Node does not exist" << endl; } return 0; } BST::BST() { root = nullptr; } int BST::insert(int key, string value) { BSTElement value); assert(new_node); new_node->setKey(key); new_node->setLeft(nullptr); new_node->setRight(nullptr); if (root == nullptr) { root = new_node; } else add_node(root, new_node); return 1; //Success } BSTElement BSTElement while (current != nullptr) { if (element < current->getKey()) { current = current->getLeft(); } else if (element > current->getKey()) { current = current->getRight(); } else return current; } return nullptr; } void BST::rotateRight(BSTElement { // Your code here } void BST::rotateLeft(BSTElement { // Your code here } void BST::display() { string hilite_color = "orange", def_color = "green", end_color = "red"; Bridges::initialize(7, getEnvVar("BRIDGESUSERNAME"), getEnvVar("BRIDGESKEY")); Bridges::setTitle("Binary Search Tree Visualization"); Bridges::setDataStructure(root); root->getVisualizer()->setColor(Color("red")); Bridges::visualize(); } // Private methods void BST::add_node(BSTElement string> *new_node) { if (new_node->getKey() < parent->getKey()) { if (parent->getLeft() == nullptr) parent->setLeft(new_node); else add_node(parent->getLeft(), new_node); } else if (new_node->getKey() > parent->getKey()) { if (parent->getRight() == nullptr) parent->setRight(new_node); else add_node(parent->getRight(), new_node); } } BSTElement *node) const { // Your code here } pivotFamily BST::findFamily(BSTElement { pivotFamily pF; pF.pivot = pivot; pF.pivot_parent = findParentNode(pivot); pF.pivot_leftchild = pivot->getLeft(); pF.pivot_rightchild = pivot->getRight(); pF.pivot_parent_leftchild = (pF.pivot_parent)->getLeft(); //possibly same as pivot pF.pivot_parent_rightchild = (pF.pivot_parent)->getRight(); //possible same as pivot pF.pivot_grandparent = findParentNode(pF.pivot_parent); return pF; } string getEnvVar(string const & key) { char * val = getenv( key.c_str() ); return val == NULL ? string("") : string(val); }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started