1 מבוא למדעי המחשב הרצאה 21: Queue, Iterator & Iterable

Preview:

Citation preview

1

מבוא למדעי המחשב

: 21הרצאה Queue, Iterator & Iterable

2

3

תור – מבנה נתונים אבסטרקטי

public interface Queue {

public void enqueue(Object o);

public Object dequeue();

public boolean isEmpty();

}

4

תור – שימושים בעולם התוכנה השימושים של תורים בעולם התוכנה

מזכירים מאוד תורים במציאות:

מקלדת

( שידור סרט באינטרנטYouTube)

( שימוש ברשת לטובת מימוש של טלפוןVoIP)

...ועוד

5

מימוש נאיבי לתור

front = 0 numOfElements = 0

6

מימוש נאיבי לתור

front = 0

enqueue )A(

A

numOfElements = 1

7

מימוש נאיבי לתור

front = 0

enqueue )B(

A B

numOfElements = 2

8

מימוש נאיבי לתור

front = 0

enqueue )C(

A B

numOfElements = 3

C

9

מימוש נאיבי לתור

front = 1

dequeue )(

B

numOfElements = 2

C

10

מימוש נאיבי לתור

front = 1

enqueue )D(

B

numOfElements = 3

C D

11

מימוש נאיבי לתור

front = 2

dequeue )(

numOfElements = 2

C D

12

תור מעגלי בעל קיבולת חסומה

A

BC

0

23

n-1

1front

numOfElements = 3

13

תור מעגלי בעל קיבולת חסומה

BC

0

23

n-1

1

front

numOfElements = 2

dequeue )(

14

תור מעגלי בעל קיבולת חסומה

BC

0

23

n-1

1

front

numOfElements = 3

enqueue )D(

D

15

תור מעגלי בעל קיבולת חסומה

W

X0

1

n-1

front

numOfElements = 4

n-2U

V

תור מעגלי בעל קיבולת חסומהpublic class CircularQueue implements Queue{

private Array arr;private int front, numOfElements, capacity;

public CircularQueue(int capacity){this.capacity = capacity;arr = new FixedSizeArray(capacity);front = 0;numOfElements = 0;

}

תור מעגלי בעל קיבולת חסומהpublic Object dequeue(){

if (isEmpty()){throw new EmptyQueueException();

}

Object res = arr.get(front);arr.set(front, null);front = (front+1) % capacity;numOfElements = numOfElements-1;return res;

}

תור מעגלי בעל קיבולת חסומהpublic void enqueue(Object o){

if (numOfElements == arr.size()){throw new RuntimeException(

"Queue is full!");}

arr.set((front + numOfElements) % capacity, o);

numOfElements = numOfElements+1;}

public boolean isEmpty(){return numOfElements == 0;

}} //class CircularQueue

19

Exceptionיצירת סוג חדש של class EmptyQueueException extends RuntimeException{

public EmptyQueueException(){

super();

}

}//class EmptyQueueException

20

מימוש תור בעזרת מחסניתpublic class QueueAsStack implements Queue{

private Stack stack;

public QueueAsStack () {

stack = new StackAsArray();

}

public boolean isEmpty() {// easy...

return stack.isEmpty();

}

public void enqueue(Object o) {//quit easy as well...

stack.push(o);

}

}

21

מימוש תור בעזרת מחסניתpublic Object dequeue() { // hard work...

if (stack.isEmpty()) throw new EmptyQueueException();

Stack auxStack = new StackAsArray();

while(!stack.isEmpty()) auxStack.push(stack.pop());

Object ret = auxStack.pop();

while(!auxStack.isEmpty()) stack.push(auxStack.pop());

return ret;

}

}//class QueueAsStack

22

ניתוח הפעולות )לתור ולמחסנית(פעולות יעילות

הכנסת מספר קטן של פריטים הוצאת איבר ראשון

פעולות לא יעילות הכנסת מספר גדול של פריטים )תלוי

במימוש המערך(מציאת איבר בעל ערך מינימאלימציאת איבר בעל מפתח מסוים

23

Iterator כיצד ניתן לאפשר בנאי מעתיק של מבנה

שלמדנו?Setהנתונים כיצד ניתן לבצע חיתוך או איחוד בין שתי

קבוצות? ישנו צורך בפונקציונאליות חשובה ברוב

מבני הנתונים שעד כה התעלמנו ממנה – היכולת לעבור על כל האיברים.

24

Iteratorpublic interface Iterator{public boolean hasNext();public Object next();public void remove();

}public interface Iterable {public Iterator iterator();

}

25

נעדכן את ממשק הקבוצה

public interface Set extends Iterable{public void add(Object data);public void remove(Object data);public boolean contains(Object data);public int size();

}

26

נוסיף את השיטה הדרושה במימוש הקבוצה

public class SetAsArray implements Set {

private Array arr;private int size;

// ...

public Iterator iterator() {return new ArrayIterator(arr, size);

}}

27

נגדיר איטרטור עבור מערכיםpublic class ArrayIterator implements Iterator {private Array arr;private int nextIx, size;

public ArrayIterator(Array arr, int size) {this.arr = arr;this.size = size;nextIx = 0;

}

28

נגדיר איטרטור עבור מערכיםpublic boolean hasNext() {

return nextIx < size;}public Object next() {

if (!hasNext()) throw new NoSuchElementException();

nextIx = nextIx+1;return arr.get(nextIx-1);

}public void remove() {

throw new UnsupportedOperationException();}

} //class ArrayIterator

29

נוסיף בנאי מעתיק במימוש הקבוצהpublic class SetAsArray implements Set {

private Array arr;int size;

public SetAsArray(){arr = new DynamicArray();size = 0;

}public SetAsArray(Set toCopy){

this();if (toCopy == null)

throw new NullPointerException("arguemnt to constructor is null");

Iterator iter = toCopy.iterator();while (iter.hasNext()) add(iter.next());

}...}

Recommended