Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.util.ArrayList;
- import java.util.List;
- public class Main {
- public static void main(String[] args) throws IOException {
- BufferedReader reader = new BufferedReader(new InputStreamReader(
- System.in));
- String str = "";
- Order agent = new Order();
- while ((str = reader.readLine()) != null && str.length() != 0) {
- String[] input = str.split(" ");
- switch (input[0].toUpperCase()) {
- case ("ADD"):
- String from = input[1];
- String to = input[2];
- Laci inputan = new Laci(from);
- Laci satunya = new Laci(to);
- agent.add(inputan, to);
- break;
- case ("PUT"):
- String key = input[1];
- int weight = Integer.parseInt(input[2]);
- String laci = input[3];
- agent.put(key, weight, laci);
- break;
- case ("THROW"):
- String thing = input[1];
- agent.buang(thing);
- break;
- case ("MOVE"):
- break;
- case ("CETAK"):
- String cetak = input[1];
- agent.cetak(cetak);
- break;
- case ("DEPTH"):
- String kunciMasuk = input[1];
- String laciNya = input[2];
- agent.depth(kunciMasuk, laciNya);
- break;
- case ("FIND"):
- String tobeFind = input[1];
- TreeNode<Bismillah> temp;
- if (tobeFind.substring(0, 5).equalsIgnoreCase("kunci")) {
- temp = new TreeNode<Bismillah>(new Kunci(tobeFind, 0));
- } else {
- temp = (new TreeNode<Bismillah>(new Laci(tobeFind)));
- }
- agent.cari(temp);
- break;
- }
- }
- }
- }
- class TreeNode<E extends Comparable<E>> {
- E data;
- List<TreeNode<E>> children;
- TreeNode<E> parent;
- public TreeNode(E data) {
- this.data = data;
- this.children = new ArrayList<TreeNode<E>>();
- }
- public TreeNode(TreeNode<E> node) {
- this.data = (E) node.getData();
- children = new ArrayList<TreeNode<E>>();
- }
- public void addChild(TreeNode<E> child) {
- child.parent = this;
- children.add(child);
- }
- public void addChildAt(int index, TreeNode<E> child) {
- child.parent = this;
- this.children.add(index, child);
- }
- public void setChildren(List<TreeNode<E>> children) {
- for (TreeNode<E> child : children)
- child.parent = this;
- this.children = children;
- }
- public void removeChildren() {
- this.children = new ArrayList<TreeNode<E>>();
- }
- public TreeNode<E> removeChildAt(int index) {
- return children.remove(index);
- }
- public void removeThisIfItsAChild(TreeNode<E> childToBeDeleted) {
- List<TreeNode<E>> list = getChildren();
- list.remove(childToBeDeleted);
- }
- public E getData() {
- return this.data;
- }
- public void setData(E data) {
- this.data = data;
- }
- public TreeNode<E> getParent() {
- return this.parent;
- }
- public void setParent(TreeNode<E> parent) {
- this.parent = parent;
- }
- public List<TreeNode<E>> getChildren() {
- return this.children;
- }
- public TreeNode<E> getChildAt(int index) {
- return children.get(index);
- }
- @Override
- public boolean equals(Object obj) {
- if (null == obj)
- return false;
- if (obj instanceof TreeNode) {
- if (((TreeNode<?>) obj).getData().equals(this.data))
- return true;
- }
- return false;
- }
- @Override
- public String toString() {
- return this.data.toString();
- }
- }
- class Order {
- Tree<Bismillah> tempat = new Tree<Bismillah>(new TreeNode<Bismillah>(
- new Laci("laci1")));
- public void add(Laci a, String b) {
- Laci baru = new Laci(b);
- TreeNode<Bismillah> baru2 = new TreeNode<Bismillah>(baru);
- TreeNode<Bismillah> inputan = new TreeNode<Bismillah>(a);
- TreeNode<Bismillah> barulagi = tempat.find(baru2);
- barulagi.addChild(inputan);
- }
- public void put(String key, int weight, String laci) {
- Laci baru = new Laci(laci);
- Kunci kunci = new Kunci(key, weight);
- TreeNode<Bismillah> baru2 = new TreeNode<Bismillah>(baru);
- TreeNode<Bismillah> kuncitemp = new TreeNode<Bismillah>(kunci);
- TreeNode<Bismillah> barulagi = tempat.find(baru2);
- barulagi.addChild(kuncitemp);
- System.out.println(key + " masuk di " + laci);
- }
- public void buang(String x) {
- TreeNode<Bismillah> sementara;
- if (x.substring(0, 5).equalsIgnoreCase("kunci")) {
- sementara = tempat.find(new TreeNode<Bismillah>(new Kunci(x, 0)));
- } else {
- sementara = tempat.find(new TreeNode<Bismillah>(new Laci(x)));
- }
- sementara.parent.removeChildren();
- }
- public void moveKunci() {
- }
- public void cetak(String x) {
- cetak(x, 0);
- }
- // THIS PARTS
- private void cetak(String x, int acc) {
- TreeNode<Bismillah> temp;
- if (x.substring(0, 5).equalsIgnoreCase("kunci")) {
- temp = tempat.find(new TreeNode<Bismillah>(new Kunci(x, 5)));
- } else {
- temp = tempat.find(new TreeNode<Bismillah>(new Laci(x)));
- }
- for (int i = 0; i < acc; i++) {
- System.out.print(" ");
- }
- System.out.println("> " + temp.getData().getNama() + " "
- + temp.getData().getBerat());
- for (TreeNode<Bismillah> child : temp.getChildren()) {
- cetak(child.getData().getNama(), acc + 1);
- }
- }
- public void depth(String kunciMasuk, String laciNya) {
- depth(kunciMasuk, laciNya,
- tempat.find(new TreeNode<Bismillah>(new Laci(laciNya))), -1);
- }
- private void depth(String key, String laci, TreeNode<Bismillah> x, int acc) {
- if (tempat.exists(new Kunci(key, 0))) {
- if (x.data.toString().equals(key.toString())) {
- System.out.println(x.data.toString() + " berada di level "
- + acc + " " + laci);
- } else {
- for (TreeNode<Bismillah> child : x.getChildren()) {
- depth(key, laci, child, acc + 1);
- }
- }
- } else {
- System.out.println("tidak ditemukan!");
- }
- }
- // THIS PARTS TOO
- public void cari(TreeNode<Bismillah> temp) {
- String parent = "";
- String returnString = "";
- TreeNode<Bismillah> temp2 = tempat.find(temp);
- System.out.println("k");
- if (temp2.getParent() == null) {
- parent = "";
- } else {
- parent = "> " + temp2.getParent().toString();
- returnString += parent + "\n" + " ";
- cari(temp2.getParent());
- }
- System.out.print(returnString);
- }
- }
- class Tree<E extends Comparable<E>> {
- private TreeNode<E> root;
- public Tree(TreeNode<E> root) {
- this.root = root;
- }
- public Tree() {
- this.root = null;
- }
- public boolean isEmpty() {
- return root == null;
- }
- public TreeNode<E> getRoot() {
- return root;
- }
- public void setRoot(TreeNode<E> root) {
- this.root = root;
- }
- public boolean exists(E key) {
- return find(root, key);
- }
- public int getNumberOfNodes() {
- return getNumberOfDescendants(root) + 1;
- }
- public int getNumberOfDescendants(TreeNode<E> node) {
- int n = node.getChildren().size();
- for (TreeNode<E> child : node.getChildren())
- n += getNumberOfDescendants((TreeNode<E>) child);
- return n;
- }
- public TreeNode<E> find(TreeNode<E> nodeToFind) {
- TreeNode<E> returnNode = null;
- if (root != null) {
- returnNode = findNode(this.root, nodeToFind.data);
- }
- return returnNode;
- }
- private boolean find(TreeNode<E> node, E keyNode) {
- boolean found = false;
- if (node.getData().compareTo(keyNode) == 0) {
- return true;
- } else {
- for (TreeNode<E> child : node.getChildren())
- if (find((TreeNode<E>) child, keyNode))
- found = true;
- }
- return found;
- }
- private TreeNode<E> findNode(TreeNode<E> node, E keyNode) {
- if (node == null)
- return null;
- if (node.getData().compareTo(keyNode) == 0)
- return node;
- else {
- TreeNode<E> cnode = null;
- for (TreeNode<E> child : node.getChildren())
- if ((cnode = findNode(child, keyNode)) != null)
- return cnode;
- }
- return null;
- }
- public ArrayList<TreeNode<E>> getPreOrderTraversal() {
- ArrayList<TreeNode<E>> preOrder = new ArrayList<TreeNode<E>>();
- buildPreOrder(root, preOrder);
- return preOrder;
- }
- // isEmpty
- public void buildPreOrder(TreeNode<E> node, ArrayList<TreeNode<E>> preOrder) {
- System.out.println(node.toString());
- preOrder.add(node);
- for (TreeNode<E> child : node.getChildren())
- buildPreOrder((TreeNode<E>) child, preOrder);
- }
- public void remove(E data) {
- TreeNode<E> temp = find(new TreeNode<E>(data));
- while (temp.getData() != null) {
- if (temp == null) {
- } else {
- TreeNode<E> ortu = temp.getParent();
- ortu.removeChildren();
- }
- }
- }
- public ArrayList<TreeNode<E>> getLongestPathFromRootToAnyLeaf() {
- ArrayList<TreeNode<E>> longestPath = null;
- int max = 0;
- for (ArrayList<TreeNode<E>> path : getPathsFromRootToAnyLeaf()) {
- if (path.size() > max) {
- max = path.size();
- longestPath = path;
- }
- }
- return longestPath;
- }
- public int getMaxDepth() {
- return getLongestPathFromRootToAnyLeaf().size();
- }
- public ArrayList<ArrayList<TreeNode<E>>> getPathsFromRootToAnyLeaf() {
- ArrayList<ArrayList<TreeNode<E>>> paths = new ArrayList<ArrayList<TreeNode<E>>>();
- ArrayList<TreeNode<E>> currentPath = new ArrayList<TreeNode<E>>();
- getPath(root, currentPath, paths);
- return paths;
- }
- private void getPath(TreeNode<E> node, ArrayList<TreeNode<E>> currentPath,
- ArrayList<ArrayList<TreeNode<E>>> paths) {
- if (currentPath == null)
- return;
- currentPath.add(node);
- if (node.getChildren().size() == 0) {
- // This is a leaf
- paths.add(clone(currentPath));
- }
- for (TreeNode<E> child : node.getChildren())
- getPath(child, currentPath, paths);
- int index = currentPath.indexOf(node);
- for (int i = index; i < currentPath.size(); i++)
- currentPath.remove(index);
- }
- private ArrayList<TreeNode<E>> clone(ArrayList<TreeNode<E>> list) {
- ArrayList<TreeNode<E>> newList = new ArrayList<TreeNode<E>>();
- for (TreeNode<E> node : list)
- newList.add(new TreeNode<E>(node));
- return newList;
- }
- // MakeEmpty
- // Find Node
- // Print Pre-Order
- }
- class Laci implements Bismillah {
- String nama;
- int berat;
- public Laci(String nama) {
- this.nama = nama;
- berat = 1;
- }
- public Laci(String nama, int berat) {
- this.nama = nama;
- this.berat = berat;
- }
- public String getNama() {
- return nama;
- }
- public int getBerat() {
- return berat;
- }
- // public int compareTo(Bismillah other) {
- // if (!this.getNama().equalsIgnoreCase(other.getNama())) {
- // return this.getNama().compareTo(other.getNama());
- // } else {
- // return this.getBerat() - other.getBerat();
- // }
- // }
- public int compareTo (Bismillah other) {
- return this.getNama().compareTo(other.getNama());
- }
- }
- class Kunci implements Bismillah {
- String nama;
- int berat;
- public Kunci(String nama, int berat) {
- this.nama = nama;
- this.berat = berat;
- }
- public String getNama() {
- return nama;
- }
- public void setNama(String nama) {
- this.nama = nama;
- }
- public int getBerat() {
- return berat;
- }
- public void setBerat(int berat) {
- this.berat = berat;
- }
- // public int compareTo(Bismillah other) {
- // if (!this.getNama().equalsIgnoreCase(other.getNama())) {
- // return this.getNama().compareTo(other.getNama());
- // } else {
- // return this.getBerat() - other.getBerat();
- // }
- // }
- public int compareTo (Bismillah other) {
- return this.getNama().compareTo(other.getNama());
- }
- }
- public interface Bismillah extends Comparable<Bismillah> {
- public String getNama();
- public int getBerat();
- public int compareTo(Bismillah other);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement