Skip to content

Instantly share code, notes, and snippets.

@zachschultz
zachschultz / MergeSort.java
Created Mar 2, 2015
Java code for Merge Sort
View MergeSort.java
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 Mar 1, 2015
Create a Linked List for each depth of a Binary Search Tree
View LinkedListForEachDepth.java
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 Mar 1, 2015
Create a minimum height BST from a sorted array
View MakeBSTFromArray.java
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 Feb 28, 2015
Delete a node from a Binary Search Tree
View DeleteNodeBST.java
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 Feb 27, 2015
Iterative Depth First Search on Nodes
View DFS_Iterative.java
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);
View IsTreeBinarySearchTree.java
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 Aug 29, 2015
Check if a Binary Tree is balanced
View IsTreeBalanced.java
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) {
You can’t perform that action at this time.