Question: 8 . 1 1 LAB: AVL tree Nth largest operation Step 1 : Inspect the BSTNode.h and BinarySearchTree.h files Inspect the BSTNode class declaration for
LAB: AVL tree Nth largest operation
Step : Inspect the BSTNode.h and BinarySearchTree.h files
Inspect the BSTNode class declaration for a binary search tree node in BSTNode.h Access BSTNode.h by clicking on the orange arrow next to main.cpp at the top of the coding window. The BSTNode class has private member variables for the key, parent pointer, left child pointer, and right child pointer. Accessor functions exist for each.
Inspect the BinarySearchTree class declaration for a binary search tree node in BinarySearchTree.h The GetNthKey function is the only pure virtual function that exists.
Step : Inspect other files related to the inheritance hierarchy
Classes AVLNode and AVLTree inherit from BSTNode and BinarySearchTree, respectively. Each class is implemented in a read only header file.
Classes ExtendedAVLNode and ExtendedAVLTree are declared, but implementations are incomplete. Both classes must be implemented in this lab.
Six class names are shown in boxes. White boxes representing read only, completed class implementations. Four white boxes exist: BinarySearchTree, AVLTree, BSTNode, and AVLNode. Yellow boxes represent incomplete class implementations. Two yellow boxes exist: ExtendedAVLTree and ExtendedAVLNode. Arrows between boxes indicate inheritance. ExtendedAVLTree inherits from AVLTree, AVLTree inherits from BinarySearchTree, ExtendedAVLNode inherits from AVLNode, and AVLNode inherits from BSTNode.
Step : Understand the purpose of the subtreeKeyCount variable
The ExtendedAVLNode class inherits from AVLNode and adds one integer member variable, subtreeKeyCount. Each node's subtree key count is the number of keys in the subtree rooted at that node. Ex:
Sample AVL tree with subtree key counts shown outside each node. Root node has key and a subtreeKeyCount Root's left child has key and subtreeKeyCount Root's right child has key and subtreeKeyCount Node s right child has key and subtreeKeyCount Node s left child has key and subtreeKeyCount Node s right has key and subtreeKeyCount Node s right child has key and subtreeKeyCount
ExtendedAVLNode's constructor and GetSubtreeKeyCount member functions are already implemented and should not be changed. Additional member functions are needed to ensure that subtreeKeyCount remains accurate.
Step : Implement ExtendedAVLTree and ExtendedAVLNode
Each node in an ExtendedAVLTree must have a correct subtreeKeyCount after an insertion or removal operation. Determine which member functions in AVLTree and AVLNode must be overridden in ExtendedAVLTree and ExtendedAVLNode to keep each node's subtreeKeyCount correct. New functions can be added along with overridden functions, if desired.
Hint: Consider an UpdateSubtreeKeyCount member function for the ExtendedAVLNode class. The function requires each child node's subtreeKeyCount to be correct, and updates the node's subtreeKeyCount appropriately. Overridden functions in both ExtendedAVLNode and ExtendedAVLTree can call a node's UpdateSubtreeKeyCount function as needed.
Once determinations are made, complete the implementation of both the ExtendedAVLTree and ExtendedAVLNode classes. Do not implement ExtendedAVLTree's GetNthKey in this step. GetNthKey requires correct subtree counts at each node.
Step : Run tests in develop mode and submit mode
TreeTestCommand is an abstract base class defined in TreeCommands.h A TreeTestCommand object is an executable command that operates on a binary search tree. Classes inheriting from TreeTestCommand are also declared in TreeCommands.h:
TreeInsertCommand inserts keys into the tree
TreeRemoveCommand removes keys from the tree
TreeVerifyKeysCommand verifies the tree's keys using an inorder traversal
TreeVerifySubtreeCountsCommand verifies that each node in the tree has the expected subtree key count
TreeGetNthCommand verifies that GetNthKey returns an expected value
Code in main.cpp contains three automated test cases. Each test executes a vector of TreeTestCommand objects in sequence. The third test includes TreeGetNthCommands and will not pass until the completion of Step The first two tests should pass after completion of step
Before proceeding to Step run code in develop mode and ensure that the first two tests in main.cpp pass. Then submit code and ensure that the first two unit tests pass.
Step : Implement ExtendedAVLTree's GetNthKey member function worst case Olog n
GetNthKey must return the tree's nthlargest key. The parameter n starts at for the smallest key in the tree. Ex: Suppose a tree has keys:
Then GetNthKey returns GetNthKey returns GetNthKey returns and GetNthKey returns
Determine an algorithm that uses the subtree key counts so that GetNthKey operates in worst case Olog n time.
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
