Implementing the Tree-based or Ordered Map Implementing the Tree-based or Ordered Map Input: ● Line 1 indicates number of lines to follow ● Each line starts with a command that indicates which method...

1 answer below »
See attached


Implementing the Tree-based or Ordered Map Implementing the Tree-based or Ordered Map Input: ● Line 1 indicates number of lines to follow ● Each line starts with a command that indicates which method is executed. The command is separated by a space followed by appropriate parameters. ● Method signatures: ● bool insert(ID, NAME) ● string search(ID) ● string traverse() ● bool remove(ID) Output: ● Each line prints the return value of the equivalent command. Total n lines get printed where the number of lines of input are n+1. Note: ● The main () function is already built for you. No need to implement main (). ● Last test is fake. Sample Input 1: 4 insert 20000000 A insert 10000000 B insert 30000000 C traverse Sample Output 1: 1 1 1 A, B, C Sample Input 2: 4 insert 20000000 A insert 10000000 B insert 30000000 C search 30000000 Sample Output 2: 1 1 1 C Sample Input 3: 5 insert 20000000 A insert 10000000 B insert 30000000 C remove 30000000 traverse Sample Output 3: 1 1 1 1 A, B Here is what I have so far. None of this can be changed, it’s considered a template. The only things I can add are all the way at the end where it says “//code here” #include #include #include #include #include #include using std::cout; using std::cin; using std::getline; using namespace std; //AVL tree implementation //TreeNode class that contains constructors class TreeNode { public: int idNum; string name; int height; int balanceFactor; TreeNode *left; TreeNode *right; //constructor TreeNode(string name,int x, int height) : idNum(x), name(name), height(height), left(nullptr), right(nullptr) {}; }; //Counts the number of nodes in a tree through recursive function int countNodes(TreeNode* root, int &numOfNodes) { //if the tree is empty if (root == NULL) return 0; else //If there is a node found in the tree { numOfNodes = 1 + countNodes(root->left, numOfNodes) + countNodes(root->right, numOfNodes); return numOfNodes; } } //returns the height of a tree //helper function for insert int getheight(TreeNode* root,int &numOfNodes) { int numOfRight = 0; int numOfLeft = 0; if (root == NULL) return 0; else { //storing height of the subtree numOfRight = 1 + countNodes(root->right, numOfNodes); numOfLeft = 1 + countNodes(root->left, numOfNodes); //finds which subtree has the greater height and returns the greater height if(numOfRight > numOfLeft) return numOfRight; else return numOfLeft; } } //updates the balance factor if the node passed in //helper function for insert void balanceFactor(TreeNode* root,int &numOfNodes) { int numOfRight = 0; int numOfLeft = 0; //if the root has a key if (root != NULL) { numOfRight = 1 + countNodes(root->right, numOfNodes); numOfLeft = 1 + countNodes(root->left, numOfNodes); //computing balance factor int balanceFactor = numOfLeft - numOfRight; //updating the root variable with proper balance factor root->balanceFactor = balanceFactor; } } //Traversals //Inorder vector inorder(TreeNode* &head, int &numOfNodes, int count, vector& nameVecInOrder,vector& InOrderKey) { if(head == NULL) cout<""; else="" {="" recursive="" call="" of="" left="" subtree="" inorder(head-="">left,numOfNodes, count, nameVecInOrder,InOrderKey); //pushes name into a vector inorder //This vector is passed by reference into the function and is used in the main for printing nameVecInOrder.push_back(head->name); //pushes the ID number (inorder) into the vector //This vector is used for the deletion of Nth node function InOrderKey.push_back(head->idNum); //recursive call of right subtree inorder(head->right,numOfNodes, count, nameVecInOrder,InOrderKey); } return nameVecInOrder; } //preorder void preorder(TreeNode* &head, int &numOfNodes, int count, vector& nameVecPreOrder) { if(head==NULL) cout<""; else="" {="" pushes="" name="" into="" a="" vector="" preorder="" this="" vector="" is="" passed="" by="" reference="" into="" the="" function="" and="" is="" used="" in="" the="" main="" for="" printing="" namevecpreorder.push_back(head-="">name); //recursive call of left subtree preorder(head->left,numOfNodes, count, nameVecPreOrder); //recursive call of right subtree preorder(head->right,numOfNodes, count,nameVecPreOrder); } } //postorder void postorder(TreeNode* &head, int &numOfNodes, int count, vector& nameVecPostOrder) { if(head==NULL) cout<""; else="" {="" recursive="" call="" of="" left="" subtree="" postorder(head-="">left,numOfNodes, count, nameVecPostOrder); //recursive call of right subtree postorder(head->right,numOfNodes, count, nameVecPostOrder); //pushes name into a vector postorder //This vector is passed by reference into the function and is used in the main for printing nameVecPostOrder.push_back(head->name); } } //n-Ary Level order int levelOrder (TreeNode* &root) { //creation of que for children queue queueOfNodes; //push the root of the tree into the que queueOfNodes.push(root); //variable counting levels of the tree int countLevels = 0; //if there is no root then there are zero levels printed if (root == NULL) { cout < "0"="">< endl;="" return="" 0;="" }="" while="" the="" que="" data="" structure="" is="" empty="" no="" children="" in="" the="" level="" while(queueofnodes.size()="" !="0)" {="" int="" numofnodesinlevels="queueOfNodes.size();" while="" the="" number="" of="" nodes="" in="" the="" level="" surpass="" zero="" while="" (numofnodesinlevels=""> 0) { //a temp node variable pointing to the front of the que TreeNode* temp = queueOfNodes.front(); //printing nodes on each levels if (numOfNodesinLevels != 1) { //continuing } else { //increment countLevels since we have entered a new level countLevels++; } //deque the children queueOfNodes.pop(); //enqueuing children from the left subtree if (temp->left != NULL) { queueOfNodes.push(temp->left); } //enqueuing children from the right subtree if (temp->right !=NULL) { queueOfNodes.push(temp->right); } //decrement numOfNodes since we dequeued the children numOfNodesinLevels--; } } //Print the number of levels the tree has cout < countlevels="">< endl;="" return="" countlevels;="" }="" search="" void="" searchid="" (treenode*="" &head,="" int="" &numofnodes,="" int="" &count,="" int="" keyidnum)="" {="" checks="" id="" node="" is="" null,="" if="" so="" then="" the="" count="" variable="" in="" main="" will="" not="" increment="" and="" "unsuccessful"="" will="" be="" printed="" in="" main="" if(head="=NULL)" {="" continuing="" }="" else="" {="" traverses="" through="" left="" subtree="" to="" find="" id="" searchid(head-="">left,numOfNodes, count,keyIdNum); //When key is found it increments the count var by 1 // since count is passed by reference, the main checks if count == 1 and if it does then it prints "successful" if (head->idNum == keyIdNum) { count++; cout < head-="">name < endl;="" return;="" }="" traverses="" through="" right="" subtree="" to="" find="" id="" searchid(head-="">right,numOfNodes, count,keyIdNum); } } void searchName (TreeNode* &head, int &numOfNodes, int &count, string keyName) { //Checks Id node is null, if so then the count variable in main will not increment and "unsuccessful" will be printed in main if(head==NULL) { //continuing } else { //When key is found it increments the count var by 1 // since count is passed by reference, the main checks if count == 1 and if it does then it prints "successful" if (head->name == keyName) { count++; cout < head-="">idNum < endl;="" }="" traverses="" through="" the="" left="" and="" right="" subtree="" to="" find="" same="" id="" as="" key="" searchname(head-="">left,numOfNodes, count, keyName); searchName(head->right,numOfNodes, count, keyName); } } //rotate Left, code used from Aman's PowerPoint lecture //Function used within insertion for balancing tree TreeNode* rotateLeft (TreeNode* &node, int &numOfNodes) { TreeNode* grandchild = node->right->left; TreeNode* newParent = node->right; //rotation newParent->left = node; node->right = grandchild; //height newParent->height = getheight(newParent,numOfNodes); node->height = getheight(node,numOfNodes); return newParent; } //rotate Right, code used from Aman's PowerPoint lecture //Function used within insertion for balancing tree TreeNode* rotateRight (TreeNode* &node, int &numOfNodes) { TreeNode* grandchild = node->left->right; TreeNode* newParent = node->left; //rotation newParent->right = node; node->left = grandchild; //height newParent->height = getheight(newParent,numOfNodes); node->height = getheight(node,numOfNodes); return newParent; } //Find the Succesor of the node passed in //The node passed in is root->right //This function is used in the deletion method for the case of a node having > 1 child TreeNode* findSuccesor(TreeNode* &root) { if (root == NULL) { return root; } else if (root->left == NULL) { return root; } else { return findSuccesor(root->left); } } //insert TreeNode* insert(TreeNode* &root, int key, string name, int height, int &numOfNodes) { // Standard BST Insert method from Aman's lecture PowerPoint //inserts new node if (root == nullptr) { cout < "successful"="">< endl;="" return="" new="" treenode(name,="" key,="" height);="" }="" if="" the="" node="" is="" smaller="" than="" the="" key="" else="" if="" (key="">< root-="">idNum) root->left = insert(root->left, key , name,height,numOfNodes); //If the node is greater than the key else if (key > root->idNum) root->right = insert(root->right, key, name,height,numOfNodes); //If the tree node being passed in is not unique else { cout < "unsuccessful"="">< endl;="" return="" root;="" equal="" key="" }="" avl="" tree="" implementation="" update="" height="" root-="">height = getheight(root, numOfNodes); //establishes balance factor balanceFactor(root,numOfNodes); balanceFactor(root->left,numOfNodes); balanceFactor(root->right,numOfNodes); //balance // if(tree is left heavy) if (root->balanceFactor > 1) { //left Right if (root->left->balanceFactor < 0)="" {="" root-="">left = rotateLeft(root->left ,numOfNodes); return rotateRight(root,numOfNodes); } //left left else //greater than 0 return rotateRight(root,numOfNodes); } if (root->balanceFactor < -1)="" {="" right="" left="" if="" (root-="">right->balanceFactor > 0) { root->right = rotateRight(root->right ,numOfNodes); return rotateLeft(root,numOfNodes); } //right right else //Less than 0 return rotateLeft(root,numOfNodes); } return root; } TreeNode* deletion(TreeNode* root, int key, string name, int height, int &numOfNodes, int &count1) { //BST Insert method from Aman's lecture PowerPoint but replaced the recursive call to Deletion instead of Insertion //If tree is empty if (root == NULL) { cout < "unsuccessful"="">< endl;="" return="" root;="" }="" if="" the="" node="" is="" smaller="" than="" the="" key="" else="" if="" (key="">< root-="">idNum) root->left = deletion(root->left, key , name,height,numOfNodes,count1); //If the node is smaller than the key else if (key > root->idNum) root->right = deletion(root->right, key, name,height,numOfNodes,count1); else //found key and deletion starts { //node no child/ leaf node if (root->left == NULL && root->right == NULL) { //no children TreeNode *deleteNode; //points to right subtree deleteNode = root->right; //delet node delete root; cout < "successful"="">< endl;="" return="" deletenode;="" }="" one="" child="" else="" if="" (root-="">left == NULL && root->right != NULL || root->left != NULL && root->right == NULL ) { //If there is a node on the left branch but not the right branch if(root->left == NULL && root->right != NULL) { TreeNode *deleteNode; deleteNode = root; root = root->right; free(deleteNode); cout < "successful"="">< endl;="" }="" if="" there="" is="" a="" node="" on="" the="" right="" branch="" but="" not="" the="" left="" branch="" else="" if(root-="">left != NULL && root->right == NULL ) { TreeNode *deleteNode; deleteNode = root; root = root->left; free(deleteNode); cout < "successful"="">< endl;="" }="" }="" else="" node="" with="" more="" than="" one="" child="" {="" inorder="" successor="" treenode="" *succesor="findSuccesor(root-">right); //copy the root ID number and name to successors data root->idNum = succesor->idNum; root->name = succesor->name; //the nodes right node equals the return root of the deleted root.right node in successors position root->right = deletion(root->right, succesor->idNum , succesor->name,height,numOfNodes,count1); } return root; } } //Deletion of the Nth Node TreeNode* deletionOfNthNode(TreeNode* root, int n, string name, int height, int &numOfNodes, int &count1, vector& nameVecInOrder,vector& InOrderKey) { //calls inorder traversal so the vector carrying the order of the nodes is set inorder(root, numOfNodes, 0, nameVecInOrder, InOrderKey); //stores the ID number value of which the nth node contains int keyValue = InOrderKey[n]; //calls deletion method and passes in the key value from above deletion(root, keyValue , name,height,numOfNodes,count1); } int main() { //Variables int numOfNodes = 0; int numOfInputs = 0; int count = 0; int height = 0; string name; TreeNode* root = nullptr; string input; //Number of inputs from the user cin >> numOfInputs; for (int i = 0; i < numofinputs+1;="" i++)="" {="" enter="" input="" into="" loop="" getline(cin,input,="" '\n');="" variables="" string="" temp="input;" string="" action="input;" string="" names="input;" string="" idnumstring="input;" int="" idnum="0;" int="" count1="0;"> nameVecInOrder;
Answered Same DayNov 07, 2021

