July 2018
Beginner
202 pages
5h 4m
English
The aim is to implement a right tree rotation in Java.
Modify Snippet 3.15 to make the method perform a right tree rotation instead of a left tree rotation. The following code shows the required modification:
public void rightRotate(BinaryTreeNode<K, V> nodeX, BinaryTreeNode<K, V> parent) { BinaryTreeNode<K, V> nodeY = nodeX.getLeft().get(); nodeX.setLeft(nodeY.getRight().orElse(null)); if (parent == null) this.root = nodeY; else if (parent.getRight().filter(n -> n == nodeX).isPresent()) parent.setRight(nodeY); else parent.setLeft(nodeY); nodeY.setRight(nodeX);}
Read now
Unlock full access