Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

class AVLTree The following functions are the minimum requirements for the AVL class. You can add any function from Assignment 2 to this class. You

class AVLTree

The following functions are the minimum requirements for the AVL class. You can add any function from Assignment 2 to this class. You should modify the BSTree insert function so that the tree remains balanced after each insertion.

Required Public Member Functions

  • void insert(const string &): Insert an item to the binary search tree and perform rotation if necessary.
  • int balanceFactor(Node*): Return the balance factor of a given node.
  • void printBalanceFactors(): Traverse and print the tree in inorder notation. Print the string followed by its balance factor in parentheses followed by a , and one space.
  • void visualizeTree(const string &): Generate dotty file and visualize the tree using dotty program. Call this function before and after rotation.

Recommended Private Helper Functions

These helper functions are recommended, but you can change them or add other helper functions if necessary.

  • findUnbalancedNode: Find and return the closest unbalanced node (with balance factor of 2 or -2) to the inserted node.
  • rotate: Implement four possible imbalance cases and update the parent of the given node.
  • rotateLeft: Rotate the subtree to left at the given node and returns the new subroot.
  • rotateRight: Rotate the subtree to right at the given node and returns the new subroot.
  • void printBalanceFactors(Node *)
  • void visualizeTree(ofstream &, Node *)

Implementation of visualizeTree function

void BSTree::visualizeTree(const string &outputFilename){ ofstream outFS(outputFilename.c_str()); if(!outFS.is_open()){ cout<<"Error"<left){ visualizeTree(outFS,n->left); outFS<data <<" -> " <left->data<<";"<right){ visualizeTree(outFS,n->right); outFS<data <<" -> " <right->data<<";"< 

Use the following main to test your program.

#include  #include "AVLTree.h" using namespace std; int menu() { int choice = 0; cout << endl << "Enter menu choice: "; cout << endl; cout << "1. Insert" << endl << "2. Print" << endl << "3. Quit" << endl; cin >> choice; // fix buffer just in case non-numeric choice entered // also gets rid of newline character cin.clear(); cin.ignore(256, ' '); return choice; } int main( ) { AVLTree tree; int choice = menu(); string entry; while (choice != 3) { if (choice == 1) { cout << "Enter string to insert: "; getline(cin, entry); cout << endl; tree.insert(entry); } else if (choice == 2) { tree.printBalanceFactors(); } //fix buffer just in case non-numeric choice entered choice = menu(); } return 0; } 

LAB

ACTIVITY

20.9.1: Lab 6: AVL Tree

0 / 2

Submission Instructions

Additional files provided by your instructor.

AVLTree.cpp

,

AVLTree.h

,

Node.cpp

,

main.cpp

, and

Node.h

needs to be in the correct format !

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Systems For Advanced Applications 17th International Conference Dasfaa 2012 Busan South Korea April 2012 Proceedings Part 1 Lncs 7238

Authors: Sang-goo Lee ,Zhiyong Peng ,Xiaofang Zhou ,Yang-Sae Moon ,Rainer Unland ,Jaesoo Yoo

2012 Edition

364229037X, 978-3642290374

More Books

Students also viewed these Databases questions

Question

Prepare an ID card of the continent Antarctica?

Answered: 1 week ago