Professional Documents
Culture Documents
Class Node
Class Node
Class Node
package com.yazidnuryaqin.modul9;
/*
Code was written by Yazid Nur Yaqin
*/
public class Node {
Node right, left;
int data;
2. Method isEmpty
package com.yazidnuryaqin.modul9;
/*
Code was written by Yazid Nur Yaqin
*/
public class BinarySearchTree {
Node root;
Boolean isEmpty() { return root == null; }
3. Method insert
void inser(Node input){
Node temp = root;
Node parent = null;
Boolean flag = false;
if (isEmpty()){
root = input;
} else {
while (temp != null) {
parent = temp;
if (input.data <temp.data) {
temp = temp.left;
flag = true;
} else if (input.data > temp.data){
temp = temp.right;
flag = true;
} else {
system.out.println(“Data tidak boleh ada duplikasi!”)
break;
}
If (flag) {
If (input.data > parent.data) {
parent.right =input;
} else if (input.data < parent.data) {
parent.left = input;
}else {
System.out.println(“Data tidak boleh ada duplikasi”);
}
4. Method find
//Method find
void find (int cari) {
Node temp = root;
boolean cek = false;
while (temp != null) {
if (cari < temp.data) {
temp = temp.left;
} else if (cari > temp.data) {
temp = temp.right;
}else if (cari == temp.data) {
cek = true;
break;
}
}
if (cek)
System.out.println(“Data “ + cari + “ ditemukan !”);
else {
System.out.println(“Data tidak ditemukan !”);
}
5. Method findMax
//Method findmax
Node findmax() {
Node temp = root;
If (isEmpty())
return null;
else {
while (temp.right != null) {
temp = temp.right;
}
return temp;
}
6. Method findMin
//Method findmin
Node findMin () {
Node temp = root;
if (isEmpty())
return null;
else {
while (temp.left != null)
temp = temp.left;
return temp;
}
7. Method remove
//Method remove yang dipanggil di main
void remove (int input) {
if (this.remove(input, root) == null {
System.out.println(“Data “ + input + “ tidak ada dalam tree !”);
} else {
root = remove(input, root);
}
8. Method removeAll
//removeAll
void removeAll(){
If (isEmpty())
System.out.println(“Tree masih kosong!”);
else ;
root = null;
System.out.println (Data berhasil dihapus semua”);
}
}
CHIYAAAA