Question

Java: simple bst with int keys. please complete the TODO portions (only functions after sizebelowdepth) import...

Java: simple bst with int keys. please complete the TODO portions (only functions after sizebelowdepth)

import algs13.Queue;
import stdlib.*;

/* ***********************************************************************
* A simple BST with int keys
*
* Recall that:
* Depth of root==0.
* Height of leaf==0.
* Size of empty tree==0.
* Height of empty tree=-1.
*
* TODO: complete the functions in this file.
*
* Restrictions:
* - DO NOT change the Node class.
* - DO NOT change the first line of any function: name, parameters, types.
* - you may add new functions, but don't delete anything
* - functions must be recursive, except printLeftI
* - no loops, except in printLeftI (you cannot use "while" "for" etc...)
* - no fields (variables declared outside of a function)
* - each function must have exactly one recursive helper function, which you add
* - each function must be independent --- do not call any function other than the helper
* (But you may use Math.max)
*
* See the method testAll for examples that explain the expected behavior
*************************************************************************/
public class MyIntSET {
        private Node root;

        private static class Node {
                public final int key;
                public Node left, right;

                public Node(int key) {
                        this.key = key;
                }
        }

        // Print only the elements going down the left side of the tree
        // in the BST with level order traversal "41 21 61 11 31", this should print "41
        // 21 11"
        public void printLeftI() {
                Node start = root;
                while (start != null) {
                        System.out.println(start.key + " ");
                        start = start.left;
                }
        }

        // the number of nodes in the tree
        // in the BST with level order traversal "41 21 61 11 31", the size is 5
        public int size() {
                return sizeHelper(root);
        }

        private int sizeHelper(Node start) {
                if (start == null) {
                        return 0;
                }
                return 1 + sizeHelper(start.left) + sizeHelper(start.right);
        }

        // Recall the definitions of height and depth.
        // in the BST with level order traversal "41 21 61 11 31",
        // node 41 has depth 0, height 2
        // node 21 has depth 1, height 1
        // node 61 has depth 1, height 0
        // node 11 has depth 2, height 0
        // node 31 has depth 2, height 0
        // height of the whole tree is the height of the root

        // the height of the tree
        public int height() {
                return heightHelper(root);
        }

        private int heightHelper(Node start) {
                if (start == null) {
                        return 0;
                }
                return 1 + Math.max(heightHelper(start.left), sizeHelper(start.right));
        }

        // the number of nodes with odd keys
        public int sizeOdd() {
                return 0;
        }

        public int sizeOddHelper(Node start) {
                if (start == null) {
                        return 0;
                }
                int count = 0;
                if (start.key % 2 == 1) {
                        count++;
                }
                return count + sizeHelper(start.left) + sizeHelper(start.right);
        }

        // The next three functions compute the size of the tree at depth k.
        // It should be the case that for any given k,
        //
        // sizeAbove(k) + sizeAt(k) + sizeBelow(k) = size()
        //
        // The words "above" and "below" assume that the root is at the "top".
        //
        // Suppose we have with size N and height H (so max depth also H).
        // For such a tree, we expect
        //
        // sizeAboveDepth (-1) = 0
        // sizeAtDepth (-1) = 0
        // sizeBelowDepth (-1) = N
        //
        // sizeAboveDepth (0) = 0
        // sizeAtDepth (0) = 1
        // sizeBelowDepth (0) = N-1
        //
        // sizeAboveDepth (H+1) = N
        // sizeAtDepth (H+1) = 0
        // sizeBelowDepth (H+1) = 0
        //
        // the number of nodes in the tree, at exactly depth k
        // include node n if depth(n) == k
        public int sizeAtDepth(int k) {
                return sizeAtDepthHelper(root, k, 0);
        }

        public int sizeAtDepthHelper(Node start, int k, int currentDepth) {
                if (start == null || currentDepth > k) {
                        return 0;
                }
                if (k == currentDepth) {
                        return 1;
                }
                return sizeAtDepthHelper(start.left, k, currentDepth + 1) + sizeAtDepthHelper(start.right, k, currentDepth + 1);
        }

        // the number of nodes in the tree, "above" depth k (not including k)
        // include node n if depth(n) < k
        public int sizeAboveDepth(int k) {
                return sizeAboveDepthHelper(root, k, 0);
        }

        public int sizeAboveDepthHelper(Node start, int k, int currentDepth) {
                if (start == null || currentDepth >= k) {
                        return 0;
                }
                return 1 + sizeAboveDepthHelper(start.left, k, currentDepth + 1)
                                + sizeAboveDepthHelper(start.right, k, currentDepth + 1);
        }

        // the number of nodes in the tree, "below" depth k (not including k)
        // include node n if depth(n) > k
        public int sizeBelowDepth(int k) {
                return size() - sizeAboveDepth(k + 1);
        }
// tree is perfect if for every node, size of left == size of right
    // hint: in the helper, return -1 if the tree is not perfect, otherwise return the size
    public boolean isPerfectlyBalancedS() {
        // TODO
        return false;
    }

    // tree is perfect if for every node, height of left == height of right
    // hint: in the helper, return -2 if the tree is not perfect, otherwise return the height
    public boolean isPerfectlyBalancedH() {
        // TODO
        return false;
    }

    // tree is odd-perfect if for every node, #odd descendant on left == # odd descendants on right
    // A node is odd if it has an odd key
    // hint: in the helper, return -1 if the tree is not odd-perfect, otherwise return the odd size
    public boolean isOddBalanced() {
        // TODO
        return false;
    }

    // tree is semi-perfect if every node is semi-perfect
    // A node with 0 children is semi-perfect.
    // A node with 1 child is NOT semi-perfect.
    // A node with 2 children is semi-perfect if (size-of-larger-sized-child <= size-of-smaller-sized-child * 3)
    // Here, larger and smaller have to do with the SIZE of the children, not the key values.
    // hint: in the helper, return -1 if the tree is not semi-perfect, otherwise return the size
    public boolean isSemiBalanced() {
        // TODO
        return false;
    }

    /*
     * Mutator functions
     * HINT: all of these are easier to write if the helper function returns Node, rather than void.
     */

    // remove all subtrees with odd roots (if node is odd, remove it and its descendants)
    // A node is odd if it has an odd key
    // If the root is odd, then you should end up with the empty tree
    public void removeOddSubtrees () {
        // TODO
    }

    // remove all subtrees below depth k from the original tree
    public void removeBelowDepth(int k) {
        // TODO
    }

    // add a child with key=0 to all nodes that have only one child
    // (you do not need to retain symmetric order or uniqueness of keys, obviously)
    public void addZeroToSingles() {
        // TODO
    }

    // remove all leaves from the original tree
    // if you start with "41", then the result is the empty tree.
    // if you start with "41 21 61", then the result is the tree "41"
    // if you start with the BST "41 21 11  1", then the result is the tree "41 21 11"
    // if you start with the BST "41 21 61 11", then the result is the tree "41 21"
    // Hint: This requires that you check for "leafiness" before the recursive calls
    public void removeLeaves() {
        // TODO
    }

    // remove all nodes that have only one child by "promoting" that child
    // repeat this recursively as you go up, so the final result should have no nodes with only one child
    // if you start with "41", the tree is unchanged.
    // if you start with "41 21 61", the tree is unchanged.
    // if you start with the BST "41 21 11  1", then the result is the tree "1"
    // if you start with the BST "41 21 61 11", then the result is the tree "41 11 61"
    // Hint: This requires that you check for "singleness" after the recursive calls
    public void removeSingles() {
        // TODO
    }
}
0 0
Add a comment Improve this question Transcribed image text
Answer #1

MyIntSET.java

package algs32;
import algs13.Queue;
import stdlib.*;

public class MyIntSET {
   private Node root;
   private static class Node {
       public final int key;
       public Node left, right;
       public Node(int key) { this.key = key; }
   }
  
   // Print only the elements going down the left side of the tree
   // in the BST with level order traversal "41 21 61 11 31", this should print "41 21 11"
   public void printLeftI () {
       // TODO
       printLeftI(root);
   }
   private void printLeftI(Node temp) {
       if(temp != null) {
           System.out.print(temp.key + " ");
           printLeftI(temp.left);
       }
   }

   // the number of nodes in the tree
   // in the BST with level order traversal "41 21 61 11 31", the size is 5
   public int size() {
       // TODO
       return size(root);
   }

   private int size(Node temp) {
      
       if(temp != null) {
           return 1 + size(temp.left) + size(temp.right);
       }
       else
           return 0;
   }
   public int height() {
       // TODO
       return height(root);
   }
  
   private int height(Node temp) {
      
       if(temp == null)
           return -1;
      
       else {
           int left = height(temp.left);
           int right = height(temp.right);
          
           if(left > right)
               return (left + 1);
           else
               return (right + 1);
          
       }
      
      
   }
  
   // the number of nodes with odd keys
   public int sizeOdd() {
       // TODO
       return sizeOdd(root);
   }
  
   private int sizeOdd(Node temp) {
      
       if(temp != null) {
           if(temp.key % 2 != 0)
               return 1 + sizeOdd(temp.left) + sizeOdd(temp.right);
           else
               return sizeOdd(temp.left) + sizeOdd(temp.right);
       }
       else
           return 0;
   }
   public int sizeAtDepth(int k) {
       // TODO
       return sizeAtDepth(root, k, 0);
   }
  
   private int sizeAtDepth(Node temp, int k , int d) {
      
       if(temp != null) {
           if(d == k)
               return 1;  
           return sizeAtDepth(temp.left, k, d+1) + sizeAtDepth(temp.right, k, d+1);
       }
       return 0;
   }
   public int sizeAboveDepth(int k) {
       // TODO
       return sizeAboveDepth(root, k , 0);
   }
  
   private int sizeAboveDepth(Node temp, int k, int d) {
      
       if(temp != null) {
           if(d < k)
               return 1 + sizeAboveDepth(temp.left, k, d+1) + sizeAboveDepth(temp.right, k, d+1);
       }
       return 0;
   }
   public int sizeBelowDepth(int k) {
       // TODO
       return sizeBelowDepth(root, k, 0);
   }
  
   private int sizeBelowDepth(Node temp, int k, int d) {
       if(temp != null) {
           if(d > k)
               return 1 + sizeBelowDepth(temp.left, k, d+1) + sizeBelowDepth(temp.right, k, d+1);
           return sizeBelowDepth(temp.left, k, d+1) + sizeBelowDepth(temp.right, k, d+1);
       }
       return 0;
   }
   public boolean isPerfectlyBalancedS() {
       // TODO
       if (isPerfectlyBalancedS(root) == -1)
           return false;
       else {
           return true;
       }
   }

   private int isPerfectlyBalancedS(Node temp) {
      
       if(temp == null)
           return 0;
      
           int left = isPerfectlyBalancedS(temp.left);
           if(left == -1) return -1;
           int right = isPerfectlyBalancedS(temp.right);
           if( right == -1) return -1;
          
           int dif = Math.abs(left - right);
          
           if(dif != 0 )
               return -1;
           if(dif == 0)
               return (left + right)+ 1;
      
      
       return 0;
   }
   public boolean isPerfectlyBalancedH() {
       // TODO
       if(isPerfectlyBalancedH(root) == -2)
           return false;
       else {
           return true;
          
       }  
       }

   private int isPerfectlyBalancedH(Node temp) {
      
       if(temp == null)
           return 0;
      
       int left = isPerfectlyBalancedH(temp.left);
       if(left == -2) return -2;
       int right = isPerfectlyBalancedH(temp.right);
       if(right == -2) return -2;
      
       int dif = Math.abs(left -right);
       if(dif > 0)
           return -2;
      
       return Math.max(left, right) + 1;
      
      
   }
   public boolean isOddBalanced() {
       // TODO
       if(isOddBalanced(root) == -1)
           return false;
       else
           return true;
   }
  
  
   private int isOddBalanced(Node temp) {
      
       if(temp == null)
           return 0;
      
       int left = isOddBalanced(temp.left);
       if(left == -1) return -1;
       int right = isOddBalanced(temp.right);
       if(right == -1) return -1;
      
       int dif = Math.abs(left - right);
      
       if(dif != 0)
           return -1;
      
       if(temp.key %2 != 0)
           return left + right +1;
      
       return 0;
      
      
   }
   public boolean isSemiBalanced() {
       // TODO
       if (isSemiBalanced(root) ==-1)
           return false;
       else
           return true;
   }

   private int isSemiBalanced(Node temp) {
      
       if(temp == null)
           return 0;
      
           int left = isSemiBalanced(temp.left);
           if(left == -1) return -1;
           int right = isSemiBalanced(temp.right);
           if( right == -1) return -1;
          
           boolean verif = false;
           if(left < right) {
               if(right <= left *3) {
                   verif = true;}
           }
           else if (right < left) {
               if(left <= right *3) {
                   verif = true;
               }
           }
           else if(right == left)
               verif = true;
          
           if(verif)
               return left + right +1;
      
      
       return -1;
   }
  
   public void removeOddSubtrees () {
       // TODO
       root = removeOddSubtrees(root);
   }
  
   private Node removeOddSubtrees(Node temp) {
          
           if(temp != null) {
              
               if(temp.left != null && temp.left.key % 2 != 0) {
                   temp.left = null;
               }
               if(temp.right != null && temp.right.key % 2 != 0) {
                   temp.right = null;
               }
               if(temp.key % 2 != 0)
                   temp = null;  
              
               if(temp!=null) {
               removeOddSubtrees(temp.left);
               removeOddSubtrees(temp.right);
               }
          
       }
       return temp;
      
   }
   public void removeBelowDepth(int k) {
       // TODO
       root = removeBelowDepth(root, k, 0);
   }
  
   private Node removeBelowDepth(Node temp, int k, int d) {
      
       if(temp != null) {
          
           if(d == k) {
               temp.left = null;
               temp.right = null;
           }
          
           removeBelowDepth(temp.left, k, d+1);
           removeBelowDepth(temp.right, k, d+1);
          
          
       }
      
       return temp;
   }
   public void addZeroToSingles() {
       // TODO
       addZeroToSingles(root);
   }
  
   private void addZeroToSingles(Node temp) {
      
       if(temp != null) {
          
           if(temp.left == null && temp.right != null || temp.left != null && temp.right == null) {
               Node add = new Node(0);
               if(temp.left == null)
                   temp.left = add;
               else
                   temp.right = add;      
           }
           addZeroToSingles(temp.left);
           addZeroToSingles(temp.right);
          
       }
   }
   public void removeLeaves() {
       // TODO
       root = removeLeaves(root);
      
   }
  
   private Node removeLeaves(Node temp) {
      
       if(temp != null) {
          
           if(temp.left != null && temp.left.left == null && temp.left.right == null){
               temp.left = null;
           }
           if(temp.right != null && temp.right.left == null && temp.right.right == null){
               temp.right = null;
           }
           if(temp.left == null && temp.right == null) {
               temp = null;
           }
          
           if(temp != null && temp.left != null)removeLeaves(temp.left);
           if(temp != null && temp.right != null)removeLeaves(temp.right);
          
       }
      
       return temp;
   }
   public void removeSingles() {
       // TODO
       root = removeSingles(root);
   }
  
   private Node removeSingles(Node temp) {
      
       if(temp != null) {
          
           removeSingles(temp.left);
           removeSingles(temp.right);
           if((temp.left == null && temp.right != null))
               temp = temp.right;
              
           else if((temp.right == null && temp.left != null))
           temp = temp.left;
          
       }
      
       return temp;
      
   }

  
}

Add a comment
Know the answer?
Add Answer to:
Java: simple bst with int keys. please complete the TODO portions (only functions after sizebelowdepth) import...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT