Presentation is loading. Please wait.

Presentation is loading. Please wait.

מבנה נתונים או טיפוס נתונים מופשט חלק ב – פעולות על רשימה הוכן ע " י ולרי פקר דצמבר 2015.

Similar presentations


Presentation on theme: "מבנה נתונים או טיפוס נתונים מופשט חלק ב – פעולות על רשימה הוכן ע " י ולרי פקר דצמבר 2015."— Presentation transcript:

1 מבנה נתונים או טיפוס נתונים מופשט חלק ב – פעולות על רשימה הוכן ע " י ולרי פקר דצמבר 2015

2 " מקום ברשימה " הפניה לחוליה ─ חושף ייצוג ומימוש ( אינה טיפוס נתונים מופשט ) הכנסה בסדר גודל O(1) הוצאה בסדר גודל O(1) ( ברשימה דו - כוונית ) תנועה קדימה בסדר גודל O(1) ( גם אחורה ברשימה דו - כוונית ) גישה למקום ברשימה בסדר גודל O(1) ביצוע הכנסה, הוצאה, תנועה וגישה ברשימה שבה מקום = הפניה לחוליה יעיל ביצוע הכנסה, הוצאה, תנועה וגישה ברשימה שבה מקום = הפניה לחוליה יעיל 1 הפניה לחוליה

3 הפניה לחוליה מחלקת הרשימה פותרת חלק מהבעיות : יש רשימה ריקה יש הכנסה והוצאה במקום הראשון ביצוע הכנסה, הוצאה, תנועה וגישה ברשימה שבה מקום = הפניה לחוליה יעיל אבל – דורשת שימוש במחלקה Node לפעילות שוטפת – אינה טיפוס נתונים מופשט לכן, ניתנת לקלקול – הפתרון לא מקובל !! " מקום ברשימה " 1 הפניה לחוליה

4 " מקום ברשימה " 2 הפניה לחוליה משופרת לחוליה המשופרת יש שני מצבים : חוליה במצב בידוד חוליה בתוך רשימה נמלה בודדת נמלים הן חרקים חברותיים

5 נמלה בודדת toList = null next = null prev = null first null 4 last count prevnext toList toList != null next != null prev = null toList != null next != null prev != null toList != null next != null prev != null toList != null next = null prev != null " מקום ברשימה " 2 הפניה לחוליה משופרת

6 כל חוליה שייכת לרשימה. הרשימה מורכבת מהחוליות. חוליות ברשימה הן לא חוליות בודדות. הרשימה נעזרת בחוליות שאינן בודדות. first null 4 last count " מקום ברשימה " 2 הפניה לחוליה משופרת

7 prevnext value toList public class Node { internal T value; internal Node next; internal Node prev; internal ListLinked toList; public Node(T value) { this.value = value; this.next = null; this.prev = null; this.toList = null; } toList = null next = null prev = null " מקום ברשימה " 2 הפניה לחוליה משופרת הרשאה internal: מותרת גישה מכל מקום בתוך האסמבלי של הקובץ EXE, DLL וכו'. הרשאה internal: מותרת גישה מכל מקום בתוך האסמבלי של הקובץ EXE, DLL וכו'.

8 public T GetValue() { return this.value; } public void SetValue(T value){ this.value = value; } public ListLinked GetList(){ return this.toList; } אין פעולות: SetNext, SetToList ו-SetPrevious אין פעולות: SetNext, SetToList ו-SetPrevious " מקום ברשימה " 2 הפניה לחוליה משופרת prevnext value toList toList = null next = null prev = null

9 public class ListLinked { private Node first; private Node last; private int count; public ListLinked() { this.first = null; this.last = null; this.count = 0; }  } public class Node { internal T value; internal Node next; internal Node prev; internal ListLinked toList; public Node(T value) { this.value = value; this.next = null; this.prev = null; this.toList = null; }  } שתי המחלקות נמצאות באותו מרחב השמות!!! באותו הקובץ DLL שתי המחלקות נמצאות באותו מרחב השמות!!! באותו הקובץ DLL " מקום ברשימה " 2 הפניה לחוליה משופרת

10 List - Node first - Node last - int count Node ~ T value ~ Node next ~ Node prev ~ List toList " מקום ברשימה " 2 הפניה לחוליה משופרת ניתן להתייחס אל המחלקה Node כאל מחלקת עזר, שהשימוש המתוכנן בה נעשה על ידי המחלקה List ואין שום כוונה לנצל אותה מעבר לכך. בתוך המחלקה List עצמה ניתן לפנות לתכונות פנימיות של המחלקה Node שהוגדרו כ -internal. התכונות next, prev ו -toList של המחלקה Node מוסתרות כלפי חוץ ואין פעולות שמאפשרות לשנות אותן. התכונות next, prev ו -toList מוכרות בתוך המחלקה List וניתן לשנות אותן בצורה מבוקרת.

11 " מקום ברשימה " 2 הפניה לחוליה משופרת פעולת הכנסה פעולה המכניסה לרשימה חוליה q רק במצב בודד אחרי המקום ברשימה p. אחרי הכנסת החוליה, היא הופכת לחלק מהרשימה. p first null 5 last count q p first null 4 last count null q

12 " מקום ברשימה " 2 הפניה לחוליה משופרת פעולת הוצאה פעולה המוציאה מן הרשימה את החוליה הנמצאת בה במקום p. הפעולה מבודדת את החוליה p. first null 4 last count null p p first null 5 last count

13 " מקום ברשימה " 2 הפניה לחוליה משופרת פעולת הוצאה ופעולת הכנסה את הצלחת או אי הצלחת הפעולות אפשר לבדוק בעזרת כמות החוליות הנמצאות ברשימה לפני ואחרי הפעולה. p first null 5 last count q p first null 4 last count null q

14 first null 5 last count q p first null 4 last count (p.GetList() == q.GetList()) " מקום ברשימה " 2 הפניה לחוליה משופרת המערכת מאפשרת לזהות חוליה ידידותית, זרה או בודדת.

15 מחלקת הרשימה היא טיפוס נתונים מופשט. יש רשימה ריקה יש הכנסה והוצאה במקום הראשון ביצוע הכנסה, הוצאה, תנועה וגישה ברשימה שבה מקום = הפניה לחוליה יעיל אבל – דורשת שימוש במחלקה Node לפעילות שוטפת. – מימוש המחלקה Node דורש הרשאת הגישה internal. – המחלקה Node ניתנת לשימוש רק כחלק אינטגראלי של המחלקה רשימה !! – מימוש בשפת Java? " מקום ברשימה " 2 הפניה לחוליה משופרת

16 16 1 2 3 4 5 6 7 8 ייצוג ומימוש מוסתרים, אבל for (int i=0; i< lst.GetSize(); i++) {.......... lst.GetValue(i)..... } lst.GetSize() = n 1+2+3+.....+n = (1+n)*n/2 O(n 2 ) אי-אפשר לשפר יעילות, כי זה יישום נפוץ O(n) ? " מקום ברשימה " 3 אינדקס מספרי T GetValue(int at) void SetValue(int at,T x)

17 " מקום ברשימה " 3 אינדקס מספרי List sortedLst = new List (); int x, y, i, j; for (i=0; i< lst.GetSize(); i++) { x = lst.GetValue(i); for (j = 0; j < i - 1; j++){ y = sortedLst.GetValue(j); if (y >= x) break; } sortedLst.Insert(j, x); } lst.GetSize() = n 2(1+2+3+  +n) + (n + 2(n-1) + 3(n-2) +  n(n - (n -1)) = = 2(1+2+3+  +n) + (n + 2n + 3n +  +n 2 ) – (1 + 2 + 3 + 4 +  + n – 1) = = (1 + 2 + 3 +  + n) + n(1 + 2 + 3 +  + n) = O(n 3 ) O(n 2 ) ?

18 " מקום ברשימה " אינדקס מספרי הייצוג והמימוש מוסתרים ( טיפוס נתונים מופשט ) ─ הכנסה בסדר גודל O(n) ─ הוצאה בסדר גודל O(n) ─ תנועה קדימה בסדר גודל O(n) ─ גישה למקום ברשימה בסדר גודל (n)O ─ סריקה בסדר גודל O(n) ─ מיון בסדר גודל O(n 3 ) ביצוע הכנסה, הוצאה, תנועה וגישה, סריקה מיון ברשימה שבה מקום = אינדקס מספרי לא יעיל ביצוע הכנסה, הוצאה, תנועה וגישה, סריקה מיון ברשימה שבה מקום = אינדקס מספרי לא יעיל 3 אינדקס מספרי

19 19 public class List { private Node first; public List(){ this.first = null; } public List(T value){ this.first = new Node(value, null); } public GetFirst(){ return this.first; }... } מקום הוא אינדקס מספרי הרשימה היא טיפוס נתונים מופשט עוטפת את המחלקה Node מקום ברשימה הוא אינדקס " מקום ברשימה " 3 אינדקס מספרי

20 20 1 2 3 4 5 6 7 8 ייצוג ומימוש מוסתרים while (lst.MoveNext()) {.......... lst.GetValue()..... } O(n) " מקום ברשימה " 3 מקום נוכחי ברשימה T GetValue( ) void SetValue(T x)

21 " מקום ברשימה " מקום הוא הפניה נוכחית ייצוג ומימוש מוסתרים ( טיפוס נתונים מופשט ) הכנסה בסדר גודל O(1) הוצאה בסדר גודל O(1) ( ברשימה דו - כוונית ) תנועה קדימה בסדר גודל O(1) ( גם אחורה ברשימה דו - כוונית ) גישה למקום נוכחי ברשימה בסדר גודל O(1) ביצוע הכנסה, הוצאה, תנועה וגישה ברשימה שבה מקום = מקום נוכחי ברשימה יעיל ביצוע הכנסה, הוצאה, תנועה וגישה ברשימה שבה מקום = מקום נוכחי ברשימה יעיל 4 מקום נוכחי ברשימה

22 22 public class List { private Node first; private Node currPos; public List(){ this.first = null; this.currPos = null; } public List(T value){ this.first = new Node(value, null); this.currPos = null; }... } מקום הוא הפניה נוכחית הרשימה היא טיפוס נתונים מופשט עוטפת את המחלקה Node מקום ברשימה הוא מקום נוכחי " מקום ברשימה " 4 מקום נוכחי ברשימה

23 " מקום ברשימה " מקום הוא הפניה נוכחית ייצוג ומימוש מוסתרים ( טיפוס נתונים מופשט ) הכנסה בסדר גודל O(1) הוצאה בסדר גודל O(1) ( ברשימה דו - כוונית ) תנועה קדימה בסדר גודל O(1) ( גם אחורה ברשימה דו - כוונית ) גישה למקום ברשימה בסדר גודל O(1) ─ אפשר להחזיק רק מקום נוכחי אחד בלבד. 4 מקום נוכחי ברשימה

24 Enumerator 24 " מקום ברשימה " 5 דרך אלטרנטיבית מאפשר לבצע כמה סריקות במקביל על הרשימה מאפשר לבצע כמה סריקות במקביל על הרשימה

25 25 enumC 3 enumB enumA 1 2 3 4 5 6 7 8 ייצוג ומימוש מוסתרים enum = lst.GetEnumerator(); while (enum.MoveNext()) {.......... enum.GetValue()..... } O(n) 1 2 3 4 5 1 2 13 4 5 6 8 7 2 " מקום ברשימה " 5 Enumerator

26 ListEnumerator currentPos lst 26 " מקום ברשימה " 5 Enumerator ListEnumerator currentPos lst first null value

27 פעולה שבה מוחזר ערך בעזרת האופרטור yield, נקראת פעולה מחוללת Enumerator. קריאה אליה מחזירה עצם המשמש Enumerator. בקריאה ראשונה לעצם מוחזר הערך, שמופיע בהוראה שמחזירה ערך, בעזרת האופרטור yield. ובכל קריאה נוספת הפעולה ממשיכה מהמקום בו היא הפסיקה בקריאה הקודמת, על ערכים מוחזרים !! אופרטור yield 5 Enumerator מאפשר לבצע סריקות על ערכים המחזירים מאפשר לבצע סריקות על ערכים המחזירים

28 אובייקט זה יעבור על אוסף הנתונים פריט אחר פריט, לפי סדר מסוים. ב -C# מוגדר ממשק לעבודה עם אובייקט כזה : 28 " מקום ברשימה " 5 Enumerator IEnumerator

29 public interface IEnumerator : IDisposable, IEnumerator { /// Advances the enumerator to the next /// element of the collection. /// true if the enumerator was successfully ///advanced to the next element; false if the enumerator /// has passed the end of the collection. bool MoveNext(); /// Return element in the collection at the /// current position of the enumerator. /// element in the collection T IEnumerator.Current; /// Sets the enumerator to its initial position, /// which is before the first element in the collection. /// void Reset(); } " מקום ברשימה "

30 5 Enumerator שימוש יהיה משותף לאוספים רבים, למשל לרשימה. ב -C# מוגדר ממשק IEnumerable. כל אוסף שיממש את הממשק הוא אוסף ניתן למעבר פריט - אחר - פריט, ובאנגלית : Enumerable. public interface IEnumerable : IEnumerable { /// /// Returns an Enumerator over a set of elements. /// /// IEnumerator IEnumerator GetEnumerator(); }

31 עכשיו כל מה שנותר לעשות הוא לממש את המחלקה ListEnumerator. המחלקה ListEnumerator מממשת את הממשק IEnumerator. public class List : IEnumerable { private Node first ; public List (){ this. first = null; } … // כל שאר הפעולות יישארו ללא שינוי public IEnumerator GetEnumerator(){ return new ListEnumerator (this); } 31 " מקום ברשימה " 5 Enumerator

32 32 public interface Iterable { /** * Returns an iterator over a set of elements. * * @return an Iterator. */ Iterator iterator(); } " מקום ברשימה בשפת Java" Iterator

33 public interface Iterator { /** * Returns true if the iteration has more elements. * @return true if the iterator has more elements. */ boolean hasNext(); /** * Returns the next element in the iteration. * @return the next element in the iteration. */ Object next(); /** * Removes from the underlying collection the last element returned by the * iterator (optional operation) * @exception UnsupportedOperationException if the "remove" * operation is not supported by this Iterator. * * @exception IllegalStateException if the "next" method has not * yet been called, or the "remove" method has already * been called after the last call to the "next" * method. */ void remove(); } 33 " מקום ברשימה בשפת Java" Iterator

34 רשימה C# – רשימה ממומשת על - ידי מערך פנימי. – אפשר להתייחס למקום ברשימה כאינדקס לא דומה ל -Java. – בעת הוספת פריט לרשימה, המערך הפנימי שלה, המנוהל על ידי רשימה, יגדל במידת צורך כדי להתאים את הפריט החדש. – ממשק מסובך.

35 Constructors for List Constructors for List

36 תכונות

37 פעולות NameDescription AddAdds an object to the end of the List. AddRange Adds the elements of the specified collection to the end of the List. AsReadOnly Returns a read-only IList wrapper for the current collection. BinarySearch(T) Searches the entire sorted List for an element using the default comparer and returns the zero- based index of the element. BinarySearch (T, IComparer ) Searches the entire sorted List for an element using the specified comparer and returns the zero- based index of the element. BinarySearch (Int32, Int32, T, IComparer ) Searches a range of elements in the sorted List for an element using the specified comparer and returns the zero-based index of the element.

38 פעולותClear Removes all elements - makes the list empty Contains Determines whether an element is in the List. ConvertAll ConvertAll Converts the elements in the current List to another type, and returns a list containing the converted elements. CopyTo ( array ) Copies the entire List to a compatible one-dimensional array of T, starting at the beginning of the target array. CopyTo ( array, Int32) Copies the entire List to a compatible one-dimensional array, starting at the specified index of the target array. CopyTo (Int32, array, Int32, Int32) Copies a range of elements from the List to a compatible one-dimensional array, starting at the specified index of the target array. Equals(Object) Determines whether the specified Object is equal to the current Object. (Inherited from Object.) Exists Determines whether the List contains elements that match the conditions defined by the specified predicate.

39 פעולותFind Searches for an element that matches the conditions defined by the specified predicate, and returns the first occurrence within the entire List. FindAll Retrieves all the elements that match the conditions defined by the specified predicate. FindIndex (Predicate ) Searches for an element that matches the conditions defined by the specified predicate, and returns the zero-based index of the first occurrence within the entire List. FindIndex (Int32, Predicate ) Searches for an element that matches the conditions defined by the specified predicate, and returns the zero-based index of the first occurrence within the range of elements in the List that extends from the specified index to the last element.

40 פעולותRemove Removes first occurrence of specific object from List. RemoveAll Removes all the elements that match the conditions defined by the specified predicate. RemoveAt Removes element at the specified index of the List. RemoveRange Removes a range of elements from the List. Reverse ( ) Reverses order of the elements in the entire List. Reverse(Int32, Int32) Reverses order of the elements in the specified range. Sort ( ) Sorts the elements in the entire List using the default comparer. Sort(Comparison ) Sorts the elements in the entire List using the specified System..::. Comparison. Sort(IComparer ) Sorts the elements in the entire List using the specified comparer. Sort(Int32, Int32, IComparer ) Sorts the elements in range of elements in List using the specified comparer.

41 ToArray Copies the elements of the List to a new array. ToString Returns a String that represents the current Object. (Inherited from Object.) TrimExcess Sets the capacity to the actual number of elements in the List, if that number is less than a threshold value. TrueForAll Determines whether every element in the List matches the conditions defined by the specified predicate.פעולות


Download ppt "מבנה נתונים או טיפוס נתונים מופשט חלק ב – פעולות על רשימה הוכן ע " י ולרי פקר דצמבר 2015."

Similar presentations


Ads by Google