Advertisement
Guest User

CS132 Quiz 4 Worksheet Revised

a guest
Nov 19th, 2019
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.91 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.TreeMap;
  3.  
  4. public class Quiz_4<K extends Comparable<K>, V> {
  5.     private class Node {
  6.         private K key;
  7.         private V data;
  8.         private Node left, right;
  9.  
  10.         public Node(K key, V data) {
  11.             this.key = key;
  12.             this.data = data;
  13.         }
  14.     }
  15.  
  16.     private Node root;
  17.  
  18.     public Quiz_4() {
  19.         root = null;
  20.     }
  21.  
  22.     public void add(K key, V data) {
  23.         addAux(key, data, root);
  24.     }
  25.  
  26.     public Quiz_4<K,V> addAux(K key, V data, Node tree) {
  27.         if (tree == null) {
  28.             tree = new Node(key, data);
  29.             return this;
  30.         }
  31.         if (key.compareTo(tree.key) == 0) {
  32.             tree.data = data;
  33.             return this;
  34.         }
  35.         if (key.compareTo(root.key) < 0) {
  36.             addAux(key, data, tree.left);
  37.             return this;
  38.         }
  39.         addAux(key, data, tree.right);
  40.         return this;
  41.     }
  42.  
  43.     public int size() {
  44.         return sizeAux(root);
  45.     }
  46.  
  47.     public int sizeAux(Node tree) {
  48.         return tree == null ? 0 : 1 + sizeAux(tree.right) + sizeAux(tree.left);
  49.     }
  50.  
  51.     public V maxNonRec() {
  52.         if (root == null) {
  53.             return null;
  54.         }
  55.         Node n = root;
  56.         while (n.right != null) {
  57.             n = n.right;
  58.         }
  59.         return n.data;
  60.     }
  61.  
  62.     public V max() {
  63.         return root == null ? null : maxAux(root);
  64.     }
  65.  
  66.     public V maxAux(Node tree) {
  67.         return tree.right == null ? tree.data : maxAux(tree.right);
  68.     }
  69.  
  70.     public V min() {
  71.         return root == null ? null : minAux(root);
  72.     }
  73.  
  74.     public V minAux(Node tree) {
  75.         return tree.left == null ? tree.data : minAux(tree.left);
  76.     }
  77.  
  78.     public String postOrderTraversal() {
  79.         return postAux(root);
  80.     }
  81.  
  82.     public String postAux(Node tree) {
  83.         return tree == null ? "" : postAux(tree.left) + postAux(tree.right) + tree.key;
  84.     }
  85.  
  86.     public int getNumInteriorNodes() {
  87.         return interiorAux(root);
  88.     }
  89.  
  90.     public int interiorAux(Node tree) {
  91.         return tree.right == null && tree.left == null ? 0 : 1 + interiorAux(tree.right) + interiorAux(tree.left);
  92.     }
  93.  
  94.     public int getHeight() {
  95.         return heightAux(root);
  96.     }
  97.  
  98.     public int heightAux(Node tree) {
  99.         return tree == null ? 0 : 1 + Math.max(heightAux(tree.left), heightAux(tree.right));
  100.     }
  101.  
  102.     public void leaves(ArrayList<K> L) {
  103.         L = leavesAux(root, L);
  104.     }
  105.  
  106.     public ArrayList<K> leavesAux(Node tree, ArrayList<K> list) {
  107.         if (tree.left != null || tree.right != null) {
  108.             list = leavesAux(tree.left, list);
  109.             list = leavesAux(tree.right, list);
  110.         }
  111.         list.add(tree.key);
  112.         return list;
  113.     }
  114.  
  115.     public ArrayList<V> getDecreasingOrderList() {
  116.         ArrayList<V> list = new ArrayList<V>();
  117.         return decreasingListAux(root, list);
  118.     }
  119.  
  120.     public ArrayList<V> decreasingListAux(Node tree, ArrayList<V> list) {
  121.         if (tree == null) {
  122.             return list;
  123.         }
  124.         list = decreasingListAux(tree.right, list);
  125.         list.add(tree.data);
  126.         list = decreasingListAux(tree.left, list);
  127.         return list;
  128.     }
  129.  
  130.     public void getDataOneChildNodes(ArrayList<V> L) {
  131.         L = oneChildPolicy(root, L);
  132.     }
  133.  
  134.     public ArrayList<V> oneChildPolicy(Node tree, ArrayList<V> L) {
  135.         if (tree != null) {
  136.             if (tree.left == null ^ tree.right == null) {
  137.                 L.add(tree.data);
  138.             }
  139.             L = oneChildPolicy(tree.left, L);
  140.             L = oneChildPolicy(tree.right, L);
  141.         }
  142.         return L;
  143.     }
  144.  
  145.     public ArrayList<K> getKeyNodesAtLevel(int targetLevel) {
  146.         ArrayList<K> list = new ArrayList<K>();
  147.         return nodesAtLevelAux(targetLevel, root, list);
  148.     }
  149.  
  150.     public ArrayList<K> nodesAtLevelAux(int level, Node tree, ArrayList<K> list) {
  151.         if (tree == null) {
  152.             return list;
  153.         }
  154.         if(level == 1) {
  155.             list.add(tree.key);
  156.             return list;
  157.         }
  158.         list = nodesAtLevelAux(level-1, tree.right, list);
  159.         list = nodesAtLevelAux(level-1, tree.left, list);
  160.         return list;
  161.     }
  162.     public TreeMap<K,V> placeKeysValues() {
  163.         TreeMap<K,V> map = new TreeMap<K,V>();
  164.         return placeKeysValuesAux(root, map);
  165.     }
  166.     public TreeMap<K,V> placeKeysValuesAux(Node tree, TreeMap<K,V> map) {
  167.         if(tree == null) {
  168.             return map;
  169.         }
  170.         map = placeKeysValuesAux(tree.left, map);
  171.         map.put(tree.key,tree.data);
  172.         map = placeKeysValuesAux(tree.right, map);
  173.         return map;
  174.     }
  175. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement