Skip to content

Commit

Permalink
trying to implement some stuff from data structures class
Browse files Browse the repository at this point in the history
  • Loading branch information
Kuhron committed Oct 19, 2022
1 parent 2412291 commit d7a4bc7
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
20 changes: 20 additions & 0 deletions DataStructuresPractice/BinaryHeap.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
public class BinaryHeap<T> {
private DynamicResizingArray<T> array = new DynamicResizingArray<T>();

public BinaryHeap() {
// nothing
}

public T extractMax() {
return null; // TODO
}

public void insert(T item) {
// put it at the end and then swap up with parent as much as needed for heap property
// TODO
}

public static void main(String[] args) {
BinaryHeap heap = new BinaryHeap();
}
}
40 changes: 40 additions & 0 deletions DataStructuresPractice/DynamicResizingArray.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
public class DynamicResizingArray<T> {
private T[] array = (T[]) new Object[1];
private int capacity = 1;
public int length = 0;

public DynamicResizingArray() {
// nothing
}

public void insert(T item) {
array[length] = item;
length++;
resize();
}

private void resize() {
if (length == capacity) {
// resize to double
int newCapacity = 2 * capacity;
T[] newArray = (T[]) new Object[newCapacity];
copyItems(array, newArray);
array = newArray;
}
}

private void copyItems(T[] array, T[] newArray) {
for (int i = 0; i < array.length; i++) {
newArray[i] = array[i];
}
}

public void push(T item) {
int index = length;
setItem(index, item);
}

public void setItem(int index, T item) {
array[index] = item;
}
}

0 comments on commit d7a4bc7

Please sign in to comment.