Question: Hi ! I need help implimenting a balance ( ) method that balances a existing BST . There is more information in the photo attached
Hi I need help implimenting a balance method that balances a existing BST There is more information in the photo attached and here is the code i have so far:
public void balance
LinkedList nodes new LinkedList;
sortNodesnodes root;
root balanceTreenodes size;
private void sortNodesLinkedList nodes, Node n
ifn null
return;
sortNodesnodes nleft;
nodes.addn;
sortNodesnodes nright;
private Node balanceTreeLinkedList nodes, int start, int end
ifstart end
return null;
int middle start end;
ifstart end
middle;
Node middleNode nodes.getmiddle;
middleNode.left balanceTreenodes start,middle;
middleNode.right balanceTreenodes middle end;
return middleNode;
It is not working correctly on some of the tests:
balanceHint: Balances short left stilted tree and updates node sizes. Test Failed!
Node key: does not have correct N value, expected found
at edu.sermtests.BSTTest.assertNodeNValues:BSTTestjava
at edu.sermtests.BSTTest.testBalance:BSTTestjava
balanceHint: Balances short right stilted tree and updates node sizes. Test Failed!
Node key: does not have correct N value, expected found
at edu.sermtests.BSTTest.assertNodeNValues:BSTTestjava
at edu.sermtests.BSTTest.testBalance:BSTTestjava
balanceHint: Balances right stilted tree and updates node sizes. Test Failed!
Node key: does not have correct N value, expected found
at edu.sermtests.BSTTest.assertNodeNValues:BSTTestjava
at edu.sermtests.BSTTest.testBalance:BSTTestjava
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
