Skip to content

Instantly share code, notes, and snippets.

@dmnugent80
dmnugent80 / HashTable.java
Last active August 29, 2015 14:12
HashTable with Chaining
//Used this example as a template: http://www.algolist.net/Data_structures/Hash_table/Simple_example
/*
*HashEntry class
*/
public class HashEntry {
public HashEntry next;
private int key;
private int value;
/* Balanced delimiter problem
Opening: (, [, {
Closing: ), ], }
Valid: {}()[]
([{}])
Invalid: ([)]
*/
@dmnugent80
dmnugent80 / LongestPath.java
Last active August 29, 2015 14:12
Binary Search Tree Longest Path
/*
Interview Question:
Here's a binary tree: find the longest path within it.
So, find a path between any two leaf nodes, where the path is the longest.”
*/
public class TreeNode{
int data;
TreeNode left;
TreeNode right;
@dmnugent80
dmnugent80 / LongestPathBST.java
Created January 8, 2015 04:51
Binary Search Tree Longest Path Take 2
/*
Interview Question:
Here's a binary tree: find the longest path within it.
So, find a path between any two leaf nodes, where the path is the longest.”
*/
public class TreeNode{
int data;
TreeNode left;
TreeNode right;
@dmnugent80
dmnugent80 / LongestPathBST.java
Last active August 29, 2015 14:13
Binary Search Tree Longest Path Take 2
/*
Interview Question:
Here's a binary tree: find the longest path within it.
So, find a path between any two leaf nodes, where the path is the longest.”
*/
public class TreeNode{
int data;
TreeNode left;
TreeNode right;
@dmnugent80
dmnugent80 / DeleteDuplicates.java
Last active August 29, 2015 14:13
Delete Duplicates in an array
//Delete duplicates in an array
public static int[] deleteDuplicates( int[] arrayWithDups ){
ArrayList<Integer> arrayNoDups= new ArrayList<Integer>();
for (int i=0; i < arrayWithDups.length; i++){
if (!arrayNoDups.contains(arrayWithDups[i])){
arrayNoDups.add(arrayWithDups[i]);
}
}
int[] arrayPrimitive = new int[arrayNoDups.size()];
@dmnugent80
dmnugent80 / BFS.java
Last active August 29, 2015 14:13
Print each level of a BST on it's own line
//Breadth First Search, traverse tree and print it out level-by-level
public class TreeNode{
public int data;
public int level;
public TreeNode left;
public TreeNode right;
public TreeNode(int d, int l){
this.data = d;
this.level = l;
@dmnugent80
dmnugent80 / powRecursive.java
Last active August 29, 2015 14:13
recursive pow function
public static int pow(int x, int n){
int mid;
int temp;
if (n == 0){
return 1;
}
if (n == 1){
return x;
}
@dmnugent80
dmnugent80 / BST.java
Last active August 29, 2015 14:13
Binary Search Tree
public class TreeNode{
public int data;
public TreeNode left;
public TreeNode right;
public TreeNode(int d){
this.data = d;
this.left = null;
this.right = null;
}
}
@dmnugent80
dmnugent80 / CountOfDuplicates.java
Last active August 29, 2015 14:13
Return count of duplicated items in array
public int getCountDuplicates(int[] array){
HashSet<Integer> set = new HashSet<Integer>();
int dupCount = 0;
for (int i=0; i<array.length; i++){
if (!set.add(array[i]){
dupCount++;
}
}
return dupCount;
}