Presentation is loading. Please wait.

Presentation is loading. Please wait.

پشته Stack ساختمان داده ها و الگوريتمها. 2 آشنايي مجموعه پويا (Dynamic Set ): مجموعه اي است كه تركيب و تعداد عناصر آن ممكن است در طول زمان اجراي برنامه.

Similar presentations


Presentation on theme: "پشته Stack ساختمان داده ها و الگوريتمها. 2 آشنايي مجموعه پويا (Dynamic Set ): مجموعه اي است كه تركيب و تعداد عناصر آن ممكن است در طول زمان اجراي برنامه."— Presentation transcript:

1 پشته Stack ساختمان داده ها و الگوريتمها

2 2 آشنايي مجموعه پويا (Dynamic Set ): مجموعه اي است كه تركيب و تعداد عناصر آن ممكن است در طول زمان اجراي برنامه تغيير كند. مجموعه هاي پويا كاربردهاي زيادي در پياده سازي انواع الگوريتمهاي محاسباتي، نرم افزارهاي شبيه سازي و خود سيستم‌عامل كامپيوترها دارند. عمليات مختلفي مانند حذف واضافه نمودن اعضا، پيدا كردن يك عضو يا عضو قبل و بعد آن، تعداد اعضا، بزرگترين و كوچكترين عضو و... بر روي مجموعه هاي پويا قابل انجام است پشته، صف، صف اولويت دار و ليستهاي پيوندي ساختمان داده هايي براي نگهداري و مديريت مجموعه هاي پويا هستند.

3 3 پشته Stack هر عنصر جديدي كه به پشته وارد شود در بالاي آن قرار مي گيرد تنها بالاترين عضو پشته مجاز است از آن خارج شود پشته مثل همه ساختمان داده هاي ديگر، محدوديت اندازه دارد و ممكن است پر يا خالي باشد پشته ساختار Last In First Out است (LIFO)

4 4 مثال Stack

