Please implement a right rotation funtion:
private Node rightRotate(Node root) {
}
Remember to return the new root of the subtree to the parent so the parent can set it to be its child
package trees;
public class BinaryTree> {
private Node root;
//private int size;
public static int sumTree(Node root) {
if(root== null) {
return 0;
}
int center = 0;
if( root.item % 2 == 0) {
center = root.item;
}
int left = sumTree(root.left);
int right = sumTree(root.right);
return center + left + right;
}
/*
public static boolean equals(Node treeA, Node treeB) {
if(treeA == null && treeB ==null) {
return true;
}
else if(treeA != null && treeB ==null) {
return false;
}
else if(treeA == null && treeB != null){
return false;
} else {
if(treeA.item.equals(treeB.item)){
boolean leftSame = equals(treeA.left, treeB.left);
boolean rightSame = equals(treeA.right, treeB.right);
return leftSame && rightSame;
} else {
return false;
}
}
}
*/
public static int numberOfChars(Node root) {
if(root == null) {
return 0;
}
//int numCharsRoot = root.item.length();
//int numCharsLeft = numberOfChars(root.left);
//int numCharsRight = numberOfChars(root.right);
return root.item.length() + numberOfChars(root.left) +
numberOfChars(root.right);
}
public BinaryTree() {
this.root = null;
//this.size = 0;
}
public int size() {
return this.size(this.root);
}
private int size(Node root) {
if(root == null) {
return 0;
}
//int mySize = 1;
//int leftSize = size(root.left);
//int rightSize = size(root.right);
return 1 + size(root.left)+ size(root.right);
}
public void add(E item) {
this.root = add(this.root, item);
}
private Node add(Node root, E item) {
if (root == null) {
return new Node(item);
}
int comparisonResult = item.compareTo(root.item);
if (comparisonResult == 0) {
root.left = add(root.left, item);
return root;
} else if (comparisonResult < 0) {
root.left = add(root.left, item);
return root;
} else {
root.right = add(root.right, item);
return root;
}
}
public void remove(E item) {
this.root = remove(this.root, item);
}
private Node remove(Node root, E item) {
if (root == null) {
return null;
}
int comparisonResult = item.compareTo(root.item);
if (comparisonResult < 0) {
root.left = remove(root.left, item);
return root;
} else if (comparisonResult > 0) {
root.right = remove(root.right, item);
return root;
} else { // root is the item we want to delete
// case 1, root is leaf
if (root.left == null && root.right == null) {
return null;
} // case 2, root has only left child
else if (root.left != null && root.right == null) {
return root.left;
} else if (root.left == null && root.right != null) {
return root.right;
} else {
Node rootOfLeftSubtree = root.left;
Node parentOfPredecessor = null;
Node predecessor = null;
if (rootOfLeftSubtree.right == null) {
root.item = rootOfLeftSubtree.item;
root.left = rootOfLeftSubtree.left;
return root;
} else {
parentOfPredecessor = rootOfLeftSubtree;
Node current = rootOfLeftSubtree.right;
while (current.right != null) {
parentOfPredecessor = current;
current = current.right;
}
predecessor = current;
root.item = predecessor.item;
parentOfPredecessor.right = predecessor.left;
return root;
}
}
}
}
public boolean contains(E item) {
return contains(this.root, item);
}
private boolean contains(Node root, E item) {
if (root == null) {
return false;
}
int comparisonResult = item.compareTo(root.item);
if (comparisonResult == 0) {
return true;
} else if (comparisonResult < 0) {
return contains(root.left, item);
} else {
return contains(root.right, item);
}
}
public String toString() {
StringBuilder sb = new StringBuilder();
preOrderTraverse(root, 1, sb);
return sb.toString();
}
private String toString(Node root) {
if (root == null) {
return "";
}
String output = "";
output += toString(root.left);
output += root.item + " ";
output += toString(root.right);
return output;
}
private void preOrderTraverse(Node root, int depth, StringBuilder
sb) {
for (int i = 1; i < depth; i++) {
sb.append(" "); // indentation
}
if (root == null) {
sb.append("null\n");
} else {
sb.append(root.toString());
sb.append("\n");
preOrderTraverse(root.left, depth + 1, sb);
preOrderTraverse(root.right, depth + 1, sb);
}
}
private static class Node> {
private E item;
private Node left; // left child
private Node right; // right child
public Node(E item) {
this.item = item;
}
public String toString() {
return item.toString();
}
}
public static void main(String[] args) {
BinaryTree tree = new BinaryTree();
tree.add(5);
tree.add(1);
tree.add(0);
tree.add(2);
tree.add(4);
tree.add(3);
tree.add(12);
tree.add(7);
tree.add(15);
tree.add(14);
tree.add(20);
tree.remove(1);
System.out.println(tree);
//tree.root = tree.rightRotate(tree.root);
System.out.println(tree);
}
}
private Node rightRotate(Node root) {
Node ret = root.left;
Node T2 = ret.right;
ret.right = root;
root.left = T2;
return ret;
}
-------------------------------------------------------------------------------------------------------------------
SEE CODE/IMAGE

PLEASE COMMENT if there is any concern.
=======================================
Please implement a right rotation funtion: private Node rightRotate(Node root) { } Remember to return the...
JAVA Implement a MyQueue class which implements a queue using two stacks. private int maxCapacity = 4; private Stack stack1; private Stack stack2; Note: You can use library Stack but you are not allowed to use library Queue and any of its methods Your Queue should not accept null or empty String or space as an input You need to implement the following methods using two stacks (stack1 & stack2) and also you can add more methods as well: public...
Can you take a look at my code that why the maxDepth function is not working? public class BinaryTree { class Node{ int key; Node left,right; public Node(int item) { key = item; left = right = null; } } Node root; public void BinaryTree(){ root = null; } void...
PLEASE HELP! The assignment details are in the *noted part of the code. I REALLY need help. import java.util.LinkedList; public class TwoDTree { private TwoDTreeNode root; private int size; public TwoDTree() { clear(); } /** * Returns true if a point is already in the tree. * Returns false otherwise. * * The traversal remains the same. Start at the root, if the tree * is not empty, and compare the x-coordinates of the point passed * in and...
Hello. I have written the following function to remove a value from a Binary Search Tree using resources my professors gave and stuff I found online. The problem is I don't know how it works and I need to know how it works to finish the rest of the project. public boolean remove(T value){ if(value == null) return false; class RemoveBSTObj{ private boolean found = false; private Node removeBST(Node root, T value){ if(root == null) return root; //IF there is...
Java: Return an array of booleans in a directed graph. Please complete the TODO section in the mark(int s) function import algs13.Bag; import java.util.HashSet; // See instructions below public class MyDigraph { static class Node { private String key; private Bag<Node> adj; public Node (String key) { this.key = key; this.adj = new Bag<> (); } public String toString () { return key; } public void addEdgeTo (Node n) { adj.add (n); } public Bag<Node> adj () { return adj;...
JAVA QUESTION: *******THE QUESTION:******** /** numberOfNodesAtDepth * * Returns the number of nodes with depth == d * Precondition: none * * param: d the depth to search for * * hint: use a recursive helper function * * ToDo 4 */ public int numNodesAtDepth(int d) { return -1; } **********USEFUL CODE FROM THE ASSIGNMENT:*********** public class simpleBST<Key extends Comparable<Key>, Value> { private Node root; ...
Hi,
So I have a finished class for the most part aside of the toFile
method that takes a file absolute path +file name and writes to
that file. I'd like to write everything that is in my run method
also the toFile method. (They are the last two methods in the
class). When I write to the file this is what I get.
Instead of the desired
That I get to my counsel. I am
having trouble writing my...
Please I need help ASAP Java Programing: Binary Search Tree Fully implement the BST class in Listing 25.4 (on page 961 of the 11th Edition of the text). Design and write a (main) driver program to completely test every method in the BST class to ensure the class meets all its requirements. You should read the Listing 25.5: TestBST.java for an idea of what your program should look like. Listing 25.4 BST.java public class BST> extends AbstractTree { protected TreeNode...
I have almost done with this code to Implement Huffman Coding. However I have an error at the "Heap<Tree>". Could anyone help with solving this issue, really appreciated. Thank you!! import java.util.Scanner; public class HuffmanEncoding { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter a text: "); String text = input.nextLine(); int[] counts = getCharacterFrequency(text); // Count frequency System.out.printf("%-15s%-15s%-15s%-15s\n", "ASCII Code", "Character", "Frequency", "Code"); Tree tree = getHuffmanTree(counts); // Create a Huffman tree String[]...
Implement the following in java. 1. An insertAtBeginning(Node newNode) function, that inserts a node at the beginning(root) of the linked list. 2. A removeFromBeginning() function, that removes the node from the beginning of the linked list and assigns the next element as the new beginning(root). 3. A traverse function, that iterates the list and prints the elements in the linked list. For the insertAtBeginning(Node newNode) function: 1. Check if the root is null. If it is, just assign the new...