Presentation is loading. Please wait.

Presentation is loading. Please wait.

Data Structures and Algorithm Design (Review). Java basics Object-oriented design Stacks, queues, and deques Vectors, lists and sequences Trees and binary.

Similar presentations


Presentation on theme: "Data Structures and Algorithm Design (Review). Java basics Object-oriented design Stacks, queues, and deques Vectors, lists and sequences Trees and binary."— Presentation transcript:

1 Data Structures and Algorithm Design (Review)

2 Java basics Object-oriented design Stacks, queues, and deques Vectors, lists and sequences Trees and binary trees Data Structures and Algorithm Design: Tree traversal Merge sorting Quick sorting Set operations Graphs

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 Stacks Definition: A stack is a container of objects that are inserted and removed according to the last-in first-out (LIFO) principle. A stack S is an abstract data type (ADT) that supports following two fundamental methods: push(o): Insert object o at the top of the stack Input : Object; Output : None. pop(): Remove from the stack and return the top object on the stack; an error occurs if the stack is empty. Input : None; Output : Object

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 s i 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, s i = i – h(i).

16 h(0) h(1) 0 h(2) 1 h(3) 1 h(4) 3 h(5) 1 h(6) 0 s i = i – h(i). s0s0 1 s1s1 1 s2s2 1 s3s3 2 s4s4 1 s5s5 4 s6s6 6

17 The problem is how to compute h(i) efficiently? Step 1: p 0 = 48.97. h(0) = -1, s 0 = 0 - h(0) = 0 – (-1) = 1 0 Day 0. It is possible that h(1) = 0. Step 2: p 1 = 47.54. Pop days with prices less than or equal to p 1. At this point of time, we have only one element in the stack. It is 0 and p 0 > p 1. So h(1) = 0, s 1 = 1 - h(1) = 1 – 0 = 1. Day 1. It is possible that h(2) = 1. 0 1

18 Step 3: p 2 = 45.83. Pop days with prices less than or equal to p 2. At this point of time, we have two elements in the stack. The top one is 1 and p 1 > p 2. So h(2) = 1, s 2 = 2 - h(2) = 2 – 1 = 1. Day 2. It is possible that h(3) = 2. 1 2 0 Step 4: p 3 = 46.34. Pop days with prices less than or equal to p 3. The top one will be taken out since p 3 > p 2. The second one is 1 and p 1 > p 3. So h(3) = 1, s 3 = 3 - h(3) = 3 – 1 = 2. Day 3. It is possible that h(4) = 3. 0 1 3

19 Step 5: p 4 = 45.68. Pop days with prices less than or equal to p 4. The top one is 3 and p 3 > p 4. So h(4) = 3, s 4 = 4 - h(3) = 4 – 3 = 1. Day 4. It is possible that h(5) = 4. 0 1 3 4 Step 6: p 5 = 46.95. Pop days with prices less than or equal to p 3. The top two will be taken out since p 5 > p 4 and p 5 > p 3. The third one is 1 and p 1 > p 5. So h(5) = 1, s 5 = 5 - h(5) = 5 – 1 = 4. Day 5. It is possible that h(6) = 5. 0 1 5

20 Step 7: p 6 = 48.17. Pop days with prices less than or equal to p 3. The top two will be taken out since p 6 > p 5 and p 6 > p 1. The third one is 0 and p 0 > p 6. So h(6) = 0, s 5 = 6 - h(6) = 6 – 0 = 6. Day 6. The price on day 6. The process stops. 0 6

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

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; length = elem.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 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++; }

30 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); }

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

32 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; }

33 Singly Linked Lists

34 Class Node

35

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

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

38 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); }

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

40 Class DLNode

41

42 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:

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

44 Class MyDeque

45 Vectors, Lists, and Sequences Vectors Lists Sequences Iterators

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

47

48 A Simple Array-Based Implementation Vector ADT rank Arrayindex

49 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); } Vectors

50 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]; }

51 public Object elemAtRank (int r) {return a[r];} public int size() {return size;} public boolean isEmpty {return size()==0;} public Object replaceAtRank (int r, Object e) { Object temp=a[r]; a[r]=e; return temp; }

52 /** Inserts an element at the given rank. */ public void insertAtRank(int r, Object e) throws BoundaryViolationException { checkRank(r, size() + 1); if (size == capacity) {// an overflow capacity *= 2; Object[] B = new Object[capacity]; for (int i=0; i<size; i++) B[i] = A[i]; A = B;} for (int i=size-1; i>=r; i--)// shift elements up A[i+1] = A[i]; A[r] = e; size++; }

53 /** Removes the element stored at the given rank. */ public Object removeAtRank(int r) throws BoundaryViolationException { checkRank(r, size()); Object temp = A[r]; for (int i=r; i<size-1; i++)// shift elements down A[i] = A[i+1]; size--; return temp; } public int size( ) {return size;}

54 List: A container of elements that stores each element at a position and that keeps these positions arranged in a linear order. The position abstract data type supports only one simple method: public interface Position { Object element(); } The concept of position is similar to the concept of node in a doubly linked list. Lists

55 Position element(); Dnode element(){…}; getNext(){…}; getPrev(){…}; setNext(){…}; setPrev(){…}; setElement(){…}; impl.

56

57

58 List ADT position Doubly linked list Dnode Doubly Linked List Implementation

59 public interface List { /** Returns the number of elements in this list. */ public int size(); /** Returns whether the list is empty. */ public boolean isEmpty(); /** Returns the first node in the list. */ public Position first(); /** Returns the last node in the list. */ public Position last(); /** Returns the node after a given node in the list. */ public Position next(Position p) throws InvalidPositionException, BoundaryViolationException; /** Returns the node before a given node in the list. */ public Position prev(Position p) throws InvalidPositionException, BoundaryViolationException;

60 /** Inserts an element at the front of the list. */ public Position insertFirst(Object e); /** Inserts and element at the back of the list. */ public Position insertLast(Object e); /** Inserts an element after the given node in the list. */ public Position insertAfter(Position p, Object e) throws InvalidPositionException; /** Inserts an element before the given node in the list. */ public Position insertBefore(Position p, Object e) throws InvalidPositionException; /** Removes a node from the list. */ public Object remove(Position p) throws InvalidPositionException; /** Replaces the element stored at the given node. */ public Object replace(Position p, Object e) throws InvalidPositionException; }

61 Class NodeList

62

63

64

65

66

67

68

69 Sequence

70 In Java, the interface for sequences is an example of multiple inheritance : interface Sequence extends List, Vector { public Position atRank( int rank ) throws BoundaryViolationException; public int rankOf( Position position ) throws InvalidPositionException; } Vector interface List interface Sequence interface

71 Sequence ADT Position rank Doubly linked list NodeatRank(r) rankOf(p) Implementation of a sequence with a doubly linked list:

72 /** Implementation of a sequence by means of a doubly linked list. */ public class NodeSequence extends NodeList implements Sequence { /** Checks whether the given rank is in the range [0, n - 1] */ protected void checkRank(int r, int n) throws BoundaryViolationException { if (r = n) throw new BoundaryViolationException("Illegal rank: " + r); }

73 /** Returns the position containing the element at the given rank; * O(n) time. */ public Position atRank (int rank) { DNode node; checkRank(rank, size()); if (rank <= size()/2) { // scan forward from the head node = header.getNext(); for (int i=0; i < rank; i++) node = node.getNext(); } else { // scan backward from the tail node = trailer.getPrev(); for (int i=1; i < size()-rank; i++) node = node.getPrev();} return node; }

74 /** Gets an element at the given rank.*/ public Object elemAtRank(int r) { return atRank(r).element(); } /** Returns the rank of a given position.*/ public int rankOf(Position p) { DNode node; node = header.getNext(); for for (int i=1; i < size(); i++) { if (p == node) return i; else node = node.getNext();} }

75 /** Inserts an element at the given rank; O(n) time. */ public void insertAtRank (int rank, Object element) throws BoundaryViolationException { checkRank(rank, size() + 1); if (rank == size()) insertLast(element); else { insertBefore(atRank(rank), element); }

76 /** Removes the element stored at the given rank; O(n) time. */ public Object removeAtRank (int rank) throws BoundaryViolationException { checkRank(rank, size()); return remove(atRank(rank)); } public Object replaceAtRank (int rank, object element) throws BoundadryViolationException { checkRank(rank); return replaceElement(atRank(rank), element); }

77 Implementing a Sequence with an Array

78 Iterator

79

80 An implementation of the Iterator is always related to container, i.e., a vector, a list, or a sequence. The following is an exemplary implementation of the List Iterator. public class PositionIterator implements Iterator { protected List list; // the underlying list protected Position cur; // the current (next) position public PositionIterator() { } // default constructor public PositionIterator(List L) { // preferred constructor list = L; if (list.isEmpty()) cur = null; // list is empty else cur = list.first(); // start with the first position }

81 public boolean hasNext() { return (cur != null); } public Object next() throws NoSuchElementException { if (!hasNext()) throw new NoSuchElementException("No next position"); Position toReturn = cur; if (cur == list.last()) cur = null; // no positions left else cur = list.next(cur); // move cursor to the next position return toReturn; } class NoSuchElementException extends Exception { public NoSuchElementException() {super();} public NoSuchElementException(String s) { super(s); } }

82 In a similar way, we can establish an ElementIterator as follows. public class ElementIterator implements Iterator { protected List list; // the underlying list protected Position cur; // the current (next) position protected Object elementCur;// the current (next) element public ElementIterator() { } // default constructor public ElementIterator(List L) { // preferred constructor list = L; if (list.isEmpty()) cur = null; // list is empty else cur = list.first(); // start with the first position }

83 public boolean hasNext() { return (cur != null); } public Object next() throws NoSuchElementException { if (!hasNext()) throw new NoSuchElementException("No next position"); elementCur = cur.element(); if (cur == list.last()) cur = null; // no positions left else cur = list.next(cur); // move cursor to the next position return elementCur; }

84

85 Trees What is a tree? Tree ADT Basic algorithms on trees Tree traversal

86 What is a tree?

87 public interface Tree { public int size(); public Boolean isEmpty(); public ElementIterator elements(); public PositionIterator positions(); public void swapElements( Position v, Position w ); public Object replaceElement( Position v, Object e ); public Position root(); public Position parent( Position v ); public PositionIterator children( Position v ); public boolean isInternal( Position v ); public boolean isExternal( Position v ); public boolean isRoot( Position v ); } Tree Interface – Tree ADT

88 IspectableContainer size isElement Elements IspectablePositionContainer positions PositionContainer swapElement replaceElement InspectableTree root parent children isRoot isInternal isExternal Tree

89 A Binary Tree Interface in Java

90

91 Data Structures for Representing Trees 1. Storing a binary tree in an array 2. Storing a tree as a linked list

92 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

93 Where a node v of tree T is stored can be determined as follows. Let p(v) be the index, where v is stored. The following relationships must be satisfied: If v is the root of T, then p(v) = 1. If v is the left child of the node u, then p(v) = 2p(u) If v is the right child of the node u, then p(v) = 2p(u) + 1.

94

95

96

97

98 abefcgd

99 Class BTNode

100 Interface Hierarchy for Positions Position element(); DNode element(){…}; getNext(){…}; getPrev(){…}; setNext(){…}; setPrev(){…}; setElement(){…}; BTNnode element(){…}; getLeft(){…}; getRight(){…}; setLeft(){…}; setRight(){…}; getParent(){…} setElement(){…};

101 Also see the complete program for “LinkedBinaryTree” posted on the home page of Dr. Yangjun Chen.

102 IspectableContainer size isElement Elements IspectablePositionContainer positions PositionContainer swapElement replaceElement InspectableTree root, parent, children, isRoot isInternal, isExternal Tree InspectableBinaryTree leftChild, rightChild, sibling BinaryTree LinkedBinaryTree … …, replaceElement, swapElement, expandExternal, removeAboveExternal imple.

103 Basic Algorithms on Trees

104

105 Inorder tree traversal Inorder traversal based on recursion:

106 inorder(T, r) if … inorder(T, u) “visit” r If … inorder (T, a) inorder(T, u) if … inorder(T, w) “visit” u If … inorder (T, v) inorder(T, w) if … “visit” w if … 1 26

107 inorder(T, x) if … “visit” x if … 3 inorder(T, v) if … inorder(T, x) “visit” v If … inorder (T, y) 4

108 if … “visit” y if … 5 inorder(T, a) if … inorder(T, b) “visit” a If … inorder (T, c) 8 7 9

109 Inorder traversal based on Stack data structure Algorithm Stack-control-inorder(T, v) establish stack S; S.push(v); while (S is not empty) do {u := S.pop(); if u is leaf or u is marked, visit u; else {let v1 and v2 be the left and right child node of v, respectively; S.push(v2); mark u; S.push(u*); S.push(v1); }

110 u r* a r w u* v r* a u* v r* a print(w) v r* a print(u) x v* y r* a v* y r* a print(x) y r* a print(v) r* a print(y) a print(r) b a* c a* c print(b) c print(a)print(c)

111 Preorder Traversal Algorithm preorder(T, v): perform the “visit” action for node v for each child w of v call preorder(T, w) v w postorder(T,v) postorder(T,w) Preorder traversal based on recursion:

112 Algorithm binaryPreorder(T,v): perform the “visit” action for node v if v is an internal node call binaryPreorder(T, T.leftChild( v )) call binaryPreorder(T, T.rightChild( v ))

113 Preorder traversal based on Stack data structure Algorithm PreorderOnStack(BTree myTree, BNode v) { Establish a Stack myStack; myStack.push(v); While (myStack is not empty) do { u = myStack.pop(); visit(u); if ( u has right child) myStack.push(u.rightchild()); if ( u has left child) myStack.push(u.leftchild());} }

114 Postorder Traversal Postorder traversal based on recursion: Algorithm Postorder(T,v): for each child w of v call postorder(T,w) perform the “visit” action for node v v w postorder(T,v) postorder(T,w)

115 Postorder traversal based on Stack data structure Algorithm PostorderOnStack(T,v): {establish stack D; D.push(D); while (D is not empty) do {u := D.pop(); if u is leaf or marked, then visit u; else { let u 1, …, u k be the children of u; mark u; D.push(u); for (i = k; 0 <= i; i--) D.push(u i ):} }

116 Load a tree from disk into main memory a c g bd efFile: a; b, c, d. b; e, f. e; f; c; g. g; d;

117 a c g bd ef abefcgd public class Node1 {String x; Node2 y; } public class Node2 {Node1 x; Node2 y; }

118 S.push(root, null); While (S is not empty) do {x := S.pop( ); generate a node n for x.node_value; if x.point_to_parent is not null then generate links between n and x.point_to_parent; let x 1, …, x k be the children of x; for i = k to 1 do S.push(x i, n); } node_value Point_to_parent a; b, c, d. b; e, f. e; f; c; g. g; d; stack S:

119 XML File “The Art of Programming” “D. Knuth” “1969” “The Art of Programming” “D. Knuth”“1969”

120 XML File node_value Point_to_node stack S: Read a file into a character array A : “ T h e A r t …

121 XML File Algorithm: Scan array A; If A[i] is ‘<’ and A[i+1] is a character then { generate a node x for A[i..j], where A[j] is ‘>’ directly after A[i]; let y = S.top().pointer_to_node; make x be a child of y; S.push(A[i..j], x); If A[i] is ‘ ‘‘ ’, then { genearte a node x for A[i..j], where A[j] is ‘ ’’ ’ directly after A[i]; let y = S.top().pointer_to_node; make x be a child of y; If A[i] is ‘<’ and A[i+1] is ‘/’, then S.pop();

122 Storing a tree onto disk a df ehbk … a d e 01234567890123456789 … file: node 0 node 1 node 2 i = 0 i = 1 i = 2

123 Storing a tree onto disk a df ehbk … a d e 01234567890123456789 … file: node 0 node 1 node 2 i = 0 i = 1 i = 2 1 2 h 3 … 4

124 Storing a tree onto disk We search a tree in preorder and use a special stack data structure to control the traversal in such a way the parent address can be recorded. data flag to indicate left or right child parent address

125 Storing a tree onto disk Algorithm storing-tree(T, v) establish stack S; i = 0; S.push((v, 0, -1)) while (S in not empty) do { u := S.pop(); store u.Data in address i*3; if (u.Parent-address is not equal to –1) then {if (u.Flag == 0) then j := 0; else j := 1; store i in address (u.Parent-address)*3 + 1 + j;} let u 1, u 2 be the left and right child of u, respectively; S.push((u 2, 1, i)); S.push((u 1, 0, i)); i++; } -1 indicates that the corresponding node is the root.

126 Merge Sort

127

128 The figure here shows how the sequence is divided in the previous example. 6385244517315096 6385244517315096 8524634517315096 8524634517319650

129 The figure here shows how the sequences are merged in the previous example. 6385244517 31 5096 6385 24 4517315096 8524 63 451731 5096 8524634517319650

130

131

132 Quick Sort

133

134

135

136

137

138

139

140 Java implementation (each time choose the middle element as the pivot.) public class Sorter { public static void sort (int[] a, int from, int to) { if ((a == null) || (a.length < 2)) return; int i = from, j = to; int center = a[(from + to)/2]; do { while ((i < to) && (a[i] < center)) i++; while ((j > from) && (a[j] > center)) j--; if (i < j) { int tmp =a[i]; a [i] = a[j]; a[j] = tmp;} i++; j--; }while (i <= j); if (from < j) sort(a, from, j); if (i < to) sort(a, i, to); } }

141 The Set Abstract Data Type

142

143

144

145

146

147

148

149

150

151

152 Graphs and Graph Traversal

153

154

155

156

157

158

159

160 Data Structure for Graphs

161

162

163

164


Download ppt "Data Structures and Algorithm Design (Review). Java basics Object-oriented design Stacks, queues, and deques Vectors, lists and sequences Trees and binary."

Similar presentations


Ads by Google