Answer To: Implementing the Tree-based or Ordered Map Implementing the Tree-based or Ordered Map Input: ● Line...

Kshitij answered on Nov 07 2021
130 Votes
#include
#include
#include
#include
#include
#include
#include
using std::cout;
using std::cin;
using std::getline;
using namespace std;
//AVL tree implementation
//TreeNode class that contains constructors
class TreeNode {
public:
string idNum;
string name;
int height;
int balanceFactor;
TreeNode *left;
TreeNode *right;
//constructor
TreeNode(string name, string x, in
t height) : idNum(x), name(name),
height(height), left(nullptr), right(nullptr) {};
};
//Counts the number of nodes in a tree through recursive function
int countNodes(TreeNode *root, int &numOfNodes) {
//if the tree is empty
if (root == NULL)
return 0;
else //If there is a node found in the tree
{
numOfNodes = 1 + countNodes(root->left, numOfNodes) +
countNodes(root->right, numOfNodes);
return numOfNodes;
}
}
//returns the height of a tree
//helper function for insert
int getheight(TreeNode *root, int &numOfNodes) {
int numOfRight = 0;
int numOfLeft = 0;
if (root == NULL)
return 0;
else {
//storing height of the subtree
numOfRight = 1 + countNodes(root->right, numOfNodes);
numOfLeft = 1 + countNodes(root->left, numOfNodes);
//finds which subtree has the greater height and returns the greater height
if (numOfRight > numOfLeft)
return numOfRight;
else
return numOfLeft;
}
}
//updates the balance factor if the node passed in
//helper function for insert
void balanceFactor(TreeNode *root, int &numOfNodes) {
int numOfRight = 0;
int numOfLeft = 0;
//if the root has a key
if (root != NULL) {
numOfRight = 1 + countNodes(root->right, numOfNodes);
numOfLeft = 1 + countNodes(root->left, numOfNodes);
//computing balance factor
int balanceFactor = numOfLeft - numOfRight;
//updating the root variable with proper balance factor
root->balanceFactor = balanceFactor;
}
}
//Traversals
//Inorder
vector inorder(TreeNode *&head, int &numOfNodes, int count,
vector &nameVecInOrder, vector &InOrderKey) {
if (head == NULL)
cout << "";
else {
//recursive call of left subtree
inorder(head->left, numOfNodes, count, nameVecInOrder, InOrderKey);
//pushes name into a vector inorder
//This vector is passed by reference into the function and is used in the main for printing
nameVecInOrder.push_back(head->name);
//pushes the ID number (inorder) into the vector
//This vector is used for the deletion of Nth node function
InOrderKey.push_back(head->idNum);
//recursive call of right subtree
inorder(head->right, numOfNodes, count, nameVecInOrder, InOrderKey);
}
return nameVecInOrder;
}
//preorder
void preorder(TreeNode *&head, int &numOfNodes, int count, vector &
nameVecPreOrder) {
if (head == NULL)
cout << "";
else {
//pushes name into a vector preorder
//This vector is passed by reference into the function and is used in the main for printing
nameVecPreOrder.push_back(head->name);
//recursive call of left subtree
preorder(head->left, numOfNodes, count, nameVecPreOrder);
//recursive call of right subtree
preorder(head->right, numOfNodes, count, nameVecPreOrder);
}
}
//postorder
void postorder(TreeNode *&head, int &numOfNodes, int count, vector &
nameVecPostOrder) {
if (head == NULL)
cout << "";
else {
//recursive call of left subtree
postorder(head->left, numOfNodes, count, nameVecPostOrder);
//recursive call of right subtree
postorder(head->right, numOfNodes, count, nameVecPostOrder);
//pushes name into a vector postorder
//This vector is passed by reference into the function and is used in the main for printing
nameVecPostOrder.push_back(head->name);
}
}
//n-Ary Level order
int levelOrder(TreeNode *&root) {
//creation of que for children
queue queueOfNodes;
//push the root of the tree into the que
queueOfNodes.push(root);
//variable counting levels of the tree
int countLevels = 0;
//if there is no root then there are zero levels printed
if (root == NULL) {
cout << "0" << endl;
return 0;
}
//while the que data structure is empty
//no children in the level
while (queueOfNodes.size() != 0) {
int numOfNodesinLevels = queueOfNodes.size();
//While the number of nodes in the level surpass zero
while (numOfNodesinLevels > 0) {
//a temp node variable pointing to the front of the que
TreeNode *temp = queueOfNodes.front();
//printing nodes on each levels
...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here