Professional Documents
Culture Documents
Kegiatan Praktikum Tree Dan Graph
Kegiatan Praktikum Tree Dan Graph
1. Binary search tree seperti di bawah lalu lakukan traversal secara in-order, pre-order, dan
post-order.
Source code :
tree.insert(27);
tree.insert(14);
tree.insert(35);
tree.insert(10);
tree.insert(19);
tree.insert(31);
tree.insert(42);
tree.inorder();
tree.preorder();
tree.postorder();
2. Binary search tree seperti di bawah lalu lakukan traversal secara in-order, pre-order, dan
post-order.
Source code :
tree.insert(8);
tree.insert(3);
tree.insert(10);
tree.insert(1);
tree.insert(6);
tree.insert(7);
tree.insert(4);
tree.insert(14);
tree.insert(13);
tree.inorder();
tree.preorder();
tree.postorder();
3. Buatlah undirected graph berikut dan telusuri secara DFS dan BFS :
Source code :
theGraph.addVertex('1');
theGraph.addVertex('0');
theGraph.addVertex('2');
theGraph.addVertex('3');
theGraph.addVertex('4');
theGraph.addEdge(0, 1);
theGraph.addEdge(2, 1);
theGraph.addEdge(1, 3);
theGraph.addEdge(3, 4);
theGraph.addEdge(0, 1);
theGraph.addEdge(1, 2);
theGraph.addEdge(3, 1);
theGraph.addEdge(4, 3);
System.out.println("BFS : ");
theGraph.BFS();
System.out.println("\nDFS : ");
theGraph.DFS();
4. Buatlah undirected graph berikut dan telusuri secara BFS dan DFS :
Source code :
public static void main(String[] args) {
theGraph.addVertex('A');//0
theGraph.addVertex('B');//1
theGraph.addVertex('C');//2
theGraph.addVertex('D');//3
theGraph.addVertex('E');//4
theGraph.addVertex('F');//5
theGraph.addEdge(0, 1);
theGraph.addEdge(0, 2);
theGraph.addEdge(1, 2);
theGraph.addEdge(1, 3);
theGraph.addEdge(1, 5);
theGraph.addEdge(1, 4);
theGraph.addEdge(1, 0);
theGraph.addEdge(2, 0);
theGraph.addEdge(2, 1);
theGraph.addEdge(3, 1);
theGraph.addEdge(5, 1);
theGraph.addEdge(4, 1);
theGraph.BFS();
System.out.println();
theGraph.BFS();
System.out.println(); }