Answered step by step
Verified Expert Solution
Question
1 Approved Answer
I can not get the rotate and add tests to pass in my AVL Tree program. Both of the ways to write the rotate methods
I can not get the rotate and add tests to pass in my AVL Tree program. Both of the ways to write the rotate methods that I put in the code block do not work. Please tell me what I can do to pass them.
public BSTNode rotateLeft(BSTNode node) { BSTNode rightChild = node.getRight(); node.setRight(rightChild.getLeft()); rightChild.setLeft(node); updateHeight(node); updateHeight(rightChild); return rightChild; } public BSTNode rotateRight(BSTNode node) { if (node == null) { // if the node is null, return null throw new NullPointerException(); BSTNode x = y.left; BSTNode T2 = x.right; // Perform rotation x.right = y; y.left = T2; // Update heights y.height = Math.max(height(y.left), height(y.right))+1; x.height = Math.max(height(x.left), height(x.right))+1; return x; }
A AVLTree.java 1 X PublicAVLTreeTest.java AVLTree.java > AVLTree > > rotateLeft(BSTNode ) int leftHeight = height (node.getLeft()); // get the height of the left subtree int rightHeight = height (node.getRight()); // get the height of the right subtree node.setHeight(Math.max(leftHeight, rightHeight) + 1); // set the height of the node to the max of the left and righ TESTING Filter (e.g. text, !exclude, @tag) 0/1 tests passed (0.00%) bst-redblack-student 34ms > Y src structures > 56 57 58 structures 34ms 59 } PublicAVLTreeTest 34ms 60 61 testUpdateHeight() 2.0ms 62 { testBalanceFactor() 4.0ms 63 testLeftRotate() 6.0ms 64 > testRotateRight() 6.0ms 65 testAdd3() 5.0ms testHeight() 6.0ms testAdd6() 5.0ms 66 67 } 68 69 70 public int balance Factor (BSTNode node) if (node == null) throw new NullPointerException(); return height (node.getRight()) - height (node.getLeft()); 71 72 73 74 75 76 77 78 79 80 public BSTNode rotateLeft (BSTNode node) } BSTNode rightChild = node.getRight(); node.setRight (rightChild.getLeft()); rightChild.setLeft (node);|| updateHeight (node); updateHeight (rightChild); return rightChild; 81 82 83 84 85 { 86 87 public BSTNode rotate Right (BSTNode node) if (node == null) { // if the node is null, return null throw new NullPointerException(); 88 } 89 90 91 92 93 94 95 BSTNode leftChild = node.getLeft(); node.setLeft (leftChild.getRight()); leftChild.setRight (node); updateHeight (node); updateHeight(leftChild); 96 97 return leftChild; 98 } 99 100 > 0 A 3 Ln 75, Col 34 Tab Size: 4 UTF-8 LF {} Java
Step by Step Solution
There are 3 Steps involved in it
Step: 1
It looks like there are a few issues in your code that could be causing the rotate and add tests to fail Here are some suggestions to help you pass th...Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started