Question

In: Computer Science

Complete the Tree class within the following program: BalancingTrees.zip. There are three two classes and one...

Complete the Tree class within the following program: BalancingTrees.zip. There are three two classes and one driver in this program. You just need to complete or add the Tree class code.

In java please.

The code for tree class:

public class Tree {
   Node root;

   public Tree() {
       root = null;
   }
  
public int getHeight(Node node) {
if (node == null)
return 0;
return node.height;
}
  
public Node insertANDbalance(Node node, int element) {
  
   // base case
if (node == null)
return (new Node(element));
  
// inserting new elements into tree following Binary tree rules
if (element < node.key)
node.left = insertANDbalance(node.left, element);
else if (element > node.key)
node.right = insertANDbalance(node.right, element);
else
return node;
  
// updating heights of left and right
if (getHeight(node.left) > getHeight(node.right))
   node.height = 1 + getHeight(node.left);
else
   node.height = 1 + getHeight(node.right);
  
// balance tree as each new element is added to it
int balance;
if (node == null)
   balance = 0;
else
   balance = getHeight(node.left) - getHeight(node.right);
  
// left -> left
if (balance > 1 && element < node.left.key)
return rightRotate(node);
// right -> right
else if (balance < -1 && element > node.right.key)
return leftRotate(node);
// left -> right
else if (balance > 1 && element > node.left.key) {
node.left = leftRotate(node.left);
return rightRotate(node);
}
// right -> left
else if (balance < -1 && element < node.right.key) {
node.right = rightRotate(node.right);
return leftRotate(node);
}
  
return node;
}

   public Node rightRotate(Node root) {
       Node newRoot = root.left;
       Node Temp = newRoot.right;

       // Rotate
       newRoot.right = root;
       root.left = Temp;
      
       // Update
       if (getHeight(root.left) > getHeight(root.right))
           root.height = getHeight(root.left)+1;
       else
           root.height = getHeight(root.right)+1;

       if (getHeight(newRoot.left) > getHeight(newRoot.right) + 1)
           newRoot.height = getHeight(newRoot.left)+1;
       else
           newRoot.height = getHeight(newRoot.right)+1;
      
       return newRoot;
   }

   public Node leftRotate(Node root) {
       Node newRoot = root;
      
       // for students to complete for an assignment

       return newRoot;
   }
  


   public static void printTree(Node node, int n) {
   n += 5;

   // Base case
   if (node == null)
   return;
        
   printTree(node.right, n);
   for (int i = 5; i < n; i++)
   System.out.print(" ");
   System.out.println(node.key);
   printTree(node.left, n);
   }
}

The code for node class:

public class Node {
   protected int key, height;
   protected Node left, right;

   public Node(int item) {
       key = item;
       height = 1;
       left = right = null;
   }
}

The driver:

public class TreeDriver{
   public static void main(String[] args) {
       Tree tree = new Tree();

       /* 6
       * / \
       * 2 7
       * / \   
       * 1 4   
       * / \   
       * 3 5
       *   
       */
       tree.root = tree.insertANDbalance(tree.root, 6);
tree.root = tree.insertANDbalance(tree.root, 2);
tree.root = tree.insertANDbalance(tree.root, 1);
tree.root = tree.insertANDbalance(tree.root, 4);
tree.root = tree.insertANDbalance(tree.root, 3);
tree.root = tree.insertANDbalance(tree.root, 5);
tree.root = tree.insertANDbalance(tree.root, 7);
      
tree.printTree(tree.root, 0);
      
   }
}

Solutions

Expert Solution

Program Screenshot for Indentation Reference:

Program code to copy:

public class Tree {
    Node root;

    public Tree() {
        root = null;
    }

    public int getHeight(Node node) {
        if (node == null)
            return 0;
        return node.height;
    }

    public Node insertANDbalance(Node node, int element) {

        // base case
        if (node == null)
            return (new Node(element));

        // inserting new elements into tree following Binary tree rules
        if (element < node.key)
            node.left = insertANDbalance(node.left, element);
        else if (element > node.key)
            node.right = insertANDbalance(node.right, element);
        else
            return node;

        // updating heights of left and right
        if (getHeight(node.left) > getHeight(node.right))
            node.height = 1 + getHeight(node.left);
        else
            node.height = 1 + getHeight(node.right);

        // balance tree as each new element is added to it
        int balance;
        if (node == null)
            balance = 0;
        else
            balance = getHeight(node.left) - getHeight(node.right);

        // left -> left
        if (balance > 1 && element < node.left.key)
            return rightRotate(node);
        // right -> right
        else if (balance < -1 && element > node.right.key)
            return leftRotate(node);
        // left -> right
        else if (balance > 1 && element > node.left.key) {
            node.left = leftRotate(node.left);
            return rightRotate(node);
        }
        // right -> left
        else if (balance < -1 && element < node.right.key) {
            node.right = rightRotate(node.right);
            return leftRotate(node);
        }

        return node;
    }

    public Node rightRotate(Node root) {
        Node newRoot = root.left;
        Node Temp = newRoot.right;

        // Rotate
        newRoot.right = root;
        root.left = Temp;

        // Update
        if (getHeight(root.left) > getHeight(root.right))
            root.height = getHeight(root.left) + 1;
        else
            root.height = getHeight(root.right) + 1;

        if (getHeight(newRoot.left) > getHeight(newRoot.right) + 1)
            newRoot.height = getHeight(newRoot.left) + 1;
        else
            newRoot.height = getHeight(newRoot.right) + 1;

        return newRoot;
    }

    public Node leftRotate(Node root) {
        Node newRoot = root.right;

        Node Temp = newRoot.left;

        // Rotate
        newRoot.left = root;
        root.right = Temp;

        // Update
        if (getHeight(root.left) > getHeight(root.right))
            root.height = getHeight(root.left) + 1;
        else
            root.height = getHeight(root.right) + 1;

        if (getHeight(newRoot.left) > getHeight(newRoot.right) + 1)
            newRoot.height = getHeight(newRoot.left) + 1;
        else
            newRoot.height = getHeight(newRoot.right) + 1;

        // return new root
        return newRoot;
    }

    public static void printTree(Node node, int n) {
        n += 5;

        // Base case
        if (node == null)
            return;

        printTree(node.right, n);
        for (int i = 5; i < n; i++)
            System.out.print(" ");
        System.out.println(node.key);
        printTree(node.left, n);
    }
}

Sample Output:

This created a balanced tree:


Related Solutions

Java program Create two classes based on the java code below. One class for the main...
Java program Create two classes based on the java code below. One class for the main method (named InvestmentTest) and the other is an Investment class. The InvestmentTest class has a main method and the Investment class consists of the necessary methods and fields for each investment as described below. 1.The Investment class has the following members: a. At least six private fields (instance variables) to store an Investment name, number of shares, buying price, selling price, and buying commission...
Classes and Objects Write a program that will create two classes; Services and Supplies. Class Services...
Classes and Objects Write a program that will create two classes; Services and Supplies. Class Services should have two private attributes numberOfHours and ratePerHour of type float. Class Supplies should also have two private attributes numberOfItems and pricePerItem of type float. For each class, provide its getter and setter functions, and a constructor that will take the two of its private attributes. Create method calculateSales() for each class that will calculate the cost accrued. For example, the cost accrued for...
1. You are to write a simple program with two classes. One controller class and a class to hold your object definition.
1. You are to write a simple program with two classes. One controller class and a class to hold your object definition. (Similar to what we used in class) 2. Use a package in your project, the package name should be xxxprojectname. xxx is your initials taken from the first three characters of your Cal Poly email address. 3. Read in the following from the JOptionPane input window: a. Customer First Name b. Customer Last Name c. Customer Phone Number...
C++ tree program (do NOT use STRUCT, use classes)    Program 1 Implement a Binary tree...
C++ tree program (do NOT use STRUCT, use classes)    Program 1 Implement a Binary tree using an array    Program 2 Implement a tree using linked list - pointer Binary Tree    Program 3 - Convert program 1 to a template
In Python, create a program with 2 classes that do the following. HashCreate class, this class...
In Python, create a program with 2 classes that do the following. HashCreate class, this class will accept a directory and hash each file in the directory and store the results in a dictionary. The dictionary will contain the hash value and file name. HashVerify, the second class will accept the dictionary as input and save that in an instance attribute. This class must also contain a method for lookups that require a file path as input. The lookup method...
Round Tree Manor is a hotel that provides two types of rooms with three rental classes:...
Round Tree Manor is a hotel that provides two types of rooms with three rental classes: Super Saver, Deluxe, and Business. The profit per night for each type of room and rental class is as follows: Rental Class Room Super Saver Deluxe Business Type I $36 $38 — Type II $15 $26 $38 Type I rooms do not have wireless Internet access and are not available for the Business rental class. Round Tree's management makes a forecast of the demand...
Round Tree Manor is a hotel that provides two types of rooms with three rental classes:...
Round Tree Manor is a hotel that provides two types of rooms with three rental classes: Super Saver, Deluxe, and Business. The profit per night for each type of room and rental class is as follows: Rental Class Super Saver Deluxe Business Type I $30 $35 -- Room Type II $20 $30 $40 Type I rooms do not have Internet access and are not available for the Business rental class. Round Tree’s management makes a forecast of the demand by...
Round Tree Manor is a hotel that provides two types of rooms with three rental classes:...
Round Tree Manor is a hotel that provides two types of rooms with three rental classes: Super Saver, Deluxe, and Business. The profit per night for each type of room and rental class is as follows: Rental Class Super Saver Deluxe Business Type I $30 $35 -- Room Type II $20 $30 $40 Type I rooms do not have Internet access and are not available for the Business rental class. Round Tree’s management makes a forecast of the demand by...
Round Tree Manor is a hotel that provides two types of rooms with three rental classes:...
Round Tree Manor is a hotel that provides two types of rooms with three rental classes: Super Saver, Deluxe, and Business. The profit per night for each type of room and rental class is as follows: Rental Class Room Super Saver Deluxe    Business Type I    $32 $43    — Type II $17 $35 $39 Type I rooms do not have wireless Internet access and are not available for the Business rental class. Round Tree's management makes a forecast...
Round Tree Manor is a hotel that provides two types of rooms with three rental classes:...
Round Tree Manor is a hotel that provides two types of rooms with three rental classes: Super Saver, Deluxe, and Business. The profit per night for each type of room and rental class is as follows: Rental Class Super Saver Deluxe Business Room Type I (Mountain View) $35 $40 - Type II (Street View) $25 $35 $45 Round Tree's management makes a forecast of the demand by rental class for each night in the future. A linear programming model developed...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT