Question

In: Computer Science

Java programming. Purpose: Understanding the rotation operation in a search tree Understanding heap & priority queue...

Java programming.

Purpose:

  • Understanding the rotation operation in a search tree
  • Understanding heap & priority queue

Problems:

  • Implementation: Rotation (left & right rotations)
    • Use the code template below.
    • Implement the rotateLeft() and rotateRight() functions.
    • How to test your implementation? When a left rotation is applied to the root, apparently the root is changed. One right rotation will restore the tree back to the original

Sample input:

7
30 15 40 5 20 35 45

Sample output:

in-order: 5 15 20 30 35 40 45 
before rotation: 
pre-order: 30 15 5 20 40 35 45 
after rotating left: 
pre-order: 40 30 15 5 20 35 45 
after rotating right: 
pre-order: 30 15 5 20 40 35 45

USE THE BELOW TEMPLATE:

import java.util.*;
class Node <E> {
private Node<E> left;
private Node<E> right;
private E data;
Node(E data) {
this.data = data;
left = null;
right = null;
}
void setData(E d) {
data = d;
}
E getData() {
return data;
}
void setLeft(Node<E> i) {
left = i;
}
void setRight(Node<E> i) {
right = i;
}
Boolean hasLeft() {
return left != null;
}
Node<E> getLeft() {
return left;
}
Boolean hasRight() {
return right != null;
}
Node<E> getRight() {
return right;
}
}
class BST <E extends Comparable<?super E>> {
private Node<E> root;
BST() {
root = null;
}
Node<E> getRoot() {
return root;
}
void inOrder() {
System.out.print("in-order: ");
inOrder(root);
System.out.println();
}
void inOrder(Node<E>root) {
if(root == null) return;
inOrder(root.getLeft());
System.out.print(root.getData() + " ");
inOrder(root.getRight());
}
void preOrder() {
System.out.print("pre-order: ");
preOrder(root);
System.out.println();
}
void preOrder(Node<E>root) {
if(root == null) return;
System.out.print(root.getData() + " ");
if(root.hasLeft()) preOrder(root.getLeft());
if(root.hasRight()) preOrder(root.getRight());
}
void insert(E data) {
root = insert(root, data);
}
Node<E> insert(Node<E> root, E data) {
if (root == null) {
return new Node<E>(data);
} else {
Node<E> cur;
if (root.getData().compareTo(data) > 0) {
cur = insert(root.getLeft(), data);
root.setLeft(cur);
} else {
cur = insert(root.getRight(), data);
root.setRight(cur);
}
return root;
}
}
//apply left rotate to the root node
void rotateLeft() {
}
//apply right rotate to the root node
void rotateRight() {
}
}
public class RotationMain {
public static void main(String[] argv) {
int n;
Scanner in = new Scanner(System.in);
n = in.nextInt();
BST<Integer> tree = new BST<Integer>();
for(int i = 0; i < n; i ++) {
tree.insert(in.nextInt());
}
in.close();
//if BST is correctly built, items will be displayed in sorted order using
//in-order traversal
tree.inOrder();
System.out.println("before rotation: ");
tree.preOrder();
System.out.println("after rotating left: ");
tree.rotateLeft();
tree.preOrder();
System.out.println("after rotating right: ");
tree.rotateRight();
tree.preOrder();
}
}

Solutions

Expert Solution

For the above code to be updated we need to update the pointers in order to make sure that the tree should rotate left or right.

Following is the updated code in JAVA, It is in bold

import java.util.*;
class Node <E> {
private Node<E> left;
private Node<E> right;
private E data;
Node(E data) {
this.data = data;
left = null;
right = null;
}
void setData(E d) {
data = d;
}
E getData() {
return data;
}
void setLeft(Node<E> i) {
left = i;
}
void setRight(Node<E> i) {
right = i;
}
Boolean hasLeft() {
return left != null;
}
Node<E> getLeft() {
return left;
}
Boolean hasRight() {
return right != null;
}
Node<E> getRight() {
return right;
}
}
class BST <E extends Comparable<?super E>> {
private Node<E> root;
BST() {
root = null;
}
Node<E> getRoot() {
return root;
}
void inOrder() {
System.out.print("in-order: ");
inOrder(root);
System.out.println();
}
void inOrder(Node<E>root) {
if(root == null) return;
inOrder(root.getLeft());
System.out.print(root.getData() + " ");
inOrder(root.getRight());
}
void preOrder() {
System.out.print("pre-order: ");
preOrder(root);
System.out.println();
}
void preOrder(Node<E>root) {
if(root == null) return;
System.out.print(root.getData() + " ");
if(root.hasLeft()) preOrder(root.getLeft());
if(root.hasRight()) preOrder(root.getRight());
}
void insert(E data) {
root = insert(root, data);
}
Node<E> insert(Node<E> root, E data) {
if (root == null) {
return new Node<E>(data);
} else {
Node<E> cur;
if (root.getData().compareTo(data) > 0) {
cur = insert(root.getLeft(), data);
root.setLeft(cur);
} else {
cur = insert(root.getRight(), data);
root.setRight(cur);
}
return root;
}
}
//apply left rotate to the root node
void rotateLeft() {
   Node x = this.getRoot().getLeft();
Node T2 = x.getRight();

// Perform rotation
x.setRight(this.getRoot());
this.getRoot().setLeft(T2);
this.root = x;
}
//apply right rotate to the root node
void rotateRight() {
   Node y = this.getRoot().getRight();
Node T2 = y.getLeft();

// Perform rotation
y.setLeft(this.getRoot());
this.getRoot().setRight(T2);
  
this.root = y;
}
}

public class RotationMain {
public static void main(String[] argv) {
int n;
Scanner in = new Scanner(System.in);
n = in.nextInt();
BST<Integer> tree = new BST<Integer>();
for(int i = 0; i < n; i ++) {
tree.insert(in.nextInt());
}
in.close();
//if BST is correctly built, items will be displayed in sorted order using
//in-order traversal
tree.inOrder();
System.out.println("before rotation: ");
tree.preOrder();
System.out.println("after rotating left: ");
tree.rotateLeft();
tree.preOrder();
System.out.println("after rotating right: ");
tree.rotateRight();
tree.preOrder();
}
}

The following is the output for the same.

That was a nice question to answer
Friend, If you have any doubts in understanding do let me know in the comment section. I will be happy to help you further.
Please like it if you think effort deserves like.
Thanks


Related Solutions

Language C++ Implement a Priority Queue with a Binary HEAP. Use a Max Heap. Create a...
Language C++ Implement a Priority Queue with a Binary HEAP. Use a Max Heap. Create a class called Node: Have a Name and Priority.Data set - 10 is the highest priority, 1 is lowest priority. Enqueue and dequeue in the following order. Function  Name, Priority Enqueue  Joe, 3 Enqueue  Fred,1 Enqueue Tuyet,9 Enqueue  Jose, 6 Dequeue Enqueue  Jing, 2 Enqueue  Xi, 5 Enqueue  Moe, 3 DequeueEnqueue  Miko, 7 Enqueue Vlady, 8 Enqueue Frank, 9 Enqueue  Anny, 3 DequeueEnqueue  Xi, 2 Enqueue  Wali, 2 Enqueue  xChe, 6 Enqueue  xVerra, 8 Dequeue Dequeue Dequeue Dequeue...
A priority queue can be implemented as a heap because a. The root can be easily...
A priority queue can be implemented as a heap because a. The root can be easily be identified as the topmost priority. b. The heap is not always sorted so any value can be the top priority. c. The heap always has a left bottom node that can be the top priority. d. None of the above.
write C program to implement the priority queue with the operation insert
write C program to implement the priority queue with the operation insert
A max-heap can be used as a max-priority queue, which is an abstract data type having...
A max-heap can be used as a max-priority queue, which is an abstract data type having the operations Insert, Maximum, Extract-Max, and Increase-Key. a. Describe how to implement a FIFO queue with a priority queue, and analyze the time complexity of the implementation (Insert and Delete operations), assuming that a heap is used for the priority queue. b. Describe how to implement a LIFO queue (i.e. a stack) with a priority queue, and analyze the time complexity of the implementation...
IN JAVA Given a binary search tree, extract min operation finds the node with minimum key...
IN JAVA Given a binary search tree, extract min operation finds the node with minimum key and then takes it out of the tree. The program can be C++/Java or C-style pseudocode. Do not use function call to either Min or delete done in the class. Write this from scratch. (No need to ensure AVL properties, just show it for a generic BST) Node * extract min(Node * x) { }
How to write a max-heap implementation of a templated priority queue class using the STL std::vector...
How to write a max-heap implementation of a templated priority queue class using the STL std::vector class to hold the data array
Building a Priority Queue in C++ using a min-heap. Not using C++ Standard Template Library. Trying...
Building a Priority Queue in C++ using a min-heap. Not using C++ Standard Template Library. Trying to understand how this works. Thank you
java binary heap operation counts /* * Add operation counts to all methods - 4pts *...
java binary heap operation counts /* * Add operation counts to all methods - 4pts * No other methods/variables should be added/modified */ public class A6BH <E extends Comparable<? super E>> {    private int defaultSize = 4;    private int count = 0;    private E[] heap;    private int opCount = 0;       public int getOpCount()    {        return opCount;    }    public void resetOpCount()    {        opCount = 0;    }...
In C#, write a method that adds a Huffman tree (implemented via a minimum priority queue)...
In C#, write a method that adds a Huffman tree (implemented via a minimum priority queue) to a dictionary table. Include any necessary fields/properties, but keep the functionality within one single method.
Write a java class for a Priority Queue. Use an arraylist, and include enque, deque, and...
Write a java class for a Priority Queue. Use an arraylist, and include enque, deque, and a method to get all the values of the queue. (This is not writing a file implementing the java class PriorityQueue, but rather you are writing a program that is a priority queue).
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT