Skip to content

Instantly share code, notes, and snippets.

View zachschultz's full-sized avatar
😀

Zach Schultz zachschultz

😀
View GitHub Profile
@zachschultz
zachschultz / MergeSort.java
Created March 2, 2015 16:39
Java code for Merge Sort
import java.util.*;
public class MergeSort {
public static void main(String[] args) {
// get array and call mergeSort() on it
mergeSort(array);
}
public static void mergeSort(int[] array) {
@zachschultz
zachschultz / LinkedListForEachDepth.java
Created March 1, 2015 17:00
Create a Linked List for each depth of a Binary Search Tree
import java.util.*;
public class LinkedListForEachDepth {
public static void main(String[] args) {
Node root = new Node(12);
// ... create BST with nodes
// create our list of LinkedLists
@zachschultz
zachschultz / MakeBSTFromArray.java
Created March 1, 2015 16:08
Create a minimum height BST from a sorted array
import java.util.*;
public class MakeBSTFromArray {
public static void main(String[] args) {
int[] arr = {0,1,2,3};
Node root = makeBSTShorter(arr, 0, arr.length-1);
System.out.println("Level order: ");
@zachschultz
zachschultz / DeleteNodeBST.java
Created February 28, 2015 19:41
Delete a node from a Binary Search Tree
import java.util.*;
public class DeleteNodeBST {
public static void main(String[] args) {
Node root = new Node(12);
// MAKE A TREE OF NODES...
delete(root, 14);
@zachschultz
zachschultz / DFS_Iterative.java
Created February 27, 2015 22:29
Iterative Depth First Search on Nodes
public void DFS_Iterative(Node n) {
Stack<Node> stack = new Stack<Node>();
stack.push(n);
while (!stack.isEmpty()) {
Node node = stack.pop();
if (!node.visited) {
node.visited = true;
System.out.println(node);
import java.util.*;
public class IsTreeBinarySearchTree {
public static void main(String[] args) {
// build a tree with Nodes...root is root Node
System.out.println((isTreeBST(root) ? "Tree is BST!" : "Tree is NOT BST!"));
}
@zachschultz
zachschultz / IsTreeBalanced.java
Last active August 29, 2015 14:16
Check if a Binary Tree is balanced
public class IsTreeBalanced {
public static void main(String[] args) {
// build a tree with Nodes...root is root Node
isBalanced(root);
}
public static int getHeight(Node root) {