Skip to content

Commit

Permalink
Add Clear method to the tree and heap
Browse files Browse the repository at this point in the history
Main test
Enhance identifiers
  • Loading branch information
Abdalrahman-Alhamod committed Jul 28, 2023
1 parent f6acb52 commit 3d84cd1
Show file tree
Hide file tree
Showing 8 changed files with 160 additions and 40 deletions.
2 changes: 1 addition & 1 deletion src/AVL.java
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ else if (balanceFactor < -1) {
* @return the root of the updated subtree
* @implNote This method has a time complexity of O(log(n))
*/
public boolean insertIterative(Node<T> node, T value) {
private boolean insertIterative(Node<T> node, T value) {
Node<T> newNode = new Node<>(value);
if (node == null) {
root = newNode;
Expand Down
4 changes: 2 additions & 2 deletions src/BST.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ private Node<T> insertRecursive(Node<T> node, T value) {
* @return true if the value was inserted, or false if it already exists in the tree
* @implNote This method has a time complexity of O(h), where h is the height of the subtree.
*/
public boolean insertIterative(Node<T> node, T value) {
private boolean insertIterative(Node<T> node, T value) {
Node<T> newNode = new Node<>(value);

// If the tree is empty, set the root node to be the new node
Expand Down Expand Up @@ -313,7 +313,7 @@ else if (node.getRight() == null) {
* @return true if the node was found and removed, false otherwise
* @implNote This method has a time complexity of O(h), where h is the height of the subtree.
*/
public boolean deleteIterative(T value) {
private boolean deleteIterative(T value) {
Node<T> parent = null;
Node<T> current = root;
while (current != null) {
Expand Down
9 changes: 9 additions & 0 deletions src/BinaryTree.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public BinaryTree() {
size = 0;
useRecursiveApproach = true;
}

/**
* Constructs a binary tree with the given root node.
*
Expand Down Expand Up @@ -969,6 +970,14 @@ public void setUseRecursiveApproach(boolean useRecursiveApproach) {
this.useRecursiveApproach = useRecursiveApproach;
}

/**
* Makes the tree empty
*/
public void clear() {
root = null;
size = 0;
}

@Override
public String toString() {
return printTree(root);
Expand Down
2 changes: 1 addition & 1 deletion src/ExecutionTimeCalculator.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public static long getExecutionTime() {
*
* @return The formatted execution time in seconds (e.g., "0.001232 s").
*/
public static String getFormattedExecutionTime() {
private static String getFormattedExecutionTime() {
double executionTimeSeconds = executionTime / 1_000_000_000.0; // Convert nanoseconds to seconds
return String.format(Locale.US, "%.6f s", executionTimeSeconds);
}
Expand Down
20 changes: 9 additions & 11 deletions src/Heap.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,17 +85,6 @@ protected void swap(int one, int two) {
heapArray.set(two, temp);
}

/**
* Prints the elements of the heap array.
*/
public void printHeapArray() {
System.out.print("Heap Array : ");
for (int i = 1; i <= heapSize; i++) {
System.out.print(heapArray.get(i) + " ");
}
System.out.println();
}

/**
* Prints the elements of the heap as a tree.
*/
Expand Down Expand Up @@ -172,6 +161,15 @@ public void setUseRecursiveApproach(boolean useRecursiveApproach) {
this.useRecursiveApproach = useRecursiveApproach;
}

/**
* Makes the heap empty
*/
public void clear() {
heapArray = new ArrayList<>();
heapArray.add(null);
heapSize = 0;
}

public String toString() {
ArrayList<T> array = new ArrayList<>(heapArray);
array.remove(0);
Expand Down
134 changes: 123 additions & 11 deletions src/Main.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,131 @@
import java.util.ArrayList;

import static java.util.Collections.reverse;
import java.util.function.Consumer;

/**
* Author :
* <pre>
*
* /$$$$$$ /$$ /$$ /$$ /$$ /$$ /$$
* /$$__ $$| $$ | $$ | $$ | $$| $$$ /$$$
* | $$ \ $$| $$$$$$$ /$$$$$$$ | $$ | $$| $$$$ /$$$$
* | $$$$$$$$| $$__ $$ /$$__ $$ | $$$$$$$$| $$ $$/$$ $$
* | $$__ $$| $$ \ $$| $$ | $$ | $$__ $$| $$ $$$| $$
* | $$ | $$| $$ | $$| $$ | $$ | $$ | $$| $$\ $ | $$
* | $$ | $$| $$$$$$$/| $$$$$$$ | $$ | $$| $$ \/ | $$
* |__/ |__/|_______/ \_______//$$$$$$|__/ |__/|__/ |__/
* |______/
*
* </pre>
* Contact me :<br>
* <a href="mailto:abd.alrrahman.alhamod@gmail.com">E-mail</a><br>
* <a href="https://github.com/Abdalrahman-Alhamod">Github</a><br>
* <a href="https://www.linkedin.com/in/abd-alrrahman-alhamod/">LinkedIn</a><br>
* <a href="https://www.facebook.com/profile.php?id=100011427430343">Facebook</a>
*/
public class Main {
public static void main(String[] args) {
MinHeap<Integer> heap = new MinHeap<>();
ArrayList<Integer> array = new ArrayList<>();
heap.setUseRecursiveApproach(false);

Consumer<BST<Integer>> testBST = integerBST -> {
integerBST.insert(4);
integerBST.insert(7);
integerBST.insert(2);
integerBST.insert(9);
integerBST.insert(10);
integerBST.insert(3);
integerBST.insert(8);
integerBST.insert(1);
System.out.println(integerBST);
};

System.out.println("#################################################");

// BST Recursive Testing
System.out.println("Recursive BST");
ExecutionTimeCalculator.start();
BST<Integer> bstTree = new BST<>();
bstTree.setUseRecursiveApproach(true);
testBST.accept(bstTree);
ExecutionTimeCalculator.stop();
ExecutionTimeCalculator.printExecutionTime();

System.out.println("#################################################");

// BST Iterative Testing
System.out.println("Iterative BST");
ExecutionTimeCalculator.start();
bstTree.clear();
bstTree.setUseRecursiveApproach(false);
testBST.accept(bstTree);
ExecutionTimeCalculator.stop();
ExecutionTimeCalculator.printExecutionTime();

System.out.println("*************************************************");

Consumer<AVL<Integer>> avlTest = integerAVL -> {
for (int i = 1; i <= 13; i++) {
integerAVL.insert(i);
}
System.out.println(integerAVL);
};

System.out.println("#################################################");

// AVL Recursive Testing
System.out.println("Recursive AVL");
AVL<Integer> avlTree = new AVL<>();
avlTree.setUseRecursiveApproach(true);
ExecutionTimeCalculator.start();
avlTest.accept(avlTree);
ExecutionTimeCalculator.stop();
ExecutionTimeCalculator.printExecutionTime();

System.out.println("#################################################");

// AVL Recursive Testing
System.out.println("Iterative AVL");
avlTree.clear();
avlTree.setUseRecursiveApproach(false);
ExecutionTimeCalculator.start();
for (int i = 1; i <= 1E5; i++) {
array.add(i);
}
reverse(array);
heap.heapSort(array);
avlTest.accept(avlTree);
ExecutionTimeCalculator.stop();
ExecutionTimeCalculator.printExecutionTime();

System.out.println("*************************************************");

Consumer<PriorityQueue<Integer>> testPriorityQueue = integerPriorityQueue -> {
integerPriorityQueue.enqueue(3);
integerPriorityQueue.enqueue(7);
integerPriorityQueue.enqueue(8);
integerPriorityQueue.enqueue(2);
integerPriorityQueue.enqueue(10);
integerPriorityQueue.enqueue(9);
integerPriorityQueue.enqueue(4);
integerPriorityQueue.enqueue(11);
System.out.println(integerPriorityQueue);
integerPriorityQueue.printHeapTree();
};

System.out.println("#################################################");

// Priority Queue Recursive Testing
System.out.println("Recursive Priority Queue (Max Heap)");
PriorityQueue<Integer> priorityQueue = new PriorityQueue<>();
priorityQueue.setUseRecursiveApproach(true);
ExecutionTimeCalculator.start();
testPriorityQueue.accept(priorityQueue);
ExecutionTimeCalculator.stop();
ExecutionTimeCalculator.printExecutionTime();

System.out.println("#################################################");

// Priority Queue Recursive Testing
System.out.println("Iterative Priority Queue (Max Heap)");
priorityQueue = new PriorityQueue<>();
priorityQueue.setUseRecursiveApproach(false);
ExecutionTimeCalculator.start();
testPriorityQueue.accept(priorityQueue);
ExecutionTimeCalculator.stop();
ExecutionTimeCalculator.printExecutionTime();

System.out.println("#################################################");
}
}
17 changes: 9 additions & 8 deletions src/MaxHeap.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public MaxHeap(ArrayList<T> heapArray) {
* @param index the index to start heapifying from
* @implNote This method has a time complexity of O(log(n))
*/
protected void heapifyDown(int index) {
private void heapifyDown(int index) {
if (useRecursiveApproach)
heapifyDownRecursive(index);
else
Expand All @@ -46,7 +46,7 @@ protected void heapifyDown(int index) {
* @param index the index to start heapifying from
* @implNote This method has a time complexity of O(log(n))
*/
protected void heapifyDownRecursive(int index) {
private void heapifyDownRecursive(int index) {
int leftChildIndex = getLeftChildIndex(index);
int rightChildIndex = getRightChildIndex(index);
int largest = index;
Expand Down Expand Up @@ -74,7 +74,7 @@ protected void heapifyDownRecursive(int index) {
* @param index the index to start heapifying from
* @implNote This method has a time complexity of O(log(n))
*/
protected void heapifyDownIterative(int index) {
private void heapifyDownIterative(int index) {
int current = index;

while (true) {
Expand Down Expand Up @@ -108,7 +108,7 @@ protected void heapifyDownIterative(int index) {
* @param index the index to start heapifying from
* @implNote This method has a time complexity of O(log(n))
*/
protected void heapifyUp(int index) {
private void heapifyUp(int index) {
if (useRecursiveApproach)
heapifyUpRecursive(index);
else
Expand All @@ -123,7 +123,7 @@ protected void heapifyUp(int index) {
* @param index the index to start heapifying from
* @implNote This method has a time complexity of O(log(n))
*/
protected void heapifyUpRecursive(int index) {
private void heapifyUpRecursive(int index) {
int parentIndex = getParentIndex(index);
if (parentIndex >= 1 && heapArray.get(index).compareTo(heapArray.get(parentIndex)) > 0) {
swap(index, parentIndex);
Expand All @@ -139,7 +139,7 @@ protected void heapifyUpRecursive(int index) {
* @param index the index to start heapifying from
* @implNote This method has a time complexity of O(log(n))
*/
protected void heapifyUpIterative(int index) {
private void heapifyUpIterative(int index) {
int current = index;
int parentIndex;

Expand Down Expand Up @@ -229,12 +229,13 @@ public boolean delete(T element) {

/**
* This method builds a max heap from an ArrayList of elements.
* It assumes that the ArrayList is a complete binary tree, and starts heapifying from the last non-leaf node to the root.
* It assumes that the ArrayList is a complete binary tree,
* and starts heapifying from the last non-leaf node to the root.
*
* @param array the ArrayList of elements to be transformed into a max heap
* @implNote This method has a time complexity of O(n)
*/
private void buildMaxHeapArray(ArrayList<T> array) {
public void buildMaxHeapArray(ArrayList<T> array) {
// Time Complexity : O(n)
heapSize = array.size();
array.add(0, null);
Expand Down
12 changes: 6 additions & 6 deletions src/MinHeap.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public MinHeap(ArrayList<T> heapArray) {
* @param index the index of the element to heapify
* @implNote This method has a time complexity of O(log(n))
*/
protected void heapifyTopDown(int index) {
private void heapifyTopDown(int index) {
if (useRecursiveApproach)
heapifyTopDownRecursive(index);
else
Expand All @@ -44,7 +44,7 @@ protected void heapifyTopDown(int index) {
* @param index the index of the element to heapify
* @implNote This method has a time complexity of O(log(n))
*/
protected void heapifyTopDownRecursive(int index) {
private void heapifyTopDownRecursive(int index) {
int smallest = index;
int right = getRightChildIndex(index);
int left = getLeftChildIndex(index);
Expand All @@ -65,7 +65,7 @@ protected void heapifyTopDownRecursive(int index) {
* @param index the index of the element to heapify
* @implNote This method has a time complexity of O(log(n))
*/
protected void heapifyTopDownIterative(int index) {
private void heapifyTopDownIterative(int index) {
int current = index;

while (true) {
Expand Down Expand Up @@ -110,7 +110,7 @@ public void insert(T element) {
* @param index the index of the element to heapify
* @implNote This method has a time complexity of O(log(n))
*/
protected void heapifyBottomUp(int index) {
private void heapifyBottomUp(int index) {
if (useRecursiveApproach)
heapifyBottomUpRecursive(index);
else
Expand All @@ -124,7 +124,7 @@ protected void heapifyBottomUp(int index) {
* @param index the index of the element to heapify
* @implNote This method has a time complexity of O(log(n))
*/
protected void heapifyBottomUpRecursive(int index) {
private void heapifyBottomUpRecursive(int index) {
if (index != 1 && heapArray.get(index).compareTo(heapArray.get(getParentIndex(index))) < 0) {
swap(index, getParentIndex(index));
heapifyBottomUpRecursive(getParentIndex(index));
Expand All @@ -138,7 +138,7 @@ protected void heapifyBottomUpRecursive(int index) {
* @param index the index of the element to heapify
* @implNote This method has a time complexity of O(log(n))
*/
protected void heapifyBottomUpIterative(int index) throws IndexOutOfBoundsException {
private void heapifyBottomUpIterative(int index) throws IndexOutOfBoundsException {
int current = index;

while (current > 1) {
Expand Down

0 comments on commit 3d84cd1

Please sign in to comment.