0% found this document useful (0 votes)
67 views8 pages

Laporan Praktikum Binary Search Tree

The document describes a program for implementing and testing a binary search tree data structure. It includes the BinarySearchTree class with methods for adding nodes, finding nodes, and removing nodes from the tree. It also includes a Main class that adds sample data to a binary search tree, and demonstrates removing a node. The output shows the inorder, preorder, and postorder traversals of the tree before and after adding a node and removing a node.

Uploaded by

Rafiq Muzakki
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
67 views8 pages

Laporan Praktikum Binary Search Tree

The document describes a program for implementing and testing a binary search tree data structure. It includes the BinarySearchTree class with methods for adding nodes, finding nodes, and removing nodes from the tree. It also includes a Main class that adds sample data to a binary search tree, and demonstrates removing a node. The output shows the inorder, preorder, and postorder traversals of the tree before and after adding a node and removing a node.

Uploaded by

Rafiq Muzakki
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Algoritma dan Struktur Data

Resmi

Praktikum Search Binary Tree


-

Praditya Nafiis Muhammad

2 D3 IT B

2103181032

Yuliana Setyowati

11 Desember 2019
A. Listing Program
 BinarySearchTree.java
package binarysearchtree;
public class BinarySearchTree<T> {
private STNode<T> root;
private int treeSize;

public BinarySearchTree() {
root = null;
}

public STNode<T> getRoot() {


return root;
}

public void setRoot(STNode<T> root) {


this.root = root;
}

public int getTreeSize() {


return treeSize;
}

public void setTreeSize(int treeSize) {


this.treeSize = treeSize;
}

public boolean add(T item) {


STNode<T> t = root, parent = null, newNode;
int orderValue = 0;
while (t != null) {
parent = t;
orderValue = ((Comparable<T>) item).compareTo(t.nodeValue);
if (orderValue == 0) {
return false;
} else if (orderValue < 0) {
t = t.left;
} else {
t = t.right;
}
}
newNode = new STNode<T>(item, parent);
if (parent == null) {
root = newNode;
} else if (orderValue < 0) {
parent.left = newNode;
} else {
parent.right = newNode;
}
treeSize++;
return true;
}

public STNode<T> findNode(T item) {


STNode<T> t = root, parent = null, newNode;
int orderValue = 0;
while (t != null) {
orderValue = ((Comparable<T>) item).compareTo(
t.nodeValue);
if (orderValue == 0) {
return t;
} else if (orderValue < 0) {
t = t.left;
} else {
t = t.right;
}
}
return null;
}

public boolean find(T item) {


STNode<T> t = findNode(item);
T value = null;
if (t != null) {
value = t.nodeValue;
return true;
}
return false;
}

public T first() {
STNode<T> nextNode = root;

// if the set is empty, return null


if (nextNode == null) {
return null;
}

// first node is the furthest node left from root


while (nextNode.left != null) {
nextNode = nextNode.left;
}

return nextNode.nodeValue;
}

public T last() {
STNode<T> nextNode = root;

// if the set is empty, return null


if (nextNode == null) {
return null;
}

// first node is the furthest node left from root


while (nextNode.right != null) {
nextNode = nextNode.right;
}

return nextNode.nodeValue;
}

public void removeNode(STNode<T> dNode) {


if (dNode == null) {
return;
}
STNode<T> pNode, rNode;
dNode = findNode(dNode.nodeValue);
pNode = dNode.parent;
if (dNode.left == null || dNode.right == null) {
if (dNode.right == null) {
rNode = dNode.left;
} else {
rNode = dNode.right;
}

if (rNode != null)
{
rNode.parent = pNode;
}
if (pNode == null) {
root = rNode;
}
else if (((Comparable<T>) dNode.nodeValue).
compareTo(pNode.nodeValue) < 0) {
pNode.left = rNode;
} else {
pNode.right = rNode;
}
} else {
STNode<T> pOfRNode = dNode;
rNode = dNode.right;
pOfRNode = dNode;
while (rNode.left != null) {
pOfRNode = rNode;
rNode = rNode.left;
}

}
}
}

 Main.java
package binarysearchtree;
public class Main {

public static void main(String[] args) {


BinarySearchTree<Integer> bst = new BinarySearchTree<Integer>();

bst.add(48);
bst.add(29);
bst.add(60);
bst.add(10);
bst.add(32);
bst.add(84);
bst.add(75);
bst.add(100);

System.out.println(BinaryTree.inorderDisplay(bst.getRoot()));
System.out.println(BinaryTree.preorderDisplay(bst.getRoot()));
System.out.println(BinaryTree.postorderDisplay(bst.getRoot()));
System.out.println();

System.out.println("Add 8");
bst.add(8);
System.out.println(BinaryTree.inorderDisplay(bst.getRoot()));
System.out.println(BinaryTree.preorderDisplay(bst.getRoot()));
System.out.println(BinaryTree.postorderDisplay(bst.getRoot()));
System.out.println();

System.out.println("Remove 60");
bst.removeNode(new STNode (60));
System.out.println(BinaryTree.inorderDisplay(bst.getRoot()));
System.out.println(BinaryTree.preorderDisplay(bst.getRoot()));
System.out.println(BinaryTree.postorderDisplay(bst.getRoot()));
System.out.println();
}
}

B. Output Program
C. Pohon Binary
 Add 8

T Parent = T Order Value If order value New Node If order value Tree size ++

48 48 8<48 t = t.left - - -
(T) t = 29

29 29 8<29 t = t.left - - -
(T) t = 10

10 10 8<10 t = t.left - - -
(T) t = null

nul Parentleft =
l - - - null | 10 | 8 | null newNode 8
Parentleft = 8

Inorder = 8, 10, 29, 32, 48, 60, 75, 84, 100


Preorder = 48, 29, 10, 8, 32, 60, 84, 75, 100
Postorder = 8, 10, 32, 29, 75, 100, 84, 60, 48
 Remove 60

d p if (dNode.left == null || rNode while(rNode.left!=null)


dNode.right == null)

6 4 dNode.left == null (T) rNode = dNode.right


0 8 rNode = 84

rNode.parent = pNode
rNode.parent = 48

dNode < PNode -


60 < 48
(F)

PNode.right = rNode
PNode.right = 84

rNode = dNode.right True


rNode = 84 Pofrnode = rNode
P.ofrNode = dNode Pofrnode = 84
P.ofrNode = 60 rNode = rNode.left
rNode= 75
Node R sebagai anak kanan dari node P

You might also like