1 / 164

Data Structures and Algorithm Design (Review)

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.

awolfgang
Download Presentation

Data Structures and Algorithm Design (Review)

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Data Structures and Algorithm Design (Review)

  2. 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

  3. 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 ];

  4. Object-Oriented Design Inheritance Polymorphism method overriding method overloading Keyword: this Exception Interface, Abstract Classes Type casting

  5. Stacks, Queues, and Deques Stacks Queues Deques Singly linked lists Doubly linked lists Sample case study application

  6. 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.

  7. public interface Stack { public void push( Object element ); public Object pop() throws StackEmptyException; public int size(); public boolean isEmpty(); public Object top() throws StackEmptyException; }

  8. 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 ); }

  9. 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 ]; }

  10. public Object pop() throws StackEmptyException { Object elem; if( isEmpty() ) throw new StackEmptyException( "Stack is Empty." ); elem = S[ top ]; S[ top-- ] = null; return elem; } }

  11. 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++; }

  12. 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; } }

  13. 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.

  14. Java Implementation

  15. 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).

  16. 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

  17. 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.

  18. 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

  19. 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

  20. 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

  21. (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

  22. 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

  23. Queues Definition: A queue is a container of objects that are inserted and removed according to the first-in first-out (FIFO) principle.

  24. 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; }

  25. public void enqueue(Object element) throws QueueFullException { if (size()==length-1) throw new QueueFullException(); else { elem[rear] = element; rear = (rear+1)%length; } }

  26. 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); }

  27. 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]; } }

  28. 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++; }

  29. 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); } }

  30. 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.

  31. 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; }

  32. Singly Linked Lists

  33. Class Node

  34. How to generate a singly linked list? class HeadTail { Node head; Node tail; HeadTail(Node x, Node y) { head = x; tail = y; } }

  35. 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(); } }

  36. 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); } }

  37. Doubly Linked List Difference from singly linked lists: - each node contains two links. - two extra nodes: header and trailer, which contain no elements.

  38. Class DLNode

  39. 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:

  40. public interface Deque { void insertFirst(Object e); void insertLast(Object e); Object removeFirst(); Object removeLast(); Object first(); Object last(); int size(); boolean isEmpty(); } 

  41. Class MyDeque

  42. Vectors, Lists, and Sequences Vectors Lists Sequences Iterators

  43. Vector (interface) List (interface) impl. extends impl. extends ArrayVector (class) Sequence (interface) NodeList (class) impl. impl. extends extends ArraySequence (class) NodeSequence (class)

  44. A Simple Array-Based Implementation rank Vector ADT Array index

  45. 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); }

  46. 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]; }

More Related