1.64k likes | 1.65k Views
Data Structures and Algorithm Design (Review). Data Structures and Algorithm Design:. Graphs. Java basics. Quick sorting. Tree traversal. Merge sorting. Set operations. Trees and binary trees. Object-oriented design. Stacks, queues, and deques. Vectors, lists and sequences.
E N D
Data Structures and Algorithm Design (Review)
Data Structures and Algorithm Design: Graphs Java basics Quick sorting Tree traversal Merge sorting Set operations Trees and binary trees Object-oriented design Stacks, queues, and deques Vectors, lists and sequences
Java Basics Class Class Modifiers abstract, final, public Variable Modifiers public, protected, private, static, final Methods Method Modifiers public, protected, private, abstract, final, static Arrays int[] a = new int[ 10 ]; float[][] x = new float[ 8 ][ 10 ]; a[ i ] = 138; x[ i ][ i + 1 ] = 2.189 + x[ i ][ i ];
Object-Oriented Design Inheritance Polymorphism method overriding method overloading Keyword: this Exception Interface, Abstract Classes Type casting
Stacks, Queues, and Deques Stacks Queues Deques Singly linked lists Doubly linked lists Sample case study application
A stack is an abstract data type (ADT) that supports following S two fundamental methods: push(o): Insert object o at the top of the s tack Input Output : Object; : None. pop(): Remove from the stack and return the top object on the stack; an error occurs if the stack is empty. Input Output : None; : Object Stacks Definition: A stack is a container of objects that are inserted and removed according to the last-in first-out (LIFO) principle.
public interface Stack { public void push( Object element ); public Object pop() throws StackEmptyException; public int size(); public boolean isEmpty(); public Object top() throws StackEmptyException; }
public class ArrayStack implements Stack { public static final int CAPACITY = 1000; private in capacity; private Object [] S; private int top = -1; public ArrayStatck() { this( CAPACITY ); } public ArrayStack( int cap ) { capacity = cap; S = new Object[ capacity ]; } public int size() { return ( top + 1 ); }
public boolean isEmpty() { return( top < 0 ); } public void push( Object obj ) throws StackFullException { if( size() == capacity ) throw new StackFullException( "Stack overflow" ); S[ ++top ] = obj; } public Object top() throws StackEmptyException { if( isEmpty() ) throw new StackEmptyException( "Stack is empty." ); return S[ top ]; }
public Object pop() throws StackEmptyException { Object elem; if( isEmpty() ) throw new StackEmptyException( "Stack is Empty." ); elem = S[ top ]; S[ top-- ] = null; return elem; } }
public class NodeStack implements Stack { protected Node top; // reference to the head node protected int size; // number of elements in the stack public NodeStack() { // constructs an empty stack top = null; size = 0; } public int size() { return size; } public boolean isEmpty() { if (top == null) return true; return false; } public void push(Object elem) { Node v = new Node(elem, top);// create and link-in a top = v; //new node size++; }
public Object top() throws EmptyStackException { if (isEmpty()) throw new EmptyStackException("Stack is empty."); return top.getElement(); } public Object pop() throws EmptyStackException { if (isEmpty()) throw new EmptyStackException("Stack is empty."); Object temp = top.getElement(); top = top.getNext(); // link-out the former top node size--; return temp; } }
Sample Case Study Application (1) We want to write a program to calculate the span of the stock’s price on a given day. The span of the stock’s price on a given day: The maximum number of the consecutive days up to the current day such that the stock price on each of those days has been less than or equal to the price on the current day.
Main idea: The span si on a certain day i can be easily computed if we know the closest day preceding day i, such that the price on that day is higher than the price on day i. If such a preceding day exists for a day i, let us denote it with h(i), and otherwise let us define h(i) = -1. Then, si = i – h(i).
h(0) h(1) h(2) h(3) h(4) h(5) h(6) -1 0 1 1 3 1 0 si = i – h(i). s0 s1 s2 s3 s4 s5 s6 1 1 1 2 1 4 6
0 1 0 Step 2: p1 = 47.54. Pop days with prices less than or equal to p1. At this point of time, we have only one element in the stack. It is 0 and p0 > p1. So h(1) = 0, s1 = 1 - h(1) = 1 – 0 = 1. Day 1. It is possible that h(2) = 1. The problem is how to compute h(i) efficiently? Step 1: p0 = 48.97. h(0) = -1, s0 = 0 - h(0) = 0 – (-1) = 1 Day 0. It is possible that h(1) = 0.
Day 3. It is possible that h(4) = 3. 3 1 0 0 Step 4: p3 = 46.34. Pop days with prices less than or equal to p3. The top one will be taken out since p3 > p2. The second one is 1 and p1 > p3. So h(3) = 1, s3 = 3 - h(3) = 3 – 1 = 2. Step 3: p2 = 45.83. Pop days with prices less than or equal to p2. At this point of time, we have two elements in the stack. The top one is 1 and p1 > p2. So h(2) = 1, s2 = 2 - h(2) = 2 – 1 = 1. Day 2. It is possible that h(3) = 2. 2 1
Day 5. It is possible that h(6) = 5. 5 1 0 Step 6: p5 = 46.95. Pop days with prices less than or equal to p3. The top two will be taken out since p5 > p4 and p5 > p3. The third one is 1 and p1 > p5. So h(5) = 1, s5 = 5 - h(5) = 5 – 1 = 4. Step 5: p4 = 45.68. Pop days with prices less than or equal to p4. The top one is 3 and p3 > p4. So h(4) = 3, s4 = 4 - h(3) = 4 – 3 = 1. Day 4. It is possible that h(5) = 4. 4 3 1 0
Step 7: p6 = 48.17. Pop days with prices less than or equal to p3. The top two will be taken out since p6 > p5 and p6 > p1. The third one is 0 and p0 > p6. So h(6) = 0, s5 = 6 - h(6) = 6 – 0 = 6. Day 6. The price on day 6. The process stops. 6 0
(2) Calculate the following expression using ArrayStack to control the computation: “1+2+3-4-5+6-7+8-9”. public class Expression-computation{ //start class public static void main( String args[] ) //start main body { String s = "1+2+3-4-5+6-7+8-9"; Stack data = new ArrayStack(); int temp; char operator; int a = 0; data.push (new Integer (1)); for (int x = 1; x < s.length(); x++) { if (s.charAt(x) == '+‘ || s.charAt(x) == ‘-’) data.push(new Character(s.charAt(x))); else { //else it is a number
operator = (Character) data.pop(); a = ((Integer)data.pop()).intValue(); if (operator == ‘+’) temp = a + charAt(x); else temp = a – charAt(x); data.push(new Integer(temp)); } System.out.println("The answer is: " + ((Integer) data.pop()).intValue()); } // end method main }// end class
Queues Definition: A queue is a container of objects that are inserted and removed according to the first-in first-out (FIFO) principle.
class ArrayQueue implements Queue { private Object[] elem; private int front, rear; private static final int DEFAULT_LENGTH = 100; private int length; public ArrayQueue() { this(DEFAULT_LENGTH); } public ArrayQueue(int length) { elem = new Object[length]; front = rear = 0; length = elem.length; }
public void enqueue(Object element) throws QueueFullException { if (size()==length-1) throw new QueueFullException(); else { elem[rear] = element; rear = (rear+1)%length; } }
public Object dequeue() throws QueueEmptyException { if (isEmpty()) throw new QueueEmptyException(); else { Object temp = elem[front]; elem[front] = null; front = (front+1)%length; return temp; } } private boolean isFull() { return (rear-front)==(length-1); }
public int size() { return (length-front+rear)%length; } public boolean isEmpty() { return front==rear; } public Object front() throws QueueEmptyException { if (isEmpty()) throw new QueueEmptyException(); else return elem[front]; } }
public class ListQueue implements Queue { protected Node front, rear; //reference to the front and rear node protected int size; // number of elements in the queue public ListStack() { // constructs an empty queue front = null; rear = null; size = 0; } public int size() { return size; } public boolean isEmpty() { if (front == null) return true; return false; } public void enqueue(Object elem) { Node v = new Node(elem, null);//create and link-in a new node if (size == 0) {front = v; rear = v;} else {rear.setNext(v); rear = v; size++; }
public Object front() throws QueueEmptyException { if (isEmpty()) throw new QueueEmptyException("Stack is empty."); return front.getElement(); } public Object dequeue() throws QueueEmptyException { if (isEmpty()) throw new QueueEmptyException(“Queue is empty."); Object temp = front.getElement(); front = front.getNext(); // link-out the former front node size--; return temp; } } /** * Runtime exception thrown when one tries to perform operation * front or dequeue on an empty queue. */ public class QueueEmptyException extends RuntimeException { public QueueEmptyException(String err) { super(err); } }
Application case: A breadth-first search traverses a tree as shown in the following Figure. Write an algorithm (not a Java program) to search a tree in the breadth-first manner by using the queue data structure to control the process.
Algorithm: create a Queue Q; put root of the tree into Q; while (Q is not empty) { t Q.dequeue(); if (t’s left child is not a leaf) put t’s left child into Q; if (t’s right child is not a leaf) put t’s right child into Q; visit t; }
How to generate a singly linked list? class HeadTail { Node head; Node tail; HeadTail(Node x, Node y) { head = x; tail = y; } }
public class GeneratingList { public static void main (String[] args) { String [] arr1 = {"Winnipeg","Vancouver","Bejing","Athen“ "London","Berlin","Toronto","Seattle“ "Rome","Baltimore"}; HeadTail a = linkedList(arr1); Node x = a.head; while (x != null) { System.out.println(x.getElement()); x = x.getNext(); } }
public static HeadTail linkedList(String[] b) { Node head = null; Node tail = null; Node x = null; for (int i = 0; i < b.length; i++) {x = new Node(); x.setElement(b[i]); if (i == 0 ) {x.setNext(null); tail = x;} else x.setNext(head); head = x; } return new HeadTail(head, tail); } }
Doubly Linked List Difference from singly linked lists: - each node contains two links. - two extra nodes: header and trailer, which contain no elements.
Deques Definition: A double-ended queue is a queue that supports insertion and deletion at both the front and the rear of the queue. A deque D is an abstract data type that supports the following four fundamental methods:
public interface Deque { void insertFirst(Object e); void insertLast(Object e); Object removeFirst(); Object removeLast(); Object first(); Object last(); int size(); boolean isEmpty(); }
Vectors, Lists, and Sequences Vectors Lists Sequences Iterators
Vector (interface) List (interface) impl. extends impl. extends ArrayVector (class) Sequence (interface) NodeList (class) impl. impl. extends extends ArraySequence (class) NodeSequence (class)
A Simple Array-Based Implementation rank Vector ADT Array index
Vectors public interface Vector { public int size(); public boolean isEmpty(); public Object elemAtRank(int r); public Object replaceAtRank(int r, Object e); public void insertAtRank(int r, Object e); public Object removeAtRank(int r); }
public class ArrayVector implements Vector { private Object[] A; // array storing the elements of the vector private int capacity = 16; // initial length of array A private int size = 0; // number of elements stored in the vector /** Creates the vector with initial capacity 16. */ public ArrayVector() { A = new Object[capacity]; }