Binary Tree Search
15.22 |
Diposting oleh
Unknown |
Edit Entri
Metode Binary Search merupakan metode yang membandingkan batas
atas dan batas bawah dalam suatu kumpulan array, sampai di temukannya
angka yang di tuju. berikut saya cantumkan source codenya dan flowchartnya :
import java.util.Scanner; class BinarySearch { public static void main(String args[]) { int c, first, last, middle, n, search, array[]; Scanner in = new Scanner(System.in); System.out.println("Enter number of elements"); n = in.nextInt(); array = new int[n]; System.out.println("Enter " + n + " integers"); for (c = 0; c < n; c++) array[c] = in.nextInt(); System.out.println("Enter value to find"); search = in.nextInt(); first = 0; last = n - 1; middle = (first + last)/2; while( first <= last ) { if ( array[middle] < search ) first = middle + 1; else if ( array[middle] == search ) { System.out.println(search + " found at location " + (middle + 1) + "."); break; } else last = middle - 1; middle = (first + last)/2; } if ( first > last ) System.out.println(search + " is not present in the list.\n"); } }
Order LinkedList
16.40 |
Diposting oleh
Unknown |
Edit Entri
Linked list tidak lain adalah suatu struktur data yg merupakan suatu
rangkaian atau daftar record berjenis sama. Kemudian dihubungkan melalui
bantuan pointer. Pengalokasian daftar dapat dilakukan secara dinamis
sehingga isi dari daftar dapat dimanipulasi. Untuk memahami
Berikut saya cantumkan sourcecode dari Order LinkedList
package ordered.linkedlist;
class Node {
int data;
Node prev;
Node next;
}
public class OrderedLinkedList {
static Node head, tail;
static void insert(int new_data) {
Node new_node = new Node();
new_node.data = new_data;
if (head == null && tail == null) { // Jika LinkList Kosong
head = new_node;
tail = new_node;
} else if (new_node.data <= head.data) { //new_data < head
head.prev = new_node;
new_node.next = head;
head = new_node;
} else if (new_node.data >= tail.data) { //new_data > tail
new_node.prev = tail;
tail.next = new_node;
tail = new_node;
} else { //data di antara link list
Node position = head;
while (position.data < new_node.data) {
position = position.next;
}
position.prev.next = new_node;
new_node.prev = position.prev;
position.prev = new_node; //data sebelum position berganti jadi new_node
new_node.next = position;
}
}
static void delete(int data) {
if (head == null && tail == null) {
// ridak bisa dihapus, karena tidak ada data
} else if (head == tail && head.data == data) { //satu data saja
head = null;
tail = null;
} else if (head.data == data) { //data yg dihapus ==head
head.next.prev = null;
head = head.next;
} else if (tail.data == data) { //data yg dihapus == tail
tail.prev.next = null;
tail = tail.prev;
} else {
Node position = head;
while (position!= null && position.data != data) {
position = position.next;
}
if (position == null) {
System.out.println("Data Tidak Ada");
} else if (position != null) {
Node previous = position.prev;
Node next = position.next;
previous.next = next;
next.prev = previous;
}
}
}
static void traverse() { //sama dengan view() pada double_linklist
Node x = head;
while (x != null) {
System.out.print(x.data + " - ");
x = x.next;
}
System.out.println();
}
public static void main(String[] args) {
insert(7);
insert(16);
insert(4);
insert(6);
insert(8);
insert(15);
insert(13);
traverse(10);
}
}
Berikut saya cantumkan sourcecode dari Order LinkedList
package ordered.linkedlist;
class Node {
int data;
Node prev;
Node next;
}
public class OrderedLinkedList {
static Node head, tail;
static void insert(int new_data) {
Node new_node = new Node();
new_node.data = new_data;
if (head == null && tail == null) { // Jika LinkList Kosong
head = new_node;
tail = new_node;
} else if (new_node.data <= head.data) { //new_data < head
head.prev = new_node;
new_node.next = head;
head = new_node;
} else if (new_node.data >= tail.data) { //new_data > tail
new_node.prev = tail;
tail.next = new_node;
tail = new_node;
} else { //data di antara link list
Node position = head;
while (position.data < new_node.data) {
position = position.next;
}
position.prev.next = new_node;
new_node.prev = position.prev;
position.prev = new_node; //data sebelum position berganti jadi new_node
new_node.next = position;
}
}
static void delete(int data) {
if (head == null && tail == null) {
// ridak bisa dihapus, karena tidak ada data
} else if (head == tail && head.data == data) { //satu data saja
head = null;
tail = null;
} else if (head.data == data) { //data yg dihapus ==head
head.next.prev = null;
head = head.next;
} else if (tail.data == data) { //data yg dihapus == tail
tail.prev.next = null;
tail = tail.prev;
} else {
Node position = head;
while (position!= null && position.data != data) {
position = position.next;
}
if (position == null) {
System.out.println("Data Tidak Ada");
} else if (position != null) {
Node previous = position.prev;
Node next = position.next;
previous.next = next;
next.prev = previous;
}
}
}
static void traverse() { //sama dengan view() pada double_linklist
Node x = head;
while (x != null) {
System.out.print(x.data + " - ");
x = x.next;
}
System.out.println();
}
public static void main(String[] args) {
insert(7);
insert(16);
insert(4);
insert(6);
insert(8);
insert(15);
insert(13);
traverse(10);
}
}
Langganan:
Postingan (Atom)