Advertisement
Guest User

Main.java

a guest
May 25th, 2015
258
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 11.24 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.util.ArrayList;
  5. import java.util.List;
  6.  
  7.  
  8. public class Main {
  9.     public static void main(String[] args) throws IOException {
  10.         BufferedReader reader = new BufferedReader(new InputStreamReader(
  11.                 System.in));
  12.         String str = "";
  13.         Order agent = new Order();
  14.         while ((str = reader.readLine()) != null && str.length() != 0) {
  15.             String[] input = str.split(" ");
  16.             switch (input[0].toUpperCase()) {
  17.             case ("ADD"):
  18.                 String from = input[1];
  19.                 String to = input[2];
  20.                 Laci inputan = new Laci(from);
  21.                 Laci satunya = new Laci(to);
  22.                 agent.add(inputan, to);
  23.                 break;
  24.             case ("PUT"):
  25.                 String key = input[1];
  26.                 int weight = Integer.parseInt(input[2]);
  27.                 String laci = input[3];
  28.                 agent.put(key, weight, laci);
  29.                 break;
  30.             case ("THROW"):
  31.                 String thing = input[1];
  32.                 agent.buang(thing);
  33.                 break;
  34.             case ("MOVE"):
  35.                 break;
  36.             case ("CETAK"):
  37.                 String cetak = input[1];
  38.                 agent.cetak(cetak);
  39.                 break;
  40.             case ("DEPTH"):
  41.                 String kunciMasuk = input[1];
  42.                 String laciNya = input[2];
  43.                 agent.depth(kunciMasuk, laciNya);
  44.                 break;
  45.  
  46.             case ("FIND"):
  47.                 String tobeFind = input[1];
  48.                 TreeNode<Bismillah> temp;
  49.                 if (tobeFind.substring(0, 5).equalsIgnoreCase("kunci")) {
  50.                     temp = new TreeNode<Bismillah>(new Kunci(tobeFind, 0));
  51.                 } else {
  52.                     temp = (new TreeNode<Bismillah>(new Laci(tobeFind)));
  53.                 }
  54.                 agent.cari(temp);
  55.  
  56.                 break;
  57.             }
  58.         }
  59.     }
  60. }
  61.  
  62. class TreeNode<E extends Comparable<E>> {
  63.  
  64.     E data;
  65.     List<TreeNode<E>> children;
  66.     TreeNode<E> parent;
  67.  
  68.     public TreeNode(E data) {
  69.         this.data = data;
  70.         this.children = new ArrayList<TreeNode<E>>();
  71.     }
  72.  
  73.     public TreeNode(TreeNode<E> node) {
  74.         this.data = (E) node.getData();
  75.         children = new ArrayList<TreeNode<E>>();
  76.     }
  77.  
  78.     public void addChild(TreeNode<E> child) {
  79.         child.parent = this;
  80.         children.add(child);
  81.     }
  82.  
  83.     public void addChildAt(int index, TreeNode<E> child) {
  84.         child.parent = this;
  85.         this.children.add(index, child);
  86.     }
  87.  
  88.     public void setChildren(List<TreeNode<E>> children) {
  89.         for (TreeNode<E> child : children)
  90.             child.parent = this;
  91.  
  92.         this.children = children;
  93.     }
  94.  
  95.     public void removeChildren() {
  96.         this.children = new ArrayList<TreeNode<E>>();
  97.     }
  98.  
  99.     public TreeNode<E> removeChildAt(int index) {
  100.         return children.remove(index);
  101.     }
  102.  
  103.     public void removeThisIfItsAChild(TreeNode<E> childToBeDeleted) {
  104.         List<TreeNode<E>> list = getChildren();
  105.         list.remove(childToBeDeleted);
  106.     }
  107.  
  108.     public E getData() {
  109.         return this.data;
  110.     }
  111.  
  112.     public void setData(E data) {
  113.         this.data = data;
  114.     }
  115.  
  116.     public TreeNode<E> getParent() {
  117.         return this.parent;
  118.     }
  119.  
  120.     public void setParent(TreeNode<E> parent) {
  121.         this.parent = parent;
  122.     }
  123.  
  124.     public List<TreeNode<E>> getChildren() {
  125.         return this.children;
  126.     }
  127.  
  128.     public TreeNode<E> getChildAt(int index) {
  129.         return children.get(index);
  130.     }
  131.  
  132.     @Override
  133.     public boolean equals(Object obj) {
  134.         if (null == obj)
  135.             return false;
  136.  
  137.         if (obj instanceof TreeNode) {
  138.             if (((TreeNode<?>) obj).getData().equals(this.data))
  139.                 return true;
  140.         }
  141.  
  142.         return false;
  143.     }
  144.  
  145.     @Override
  146.     public String toString() {
  147.         return this.data.toString();
  148.     }
  149. }
  150.  
  151. class Order {
  152.  
  153.     Tree<Bismillah> tempat = new Tree<Bismillah>(new TreeNode<Bismillah>(
  154.             new Laci("laci1")));
  155.  
  156.     public void add(Laci a, String b) {
  157.         Laci baru = new Laci(b);
  158.         TreeNode<Bismillah> baru2 = new TreeNode<Bismillah>(baru);
  159.         TreeNode<Bismillah> inputan = new TreeNode<Bismillah>(a);
  160.         TreeNode<Bismillah> barulagi = tempat.find(baru2);
  161.         barulagi.addChild(inputan);
  162.  
  163.     }
  164.  
  165.     public void put(String key, int weight, String laci) {
  166.         Laci baru = new Laci(laci);
  167.         Kunci kunci = new Kunci(key, weight);
  168.         TreeNode<Bismillah> baru2 = new TreeNode<Bismillah>(baru);
  169.         TreeNode<Bismillah> kuncitemp = new TreeNode<Bismillah>(kunci);
  170.         TreeNode<Bismillah> barulagi = tempat.find(baru2);
  171.         barulagi.addChild(kuncitemp);
  172.         System.out.println(key + " masuk di " + laci);
  173.     }
  174.  
  175.     public void buang(String x) {
  176.         TreeNode<Bismillah> sementara;
  177.         if (x.substring(0, 5).equalsIgnoreCase("kunci")) {
  178.             sementara = tempat.find(new TreeNode<Bismillah>(new Kunci(x, 0)));
  179.         } else {
  180.             sementara = tempat.find(new TreeNode<Bismillah>(new Laci(x)));
  181.         }
  182.         sementara.parent.removeChildren();
  183.     }
  184.  
  185.     public void moveKunci() {
  186.  
  187.     }
  188.  
  189.     public void cetak(String x) {
  190.         cetak(x, 0);
  191.     }
  192.  
  193.     // THIS PARTS
  194.    
  195.     private void cetak(String x, int acc) {
  196.         TreeNode<Bismillah> temp;
  197.         if (x.substring(0, 5).equalsIgnoreCase("kunci")) {
  198.             temp = tempat.find(new TreeNode<Bismillah>(new Kunci(x, 5)));
  199.            
  200.         } else {
  201.             temp = tempat.find(new TreeNode<Bismillah>(new Laci(x)));
  202.         }
  203.         for (int i = 0; i < acc; i++) {
  204.             System.out.print("  ");
  205.         }
  206.        
  207.         System.out.println("> " + temp.getData().getNama() + " "
  208.                 + temp.getData().getBerat());
  209.  
  210.         for (TreeNode<Bismillah> child : temp.getChildren()) {
  211.             cetak(child.getData().getNama(), acc + 1);
  212.         }
  213.  
  214.     }
  215.  
  216.     public void depth(String kunciMasuk, String laciNya) {
  217.         depth(kunciMasuk, laciNya,
  218.                 tempat.find(new TreeNode<Bismillah>(new Laci(laciNya))), -1);
  219.     }
  220.  
  221.     private void depth(String key, String laci, TreeNode<Bismillah> x, int acc) {
  222.         if (tempat.exists(new Kunci(key, 0))) {
  223.             if (x.data.toString().equals(key.toString())) {
  224.                 System.out.println(x.data.toString() + " berada di level "
  225.                         + acc + " " + laci);
  226.  
  227.             } else {
  228.                 for (TreeNode<Bismillah> child : x.getChildren()) {
  229.                     depth(key, laci, child, acc + 1);
  230.                 }
  231.  
  232.             }
  233.         } else {
  234.             System.out.println("tidak ditemukan!");
  235.  
  236.         }
  237.     }
  238.  
  239.     // THIS PARTS TOO
  240.    
  241.     public void cari(TreeNode<Bismillah> temp) {
  242.         String parent = "";
  243.         String returnString = "";
  244.  
  245.         TreeNode<Bismillah> temp2 = tempat.find(temp);
  246.         System.out.println("k");
  247.         if (temp2.getParent() == null) {
  248.             parent = "";
  249.         } else {
  250.             parent = "> " + temp2.getParent().toString();
  251.             returnString += parent + "\n" + "  ";
  252.             cari(temp2.getParent());
  253.         }
  254.         System.out.print(returnString);
  255.     }
  256.  
  257. }
  258.  
  259. class Tree<E extends Comparable<E>> {
  260.  
  261.     private TreeNode<E> root;
  262.  
  263.     public Tree(TreeNode<E> root) {
  264.         this.root = root;
  265.     }
  266.  
  267.     public Tree() {
  268.         this.root = null;
  269.     }
  270.  
  271.     public boolean isEmpty() {
  272.         return root == null;
  273.     }
  274.  
  275.     public TreeNode<E> getRoot() {
  276.         return root;
  277.     }
  278.  
  279.     public void setRoot(TreeNode<E> root) {
  280.         this.root = root;
  281.     }
  282.  
  283.     public boolean exists(E key) {
  284.         return find(root, key);
  285.     }
  286.  
  287.     public int getNumberOfNodes() {
  288.         return getNumberOfDescendants(root) + 1;
  289.     }
  290.  
  291.     public int getNumberOfDescendants(TreeNode<E> node) {
  292.         int n = node.getChildren().size();
  293.         for (TreeNode<E> child : node.getChildren())
  294.             n += getNumberOfDescendants((TreeNode<E>) child);
  295.         return n;
  296.     }
  297.  
  298.     public TreeNode<E> find(TreeNode<E> nodeToFind) {
  299.  
  300.         TreeNode<E> returnNode = null;
  301.  
  302.         if (root != null) {
  303.             returnNode = findNode(this.root, nodeToFind.data);
  304.         }
  305.  
  306.         return returnNode;
  307.     }
  308.  
  309.     private boolean find(TreeNode<E> node, E keyNode) {
  310.         boolean found = false;
  311.         if (node.getData().compareTo(keyNode) == 0) {
  312.             return true;
  313.         } else {
  314.             for (TreeNode<E> child : node.getChildren())
  315.                 if (find((TreeNode<E>) child, keyNode))
  316.                     found = true;
  317.         }
  318.         return found;
  319.     }
  320.  
  321.     private TreeNode<E> findNode(TreeNode<E> node, E keyNode) {
  322.         if (node == null)
  323.             return null;
  324.         if (node.getData().compareTo(keyNode) == 0)
  325.             return node;
  326.         else {
  327.             TreeNode<E> cnode = null;
  328.             for (TreeNode<E> child : node.getChildren())
  329.                 if ((cnode = findNode(child, keyNode)) != null)
  330.                     return cnode;
  331.         }
  332.         return null;
  333.     }
  334.  
  335.     public ArrayList<TreeNode<E>> getPreOrderTraversal() {
  336.         ArrayList<TreeNode<E>> preOrder = new ArrayList<TreeNode<E>>();
  337.         buildPreOrder(root, preOrder);
  338.         return preOrder;
  339.     }
  340.  
  341.     // isEmpty
  342.  
  343.     public void buildPreOrder(TreeNode<E> node, ArrayList<TreeNode<E>> preOrder) {
  344.         System.out.println(node.toString());
  345.         preOrder.add(node);
  346.         for (TreeNode<E> child : node.getChildren())
  347.             buildPreOrder((TreeNode<E>) child, preOrder);
  348.     }
  349.  
  350.     public void remove(E data) {
  351.         TreeNode<E> temp = find(new TreeNode<E>(data));
  352.  
  353.         while (temp.getData() != null) {
  354.             if (temp == null) {
  355.             } else {
  356.                 TreeNode<E> ortu = temp.getParent();
  357.                 ortu.removeChildren();
  358.             }
  359.         }
  360.     }
  361.  
  362.     public ArrayList<TreeNode<E>> getLongestPathFromRootToAnyLeaf() {
  363.         ArrayList<TreeNode<E>> longestPath = null;
  364.         int max = 0;
  365.         for (ArrayList<TreeNode<E>> path : getPathsFromRootToAnyLeaf()) {
  366.             if (path.size() > max) {
  367.                 max = path.size();
  368.                 longestPath = path;
  369.             }
  370.         }
  371.         return longestPath;
  372.     }
  373.  
  374.     public int getMaxDepth() {
  375.         return getLongestPathFromRootToAnyLeaf().size();
  376.     }
  377.  
  378.     public ArrayList<ArrayList<TreeNode<E>>> getPathsFromRootToAnyLeaf() {
  379.         ArrayList<ArrayList<TreeNode<E>>> paths = new ArrayList<ArrayList<TreeNode<E>>>();
  380.         ArrayList<TreeNode<E>> currentPath = new ArrayList<TreeNode<E>>();
  381.         getPath(root, currentPath, paths);
  382.  
  383.         return paths;
  384.     }
  385.  
  386.     private void getPath(TreeNode<E> node, ArrayList<TreeNode<E>> currentPath,
  387.             ArrayList<ArrayList<TreeNode<E>>> paths) {
  388.         if (currentPath == null)
  389.             return;
  390.  
  391.         currentPath.add(node);
  392.  
  393.         if (node.getChildren().size() == 0) {
  394.             // This is a leaf
  395.             paths.add(clone(currentPath));
  396.         }
  397.         for (TreeNode<E> child : node.getChildren())
  398.             getPath(child, currentPath, paths);
  399.  
  400.         int index = currentPath.indexOf(node);
  401.         for (int i = index; i < currentPath.size(); i++)
  402.             currentPath.remove(index);
  403.     }
  404.  
  405.     private ArrayList<TreeNode<E>> clone(ArrayList<TreeNode<E>> list) {
  406.         ArrayList<TreeNode<E>> newList = new ArrayList<TreeNode<E>>();
  407.         for (TreeNode<E> node : list)
  408.             newList.add(new TreeNode<E>(node));
  409.  
  410.         return newList;
  411.     }
  412.  
  413.     // MakeEmpty
  414.  
  415.     // Find Node
  416.  
  417.     // Print Pre-Order
  418. }
  419.  
  420. class Laci implements Bismillah {
  421.     String nama;
  422.     int berat;
  423.  
  424.     public Laci(String nama) {
  425.         this.nama = nama;
  426.         berat = 1;
  427.     }
  428.  
  429.     public Laci(String nama, int berat) {
  430.         this.nama = nama;
  431.         this.berat = berat;
  432.     }
  433.  
  434.     public String getNama() {
  435.         return nama;
  436.     }
  437.  
  438.     public int getBerat() {
  439.         return berat;
  440.     }
  441.  
  442. //  public int compareTo(Bismillah other) {
  443. //      if (!this.getNama().equalsIgnoreCase(other.getNama())) {
  444. //          return this.getNama().compareTo(other.getNama());
  445. //      } else {
  446. //          return this.getBerat() - other.getBerat();
  447. //      }
  448. //  }
  449.  
  450.    
  451.    
  452.     public int compareTo (Bismillah other) {
  453.         return this.getNama().compareTo(other.getNama());
  454.     }
  455. }
  456.  
  457. class Kunci implements Bismillah {
  458.  
  459.     String nama;
  460.     int berat;
  461.  
  462.     public Kunci(String nama, int berat) {
  463.         this.nama = nama;
  464.         this.berat = berat;
  465.     }
  466.  
  467.     public String getNama() {
  468.         return nama;
  469.     }
  470.  
  471.     public void setNama(String nama) {
  472.         this.nama = nama;
  473.     }
  474.  
  475.     public int getBerat() {
  476.         return berat;
  477.     }
  478.  
  479.     public void setBerat(int berat) {
  480.         this.berat = berat;
  481.     }
  482.  
  483. //  public int compareTo(Bismillah other) {
  484. //      if (!this.getNama().equalsIgnoreCase(other.getNama())) {
  485. //          return this.getNama().compareTo(other.getNama());
  486. //      } else {
  487. //          return this.getBerat() - other.getBerat();
  488. //      }
  489. //  }
  490.  
  491.     public int compareTo (Bismillah other) {
  492.         return this.getNama().compareTo(other.getNama());
  493.     }
  494. }
  495.  
  496. public interface Bismillah extends Comparable<Bismillah> {
  497.     public String getNama();
  498.     public int getBerat();
  499.     public int compareTo(Bismillah other);
  500. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement