A model of computation consisting of a set of states, a start state. An input alphabet, and a transition function that maps input symbols and current states to a next state.
Def: finite state machine|
final int A = 1; final int B = 2; final int C = 3; int i = 0; int state = A; while(state != C){ switch(state){ case A: System.out.print("A"); i++; if(i>=10){state = B;} break; case B: System.out.print("B"); i++; if(i>=20){ state = C; System.out.println("\nExiting...");} break; default: break; } } |
A set of items connected by edges. Each edge called a vertex or a node.
A function which maps keys to integers. Takes input “n” and returns a hash value “h."
public static int hash(String s){More flexible than stacks & queues. Objects stored non-contiguously. Objects can be removed or inserted at any point.
A linear data structure where each item has a link to the next item in addition to its own data. Each node usually has two items, data and a link to another node.
private Node head = null;In a typical list, the list ends in null(or a link to null). The circular list is one in which the end node links to the start or to some other node in the list.
public void loopList(){The basic list node has its data and a link to the next node. A double link list node has a link to the next node and the previous node.
public void addTo(int data){Function that returns the greatest or smallest item. Useful in sorting.
data structure where items are typically inserted from the back and removed from the front.
public class BasicQueueRecursive loops make calls to themselves until a condition is met, as opposed iteration which is a loop that repeats until a condition is met.
Iteration:Backtracking is a search method. Similar to going through a maze, one option is followed until exhausted then we backtrack to the next option and try that one.
Arrange items in a predetermined order
Terms:Repeatedly moving through array and exchanging neighbors that are not in order.
void BubbleSort()Non-stable. Builds a heap and repeatedly extracts the maximum item. A heap is where every node has a key more extreme than or equal to the one of its parents.
void RebuildHeap(int Root, int Limit)Stable. Uses original array to hold the sets of keys. “In-place” sort, by taking the next item repeatedly and inserting it into the final structure with respect to items already sorted.
public void InsertionSort()Stable. Splits arrays into two groups then recursively sorts each group and then merges them into a final sorted sequence.
public void mergeSort(int tempArr[], int bottom, int top)Used for analyzing comparison algorithms. The number of times the major operation occurs, comparison. The worst case is O(n), for too many comparisons and O(1) is the best case, just one comparison. A log of n is much smaller than n, so O(log n) is much smaller than O(n)
Non-stable. Select some key in the array as a pivot key. This is used to separate the array into a right and left partition. Quick sort is then applied to the left and right recursively.
int Partition(int F, int L)Distributes each item into a bucket according to part of the item’s key. A bucket is an area of storage where items with a common property are stored.
Look through list finding the smallest item and moving it to the final location.
int IndexOfLargest(int A[], int Size)A in-place sort which repeatedly reorders small subsets.
public int getexp(int x, int N)An oblivious comparison-based algorithm is such that the sequence of comparisons performed is the same for all inputs of any given size. This kind of algorithm has received much attention since it shown as an implementation of circuits:
Take unsorted keys in an array and insert them into an empty binary tree. Then perform an in-order traversal.
Stacks follow the LIFO model, Last In First Out. Items are inserted at the top of the stack moving other items down in position. Items are removed from the top also and the other items move up in position. The insert function is called PUSH and the removal function is called a POP. In order to access items deep within the stack all the preceding items must be removed. A data structure in which only the most recently added item can be removed. If you need the item at the bottom, everything on the top must be “popped” off first.
| --> |
|
| --> |
|
null if the stack is empty. */null if the stack is empty. */true if the stack is empty,false otherwise. */A data structure made up of child nodes linked to parent nodes.
Terms:A nearly balanced tree where the height of the left and right sub-trees differ no more than one. If more items are inserted, the tree is re-balanced. At each node the height of the two sub tress must differ by at most 1. When insertions/deletions happen, the balance changes. The tree is fixed by rotation
AVL Trees, moreA balanced search tree in which every node has between ceiling(m/2) and m children where m>1 is a fixed integer. A
generalization of 2-3 trees. A “multi-way” tree, designed for disks
Each page has between n & 2n items, except the root which may have as few as 1. Each page has either a leaf page with no
descendants or has m+1 descendants to go with its m terms(n Each node has two children. The children are either empty(null) or are trees themselves, typically left and right. Has a finite set of nodes that are either empty or consist of exactly one root node and at most two sub-trees(right and
left). Each node has at most two children. Each node, except the root node, has exactly one parent node. Once a link has
been followed from a parent to a child, it is not possible to get to the parent by following more links. Children of a node
are trees themselves. Trees with nodes that include keys so that the nodes are sorted with nodes on the left being lesser than those on the
right. Insertion scheme is if n Pre-order: At each node, visit the first node, perform pre-order of left sub-tree then
pre-order the right tree. In-order: At each node, perform in-order of left sub-tree, visit node, then in-order right
tree. Post-order: At each node, perform post-order of left sub-tree, then post order right
tree, then visit node Level-order: Traverse the levels from top to bottom. Within each level, visit nodes
from left to right.
Binary Tree
/ \
C B
/\ /\
n n n n
Binary Search Tree
{
int value; //can be string, char, other ADTs
Tree left;
Tree right;
Tree(int v){
value = v;
left = null;
right = null;
}
Tree(){
left = null;
right = null;
}
public void insert(int v){
//this is the left side of the tree
if(v < value){
//if it’s empty, enter it as a new node
if(left==null){
left = new Tree(v);
//if the node is occupied, insert it again
}else{left.insert(v);}
}
//this is the right side of the tree
else if(v > value){
if(right==null){
right = new Tree(v);
}else{right.insert(v);}
}
}
}
Tree Traversals
System.out.print(value+" ");
if(left != null){
left.preOrder();
}
if(right != null){
right.preOrder();
}
}
if(left != null){
left.inOrder();
}
System.out.print(value+" ");
if(right != null){
right.inOrder();
}
}
if(left != null){
left.postOrder();
}
if(right != null){
right.postOrder();
}
System.out.print(value+" ");
}
Reconstructing Binary Trees From Traversal
Sequences
Other Sources
8 Puzzle
8 Puzzle GUI
AVL Trees
Abstract Data Types
Different Algorithms
DS Examples
DS Intro
DS Programs in C
DS
Programs in Java
Java Examples
Hashing
Hash Table Code
Hash Tables
Huffman Encoding
Huffman Encoding(compression)
Huffman Encoding, more
Huffman Encoding, more
KWIC.java
Linked Lists
Linked Lists, more
Reconstructing Binary Trees From Traversal
Sequences
Sorting Networks and the END search algorithm
Sorting networks, more
Sorting Networks, more
Trees,
more
Trees, more
Books:
Data Structures in Java, Standish
Data Structures and Algorithm Analysis in C, Weiss