1 / 112

Data Structures and Algorithm Design (Review)

Data Structures and Algorithm Design (Review). Data Structures and Algorithm Design:. Java basics. … …. Trees and binary trees. Object-oriented design. Stacks, queues, and deques. Vectors, lists and sequences. Java Basics. Class Class Modifiers abstract, final, public

mgonzales
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: Java basics … … 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. Java Basics Recursion public class RecursionExample { public static int function(int n) { if (n==0) return 1; if (n==1) return 1; if (n==2) return 3; else return 3*function(n-3) + 4*function(n-2) + 5*function(n-1); } public static void main(String []args){ int num = Integer.parseInt(args[0]); System.out.println(num); int b = function(num); System.out.println("f =" + " " + b); }}

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

  6. import java.util.*; • import java.lang.*; • interface CanFight { void fight ( );} • interface CanSwim { void swim ( );} • interface CanFly {void fly ( );} • class ActionCharacter { public void fight( ) { }} • class Hero extends ActionCharacter • implements CanFight, CanSwim, CanFly { • public void fight ( ) {System.out.println(“Can fight!”);} • public void swim ( ) {System.out.println(“Can swim!”); } • public void fly ( ) {System.out.println(“Can fly!”);} • }

  7. public class Adventure { • static void t(CanFight x) { x.fight();} • static void u(CanSwim x) { x.swim();} • static void v(CanFly x) { x.fly();} • static void w(ActionCharacter x) { x.fight();} • public static void main (String[ ] args) { • Hero h = new Hero( ); • t(h); //Treat it as a CanFight • u(h); //Treat it as a CanSwim • v(h); //Treat it as a CanFly • w(h); //Treat it as an ActionCharacter • } • }

  8. ActionCharacter CanFight CanSwim CanFly Hero h subclass implementation • static void t(CanFight x) { x.fight();} • static void u(CanSwim x) { x.swim();} • static void v(CanFly x) { x.fly();} • static void w(ActionCharacter x) { x.fight();} instantiation • Hero h = new Hero( ) • t(h); //Treat it as a CanFight • u(h); //Treat it as a CanSwim • v(h); //Treat it as a CanFly • w(h); //Treat it as an ActionCharacter

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

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

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

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

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

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

  15. Sample Case Study Application 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.

  16. Java Implementation

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

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

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

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

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

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

  23. How to calculate 1+2-3+4-5+6+7-8+9? public class ExpressionAlculation { public static void main(String [] args){ String s = "1+2-3+4-5+6+7-8+9"; char c = 0; int result = 0; Object temp; Stack myStack = new ArrayStack(); myStack.push(new Integer(1)); for (int i = 1; i < s.length(); i++){ if (s.charAt(i) == ‘+’ || s.charAt(i) == ‘-’) myStack.push(new Character(s.charAt(i))); else { temp = myStack.pop(); result = ((Integer)myStack.pop()).intValue(); c = s.charAt(i); if (temp == ‘+’) result = result + (c-'0'); else result = result - (c-'0'); myStack.push(new Integer(result));} } System.out.println("Total is " + result); } }

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

  25. 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; this.length = length; }

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

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

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

  29. Queues Application: Search a tree in the breadth-first-manner enqueue(root); while (the queue is not empty) do {x := dequeue; print(x); let x1, x2, …, xk be the children of x; for (i = k to 1) do {enqueue(xi);} }

  30. step1: step4: step3: 1 step2: 4 5 6 7 3 4 5 2 3 visit(3) visit(2) visit(1) step5: 5 6 7 8 9 visit(4) Queues Sample trace: step6: … … 6 7 8 9 10 11 visit(5)

  31. Singly Linked Lists

  32. Class Node

  33. Node x = new Node(); x.setElement(new String(“Baltimore”));

  34. x.setNext(head); head = x;

  35. head = head.getNext();

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

  37. public class GeneratingList { Node head = null; Node tail = null; Public Head_and_Tail linked_list () { Node x = null; for (int i = 0; i < 10; i++) {x = new Node(); x.setElement(new Integer(i)); if (i == 0 ) {x.setNext(null); tail = x;} else x.setNext(head); head = x; } return new Head_and_Tail(head, tail);} }

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

  39. Class DLNode

  40. Have a new node: header trailer Baltimore Rome Seattle Toronto DLNode x = new DLNode(); x.setElement(new String(“Toronto”)); (x.element  new String(“Toronto”))

  41. Update the links: trailer header Baltimore Rome Seattle Toronto x.setPrev(header); x.setNext(header.getNext()); (header.getNext()).setPrev(x); header.setNext(x); x.prev  header; x.next  header.next; header.next.prev  x; header.next  x;

  42. Update the links: trailer header Toronto Baltimore Rome Seattle ((trailer.getPrev()).getPrev).setNext(trailer); trailer.setPrev((trailer.getPrev()).getPrev()); trailer.prev.prev.next  trailer; trailer.prev  trailer.prev.prev;

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

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

  45. Class MyDeque

  46. Implementing Stacks and Queues with Deques

More Related