APPENDIX CLASS NODE AS TREE OBJECT public class Node public int data; public Node left; public Node right; public Node parent; public Node(int i) data=i; PROCEDURE BUILDING TREE public class Tree public Node root; public void cekroot() System.out.println("nilai root : "+root.data); public boolean isempty() return root==null; public void insert(int i) Node temp = new Node(i); Node y = null; Node x = root; if (isempty()) System.out.println("nilai "+i+" menjadi root"); root = temp; else while(x!= null) A
y = x; if(i < x.data) x= x.left; else x = x.right; if( i < y.data) if(y.left == null) y.left=temp; System.out.println("nilai "+i+" masuk sebelah kiri "+y.data); else y.right = temp; System.out.println("nilai "+i+" masuk sebelah kanan "+y.data); else if(y.right == null) y.right = temp; System.out.println("nilai "+i+" masuk sebelah kanan "+y.data); else y.left=temp; System.out.println("nilai "+i+" masuk sebelah kiri "+y.data); public void min() Node temp=root; while(temp.left!=null) temp=temp.left; System.out.println("nilai min :" +temp.data); public void max() B
Node temp=root; while(temp.right!=null) temp=temp.right; System.out.println("nilai max:" +temp.data); public void cetak(node i) Node temp=i; if(temp!=null) cetak(temp.parent); System.out.println(temp.data); cetak(temp.left); cetak(temp.right); public void printnode() System.out.println("Cetak Tree"); cetak(root); DisplayThread dt = new DisplayThread(); public void maxheap(node root, Node prev, PaintTree painttree) if(root==null) return; try maxheap(root.left,root,painttree); maxheap(root.right,root,painttree); if( prev!= null && root.data < prev.data) swapper(root,prev); prev = root; maxheap(prev, prev,painttree); catch (Exception e) e.printstacktrace(); C
public void swapper(node node1, Node node2) int temp= node1.data; node1.data = node2.data; node2.data = temp; public void swapmax(painttree painttree) maxheap(root, root,painttree); public void printmax() System.out.println("\nHasil swap:"); cetak(root); public Node[] dualheap(node root) Node data = root; Node result[] = new Node[2]; Tree tree1 = new Tree(); tree1.root = root.left; Tree tree2 = new Tree(); tree2.root = root.right; result[0] = tree1.root; result[1] = tree2.root; cetakheap(root); System.out.println("\n===Tree Split Pertama==="); tree1.cetakheap(tree1.root); System.out.println("\n===Tree Split kedua==="); tree2.cetakheap(tree2.root); return result; public void cetakdual() dualheap(root); public void cetakheap(node root) if(root == null) return; if(this.root.data == root.data ) System.out.println("nilai "+root.data+" Sebagai root"); if(root.right!= null) D
System.out.println("nilai "+root.right.data+" masuk sebelah kanan "+root.data); cetakheap(root.right); if(root.left!= null) System.out.println("nilai "+root.left.data+" masuk sebelah kiri "+root.data); cetakheap(root.left); public void deletetree() root = null; System.out.println("Tree deleted"); CLASS TREE DISPLAY import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.border.border; public class TreeDisplay extends JPanel private Tree tree; private JTextField jtfkey = new JTextField(5); private PaintTree painttree; private PaintTree painttree1; private PaintTree painttree2; private JButton jbtinsert = new JButton("Insert"); private JButton jbtsorting = new JButton("Sorting"); private JButton jbtclear = new JButton("Clear"); private Timer timer; public TreeDisplay(Tree tree) this.tree = tree; painttree = new PaintTree(tree); SwingUtilities.invokeLater(new Runnable() public void run() setui(); ); E
public void setui() this.setlayout(new BorderLayout()); add(painttree, BorderLayout.CENTER); JPanel panel = new JPanel(); panel.add(new JLabel("Enter a key: ")); panel.add(jtfkey); panel.add(jbtinsert); panel.add(jbtclear); panel.add(jbtsorting); panel.add(jbtsorting); add(panel, BorderLayout.SOUTH); jbtclear.addactionlistener(new ActionListener() public void actionperformed(actionevent e) tree.deletetree(); panel.repaint(); painttree.repaint(); ); jbtsorting.addactionlistener(new ActionListener() @Override public void actionperformed(actionevent e) painttree.swapmax(painttree); tree.cetakdual(); //painttree1.dualheap(1); //dualheapdisp(painttree1); //painttree2.dualheap(2); //dualheapdisp2(painttree2); //painttree2.repaint(); painttree.timer.start(); painttree.revalidate(); painttree.repaint(); ); jbtinsert.addactionlistener(new ActionListener() public void actionperformed(actionevent e) int key = Integer.parseInt(jtfKey.getText()); tree.insert(key); F
); painttree.repaint(); CLASS PAINT TREE import java.awt.graphics; import java.awt.event.actionevent; import java.awt.event.actionlistener; import java.sql.time; import javax.swing.timer; import javax.swing.jpanel; import java.awt.color; import java.awt.graphics2d; import java.awt.font; class PaintTree extends JPanel private int radius = 20; private int vgap = 50; private Tree tree; public Timer timer; public Timer timer2; public PaintTree(Tree tree2) this.tree = tree2; protected void paintcomponent(graphics g) super.paintcomponent(g); Font font = new Font("Verdana", Font.BOLD, 12); g.setfont(font); 4); if (tree.root!= null) displaytree(g, tree.root, getwidth() / 2, 30, getwidth() / public void dualheap(int i) Node dual[] = tree.dualheap(tree.root); if(i == 1) tree.root = dual[0]; G
else if(i == 2) tree.root = dual[1]; public void cetakdual() dualheap(tree.root); private void displaytree(graphics g, Node root, int x, int y, int hgap) g.drawoval(x - radius, y - radius, 2 * radius, 2 * radius); g.drawstring(root.data + "", x - 6, y + 4); g.setcolor(color.black); if(root.right!=null) g.drawstring(root.data + "", x - 6, y + 4); g.setcolor(color.red); else g.setcolor(color.blue); if(root.left!=null) g.drawstring(root.data + "", x - 6, y + 4); g.setcolor(color.magenta); else g.setcolor(color.green); 2); if (root.left!= null) connectleftchild(g, x - hgap, y + vgap, x, y); displaytree(g, root.left, x - hgap, y + vgap, hgap / if (root.right!= null) connectrightchild(g, x + hgap, y + vgap, x, y); displaytree(g, root.right, x + hgap, y + vgap, hgap / 2); public Tree gettree() return tree; H
public void settree(tree tree) this.tree = tree; private void connectleftchild(graphics g, int x1, int y1, int x2, int y2) double d = Math.sqrt(vGap * vgap + (x2 - x1) * (x2 - x1)); int x11 = (int)(x1 + radius * (x2 - x1) / d); int y11 = (int)(y1 - radius * vgap / d); int x21 = (int)(x2 - radius * (x2 - x1) / d); int y21 = (int)(y2 + radius * vgap / d); g.drawline(x11, y11, x21, y21); private void connectrightchild(graphics g, int x1, int y1, int x2, int y2) double d = Math.sqrt(vGap * vgap + (x2 - x1) * (x2 - x1)); int x11 = (int)(x1 - radius * (x1 - x2) / d); int y11 = (int)(y1 - radius * vgap / d); int x21 = (int)(x2 + radius * (x1 - x2) / d); int y21 = (int)(y2 + radius * vgap / d); g.drawline(x11, y11, x21, y21); public void maxheap(node root, Node prev, PaintTree painttree) if(root==null) return; timer = new Timer(2000, new ActionListener() @Override public void actionperformed(actionevent e) maxheap(root.left,root,painttree); maxheap(root.right,root,painttree); prev.data) if( prev!= null && root.data < swapper(root,prev); //prev = root; Node prev2 = root; maxheap(prev2, prev2,painttree); repaint(); ); timer.start(); I
public void swapper(node node1, Node node2) int temp= node1.data; node1.data = node2.data; node2.data = temp; public void swapmax(painttree painttree) maxheap(tree.root, tree.root,painttree); CLASS DISPLAY THREAD public class DisplayThread implements Runnable PaintTree painttree; public PaintTree getpainttree() return painttree; public void setpainttree(painttree painttree) this.painttree = painttree; @Override public void run() painttree.repaint(); CLASS MAIN GUI import java.awt.borderlayout; import java.awt.dimension; import java.awt.frame; import java.awt.graphics; import java.awt.graphics2d; import javax.swing.jtextfield; import javax.swing.swingutilities; import javax.swing.japplet; J
import javax.swing.jbutton; import javax.swing.jframe; import javax.swing.jlabel; import javax.swing.jpanel; public class MainGui extends JPanel public MainGui() public static void main(string[] args) JFrame frame; frame = new JFrame("Simulation"); frame.setdefaultcloseoperation(jframe.exit_on_close); JButton jbtinsert = new JButton("Insert"); JButton jbtdelete = new JButton("Delete"); JTextField jtfkey = new JTextField(5); JPanel panel = new JPanel(); panel.add(new JLabel("Enter a key: ")); panel.add(jtfkey); panel.add(jbtinsert); panel.add(jbtdelete); Tree a=new Tree(); /*a.insert(5); a.insert(4); a.insert(7); a.insert(6); a.insert(9); a.insert(10); a.insert(8);*/ SwingUtilities.invokeLater(new Runnable() public void run() TreeDisplay treedisplay = new TreeDisplay(a); frame.getcontentpane().add(treedisplay); ); frame.pack(); frame.setsize(new Dimension(1360, 700)); frame.setvisible(true); K
L
M