Advertisement
JustMark

MWST Solution

Jan 28th, 2022
858
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.37 KB | None | 0 0
  1. package weblab;
  2.  
  3. import java.util.*;
  4.  
  5. class Solution {
  6.  
  7.     private static List<MWSTNode> getNodesAtDepth(MWSTNode start, int depth) {
  8.         List<MWSTNode> result = new ArrayList<>();
  9.         getNodesAtDepthHelper(result, start, 0, depth);
  10.         return result;
  11.     }
  12.  
  13.     private static void getNodesAtDepthHelper(List<MWSTNode> result, MWSTNode curNode, int curDepth, int targetDepth) {
  14.         if (curNode == null) return;
  15.        
  16.         if (curDepth == targetDepth) {
  17.             result.add(curNode);
  18.             return;
  19.         }
  20.  
  21.         for (MWSTNode c : curNode.getChildren()) {
  22.             getNodesAtDepthHelper(result, c, curDepth+1, targetDepth);
  23.         }
  24.     }
  25.  
  26.     private static int indexOfChild(MWSTNode parent, MWSTNode child) {
  27.         List<MWSTNode> children = parent.getChildren();
  28.  
  29.         int i = 0;
  30.         for (MWSTNode c : children) {
  31.             if (Objects.equals(c, child)) return i;
  32.             i++;
  33.         }
  34.  
  35.         return -1;
  36.     }
  37.  
  38.     /**
  39.     * See the description of the exercise.
  40.     */
  41.     public static MWSTNode getCousin(MultiWaySearchTree tree, MWSTNode node) {
  42.         if (tree == null || node == null) return null;
  43.  
  44.         MWSTNode parent = node.getParent();
  45.         if (parent == null) return null;
  46.         MWSTNode grandparent = parent.getParent();
  47.         if (grandparent == null) return null;
  48.  
  49.         int index = indexOfChild(grandparent, parent) - 1;
  50.  
  51.         while (index >= 0) {
  52.             MWSTNode start = grandparent.getChildren().get(index);
  53.             List<MWSTNode> cousins = getNodesAtDepth(start, 1);
  54.             if (cousins.size() > 0) return cousins.get(cousins.size()-1);
  55.             index--;
  56.         }
  57.  
  58.         return null;
  59.     }
  60.  
  61.     /**
  62.     * See the description of the exercise.
  63.     */
  64.     public static MWSTNode getUncle(MultiWaySearchTree tree, MWSTNode node) {
  65.         if (tree == null || node == null) return null;
  66.  
  67.         MWSTNode parent = node.getParent();
  68.         if (parent == null) return null;
  69.         MWSTNode grandparent = parent.getParent();
  70.         if (grandparent == null) return null;
  71.        
  72.         int minIndex = indexOfChild(grandparent, parent) + 1;
  73.         int index = grandparent.getChildren().size()-1;
  74.  
  75.         while (index >= minIndex) {
  76.             MWSTNode start = grandparent.getChildren().get(index);
  77.             List<MWSTNode> siblings = getNodesAtDepth(start, 0);
  78.             if (siblings.size() > 0) return siblings.get(siblings.size()-1);
  79.             index--;
  80.         }
  81.  
  82.         return null;
  83.     }
  84.  
  85.     /**
  86.     * See the description of the exercise.
  87.     */
  88.     public static boolean restrictedSearch(MultiWaySearchTree tree, int key) {
  89.         if (tree == null) return false;
  90.         return searchHelper(tree.getRoot(), 0, 2, key);
  91.     }
  92.  
  93.     private static boolean searchHelper(MWSTNode curNode, int curDepth, int maxDepth, int key) {
  94.         if (curNode == null) return false;
  95.         if (curDepth > maxDepth) return false;
  96.        
  97.         List<Integer> keys = curNode.getKeys();
  98.         List<MWSTNode> children = curNode.getChildren();
  99.  
  100.         for (Integer k : keys)
  101.             if (Objects.equals(k, key)) return true;
  102.  
  103.         int index = 0;
  104.         for (Integer k : keys) {
  105.             if (key < k) break;
  106.             index++;
  107.         }
  108.  
  109.         return (searchHelper(children.get(index), curDepth+1, maxDepth, key));
  110.     }
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement