Skip to content

Instantly share code, notes, and snippets.

@arrayed
Created December 30, 2016 18:51
Show Gist options
  • Save arrayed/12b720b86ad54f39452deb87883a0204 to your computer and use it in GitHub Desktop.
Save arrayed/12b720b86ad54f39452deb87883a0204 to your computer and use it in GitHub Desktop.
D-ary Heap Driver Software Implementation
/* DheapDriver.java
* Class - heap
* Authors - Amritpal Singh
* Website - arrayed.net
* Class description - Insert random priority integer values and random string values in heap and print out from max to minimum by using delete_max method
*/
public class DheapDriver {
private static final String ALPHANUMERIC_STRING = "0123456789abcdefghijklmnopqrstuvwxyz";
public static void main (String[] args) {
int randomNumber = 0; // used to store random value generated by Math random function
final int randMinValue = 0; // specified minimum random value parameter
final int randMaxValue = 5000; // specified maximum random value parameter
heap testHeap = new heap(1000); // create heap with 1000 elements
for (int i = 0; i < 1000; i++) {
randomNumber = randMinValue + (int)(Math.random()*randMaxValue);
testHeap.insert(randomNumber,getRandomString()); // insert random priority and string value
}
System.out.println("Priority \t String Value");
while (!testHeap.isEmpty()) {
System.out.println(testHeap.delete_max());
}
}
// method returns random string 10 chars long
public static String getRandomString() {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 10; i++) {
int ch = (int)(Math.random()*ALPHANUMERIC_STRING.length());
sb.append(ALPHANUMERIC_STRING.charAt(ch));
}
return sb.toString();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment