Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Here is my code for ExtendedAVLNode.java and ExtendedAVLTree.java. and the results. I am receiving failed tests and I am not sure exactly where my code

Here is my code for ExtendedAVLNode.java and ExtendedAVLTree.java. and the results. I am receiving failed tests and I am not sure exactly where my code needs to be updated.
public class ExtendedAVLNode extends AVLNode {
private int subtreeKeyCount;
public ExtendedAVLNode(int nodeKey){
super(nodeKey);
this.subtreeKeyCount =1;
}
public int getSubtreeKeyCount(){
return subtreeKeyCount;
}
public void updateSubtreeKeyCount(){
int leftCount =(getLeft()!= null)?((ExtendedAVLNode) getLeft()).getSubtreeKeyCount() : 0;
int rightCount =(getRight()!= null)?((ExtendedAVLNode) getRight()).getSubtreeKeyCount() : 0;
this.subtreeKeyCount = leftCount + rightCount +1;
}
@Override
public void setLeft(BSTNode left){
super.setLeft(left);
updateSubtreeKeyCount();
}
@Override
public void setRight(BSTNode right){
super.setRight(right);
updateSubtreeKeyCount();
}
}
public class ExtendedAVLTree extends AVLTree {
// Each node in an ExtendedAVLTree is an ExtendedAVLNode
@Override
protected BSTNode makeNewNode(int key){
return new ExtendedAVLNode(key);
}
public void insert(int key){
insert(key);
updateSubtreeCounts((ExtendedAVLNode) getRoot());
}
public void remove(int key){
remove(key);
updateSubtreeCounts((ExtendedAVLNode) getRoot());
}
private void updateSubtreeCounts(ExtendedAVLNode node){
if (node != null){
node.updateSubtreeKeyCount();
updateSubtreeCounts((ExtendedAVLNode) node.getLeft());
updateSubtreeCounts((ExtendedAVLNode) node.getRight());
}
}
@Override
public int getNthKey(int n){
return getNthKey((ExtendedAVLNode) getRoot(), n);
}
private int getNthKey(ExtendedAVLNode node, int n){
if (node == null){
throw new IllegalArgumentException("Invalid n");
}
int leftCount =(node.getLeft()!= null)?((ExtendedAVLNode) node.getLeft()).getSubtreeKeyCount() : 0;
if (n < leftCount){
return getNthKey((ExtendedAVLNode) node.getLeft(), n);
} else if (n == leftCount){
return node.getKey();
} else {
return getNthKey((ExtendedAVLNode) node.getRight(), n - leftCount -1);
}
}
}
Test 1- insertion and getSubtreeKeyCount()
Inserting keys: 10,20,30,55,42,19,77
PASS: Inorder key verification
Expected: [10,19,20,30,42,55,77]
Actual: [10,19,20,30,42,55,77]
PASS: Node with key 10 has a subtree key count of 2
PASS: Node with key 19 has a subtree key count of 1
FAIL: Node with key 20 has a subtree key count of 5, but the expected subtree key count is 7
PASS: Node with key 30 has a subtree key count of 1
FAIL: Node with key 42 has a subtree key count of 3, but the expected subtree key count is 4
PASS: Node with key 55 has a subtree key count of 2
PASS: Node with key 77 has a subtree key count of 1
Test 2- insertion, removal, and getSubtreeKeyCount()
Inserting keys: 86,75,23,30,98,67,53,9,19,58,14
PASS: Inorder key verification
Expected: [9,14,19,23,30,53,58,67,75,86,98]
Actual: [9,14,19,23,30,53,58,67,75,86,98]
PASS: Node with key 9 has a subtree key count of 2
PASS: Node with key 14 has a subtree key count of 1
FAIL: Node with key 19 has a subtree key count of 3, but the expected subtree key count is 4
PASS: Node with key 23 has a subtree key count of 1
FAIL: Node with key 30 has a subtree key count of 10, but the expected subtree key count is 11
PASS: Node with key 53 has a subtree key count of 1
PASS: Node with key 58 has a subtree key count of 3
PASS: Node with key 67 has a subtree key count of 1
PASS: Node with key 75 has a subtree key count of 6
PASS: Node with key 86 has a subtree key count of 2
PASS: Node with key 98 has a subtree key count of 1
Test 3- insertion, removal, getSubtreeKeyCount(), and getNthKey()
Inserting keys: 10,58,66,18,34,96,5,48,73,62,36,16,23,99,92,95,46,97
PASS: Inorder key verification
Expected: [5,10,16,18,23,34,36,46,48,58,62,66,73,92,95,96,97,99]
Actual: [5,10,16,18,23,34,36,46,48,58,62,66,73,92,95,96,97,99]
FAIL: getNthKey(11) returned 95, but expected key is 66
Summary:
Test 1: FAIL
Test 2: FAIL
Test 3: FAIL

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

Joe Celkos Data And Databases Concepts In Practice

Authors: Joe Celko

1st Edition

1558604324, 978-1558604322

More Books

Students also viewed these Databases questions

Question

9. Understand the phenomenon of code switching and interlanguage.

Answered: 1 week ago