Question: Need help with running tests C + + code Step 5 : Run tests TreeTestCommand is an abstract base class defined in TreeCommands.h . A

Need help with running tests C++ code
Step 5: Run tests
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 6. The first two tests should pass after completion of step 4.
Before proceeding to Step 6, verify that the first two tests in main.cpp pass. Then submit code and ensure that the first two unit tests pass.
Having test 2,4, and 5 Unit Test errors. Here is the error code:
----------------------------------------------------------------------
/usercode/__run_coding_rooms_unit_tests.sh: line 3: 174 Segmentation fault (core dumped)./a.out
#ifndef EXTENDEDAVLNODE_H
#define EXTENDEDAVLNODE_H
#include "AVLNode.h"
#include
class ExtendedAVLNode : public AVLNode {
private:
int subtreeKeyCount;
public:
ExtendedAVLNode(int nodeKey) : AVLNode(nodeKey){
subtreeKeyCount =1;
std::cout << "Node created with key: "<< nodeKey << std::endl;
}
int GetSubtreeKeyCount(){
return subtreeKeyCount;
}
void UpdateSubtreeKeyCount(){
int leftCount = GetLeft()?((ExtendedAVLNode*)GetLeft())->GetSubtreeKeyCount() : 0;
int rightCount = GetRight()?((ExtendedAVLNode*)GetRight())->GetSubtreeKeyCount() : 0;
subtreeKeyCount =1+ leftCount + rightCount;
std::cout << "Updated subtreeKeyCount for node "<< GetKey()<<": "<< subtreeKeyCount << std::endl;
}
virtual void SetLeft(BSTNode* newLeftChild) override {
std::cout << "Setting left child for node "<< GetKey()<< std::endl;
AVLNode::SetLeft(newLeftChild);
UpdateSubtreeKeyCount();
std::cout << "Set left child for node "<< GetKey()<< std::endl;
}
virtual void SetRight(BSTNode* newRightChild) override {
std::cout << "Setting right child for node "<< GetKey()<< std::endl;
AVLNode::SetRight(newRightChild);
UpdateSubtreeKeyCount();
std::cout << "Set right child for node "<< GetKey()<< std::endl;
}
};
#endif // EXTENDEDAVLNODE_
#ifndef EXTENDEDAVLTREE_H
#define EXTENDEDAVLTREE_H
#include "AVLTree.h"
#include "ExtendedAVLNode.h"
#include
class ExtendedAVLTree : public AVLTree {
protected:
virtual BSTNode* MakeNewNode(int key) override {
std::cout << "Creating new node with key: "<< key << std::endl;
return new ExtendedAVLNode(key);
}
virtual void InsertNode(BSTNode* node) override {
std::cout << "Inserting node with key: "<< node->GetKey()<< std::endl;
AVLTree::InsertNode(node);
UpdateSubtreeKeyCounts((ExtendedAVLNode*)node);
}
virtual bool RemoveNode(BSTNode* nodeToRemove) override {
std::cout << "Removing node with key: "<< nodeToRemove->GetKey()<< std::endl;
bool result = AVLTree::RemoveNode(nodeToRemove);
if (result){
ExtendedAVLNode* parent =(ExtendedAVLNode*)nodeToRemove->GetParent();
if (parent){
std::cout << "Updating subtree key counts starting from parent node with key: "<< parent->GetKey()<< std::endl;
UpdateSubtreeKeyCounts(parent);
} else {
std::cout << "Removed node was the root." << std::endl;
}
}
return result;
}
void UpdateSubtreeKeyCounts(ExtendedAVLNode* node){
while (node){
node->UpdateSubtreeKeyCount();
node =(ExtendedAVLNode*)node->GetParent();
}
}
public:
virtual int GetNthKey(int n) override {
std::cout << "Getting the "<< n <<"th key" << std::endl;
return GetNthKeyHelper((ExtendedAVLNode*)root, n);
}
private:
int GetNthKeyHelper(ExtendedAVLNode* node, int n){
if (!node) throw std::out_of_range("Index out of range");
int leftCount = node->GetLeft()?((ExtendedAVLNode*)node->GetLeft())->GetSubtreeKeyCount() : 0;
if (n < leftCount){
return GetNthKeyHelper((ExtendedAVLNode*)node->GetLeft(), n);
} else if (n == leftCount){
return node->GetKey();
} else {
return GetNthKeyHelper((ExtendedAVLNode*)node->GetRight(), n - leftCount -1);
}
}
};
#endif

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!