import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author
*/
public class MinBinaryHeapTree<T extends Comparable<T>> {
/** array to contain data */
ArrayList<T> data;
/** size of the tree */
private int size;
/** writer */
private BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));
/**
* Default constructor
* @return -
*/
public MinBinaryHeapTree() {
this.data = new ArrayList<T>();
this.size = 0;
}
/**
* Constructor
* @param data - the data of the tree
* @return
*/
public MinBinaryHeapTree(ArrayList<T> data) {
this();
this.size = data.size();
for (int ii = 0; ii < data.size(); ii++) {
this.data.add(data.get(ii));
}
heapify();
}
public void heapSort() {
int size = data.size();
ArrayList sortedList = new ArrayList<T>();
while (data.size() > 0) {
sortedList.add(deleteMin());
}
data = sortedList;
}
/**
* Insert data into array
* @param value - the node
* @return -
*/
public void insert(T value) {
// LENGKAPI
data.add(value);
percolateUp(data.size() - 1);
size++;
}
/**
* Delete the maximum data of the array
* @return -
*/
public T deleteMin() {
// LENGKAPI
T min = data.get(0);
data.set(0, data.get(data.size()-1));
data.remove(data.size()-1);
if(data.size() > 1) {
percolateDown(0);
}
size--;
return min;
}
/**
* Check the array is empty or not
* @return boolean
*/
public boolean isEmpty() {
return size == 0;
}
/**
* Method to print the array
* @return -
*/
public void print() throws IOException {
for (int ii = 0; ii < size - 1; ii++) {
writer.write(data.get(ii).toString() + ",");
}
if (size != 0) {
writer.write(data.get(size - 1).toString());
}
writer.write("\n");
}
/**
* Flush the writer
* @return
*/
public void flushWriter() throws IOException {
writer.flush();
}
/**
* Make the tree into heap data structure
* @return -
*/
private void heapify() {
// LENGKAPI
for(int parent = parentOf(data.size()-1); parent >= 0; parent--) {
//percolatedown dari semua parent di tree
percolateDown(parent);
}
}
/**
* Method to set element in the array
* @param value - the new element
* @param leaf - index of the element
* @return -
*/
private void setElementAt(T value, int leaf) {
data.set(leaf, value);
}
/**
* Method to obtain the parent index of a node
* @param index - the node index
* @return index of the parent
*/
private int parentOf(int index) {
// LENGKAPI
return (index - 1) / 2;
}
/**
* Method to obtain the left child index of a node
* @param index - the node index
* @return index of the left child
*/
private int leftChildOf(int index) {
// LENGKAPI
return 2 * index + 1;
}
/**
* Method to obtain the right child index of a node
* @param index - the node index
* @return index of the right child
*/
private int rightChildOf(int index) {
// LENGKAPI
return 2 * index + 2;
}
/**
* Percolate Up
* @param leaf - the index of the element
* @return -
*/
private void percolateUp(int leaf) {
// LENGKAPI
int parent = parentOf(leaf); //ambil index parent
Comparable value = (Comparable) data.get(leaf); //ambil nilai dari leaf
while (leaf > 0 && (value.compareTo((Comparable) data.get(parent)) < 0)) { //jika nilai leaf lebih kecil daripada nilai parent
data.set(leaf, (T) data.get(parent)); //menurunkan parent
leaf = parent; //menaikkan index leaf
parent = parentOf(leaf); //menaikkan index parent
}
data.set(leaf, (T) value); //menempatkan nilai leaf ke tempat seharusnya
}
/**
* Percolate Down
* @param root - the index of root
* @return
*/
private void percolateDown(int root) {
// LENGKAPI
int heapSize = data.size(); //size dari array
Comparable value = (Comparable) data.get(root); //nilai dari node paling atas
while (root < heapSize) { //selama index node tidak melebihi size
int childpos = leftChildOf(root); //menyimpan child sebelah kiri
if (childpos < heapSize) { //jika index child sebelah kiri kurang dari size
//bila child sebelah kanan ada dan child sebelah kanan lebih kecil nilainya dari child sebelah kiri
if (rightChildOf(root) < heapSize && ((Comparable) data.get(childpos + 1)).compareTo(((Comparable) data.get(childpos))) < 0) {
childpos++; //child yang dituju menjadi child sebelah kanan
}
//bila nilai child lebih besar dari nilai root
if (((Comparable)data.get(childpos)).compareTo((Comparable) value) < 0) {
data.set(root, data.get(childpos)); //menaikkan child
root = childpos; //menurunkan index
} else {
data.set(root, (T) value); //menempatkan nilai parent ke tempat seharusnya
return;
}
} else { //bila child lebih kecil
data.set(root, (T) value); //langsung ditempatkan
return;
}
}
}
}
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
=================================================================================================
/**
*
* @author
*/
public class MaxBinaryHeapTree<T extends Comparable<T>> {
/** array to contain data */
private ArrayList<T> data;
/** size of the tree */
private int size;
/** writer */
private BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));
/**
* Default constructor
* @return -
*/
public MaxBinaryHeapTree() {
this.data = new ArrayList<T>();
this.size = 0;
}
/**
* Constructor
* @param data - the data of the tree
* @return
*/
public MaxBinaryHeapTree(ArrayList<T> data) {
this();
this.size = data.size();
for (int ii = 0; ii < data.size(); ii++) {
this.data.add(data.get(ii));
}
heapify();
}
/**
* Insert data into array
* @param value - the node
* @return -
*/
public void insert(T value) {
// LENGKAPI
data.add(value);
percolateUp(data.size() - 1);
size++;
}
/**
* Delete the maximum data of the array
* @return -
*/
public T deleteMax() {
// LENGKAPI
T max = data.get(0);
data.set(0, data.get(data.size()-1));
data.remove(data.size()-1);
if(data.size() > 1) {
percolateDown(0);
}
size--;
return max;
}
/**
* Check the array is empty or not
* @return boolean
*/
public boolean isEmpty() {
return size == 0;
}
/**
* Method to print the array
* @return -
*/
public void print() throws IOException {
for (int ii = 0; ii < size - 1; ii++) {
writer.write(data.get(ii).toString() + ",");
}
if (size != 0) {
writer.write(data.get(size - 1).toString());
}
writer.write("\n");
}
/**
* Flush the writer
* @return
*/
public void flushWriter() throws IOException {
writer.flush();
}
/**
* Make the tree into heap data structure
* @return -
*/
private void heapify() {
// LENGKAPI
for(int parent = parentOf(data.size()-1); parent >= 0; parent--) {
//percolatedown dari semua parent di tree
percolateDown(parent);
}
}
/**
* Method to set element in the array
* @param value - the new element
* @param leaf - index of the element
* @return -
*/
private void setElementAt(T value, int leaf) {
data.set(leaf, value);
}
/**
* Method to obtain the parent index of a node
* @param index - the node index
* @return index of the parent
*/
private int parentOf(int index) {
// LENGKAPI
return (index - 1) / 2;
}
/**
* Method to obtain the left child index of a node
* @param index - the node index
* @return index of the left child
*/
private int leftChildOf(int index) {
// LENGKAPI
return 2 * index + 1;
}
/**
* Method to obtain the right child index of a node
* @param index - the node index
* @return index of the right child
*/
private int rightChildOf(int index) {
// LENGKAPI
return 2 * index + 2;
}
/**
* Percolate Up
* @param leaf - the index of the element
* @return -
*/
private void percolateUp(int leaf) {
// LENGKAPI
int parent = parentOf(leaf); //ambil index parent
Comparable value = (Comparable) data.get(leaf); //ambil nilai dari leaf
while (leaf > 0 && (value.compareTo((Comparable) data.get(parent)) < 0)) { //jika nilai leaf lebih besar daripada nilai parent
data.set(leaf, (T) data.get(parent)); //menurunkan parent
leaf = parent; //menaikkan index leaf
parent = parentOf(leaf); //menaikkan index parent
}
data.set(leaf, (T) value); //menempatkan nilai leaf ke tempat seharusnya
}
/**
* Percolate Down
* @param root - the index of root
* @return
*/
private void percolateDown(int root) {
// LENGKAPI
int heapSize = data.size(); //size dari array
Comparable value = (Comparable) data.get(root); //nilai dari node paling atas
while (root < heapSize) { //selama index node tidak melebihi size
int childpos = leftChildOf(root); //menyimpan child sebelah kiri
if (childpos < heapSize) { //jika index child sebelah kiri kurang dari size
//bila child sebelah kanan ada dan child sebelah kanan lebih besar nilainya dari child sebelah kiri
if (rightChildOf(root) < heapSize && ((Comparable) data.get(childpos + 1)).compareTo(((Comparable) data.get(childpos))) < 0) {
childpos++; //child yang dituju menjadi child sebelah kanan
}
//bila nilai child lebih besar dari nilai root
if (((Comparable)data.get(childpos)).compareTo((Comparable) value) < 0) {
data.set(root, data.get(childpos)); //menaikkan child
root = childpos; //menurunkan index
} else {
data.set(root, (T) value); //menempatkan nilai parent ke tempat seharusnya
return;
}
} else { //bila child lebih kecil
data.set(root, (T) value); //langsung ditempatkan
return;
}
}
}
}
================================================================================================
Quiz Rabu
import java.util.StringTokenizer;
import java.util.ArrayList;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.BufferedWriter;
import java.io.OutputStreamWriter;
import java.io.IOException;
/**
* SDA1112K2A Class
* Contain the main method to execute the program
* @author Satyadharma Tirtarasa & Pandu Wicaksono
*/
public class SDA1112K2A {
/**
* Main method of this program
* @param args - first argument of the program (not used in actual
* implementation)
* @return -
*/
public static void main(String[] args) throws IOException {
// LENGKAPI
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer tokIn = new StringTokenizer(in.readLine(), ",");
int masukan = Integer.parseInt(tokIn.nextToken());
int banyakOperasi = Integer.parseInt(tokIn.nextToken());
MaxBinaryHeapTree mbTree = new MaxBinaryHeapTree();
for (int i = 0; i < masukan; i++) {
StringTokenizer tok = new StringTokenizer(in.readLine(), ",");
mbTree.insert(new Club(tok.nextToken(), Integer.parseInt(tok.nextToken()), Integer.parseInt(tok.nextToken()),Integer.parseInt(tok.nextToken()),Integer.parseInt(tok.nextToken()),Integer.parseInt(tok.nextToken())));
}
mbTree.print();
for(int i = 0; i < banyakOperasi; i++) {
StringTokenizer tok = new StringTokenizer(in.readLine());
if(tok.nextToken().equals("DanaTidakMencukupi")) {
mbTree.deleteMax();
mbTree.print();
} else {
String club = tok.nextToken();
StringTokenizer tokClub = new StringTokenizer(club, ",");
mbTree.insert(new Club(tokClub.nextToken(), Integer.parseInt(tokClub.nextToken()), Integer.parseInt(tokClub.nextToken()),Integer.parseInt(tokClub.nextToken()),Integer.parseInt(tokClub.nextToken()),Integer.parseInt(tokClub.nextToken())));
mbTree.print();
}
}
mbTree.flushWriter();
}
}
/**
* MaxBinaryHeapTree Class
* Represent a MaxBinaryHeapTree to solve the problem
* @author Satyadharma Tirtarasa & Pandu Wicaksono
*/
class MaxBinaryHeapTree<T extends Comparable<T>> {
/** array to contain data */
private ArrayList<T> data;
/** size of the tree */
private int size;
/** writer */
private BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));
/**
* Default constructor
* @return -
*/
public MaxBinaryHeapTree() {
this.data = new ArrayList<T>();
this.size = 0;
}
/**
* Constructor
* @param data - the data of the tree
* @return
*/
public MaxBinaryHeapTree(ArrayList<T> data) {
this();
this.size = data.size();
for (int ii = 0; ii < data.size(); ii++) {
this.data.add(data.get(ii));
}
heapify();
}
/**
* Insert data into array
* @param value - the node
* @return -
*/
public void insert(T value) {
// LENGKAPI
data.add(value);
percolateUp(data.size() - 1);
size++;
}
/**
* Delete the maximum data of the array
* @return -
*/
public T deleteMax() {
// LENGKAPI
T max = data.get(0);
data.set(0, data.get(data.size()-1));
data.remove(data.size()-1);
if(data.size() > 1) {
percolateDown(0);
}
size--;
return max;
}
/**
* Check the array is empty or not
* @return boolean
*/
public boolean isEmpty() {
return size == 0;
}
/**
* Method to print the array
* @return -
*/
public void print() throws IOException {
for (int ii = 0; ii < size - 1; ii++) {
writer.write(data.get(ii).toString() + ",");
}
if (size != 0) {
writer.write(data.get(size - 1).toString());
}
writer.write("\n");
}
/**
* Flush the writer
* @return
*/
public void flushWriter() throws IOException {
writer.flush();
}
/**
* Make the tree into heap data structure
* @return -
*/
private void heapify() {
// LENGKAPI
for(int parent = parentOf(data.size()-1); parent >= 0; parent--) {
percolateDown(parent);
}
}
/**
* Method to set element in the array
* @param value - the new element
* @param leaf - index of the element
* @return -
*/
private void setElementAt(T value, int leaf) {
data.set(leaf, value);
}
/**
* Method to obtain the parent index of a node
* @param index - the node index
* @return index of the parent
*/
private int parentOf(int index) {
// LENGKAPI
return (index - 1) / 2;
}
/**
* Method to obtain the left child index of a node
* @param index - the node index
* @return index of the left child
*/
private int leftChildOf(int index) {
// LENGKAPI
return 2 * index + 1;
}
/**
* Method to obtain the right child index of a node
* @param index - the node index
* @return index of the right child
*/
private int rightChildOf(int index) {
// LENGKAPI
return 2 * index + 2;
}
/**
* Percolate Up
* @param leaf - the index of the element
* @return -
*/
private void percolateUp(int leaf) {
// LENGKAPI
int parent = parentOf(leaf);
Comparable value = (Comparable) data.get(leaf);
while (leaf > 0 && (value.compareTo((Comparable) data.get(parent)) < 0)) {
data.set(leaf, (T) data.get(parent));
leaf = parent;
parent = parentOf(leaf);
}
data.set(leaf, (T) value);
}
/**
* Percolate Down
* @param root - the index of root
* @return
*/
private void percolateDown(int root) {
// LENGKAPI
int heapSize = data.size();
Comparable value = (Comparable) data.get(root);
while (root < heapSize) {
int childpos = leftChildOf(root);
if (childpos < heapSize) {
if (rightChildOf(root) < heapSize && ((Comparable) data.get(childpos + 1)).compareTo(((Comparable) data.get(childpos))) < 0) {
childpos++;
}
if (((Comparable)data.get(childpos)).compareTo((Comparable) value) < 0) {
data.set(root, data.get(childpos));
root = childpos;
} else {
data.set(root, (T) value);
return;
}
} else {
data.set(root, (T) value);
return;
}
}
}
}
/**
* Club Class
* Represent a football club used to solve the problem
* @author Satyadharma Tirtarasa & Pandu Wicaksono
*/
class Club implements Comparable<Club> {
/** The name of the club */
private String name;
/** The number of wins */
private int win;
/** The number of draw */
private int draw;
/** The number of lose */
private int lose;
/** The number of goal scored */
private int goalScored;
/** The number of goal allowed */
private int goalAllowed;
int point;
int selisihGol;
/**
* Constructor
* @param name
* @param win
* @param draw
* @param lose
* @param goalScored
* @param goalAllowed
* @return -
*/
public Club(String name, int win, int draw, int lose, int goalScored, int goalAllowed) {
this.name = name;
this.win = win;
this.draw = draw;
this.lose = lose;
this.goalScored = goalScored;
this.goalAllowed = goalAllowed;
point = 3 * win + draw;
selisihGol = goalScored - goalAllowed;
}
/**
* CompareTo method
* @param other - the other club to compare with this club
* @return the result of the comparison
*/
public int compareTo(Club other) {
// LENGKAPI
if (this.point > other.point) {
return -1;
} else if (this.point < other.point) {
return 1;
} else {
if (this.selisihGol > other.selisihGol) {
return -1;
} else if (this.selisihGol < other.selisihGol) {
return 1;
} else {
if (this.goalScored > other.goalScored) {
return -1;
} else if (this.goalScored < other.goalScored) {
return 1;
} else {
return this.name.compareTo(other.name);
}
}
}
}
/**
* toString method
* @return -
*/
public String toString() {
return name;
}
}