5 5 Stack ADT class Stack{ const MAX-SIZE= 100 ; //Max stack size int top = -1 ; // default : stack is empty int members[0... MAX-SIZE-1] void push(int x ) { if (top < MAX-SIZE-1){ top ++ ; members[top]= x ; } else { raise_error() ; }; }

6 6 Stack ADT class Stack{ //…. int pop(){ if ( top > -1 ) { int temp = members[top] ; top -- ; return temp ; }else { raise_error() ; } }

7 7 Stack ADT class Stack{ const MAX-SIZE= 100 ; //Max stack size int top = -1 ; // default : stack is empty int members[0... MAX-SIZE-1] int push(int x ) { …} int pop(){..} int numElements(){ return top ; } bool isEmpty() { return ( top == -1) ; } bool isFull() { return ( top == MAX-SIZE-1) ; } }

8 8 isFull, isEmpty اين دو تابع براي خواندن دو وضعيت ويژه پشته استفاده مي شوند. با استفاده از اين دو تابع مي توان قبل از خواندن يا نوشتن يك عضو وضعيت پشته را بررسي كرده و از بروز خطاي احتمالي جلو گيري كرد: if ( stack.isFull() == 0 ) stack.push(n) if ( stack.isEmpty() == 0 )n = stack.pop()

9 The Interface Stack public interface Stack { public boolean empty(); public Object peek(); public void push(Object theObject); public Object pop(); }

10 تطبيق پرانتزها در عبارت زير پرانتزهاي بازو بسته متناظر را پيدا کنيد: (((a+b)*c+d-e)/(f+g)-(h+j)*(k-l))/(m-n) عبارت داده شده را از راست به چپ ببينيد با يافتن پرانتز باز، محل آن را در پشته يادداشت کنيد با يافتن پرانتز بسته، محل پرانتز باز نظير آن را از پشته بخوانيد

11 مثال (((a+b)*c+d-e)/(f+g)-(h+j)*(k-l))/(m-n) 0 1 2

12 مثال (((a+b)*c+d-e)/(f+g)-(h+j)*(k-l))/(m-n) 0 1 (2,6)(1,13) 15

13 مثال (((a+b)*c+d-e)/(f+g)-(h+j)*(k-l))/(m-n) 0 1 (2,6)(1,13)(15,19) 21

14 مثال (((a+b)*c+d-e)/(f+g)-(h+j)*(k-l))/(m-n) 0 1 (2,6)(1,13)(15,19)(21,25) 27

15 مثال (((a+b)*c+d-e)/(f+g)-(h+j)*(k-l))/(m-n) 0 1 (2,6)(1,13)(15,19)(21,25)(27,31)(0,32) and so on

16 Towers Of Hanoi/Brahma ABC 1 2 3 4 64 gold disks to be moved from tower A to tower C each tower operates as a stack cannot place big disk on top of a smaller one

17 Towers Of Hanoi/Brahma 3-disk Towers Of Hanoi/Brahma A BC 1 2 3

18 Towers Of Hanoi/Brahma 3-disk Towers Of Hanoi/Brahma A BC 1 2 3

19 Towers Of Hanoi/Brahma 3-disk Towers Of Hanoi/Brahma A BC 123

20 Towers Of Hanoi/Brahma 3-disk Towers Of Hanoi/Brahma A BC 12 3

21 Towers Of Hanoi/Brahma 3-disk Towers Of Hanoi/Brahma A BC 12 3

22 Towers Of Hanoi/Brahma 3-disk Towers Of Hanoi/Brahma A BC 123

23 Towers Of Hanoi/Brahma 3-disk Towers Of Hanoi/Brahma A BC 1 2 3

24 Towers Of Hanoi/Brahma 3-disk Towers Of Hanoi/Brahma A BC 1 2 3 7 disk moves

25 Recursive Solution ABC 1 n > 0 gold disks to be moved from A to C using B move top n-1 disks from A to B using C

26 Recursive Solution A BC 1 move top disk from A to C

27 Recursive Solution A BC 1 move top n-1 disks from B to C using A

28 Recursive Solution A BC 1 moves(n) = 0 when n = 0 moves(n) = 2*moves(n-1) + 1 = 2 n -1 when n > 0

29 Towers Of Hanoi/Brahma moves(64) = 1.8 * 10 19 (approximately) Performing 10 9 moves/second, a computer would take about 570 years to complete. At 1 disk move/min, the monks will take about 3.4 * 10 13 years.

30 Method Invocation And Return public void a() { …; b(); …} public void b() { …; c(); …} public void c() { …; d(); …} public void d() { …; e(); …} public void e() { …; c(); …} return address in a() return address in b() return address in c() return address in d() return address in e() return address in c() return address in d()

31 Try-Throw-Catch When you enter a try block, push the address of this block on a stack. When an exception is thrown, pop the try block that is at the top of the stack (if the stack is empty, terminate). If the popped try block has no matching catch block, go back to the preceding step. If the popped try block has a matching catch block, execute the matching catch block.

32 Rat In A Maze

33 Move order is: right, down, left, up Block positions to avoid revisit.

34 Rat In A Maze Move order is: right, down, left, up Block positions to avoid revisit.

35 Rat In A Maze Move backward until we reach a square from which a forward move is possible.

36 Rat In A Maze Move down.

37 Rat In A Maze Move left.

38 Rat In A Maze Move down.

39 Rat In A Maze Move backward until we reach a square from which a forward move is possible.

40 Rat In A Maze Move backward until we reach a square from which a forward move is possible. Move downward.

41 Rat In A Maze Move right. Backtrack.

42 Rat In A Maze Move downward.

43 Rat In A Maze Move right.

44 Rat In A Maze Move one down and then right.

45 Rat In A Maze Move one up and then right.

46 Rat In A Maze Move down to exit and eat cheese. Path from maze entry to current position operates as a stack.

47 پياده سازي Stack public interface Stack { public boolean empty(); public Object peek(); public void push(Object theObject); public Object pop(); }

48 Derive From A Linear List Class پشته همانند يك ليست خطي مجموعه اي از عناصر هم نوع را نگهداري مي كند – ورود و خروج اعضا به اين ليست تنها از انتهاي ليست ممكن است بنابراين، منطقي است كه پشته با ارث بري، از يكي از كلاسهاي پياده سازي كننده ليست خطي مشتق شود ArrayLinearList Chain

49 Derive From ArrayLinearList – stack top is either left end or right end of linear list – empty() => isEmpty() O(1) time – peek() => get(0) or get(size() - 1) O(1) time 0123456 abcde

50 Derive From ArrayLinearList when top is left end of linear list – push(theObject) => add(0, theObject) – O(size) time – pop() => remove(0) – O(size) time 0123456 abcde

51 Derive From ArrayLinearList – when top is right end of linear list push(theObject) => add(size(), theObject) O(1) time pop() => remove(size()-1) O(1) time – use right end of list as top of stack 0123456 abcde

52 Derive From Chain – stack top is either left end or right end of linear list – empty() => isEmpty() O(1) time abcde null firstNode

53 Derive From Chain abcde null firstNode – when top is left end of linear list  peek() => get(0)  O(1) time  push(theObject) => add(0, theObject)  O(1) time  pop() => remove(0)  O(1) time

54 Derive From Chain abcde null firstNode – when top is right end of linear list peek() => get(size() - 1) O(size) time push(theObject) => add(size(), theObject) O(size) time pop() => remove(size()-1) O(size) time – use left end of list as top of stack

55 Derive From ArrayLinearList package dataStructures; import java.util.*; // has stack exception public class DerivedArrayStack extends ArrayLinearList implements Stack { // constructors come here // Stack interface methods come here }

56 Constructors /** create a stack with the given initial * capacity */ public DerivedArrayStack(int initialCapacity) {super(initialCapacity);} /** create a stack with initial capacity 10 */ public DerivedArrayStack() {this(10);}

57 empty() And peek() public boolean empty() {return isEmpty();} public Object peek() { if (empty()) throw new EmptyStackException(); return get(size() - 1) } 0123456 abcde

58 push(theObject) And pop() public void push(Object theElement) {add(size(), theElement);} public Object pop() { if (empty()) throw new EmptyStackException(); return remove(size() - 1); } 0123456 abcde

59 ارزيابي روش پياده سازي ارث بري از ليست – مزاياي استفاده از ArrayLinearList كد نويسي ساده و كم – مديريت كد از جمله توسعه يا اشكال زدايي آن راحتتر است – معايب اين روش تمام متدهاي public كلاس ArrayLinearList‌ بر روي stack‌نيز قابل انجام هستند – با تعريف پشته متناقض است متدهاي اضافه را بايد override كرد براي انجام عمليات مورد نياز، كد اضافي اجرا مي شود – pop() خالي نبودن پشته را بررسي مي كند سپس متد remove را فراخواني مي كند. اين متد نيز خالي نبودن پشته را بررسي مي كند پياده سازي بعنوان كلاس جديد براي تسريع اجراي كد – زمان و هزينه توليد نرم افزار

60 پياده سازي پشته از آرايه يك بعدي با نوع Object استفاده مي كند – متغير top براي نگهداري انتهاي پشته تعريف مي شود – اعضاي پشته در آرايه stack[0:top] قرار مي گيرند – Top element is in stack[top] – Bottom element is in stack[0]. – Stack is empty iff top = -1. – تعداد اعضاي پشته برابر top+1 است

61 پياده سازي پشته package dataStructures; import java.util.EmptyStackException; import utilities.*; // ChangeArrayLength public class ArrayStack implements Stack { // data members int top; // current top of stack Object [] stack; // element array // constructors come here // Stack interface methods come here }

62 Constructors public ArrayStack(int initialCapacity) { if (initialCapacity < 1) throw new IllegalArgumentException ("initialCapacity must be >= 1"); stack = new Object [initialCapacity]; top = -1; } public ArrayStack() {this(10);}

63 push(…) public void push(Object theElement) { // increase array size if necessary if (top == stack.length - 1) stack = ChangeArrayLength.changeLength1D (stack, 2 * stack.length); // put theElement at the top of the stack stack[++top] = theElement; } 01234 abcde top

64 pop() public Object pop() { if (empty()) throw new EmptyStackException(); Object topElement = stack[top]; stack[top--] = null; // enable garbage collection return topElement; } 01234 abcde top

65 java.util.Stack Derives from java.util.Vector. java.util.Vector is an array implementation of a linear list.

66 Performance 500,000 pop, push, and peek operations initial capacity Class 10 500,000 ArrayStack 0.44s 0.22s DerivedArrayStack 0.60s 0.38s DerivedArrayStackWithCatch 0.55s 0.33s java.util.Stack 1.15s - DerivedLinkedStack 3.20s 3.20s LinkedStack 2.96s 2.96s

67 67 تمرين با استفاده از ليست پيوندي، پشته را پياده سازي كنيد تمرينهاي بخش 10.1 را حل كنيد.


Download ppt "پشته Stack ساختمان داده ها و الگوريتمها. 2 آشنايي مجموعه پويا (Dynamic Set ): مجموعه اي است كه تركيب و تعداد عناصر آن ممكن است در طول زمان اجراي برنامه."

Similar presentations


Ads by Google