Skip to content

Instantly share code, notes, and snippets.

@chouclee
chouclee / SVD.m
Last active April 21, 2016 18:07
%% Iterative SVD
% Calculate the largetst singular value and it's corrsponding
% left-singular vector and right-singular vector
% Author: chouclee
% Date: 10/01/2014
function [L,w,R] = SVD(S)
% S=[6,8,5,9;
% 8,5,4,6;
% 9,8,7,7;
% 6,7,3,8;
@chouclee
chouclee / findNextNode.java
Created July 14, 2014 16:01
[CC150][4.6] Write an algorithm to find the ‘next’ node (i e , in-order successor) of a given node in a binary search tree where each node has a link to its parent.
// if current node has right subtree, return most left node of the right child.
// else trace back to its parent, if the parent is smaller than current node, then consider parent's parent...
// until parent node is bigger than current node.
public class findNextNode<Key extends Comparable<Key>> {
private Node root;
private class Node {
private Key key;
private Node left, right;
private Node parent;
@chouclee
chouclee / Node.java
Created July 14, 2014 15:57
[CC150][4.5] Implement a function to check if a binary tree is a binary search tree.
public class Node<Key> {
public Key key;
public Node<Key> left, right;
public Node(Key key) {
this.key = key;
}
}
@chouclee
chouclee / BST.java
Created July 3, 2014 07:45
[CC150][4.4] Given a binary search tree, design an algorithm which creates a linked list of all the nodes at each depth (i e , if you have a tree with depth D, you’ll have D linked lists)
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.ListIterator;
public class BST<Key extends Comparable<Key>> {
private Node root;
private class Node {
private Key key;
private Node left, right;
@chouclee
chouclee / BST.java
Created July 3, 2014 06:01
[CC150][4.3] Given a sorted (increasing order) array, write an algorithm to create a binary tree with minimal height.
import java.util.LinkedList;
public class BST<Key extends Comparable<Key>> {
private Node root;
private class Node {
Key key;
Node left, right;
public Node(Key key) {
@chouclee
chouclee / DGraph.java
Created July 3, 2014 05:58
[CC150][4.2] Given a directed graph, design an algorithm to find out whether there is a route between two nodes.
/* Depth First Search */
import java.util.ArrayList;
public class DGraph {
private int V;
private ArrayList<Integer>[] adj;
private boolean[] marked;
@SuppressWarnings("unchecked")
@chouclee
chouclee / BST.java
Created July 3, 2014 05:55
[CC150][4.1/5th e.d.]Implement a function to check if a binary tree is balanced. For the purposes of this question, a balanced tree is defined to be a tree such that the heights of the two subtrees of any node never differ by more than one
public class BST<Key extends Comparable<Key>> {
private Node root;
private boolean isBalanced;
private class Node {
private Key key;
private Node left, right;
public Node(Key key)
@chouclee
chouclee / BST.java
Created July 1, 2014 16:11
[CC150][4.1/4th e.d] Implement a function to check if a tree is balanced For the purposes of this question, a balanced tree is defined to be a tree such that no two leaf nodes differ in distance from the root by more than one.
public class BST<Key extends Comparable<Key>> {
private Node root;
private class Node {
private Key key;
private Node left, right;
public Node(Key key)
{ this.key = key; }
@chouclee
chouclee / stackSort.java
Last active August 29, 2015 14:03
[CC150][3.6] Write a program to sort a stack in ascending order You should not make any assumptions about how the stack is implemented The following are the only functions that should be used to write this program: push | pop | peek | isEmpty
// if stack.size() is not allowed, use insertion sort, but it's an O(n^2) (time) algorithm, space : O(n)
// else, we can use MergeSort. Each merge operation has to put the biggest element into bottom of new stack
// so an extra reverse operation is needed. Time complexity is O(nlogn), space : O(nlgn)
import java.util.Stack;
import java.util.Random;
public class stackSort {
public static<T extends Comparable<? super T>> Stack<T> insertSort(Stack<T> stack) {
Stack<T> sorted = new Stack<T>();
T temp;
@chouclee
chouclee / MyQueue.java
Created June 28, 2014 08:56
[CC150][3.5] Implement a MyQueue class which implements a queue using two stacks
import java.util.Stack;
public class MyQueue<T> {
private Stack<T> pushStack;
private Stack<T> popStack;
public MyQueue() {
pushStack = new Stack<T>();
popStack = new Stack<T>();
}