Professional Documents
Culture Documents
Philip Hwang
March 30, 2016
These are notes on the Data Structures I course taken at the Oklahoma
School of Science and Mathematics. The primary language used is Java. The
course covers various types of data structures such as BSTs, different ADTs,
Hashing etc. The textbook used is...
1 Stacks
A stack is an Abstract Data Type that is what is called a LIFO (Last in First out).
You can push items onto the top of a stack, peek for what is on the top of the stack, and
pop items off the top of the stacks. We may implement a stack with an ArrayList or a
Linked List.
Philip Hwang
Queues
1.3 LinkedList
Same basic idea as ArrayList.
1.4 Array
Same basic idea as before
2 Queues
Queues are similar to stacks, but they are what are called FIFO (First in First Out).
The following is a table of the Run Time Complexity of the various methods for different
implementations of Queues:
Philip Hwang
3
Methods
isEmpty()
isFull()
enQueue()
deQueue()
AL
O(1)
O(1)
O(1)/O(n)
O(n)/O(1)
Trees
LL
O(1)
O(1)
addLast O(1)
removeFirst O(1)
Note how For ArrayList implementation, the enQueue() and deQueue() Run Time
Complexities will essentially be the same regardless of whether we add in the front of the
sequence or the end.
3 Trees
Some terminology
root node
No parent/predessesor
child node
exactly one parent, when in degree is 1
out degree
count of edges that point out of a tree (the number of children)
Philip Hwang
Trees
3.2 Implementation
For doing the recursive methods, we need a separate static class within or BST.
Philip Hwang
Trees
Code 3.2
private static class BinaryNode<E>{
E element;
BinaryNode<E> left, right;
BinaryNode(E e){
element=e;
}
BinaryNode(E e, BinaryNode<E>lc, Binary Node<E>rc){
element=e;
left=lc;
right=rc;
}}
public class BST<E extends Comparable<?super E>>{
private BinaryNode<E> root;
public BST(){
root=null;
}
public void makeEmpty(){
root=null;
}
public boolean isEmpty(){
return root==null;
}
public boolean contains(E e){
return contains(root,e);//check later for the other contains
}
private boolean contains(BinaryNode<E> t, E e){//Note there is a recursive
way of writing this method
if(t==null)
return false;
int result=e.compareTo(t.element);
if(result<0)
return contains(t.left, e);
else if(result>0)
return contains(t.right, e);
else
return true;
}
public E findMin(){//find max would be the same essentially
if(root==null)
return null;
for( ; ;){
if(t.left==null)
return t.element;
t=t.left;
}
Philip Hwang
Hashing
4 Hashing
Hashing is a storage/retrieval data structure that supports inserts/deletes/finds methods
in constant average time.
Differences between BST and Hashing:
Does not support relative order among data.
No findMin, findMax, printSorted methods.
Hashing is working with a set of keys K that maps
K {0, ..., H.size-1}.
data
table
K is an inexhaustible supply of keys and the table has a finite number of locations. In
hash tables, collisions are not avoidable (if location already used). If there are collisions
continue looking until you find a location not taking.
Philip Hwang
Hashing
Code 4.1
int hash(String s, int H.SIZE){
int val=0;
for(int i=0;i<S.length();i++)
val+=s.charAt(i);
return val%H.Size; }
What have we learned so far?
1. Hash function must map the entire table ( 10% gets mapped for the above example)
2. Anagrams of the key map to the same table location.
How do we fix the Anagram problem above? Look at the following
int val=s.charAt(0)*27+s.charAt(1)*(27*27)+s.charAt(2)*(27*27*27);
return val% H.SIZE;
Multiplication is expensive, so we want to make the Hash function simple and fast.
Instead, we can shift the bits with something like 8 << 1 which means shift the bit once
to the left so that the value now is 16.
val=(val<<5)+s.charAt(i);
There are two types of hashing, Open Hashing and Closed Hashing
Philip Hwang
Hashing
Code 4.2
public class OpenHashTable<E>{
private List[] theLists;
private int currentSize;
public OpenHashTable(){
theList=(E[]) new Object [101];
for(int i=0;i<theList.length;i++)
theList[i]= new LinkedList<E>();
}
public boolean contains(E x){
return theList[myHash(x)].contains(x);
}
public void insert(E x){
List<E> whichList=theLists[myHash(x)];
if(!whichList.contains(x)){
whichList.add(x);
currentSize++;
}
if(currrentSize>theLists.length)
reHash();
}
public E remove(E x){
List<E> whichList=theList[myHash(x)];
if(whichList.contains(x)){
whichList.remove(x);
currentSize--;
}}
public int myHash(E e){
int hashVal=x.hashCode();
hashVal=hashVal%theList.length;
if(hashVal<0)
hashVal+=theList.length;
return hashVal;
Philip Hwang
Hashing
1
2 (1
1
1 )
1
I() =
1
2 (1
1
).
(1)2
We have for
We integrate:
Z
0
1
1
1
dx = ln
.
1x
1
This gives the average number of probes encountered for successful find insertions.
There is still clustering, but not as bad as linear, for quadratic. We use double
hashing. Here are the hash functions
Example 4.6 (Double Hashing)
h(x) = x%tableSize + h2 (x), and h2 (x) = R (x%R), where R is the largest prime
below the table size. There is room for play when there is a probe collision. You
could look at, for example, h(x) + h2 (x) + f (i) for f (i) = i; i = 0, 1, 2, ....
For deletion, we can use lazy deletion.
If the number of deleted keys goes above a certain threshold, then we will re-hash.
For too many deletions, create a new table by sequentially taking out the deletions and
inserting the needed values into a new table. For too many insertions, make a larger
table.
Code 4.7 (Inserting)
public void insert(E x){
int currPos=findPos(x);
if(isActive(currPos))
return;
table[currPos]=new HashEntry(x,true);
//rehash?
if(++currSize>table.length/2)
rehash();
}
Philip Hwang
10
Hashing
Philip Hwang
5 Sorting
5.1 Selection Sort
(n 1) + n2
Code 5.1 (Selection Sort)
public static void selectionSort(int[] num){
int min, temp;
for(int index=0; index<num.length; index++){
min=index;
for(int scan=index+1; scan<num.length;scan++)
if(num[scan]<num[index])
min=scan;
//swap
temp=num[min];
num[min]=num[index];
num[index]=temp;
}
11
Sorting
Philip Hwang
6 Merge Sort
Code 6.1 (Merge Sort)
void mergeSort(int a[], int n){
int [] tmpAry=new int[n];
mSort(a, tmpAry, 0, n);
}
12
Merge Sort
Philip Hwang
Heap
7 Heap
Heap Tree
structure property: All nodes are present except the highest level which is filled
from left to right
order property:
(For max heap), parents key is to its childrens.
13
Philip Hwang
14
Heap