You are on page 1of 5

1.

Class Node
package com.yazidnuryaqin.modul9;
/*
Code was written by Yazid Nur Yaqin
*/
public class Node {
Node right, left;
int data;

public Node (int data) { this.data =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);
}

Private Node remove (int input, Node temp) {


If (temp == null)
return null;
else {
if (this.checking(input) == true) {
if(input < temp.data) {
temp.left = remove(input, temp.left);
} else {
if (input > temp.data) {
temp.right = remove(input, temp.right);
} else {
If (temp.left != null && temp.right != null) {
temp.data = temp.right.data;
temp.findmin();
temp.right = remove (temp.data, temp.right);
} else {
temp = temp.left != null ?
temp.left : temp.right;
System.out.println(“Data “ + input “ berhasil dihapus ! “);
}
}
else {
return null;
}
return temp;
}

8. Method removeAll
//removeAll
void removeAll(){
If (isEmpty())
System.out.println(“Tree masih kosong!”);
else ;
root = null;
System.out.println (Data berhasil dihapus semua”);
}
}

LOVE UUUU AYANGGGG AKUUUUUUUU !!!!<3

CHIYAAAA

You might also like