Sie sind auf Seite 1von 11

UAS Struktur Data

Nama : Hafiz Yoga Pratama


NIM : 20210801511

1A.

1B.
package uas_struktur_data;

public class Node {

int value;
Node left;
Node right;

Node(int value) {
this.value = value;
right = null;
left = null;
}
}

package uas_struktur_data;

import java.util.*;

public class BinaryTree {

static Node root;

public static void main(String[] args) {


BinaryTree binaryTree = new BinaryTree();

Scanner input = new Scanner(System.in);


Integer bilangan;
String konfirmasi;
do {
System.out.print("Silakan inputkan bilangan : ");
bilangan = input.nextInt();

binaryTree.add(bilangan);

System.out.print("Apakah anda sudah selesai menginput ? ");


konfirmasi = input.next();
} while(konfirmasi.equalsIgnoreCase("y") ||
konfirmasi.equalsIgnoreCase("yes"));

if(konfirmasi.equalsIgnoreCase("n") ||
konfirmasi.equalsIgnoreCase("no")) {
traversePreOrder(binaryTree.root);
}
}

public void add(int value) {


root = addRecursive(root, value);
}

private Node addRecursive(Node current, int value) {

if (current == null) {
return new Node(value);
}

if (value < current.value) {


current.left = addRecursive(current.left, value);
} else if (value > current.value) {
current.right = addRecursive(current.right, value);
}

return current;
}

public boolean isEmpty() {


return root == null;
}

public int getSize() {


return getSizeRecursive(root);
}

private int getSizeRecursive(Node current) {


return current == null ? 0 : getSizeRecursive(current.left) + 1 +
getSizeRecursive(current.right);
}

public boolean containsNode(int value) {


return containsNodeRecursive(root, value);
}

private boolean containsNodeRecursive(Node current, int value) {


if (current == null) {
return false;
}
if (value == current.value) {
return true;
}

return value < current.value


? containsNodeRecursive(current.left, value)
: containsNodeRecursive(current.right, value);
}

public void delete(int value) {


root = deleteRecursive(root, value);
}

private Node deleteRecursive(Node current, int value) {


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

if (value == current.value) {
// Case 1: no children
if (current.left == null && current.right == null) {
return null;
}

// Case 2: only 1 child


if (current.right == null) {
return current.left;
}

if (current.left == null) {
return current.right;
}

// Case 3: 2 children
int smallestValue = findSmallestValue(current.right);
current.value = smallestValue;
current.right = deleteRecursive(current.right, smallestValue);
return current;
}
if (value < current.value) {
current.left = deleteRecursive(current.left, value);
return current;
}

current.right = deleteRecursive(current.right, value);


return current;
}

private int findSmallestValue(Node root) {


return root.left == null ? root.value : findSmallestValue(root.left);
}

public void traverseInOrder(Node node) {


if (node != null) {
traverseInOrder(node.left);
visit(node.value);
traverseInOrder(node.right);
}
}

public static void traversePreOrder(Node node) {


if (node != null) {
visit(node.value);
traversePreOrder(node.left);
traversePreOrder(node.right);
}
}

public static void traversePostOrder(Node node) {


if (node != null) {
traversePostOrder(node.left);
traversePostOrder(node.right);
visit(node.value);
}
}

public static void traverseLevelOrder() {


if (root == null) {
return;
}

Queue<Node> nodes = new LinkedList<>();


nodes.add(root);

while (!nodes.isEmpty()) {

Node node = nodes.remove();

System.out.print(" " + node.value);

if (node.left != null) {
nodes.add(node.left);
}

if (node.right != null) {
nodes.add(node.right);
}
}
}

public static void traverseInOrderWithoutRecursion() {


Stack<Node> stack = new Stack<>();
Node current = root;

while (current != null || !stack.isEmpty()) {


while (current != null) {
stack.push(current);
current = current.left;
}

Node top = stack.pop();


visit(top.value);
current = top.right;
}
}

public static void traversePreOrderWithoutRecursion() {


Stack<Node> stack = new Stack<>();
Node current = root;
stack.push(root);

while (current != null && !stack.isEmpty()) {


current = stack.pop();
visit(current.value);

if (current.right != null)
stack.push(current.right);

if (current.left != null)
stack.push(current.left);
}
}

public static void traversePostOrderWithoutRecursion() {


Stack<Node> stack = new Stack<>();
Node prev = root;
Node current = root;
stack.push(root);

while (current != null && !stack.isEmpty()) {


current = stack.peek();
boolean hasChild = (current.left != null || current.right != null);
boolean isPrevLastChild = (prev == current.right || (prev ==
current.left && current.right == null));

if (!hasChild || isPrevLastChild) {
current = stack.pop();
visit(current.value);
prev = current;
} else {
if (current.right != null) {
stack.push(current.right);
}
if (current.left != null) {
stack.push(current.left);
}
}
}
}

private static void visit(int value) {


System.out.print(" " + value);
}
}
1C.
package uas_struktur_data;

import java.util.*;

public class BinaryTree {

static Node root;

public static void main(String[] args) {


BinaryTree binaryTree = new BinaryTree();

Scanner input = new Scanner(System.in);


Integer bilangan;
String konfirmasi;

do {
System.out.print("Silakan inputkan bilangan : ");
bilangan = input.nextInt();

binaryTree.add(bilangan);

System.out.print("Apakah anda sudah selesai menginput ? ");


konfirmasi = input.next();
} while(konfirmasi.equalsIgnoreCase("y") ||
konfirmasi.equalsIgnoreCase("yes"));

if(konfirmasi.equalsIgnoreCase("n") ||
konfirmasi.equalsIgnoreCase("no")) {
System.out.print("Pre-Order : ");
traversePreOrder(binaryTree.root);
System.out.print(" ");
System.out.print("In-Order : ");
traverseInOrder(binaryTree.root);
System.out.print(" ");
System.out.print("Post-Order : ");
traversePostOrder(binaryTree.root);
}
}

public void add(int value) {


root = addRecursive(root, value);
}

private Node addRecursive(Node current, int value) {


if (current == null) {
return new Node(value);
}

if (value < current.value) {


current.left = addRecursive(current.left, value);
} else if (value > current.value) {
current.right = addRecursive(current.right, value);
}

return current;
}

public boolean isEmpty() {


return root == null;
}

public int getSize() {


return getSizeRecursive(root);
}

private int getSizeRecursive(Node current) {


return current == null ? 0 : getSizeRecursive(current.left) + 1 +
getSizeRecursive(current.right);
}

public boolean containsNode(int value) {


return containsNodeRecursive(root, value);
}

private boolean containsNodeRecursive(Node current, int value) {


if (current == null) {
return false;
}

if (value == current.value) {
return true;
}

return value < current.value


? containsNodeRecursive(current.left, value)
: containsNodeRecursive(current.right, value);
}

public void delete(int value) {


root = deleteRecursive(root, value);
}

private Node deleteRecursive(Node current, int value) {


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

if (value == current.value) {
// Case 1: no children
if (current.left == null && current.right == null) {
return null;
}
// Case 2: only 1 child
if (current.right == null) {
return current.left;
}

if (current.left == null) {
return current.right;
}

// Case 3: 2 children
int smallestValue = findSmallestValue(current.right);
current.value = smallestValue;
current.right = deleteRecursive(current.right, smallestValue);
return current;
}
if (value < current.value) {
current.left = deleteRecursive(current.left, value);
return current;
}

current.right = deleteRecursive(current.right, value);


return current;
}

private int findSmallestValue(Node root) {


return root.left == null ? root.value : findSmallestValue(root.left);
}

public static void traverseInOrder(Node node) {


if (node != null) {
traverseInOrder(node.left);
visit(node.value);
traverseInOrder(node.right);
}
}

public static void traversePreOrder(Node node) {


if (node != null) {
visit(node.value);
traversePreOrder(node.left);
traversePreOrder(node.right);
}
}

public static void traversePostOrder(Node node) {


if (node != null) {
traversePostOrder(node.left);
traversePostOrder(node.right);
visit(node.value);
}
}

public static void traverseLevelOrder() {


if (root == null) {
return;
}

Queue<Node> nodes = new LinkedList<>();


nodes.add(root);

while (!nodes.isEmpty()) {

Node node = nodes.remove();

System.out.print(" " + node.value);

if (node.left != null) {
nodes.add(node.left);
}

if (node.right != null) {
nodes.add(node.right);
}
}
}

public static void traverseInOrderWithoutRecursion() {


Stack<Node> stack = new Stack<>();
Node current = root;

while (current != null || !stack.isEmpty()) {


while (current != null) {
stack.push(current);
current = current.left;
}

Node top = stack.pop();


visit(top.value);
current = top.right;
}
}

public static void traversePreOrderWithoutRecursion() {


Stack<Node> stack = new Stack<>();
Node current = root;
stack.push(root);

while (current != null && !stack.isEmpty()) {


current = stack.pop();
visit(current.value);

if (current.right != null)
stack.push(current.right);

if (current.left != null)
stack.push(current.left);
}
}

public static void traversePostOrderWithoutRecursion() {


Stack<Node> stack = new Stack<>();
Node prev = root;
Node current = root;
stack.push(root);

while (current != null && !stack.isEmpty()) {


current = stack.peek();
boolean hasChild = (current.left != null || current.right != null);
boolean isPrevLastChild = (prev == current.right || (prev ==
current.left && current.right == null));

if (!hasChild || isPrevLastChild) {
current = stack.pop();
visit(current.value);
prev = current;
} else {
if (current.right != null) {
stack.push(current.right);
}
if (current.left != null) {
stack.push(current.left);
}
}
}
}

private static void visit(int value) {


System.out.print(" " + value);
}
}

2A. Jumlah Weight adalah 11 dan jumlah heightnya adalah 5


2B. Node Sub Kiri : ((A+B)^C)-(D*E)
Node Sub Kanan : (F-G)+((H*I)-(J*K))
2C.
[ ( ( 5 + 8 ) ^ 2) - ( 7 * 5 ) ] / [ ( 11 - 5 ) + ( ( 10 * 5 ) - ( 10 * 2 ) ) ]

= [ ( 13^2) - ( 35 ) ] / [ ( 6 ) + ( ( 50 ) - ( 20 ) ) ]
= [ 169 - 35 ] / [ 6 + 30 ]

= 134 / 36

= 3.7

2 (II) A.
2 (II) B.
2 (III) A.

3
I. Infix : ((A*B+(C/D)*E) – (((F*G)/(H*I)^J))
a. Perubahan infix menjadi postfix dan prefix
Postfix : EAB*CD/+*JFG*HI/^-
Prefix : -*E+*AB/CD^J/*FG*HI
b. Postfix : EAB*CD/+*JFG*HI/^-
Jika A = 5, B = I = 1, C = 11, D = 9, E = A, F = 11, G = 13, H = C, maka akan
diperoleh : A51*119/+*J1113*C1*/^-
II. Hasil Operasi :
 Top(S) = Cintia
 Create(S) = Top(S) = Cintia, Top = 1, NOEL(S) = 3
 Isempty(S) = FALSE
 POP(S) = TOP(S) = Anggia, TOP = 1, NOEL(S) = 1
 NOEL(S) = 3
 Push(Dina, Agis) = {Anggia, Budi, Cintia, Dina, Agis}

Das könnte Ihnen auch gefallen