Posts

Showing posts from June, 2021

PENUGASAN 9 -- GRAPH

Image
 Implementasi Graph Java 1. Adjacency Matrix The situation where our nodes/vertices are objects (like they most likely would be) is highly complicated and requires a lot of maintenance methods that make adjacency matrices more trouble than they're worth most of the time, so we'll only provide the implementation of the "simple" case. Source public class Graph { private int numOfNodes ; private boolean directed ; private boolean weighted ; private float [][] matrix ; /* This will allow us to safely add weighted graphs in our class since we will be able to check whether an edge exists without relying on specific special values (like 0) */ private boolean [][] isSetMatrix ; public Graph ( int numOfNodes , boolean directed , boolean weighted ) { this . directed = directed ; this . weighted = weighted ; this . numOfNodes = numOfNodes ; // Simply initializes ou...

PENUGASAN 8 -- BST

Image
 Implementasi Program BST (Binary Search Tree) A Binary search tree (referred to as BST hereafter) is a type of binary tree. It can also be defined as a node-based binary tree. BST is also referred to as ‘Ordered Binary Tree’. In BST, all the nodes in the left subtree have values that are less than the value of the root node. Similarly, all the nodes of the right subtree of the BST have values that are greater than the value of the root node. This ordering of nodes has to be true for respective subtrees as well. Method in BST 1. Insert  : Add an element to the BST by not violating the BST properties. 2. Delete : Remove a given node from the BST. The node can be the root node, non leaf, o leaf node 3. Search : Search the location of the given element in the BST. This operation checks if the tree contains the specified key. Source class BST_Class { class Node { int key ; Node left , right ; public Node ( int data ){ k...

PENUGASAN 7 -- REKURSI

Image
 Implementasi Program Tower Of Hanoi Tower of Hanoi adalah program tentang teka-teki matematika dimana terdapat tiga batang dan N piringan. Program ini bertujuan untuk memindahkan seluruh tumpukan ke batang lain. Terdapat beberapa peraturan dalam Tower of Hanoi: 1. Hanya satu piringan yang dapat dipindahkan pada satu waktu 2. Setiap gerakan terdiri dari step mengambil piringan dan meletakkannya di atas tumpukan lain. Piringan hanya dapat dipindahkan jika berada di tumpukan paling atas. 3. Tidak ada piringan yang diletakkan di atas piringan yang nilainya lebih kecil. Source // JAVA recursive function to // solve tower of hanoi puzzle import java.util.* ; import java.io.* ; import java.math.* ; /** * Write a description of class TowerOfHanoi here. * * @author Adelia Hasna Surya Putri * @version 06/06/2021 */ public class TowerOfHanoi { static void TowerOfHanoi ( int n , char from_rod , char to_rod , char aux_rod ) { if ( n =...