No Slide Title

Download Report

Transcript No Slide Title

Basic Data Structures
Elementary Structures
• Arrays
• Lists
Sequence Structures
• Stacks
• Queues
Search Structures
• Binary search Tree
• Hash Tables
Graphs
Data Structures
1
What is a data structure?
A data structure (DS) is essentially a device that collects and
allows the manipulation of data within the structure.
•DS = Model (idea) + operations
•A DS is independent of
its implementation.
•In Java, use interface to
specify operations of DS
•The interface describes the
operation, but says nothing
about its implementation.
Example: A Stack
• Model: A Stack
•Operations:
•Add to stack
•Remove from stack
•check for empty
•look into stack
•empty the stack.
Functionality specified using interface
public interface Stack{
public void push(Object o);
public Object pop();
public Object peek();
public boolean isEmpty();
public void makeEmpty();
Data Structures
2
}
Stack Structure
An array implementation of a Stack
max-1
public class StackArray impements Stack{
private Objects[] s;
private int top;
public StackArray(int size){
s = new Object[size];
top = -1;
}
public void push(Object item){ s[++top] = item;}
public Object pop(){return s[top--];}
public Object peek(){return s[top];}
public boolean isEmpty(){return (top==-1);}
top
3
3
2
1
0
public void makeEmpty(){top = -1;}
public boolean isFull(){return (top==s.length-1);}
}
Data Structures
3
Queue Structure
Implementation 1: Front always at array[0]
0 1 23
DS: A Queue
•Idea: A Queue or a line
•Operations:
•Add to the Queue
•Remove from the Queue
•peek at front element
•check for empty
•Make empty
front
back
Implementation 2: With wrap around.
public interface Queue{
public void enQueue(Object x);
public Object deQueue();
public boolean isEmpty();
01
public void makeEmpty();
front
public Object peek();
}
2
back
Can overflow
Data Structures
4
Queue Structure
public class QueueCircularArray implements Queue{
private Object[] q;
private int front, back;
private int usedSpace;
public QueueCircularArray(int size){
q = new Object[size];
front = 0; back = size-1; usedSpace=0;
}
public void enqueue(Object item){
back = (back+1)%q.length;
q[back] = item;
usedSpace++;
}
public Object dequeue(){
Object item = q[front];
q[front] = null;
front = (front+1)%q.length;
usedSpace--;
return item;
}
public boolean isEmpty(){ (usedSpace==0);
}
public boolean isFull() { (usedSpace==q.length);}
public Object peek(){return q[front];}
}
Data Structures
5
What is a Dynamic Structure?
A data structure where there is “no” size limitation.
Characteristics of dynamic structures:
• Max size usually limited by machine memory
• Amount of memory used is a function of data stored in the structure.
• Data access is usually not random
• There is overhead involved in creating and maintaining such a structure
Data Structures
6
Fixed (or static) verses Dynamic structures
Static structures:
Disadvantages:
• Fixed size results in either overflow or under use of internal
resources.
• Resizing is a computationally expensive operation.
Advantages:
• Random/Direct access to data
• Usually easy to develop data structures using static structures
Dynamic Structures:
Disadvantages:
• Lose of random access.
• Usually harder to implement.
Advantages:
• No overflow or under use problems.
• Resizing is usually not expensive.
Data Structures
7
Data Structure - a generic view:
method1()
Implementation
Either static
of Dynamic
method2()
method3()
Interface
method4()
Example:
Queue
…dequeue(…)…
Array or dynamic
implementation
of queue
…enqueue(…)…
Methods of Queue
…isEmpty(…)…
…makeEmpty(…)
Data Structures
8
Dynamic Structures - preliminaries
q
Integer p;
Integer q;
a2
q = new Integer(9); p
a2
?
6
a3
q
a3
9
p = new Integer(5);
p a
1
a2
a1
p = null;
5
p
6
a3
q
p = new Integer(6);
p a
2
a1
p
q=p;
6
q = p;
p a
2
q
9
a2
5
a2
a3
6
a3
q
9
a2
6
a2
Data Structures
9
Dynamic Structures - preliminaries
IntegerNode n1;
public class IntegerNode{
private int item;
private IntegerNode next;
n1 ?
n1 = new IntegerNode(5);
public IntegerNode(int item){
setItem(item);
next =null;
}
public void setItem(int item){
this.item = item;
}
public int getItem(){
return item;
}
public void setNext(InegerNode next){
this.next = next;
}
public IntegerNode getNext(){
return next;
}
a1
n1 a1
item 5
next
IntegerNode n2 =
new IntegerNode(7);
a2
n2 a2
item 7
next
n1.setNext(n2);
a1
n1 a1
}
item 5
next
a2
Data Structures
n2 a2
item 7
next
10
a2
Dynamic Structures - preliminaries
n2 =null;
IntegerNode n3 = n2;
n2 = n2.getNext();
a1
item 5
n1 a1
next
n1 a1
item 7
9
7 a3
5 a2
a2
n2
a3
a2
a1
a2
n3 a2
next
n2 a3
n2.setNext(new IntegerNode(3));
n2 = null;
a1
n2 = n1.getNext();
a1
n1 a1
n3 a2
a2
n2 a2
a1
n1 a1
n2.setNext(new IntegerNode(9));
n1 a1
5 a2
3
n2
n3.setNext(null);
7
a1
a4
9 a4
7 a3
5 a2
5 a2
n1 a1
a3
a2
a2
7 a3
n2 a2
5 a2
a4
a3
a2
3
9 a4
7
a3
9
Data Structures
n3 a2
n2
11
A dynamic Stack
public class DStack implements Stack{
class StackNode{
int data;
StackNode next;
StackNode(int d){data = d; next = null;}
}//inner class
private StackNode top;
private int size;
public DStack(){
Rules regarding Constructors:
ALWAYS initialize instance variables to null.
}
public void push(int d){
Rules regarding adding to dynamic structures:
Consider 2 cases
Case 1: Adding to an empty structure
Case 2: Adding to the front of structure (maybe covered in 1)
Case 2: Inserting to an internal position.
}
Data Structures
12
A dynamic Stack
public int pop(){
Rules regarding removing from a dynamic structure:
Case 1: Deleting a “root” element
Case 2: Deleting an internal element
Data Structures
13
Traversing a Structure
Moving back and fourth along the structure.
Rules regarding traversing a structure:
1. Never move the reference pointing to the root of the structure
2. Check for null before moving.
Traversing the structure below:
a1
front a1
5 a2
7 a3
an
a4
a3
a2
9 a4
3
…
#
temp a1
Plain traversal:
IntegerNode temp = front;
while(temp != null){
temp = temp.getNext();
}
view animation!
Printing the nodes in the structure:
IntegerNode temp = front;
while (temp!=null){
Integer item = temp.getItem();
System.out.println(item.toString());
temp = temp.getNext();
}
Data Structures
14
Dynamic Queue
a1
a4
a3
a2
9 a4
…enQueue(…)…
an
3
…
#
…deQueue(…)…
a1
an
…isEmpty(…)…
front
back
5 a2
7 a3
…makeEmpty(…)
… peek(…)…
Instance variables of class Queue
Data Structures
15
Dynamic Queue
public class DynamicQueue implements Queue{
class ListNode{
//inner class
int data;
ListNode next;
ListNode(int d, ListNode n){
data = d;
next = n;
}
}
private ListNode front; back;
public Queue(){
front = null;
back = null;
}
RULE
Always initialize instance references
Data Structures
16
Dynamic Queue
public class DynamicQueue implements Queue{
class ListNode{
//inner class
int data;
ListNode next;
ListNode(int d){
data = d;
next = null;
}
}
private ListNode front; back;
public void enQueue(Object o){
//Case 1- adding the initial element
ListNode n = new ListNode(o,null);
if (front == null){
//Case 2- adding subsequent elements
Data Structures
17
Dynamic Linked List
a1
5 a2
a4
a3
a2
7 a3
9 a4
an
3
…
…get(…)…
#
…remove(…)…
a1
…add(…)…
front
…search(…)
Data Structures
18
List Structure
List Structure:
public Interface List{
public Object get(int index) throws IndexOutOfBoundsException;
public void remove(int index) throws IndexOutOfBoundsException;
public void add(Object item, int index) throws IndexOutOfBoundsException;
public int search(Object item);
public boolean isEmpty();
public void makeEmpty();
public int size();
}
012
Cannot add/remove/get items
beyond “back”
Can you implement this?
back
Data Structures
19
Dynamic Linked List
public class DList implements List{
class ListNode{
int data;
ListNode next;
ListNode(int o, ListNode n){
data = o;
n = next;
}
}
Observe:
• Inner class
• A single instance
variable, front
• Client’s view same for
both dynamic and static
implementations.
private ListNode front;
public
public
public
public
public
public
public
public
}
List(){ …}
void add(Object o, int pos) throws … {…}
Object get(int pos) throws… {…}
void remove(int pos) throws… {…}
int search(Object o){…}
boolean isEmpty(){…}
int size(){…}
void makeEmpty(){…}
Data Structures
20
Dynamic Linked List
RULE:
Always initialize references to null or other known value
in all dynamic structures.
public class DList implements List{
class ListNode{
int data;
ListNode next;
ListNode(int o, ListNode n){
data = o;
n = next;
}
}
private ListNode front;
public List(){
front = null;
}
…
}
Data Structures
21
Dynamic Linked List
RULE: When making a change to any dynamic structure,
treat changing a node referenced by a named variable as
special case.
public void add(Object o, int pos)
throws IndexOutOfBoundsException {
0. Check for exception case
1. Create a new ListNode with o as the data
2. if inserting at the front of the list, then
//named variable, front, changing!!!
3. Insert the new ListNode in front so as not to
lose the remaining elements
}
4. else{
//front, named variable, does not change
5. traverse to one-before the place to add
6. Insert the new element in so as not to lose the
remaining list.
}
}
Data Structures
22
public class DList implements List{
Dynamic Linked List
…
/**
* This method will add the data object o into position pos
* in the list.
* @param o the data item to be added
* @param pos the position to which o should be added in the list
* @exception IndexOutOfBoundsException will be thrown if (pos<0)
* or (pos > size of the list)
*/
public void add(int o, int pos)
throws IndexOutOfBoundsException{
if (pos<0)||(pos>size()) throw new IndexOutOfBoundsException();
ListNode n = new ListNode(o,null);
//create a new node
if (pos == 0){
//adding to front?
n.next(front);
//add carefully!
front = n;
}
else{
//adding internally
ListNode temp = front;
//traverse
for(int i=0; i<pos-1; i++) temp = temp.next;
n.next = temp.next;
//insert in
temp.next = n;
}
Data Structures
23
}
Dynamic Linked List
RULE: When making a change to any dynamic structure,
treat making a change to a node referenced by a named variable
a special case.
public void remove(int pos)
throws IndexOutOfBoundsException {
0. Check for exception case
1. Remove the first Node?
//Named variable, front, changing!!!
2. Remove so as not to lose the rest of the
list
3. else
4. Traverse to one before the node to remove
5. Detach the node so as not to lose the rest
of the list
}
Data Structures
24
Dynamic Linked List
public class DList implements List{
…
/**
* The purpose of this method is to remove a data object from
* the list at the given position
* @param the item position to be removed
* @exception IndexOutOfBoundsException will be thrown if (pos<0)
* or (pos>=size())
*/
public void remove(int pos)
throws IndexOutOfBoundsException{
if (pos<0)||(pos>=size()) throw new IndexOutOfBoundsException();
ListNode temp;
if (pos == 0){
//remove the first node?
temp = front;
//carefully - so as not to lose
front = front.next;
//the list!
}
else{
//remove an interior node
ListNode n = front;
//traverse
for(int i=0; i<pos-1; i++) n = n.next;
temp = n.next;
n.next = temp.next;
//remove
}
temp.next = null;
}
Data Structures
25
Dynamic Linked List
public class DList …{
…
public int search(int target){
IntegerNode temp = front;
int location = 0;
while (temp!=null){
if (temp.getItem() == target)
return location;
location++;
temp = temp.getNext();
}
return -1;
}
public boolean isEmpty(){
return (front == null);
}
public int size(){
1. traverse the list counting the elements
2. return the count
}
public void makeEmpty(){
front = null;
}
}
Data Structures
26