Skip to content

Instantly share code, notes, and snippets.

@gtke
gtke / reconstruct
Created January 28, 2013 21:00
BST Reconstruct
/**
* First clears this BST, then reconstructs the BST that is
* uniquely defined by the given preorder and inorder traversals
*
* (When you finish, this BST should produce the same preorder and
* inorder traversals as those given)
*
* @param preorder a preorder traversal of the BST to reconstruct
* @param inorder an inorder traversal of the BST to reconstruct
*/
@gtke
gtke / avl tree rotations
Created February 23, 2013 07:41
AVL Tree Rotations
/*
* if BF is less than -1
* if right child - bf <=0
do left rotation
* if right child - bf > 0
do right-left rotation
* if BF is more than 1
* if left child - bf >=0
do right rotation
* if left child - bf < 0
@gtke
gtke / HeaPTests
Created February 27, 2013 21:59
Binary Heap JUnit Tests
import static org.junit.Assert.assertEquals;
import org.junit.Test;
public class BinaryHeapTest {
@Test (timeout=1000)
public void testSimpleAdd1() {
BinaryHeap<String> heap = new BinaryHeap<String>();
heap.add("6");
@gtke
gtke / AVL_Rotation_methods
Last active December 14, 2015 13:49
AVL Tree 4 rotation methods
/**
* Performs a left rotation on a node
*
* @param n The node to have the left rotation performed on
* @return The new root of the subtree that is now balanced due to the rotation
*/
private Node<T> left(Node<T> n) {
if(n != null){
Node<T> temp = n.getRight();
n.setRight(temp.getLeft());
from Myro import *
def collageMaker(fileName, files, newFileName):
picList = []
for myNum in range(files+1):
newPic = makePicture("{}.jpg".format(myNum))
newRedSum = 0
newGreenSum = 0
newBlueSum = 0
for newPixel in getPixels(newPic):
newRed = getRed(newPixel)
@gtke
gtke / DisjointSets.java
Created April 1, 2013 05:54
Disjoint Sets Class
import java.util.HashMap;
import java.util.Set;
public class DisjointSets<T> {
private HashMap<T,Node> map;
/**
* @param setItems
* the initial items (sameSet and merge will never be called with
@gtke
gtke / MST.java
Last active December 15, 2015 15:39
Kruskal's Algorithm for finding Minimum Spanning Tree
import java.util.ArrayList;
import java.util.Collection;
import java.util.PriorityQueue;
public class MST {
/**
* Run Kruskal's algorithm on the given graph and return the MST, return
* null if no MST exists for the graph
*
@gtke
gtke / Graph.java
Created April 1, 2013 05:57
Graph class to build a graph
import java.util.Collection;
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;
public class Graph {
private Collection<Edge> edges = new HashSet<Edge>();
private Set<Vertex> vertices = new HashSet<Vertex>();
@gtke
gtke / Vertex.java
Created April 1, 2013 05:57
Vertex class for a graph
public class Vertex {
private int id;
/**
* A simple vertex class
*
* @param id
*/
public Vertex(int id) {
@gtke
gtke / Edge.java
Created April 1, 2013 05:58
Edge class for a graph
public class Edge implements Comparable<Edge> {
private Vertex u, v;
private int weight;
/**
* Comparable edge class based on weight. Order of u and v does not matter.
*
* @param u
* @param v