64
CE 311 Data Structure and Algorithm Chakkaphong Suthaputchakun [email protected]

CE 311 Data Structure and Algorithm - Bangkok …bucroccs.bu.ac.th/courses/documents/CRCC10/Ch5.pdfค ว (Queue) •ค วเป นโครงสร างขอ ม ลเช

  • Upload
    others

  • View
    5

  • Download
    0

Embed Size (px)

Citation preview

Page 1: CE 311 Data Structure and Algorithm - Bangkok …bucroccs.bu.ac.th/courses/documents/CRCC10/Ch5.pdfค ว (Queue) •ค วเป นโครงสร างขอ ม ลเช

CE 311 Data Structure and Algorithm

Chakkaphong Suthaputchakun

[email protected]

Page 2: CE 311 Data Structure and Algorithm - Bangkok …bucroccs.bu.ac.th/courses/documents/CRCC10/Ch5.pdfค ว (Queue) •ค วเป นโครงสร างขอ ม ลเช

บทท 5

โครงสรางขอมลคว

(Queue)

Page 3: CE 311 Data Structure and Algorithm - Bangkok …bucroccs.bu.ac.th/courses/documents/CRCC10/Ch5.pdfค ว (Queue) •ค วเป นโครงสร างขอ ม ลเช

3

คว (Queue)

• First-In First-Out (FIFO)

Page 4: CE 311 Data Structure and Algorithm - Bangkok …bucroccs.bu.ac.th/courses/documents/CRCC10/Ch5.pdfค ว (Queue) •ค วเป นโครงสร างขอ ม ลเช

คว (Queue)

• ควเปนโครงสรางขอมลเชงเสนทสามารถเพม

ขอมลเฉพาะต าแหนงทเรยกวา Rear และลบขอมลเฉพาะต าแหนงท

เรยกวา Front

• ควเปนโครงสรางขอมลแบบเขากอนออกกอน (First In First

Out : FIFO)

Page 5: CE 311 Data Structure and Algorithm - Bangkok …bucroccs.bu.ac.th/courses/documents/CRCC10/Ch5.pdfค ว (Queue) •ค วเป นโครงสร างขอ ม ลเช

ตวอยางของคว

Elements are inserted at any time, but only item that has been in the queue the longest can be removed at any time

Page 6: CE 311 Data Structure and Algorithm - Bangkok …bucroccs.bu.ac.th/courses/documents/CRCC10/Ch5.pdfค ว (Queue) •ค วเป นโครงสร างขอ ม ลเช

Queue Operation

1. Enqueue

2. Dequeue

3. Queue Front

4. Queue Rear

Page 7: CE 311 Data Structure and Algorithm - Bangkok …bucroccs.bu.ac.th/courses/documents/CRCC10/Ch5.pdfค ว (Queue) •ค วเป นโครงสร างขอ ม ลเช

1. Enqueue

การใสหรอเพมขอมลลงในควทต าแหนง Rear

Page 8: CE 311 Data Structure and Algorithm - Bangkok …bucroccs.bu.ac.th/courses/documents/CRCC10/Ch5.pdfค ว (Queue) •ค วเป นโครงสร างขอ ม ลเช

2. Dequeue

การน าขอมลทต าแหนง Front ออกจากสแตกหรอการลบขอมลทต าแหนง Front

Page 9: CE 311 Data Structure and Algorithm - Bangkok …bucroccs.bu.ac.th/courses/documents/CRCC10/Ch5.pdfค ว (Queue) •ค วเป นโครงสร างขอ ม ลเช

3. Queue Front

การขอดขอมลในควทต าแหนง Front

Page 10: CE 311 Data Structure and Algorithm - Bangkok …bucroccs.bu.ac.th/courses/documents/CRCC10/Ch5.pdfค ว (Queue) •ค วเป นโครงสร างขอ ม ลเช

4. Queue Rear

การขอดขอมลในควทต าแหนง Rear

Page 11: CE 311 Data Structure and Algorithm - Bangkok …bucroccs.bu.ac.th/courses/documents/CRCC10/Ch5.pdfค ว (Queue) •ค วเป นโครงสร างขอ ม ลเช

Queue Implementation

ควเปนโครงสรางขอมลทผใชจะตองสรางขนมาเอง โดยสามารถ

สรางจากโครงสรางขอมลดงตอไปน

1. ลงคลสต

2. อารเรย

Page 12: CE 311 Data Structure and Algorithm - Bangkok …bucroccs.bu.ac.th/courses/documents/CRCC10/Ch5.pdfค ว (Queue) •ค วเป นโครงสร างขอ ม ลเช

1. ลงคลสต

Page 13: CE 311 Data Structure and Algorithm - Bangkok …bucroccs.bu.ac.th/courses/documents/CRCC10/Ch5.pdfค ว (Queue) •ค วเป นโครงสร างขอ ม ลเช
Page 14: CE 311 Data Structure and Algorithm - Bangkok …bucroccs.bu.ac.th/courses/documents/CRCC10/Ch5.pdfค ว (Queue) •ค วเป นโครงสร างขอ ม ลเช

ตวอยางการใชงาน

Page 15: CE 311 Data Structure and Algorithm - Bangkok …bucroccs.bu.ac.th/courses/documents/CRCC10/Ch5.pdfค ว (Queue) •ค วเป นโครงสร างขอ ม ลเช
Page 16: CE 311 Data Structure and Algorithm - Bangkok …bucroccs.bu.ac.th/courses/documents/CRCC10/Ch5.pdfค ว (Queue) •ค วเป นโครงสร างขอ ม ลเช

(1) (2)

(2)

(1)

ตวอยางการ Enqueue

Page 17: CE 311 Data Structure and Algorithm - Bangkok …bucroccs.bu.ac.th/courses/documents/CRCC10/Ch5.pdfค ว (Queue) •ค วเป นโครงสร างขอ ม ลเช

(1) (2)

(1)

ตวอยางการ Dequeue

Page 18: CE 311 Data Structure and Algorithm - Bangkok …bucroccs.bu.ac.th/courses/documents/CRCC10/Ch5.pdfค ว (Queue) •ค วเป นโครงสร างขอ ม ลเช

2. อารเรย

Page 19: CE 311 Data Structure and Algorithm - Bangkok …bucroccs.bu.ac.th/courses/documents/CRCC10/Ch5.pdfค ว (Queue) •ค วเป นโครงสร างขอ ม ลเช

ปญหาทอาจเกดขนขณะใชงาน

เมอมการ Dequeue และ Enqueue อาจจะท าใหพนทดานหลงของอารเรยเตม แตชองวางดานหนาของอารเรยยงวางอย

เมอมการ Dequeue

เมอมการ Enqueue

Page 20: CE 311 Data Structure and Algorithm - Bangkok …bucroccs.bu.ac.th/courses/documents/CRCC10/Ch5.pdfค ว (Queue) •ค วเป นโครงสร างขอ ม ลเช

ใช Circular

Queue

วธการแกปญหา

Page 21: CE 311 Data Structure and Algorithm - Bangkok …bucroccs.bu.ac.th/courses/documents/CRCC10/Ch5.pdfค ว (Queue) •ค วเป นโครงสร างขอ ม ลเช

Circular Queue

Page 22: CE 311 Data Structure and Algorithm - Bangkok …bucroccs.bu.ac.th/courses/documents/CRCC10/Ch5.pdfค ว (Queue) •ค วเป นโครงสร างขอ ม ลเช

22

99 0 1

2

98 Front

Rear

Queue Size = 3

10 20

After EnQueue( 30, Q );

Rear++ 30 3

Page 23: CE 311 Data Structure and Algorithm - Bangkok …bucroccs.bu.ac.th/courses/documents/CRCC10/Ch5.pdfค ว (Queue) •ค วเป นโครงสร างขอ ม ลเช

23

99 0 1

2

98 Front

Rear

Queue Size = 2

10 20

After DeQueue( Q );

Front++ 30 3

Page 24: CE 311 Data Structure and Algorithm - Bangkok …bucroccs.bu.ac.th/courses/documents/CRCC10/Ch5.pdfค ว (Queue) •ค วเป นโครงสร างขอ ม ลเช

24

99 0 1

2

98

Front

Rear

Queue Size = 1

10 20

After DeQueue( Q );

Front++ 30 3

Page 25: CE 311 Data Structure and Algorithm - Bangkok …bucroccs.bu.ac.th/courses/documents/CRCC10/Ch5.pdfค ว (Queue) •ค วเป นโครงสร างขอ ม ลเช

25

99 0 1

2

98

Front

Rear

Queue Size = 0

10 20

After DeQueue( Q );

Front++ 30 3

4

Front อยหลง Rear -> Empty Queue

Page 26: CE 311 Data Structure and Algorithm - Bangkok …bucroccs.bu.ac.th/courses/documents/CRCC10/Ch5.pdfค ว (Queue) •ค วเป นโครงสร างขอ ม ลเช

26

99 0 1

2

98

Front

Rear

Queue Size = 1

10 20

After EnQueue( 40, Q );

Rear++ 30 3

4 40

Page 27: CE 311 Data Structure and Algorithm - Bangkok …bucroccs.bu.ac.th/courses/documents/CRCC10/Ch5.pdfค ว (Queue) •ค วเป นโครงสร างขอ ม ลเช

27

99 0 1

2

98

Front

Rear

Queue Size = 2

10 20

After EnQueue( 50, Q );

Rear++ 30 3

4 40

50

Page 28: CE 311 Data Structure and Algorithm - Bangkok …bucroccs.bu.ac.th/courses/documents/CRCC10/Ch5.pdfค ว (Queue) •ค วเป นโครงสร างขอ ม ลเช

28

99 0 1

2

98

Front

Rear

Queue Size = 2

20

After EnQueue( 50,Q );

rear = 0 ??

10 50

rear = Successor of 99 = 0

Page 29: CE 311 Data Structure and Algorithm - Bangkok …bucroccs.bu.ac.th/courses/documents/CRCC10/Ch5.pdfค ว (Queue) •ค วเป นโครงสร างขอ ม ลเช

29

void EnQueue( ElementType X, Queue Q ) { if ( IsFull(Q) ) Error( "Full Queue" ); else { Q->Size++;

Q->Rear = Succ( Q->Rear , Q ); Q->Array[ Q->Rear ] = X; } }

Page 30: CE 311 Data Structure and Algorithm - Bangkok …bucroccs.bu.ac.th/courses/documents/CRCC10/Ch5.pdfค ว (Queue) •ค วเป นโครงสร างขอ ม ลเช

30

void DeQueue(Queue Q) { if (IsEmpty(Q)) Error("Empty Queue"); else { Q->Size--; Q->Front = Succ( Q->Front,Q ); } }

Page 31: CE 311 Data Structure and Algorithm - Bangkok …bucroccs.bu.ac.th/courses/documents/CRCC10/Ch5.pdfค ว (Queue) •ค วเป นโครงสร างขอ ม ลเช

31

static int Succ(int Value,Queue Q)

{

if (++Value == Q->Capacity)

Value = 0;

/* circular queue: go back to position 0 */

return Value;

}

Page 32: CE 311 Data Structure and Algorithm - Bangkok …bucroccs.bu.ac.th/courses/documents/CRCC10/Ch5.pdfค ว (Queue) •ค วเป นโครงสร างขอ ม ลเช

32

โครงสรางคว

typedef struct

{ int Capacity = MAXQUEUE;

int Size;

int Front;

int Rear;

ElementType * Array;

} QueueRecord , * Queue;

Page 33: CE 311 Data Structure and Algorithm - Bangkok …bucroccs.bu.ac.th/courses/documents/CRCC10/Ch5.pdfค ว (Queue) •ค วเป นโครงสร างขอ ม ลเช

33

โครงสรางคว

ca

pa

city

Fro

nt

Re

ar

Siz

e

Arra

y

10

0 1 Q

Page 34: CE 311 Data Structure and Algorithm - Bangkok …bucroccs.bu.ac.th/courses/documents/CRCC10/Ch5.pdfค ว (Queue) •ค วเป นโครงสร างขอ ม ลเช

34

IsEmpty

int IsEmpty(Queue Q){

return if(Q->Size == 0);

}

Page 35: CE 311 Data Structure and Algorithm - Bangkok …bucroccs.bu.ac.th/courses/documents/CRCC10/Ch5.pdfค ว (Queue) •ค วเป นโครงสร างขอ ม ลเช

35

IsFull

int IsFull(Queue Q){

return if(Q->Size >= MAXQUEUE);}

Page 36: CE 311 Data Structure and Algorithm - Bangkok …bucroccs.bu.ac.th/courses/documents/CRCC10/Ch5.pdfค ว (Queue) •ค วเป นโครงสร างขอ ม ลเช

Priority Queue

Page 37: CE 311 Data Structure and Algorithm - Bangkok …bucroccs.bu.ac.th/courses/documents/CRCC10/Ch5.pdfค ว (Queue) •ค วเป นโครงสร างขอ ม ลเช

37

Priority Queue

• มการจดล าดบความส าคญของขอมลจากมากไปนอย

• การเขาคว

– ขอมลทมาทหลงสามารถลดควได หากมล าดบความส าคญสงกวาสมาชกตวทมากอน

First-in First-out

Page 38: CE 311 Data Structure and Algorithm - Bangkok …bucroccs.bu.ac.th/courses/documents/CRCC10/Ch5.pdfค ว (Queue) •ค วเป นโครงสร างขอ ม ลเช

38

Priority Queue

0 1 2 3 4 5 6 7

Data 101 201 150 108 111 200 102

Priority 1 1 1 2 2 2 3

0 1 2 3 4 5 6 7

Data 101 201 150 108 111 102

Priority 1 1 1 22 22 3

After EnQueue (200 ,priority 2)

• Enqueue(130 , priority 1) ชองทเทาไร ?

front rear

Page 39: CE 311 Data Structure and Algorithm - Bangkok …bucroccs.bu.ac.th/courses/documents/CRCC10/Ch5.pdfค ว (Queue) •ค วเป นโครงสร างขอ ม ลเช

39

Array Implementation

typedef struct

{ int data;

int priority; } ElementType ;

Page 40: CE 311 Data Structure and Algorithm - Bangkok …bucroccs.bu.ac.th/courses/documents/CRCC10/Ch5.pdfค ว (Queue) •ค วเป นโครงสร างขอ ม ลเช

40

ca

pacity

Fro

nt

Rear

Siz

e

Arra

y

10

Array Implementation

0 1 …. 9 Q

ElementType data priority

Page 41: CE 311 Data Structure and Algorithm - Bangkok …bucroccs.bu.ac.th/courses/documents/CRCC10/Ch5.pdfค ว (Queue) •ค วเป นโครงสร างขอ ม ลเช

41

การอางถงสมาชกของ Queue

• Q size = 1;

• Q Array[0]

• Q Array[0].data

• Q Array[0].priority

Page 42: CE 311 Data Structure and Algorithm - Bangkok …bucroccs.bu.ac.th/courses/documents/CRCC10/Ch5.pdfค ว (Queue) •ค วเป นโครงสร างขอ ม ลเช

42

ขนตอนการเขาคว Insert(X , Q)

• พจารณาขอมลจาก ทายคว

• คนหาขอมลทม Priority <= X.Priority

• แทรกขอมล X

0 1 2 3 4 5 6 7

Data 101 201 150 108 111 200 102

Priority 1 1 1 22 22 2 3

แทรก (130,1)

Page 43: CE 311 Data Structure and Algorithm - Bangkok …bucroccs.bu.ac.th/courses/documents/CRCC10/Ch5.pdfค ว (Queue) •ค วเป นโครงสร างขอ ม ลเช

43

Algorithm: Insert( {130,1},Q)

• ให P1 = Rear, P2 = Succ of Rear

0 1 2 3 4 5 6 7

Data 101 201 150 108 111 200 102

Priority 1 1 1 22 22 2 3

Rear Front

P1 P2

Page 44: CE 311 Data Structure and Algorithm - Bangkok …bucroccs.bu.ac.th/courses/documents/CRCC10/Ch5.pdfค ว (Queue) •ค วเป นโครงสร างขอ ม ลเช

44

1. Array[p1].priority > x.priority

2. Array[p2] = Array[p1] (เลอนขอมลไปทางขวาหนงชอง)

3. เลอน P1, P2 มาทางซาย 1 ชอง 4. ท าซ าขอ 1-4 จนกระทง พบชองท Array[p1].priority <= x.priority

หรอ p2 = front

0 1 2 3 4 5 6 7

Data 101 201 150 108 111 200 102

Priority 1 1 1 22 22 2 3

P1 P2

3 2 2 2 1 1 1 Priority

102 200 111 108 150 201 101 Data 7 6 5 4 3 2 1 0

P1 P2

Page 45: CE 311 Data Structure and Algorithm - Bangkok …bucroccs.bu.ac.th/courses/documents/CRCC10/Ch5.pdfค ว (Queue) •ค วเป นโครงสร างขอ ม ลเช

45

• ให Array[p2] = Array[p1] (เลอนขอมลไปทางขวาหนงชอง)

• เลอน P1, P2 มาทางซาย 1 ชอง

• Array[p1].priority > x.priority

0 1 2 3 4 5 6 7

Data 101 201 150 108 111 200 102

Priority 1 1 1 2 2 2 3

P1 P2

P1 P2

3 2 2 2 1 1 1 Priority

102 200 111 108 150 201 101 Data 7 6 5 4 3 2 1 0

Page 46: CE 311 Data Structure and Algorithm - Bangkok …bucroccs.bu.ac.th/courses/documents/CRCC10/Ch5.pdfค ว (Queue) •ค วเป นโครงสร างขอ ม ลเช

46

• ให Array[p2] = Array[p1] (เลอนขอมลไปทางขวาหนงชอง)

• เลอน P1, P2 มาทางซาย 1 ชอง

• Array[p1].priority > x.priority

0 1 2 3 4 5 6 7

Data 101 201 150 108 111 200 102

Priority 1 1 1 2 2 2 3

P1 P2

3 2 2 2 1 1 1 Priority

102 200 111 108 201 101 Data 7 6 5 4 3 2 1 0

150

P1 P2

Page 47: CE 311 Data Structure and Algorithm - Bangkok …bucroccs.bu.ac.th/courses/documents/CRCC10/Ch5.pdfค ว (Queue) •ค วเป นโครงสร างขอ ม ลเช

47

• ให Array[p2] = Array[p1] (เลอนขอมลไปทางขวาหนงชอง)

• เลอน P1, P2 มาทางซาย 1 ชอง

• Array[p1].priority > x.priority

0 1 2 3 4 5 6 7

Data 101 201 150 108 111 200 102

Priority 1 1 1 2 2 2 3

P1 P2

3 2 2 2 1 1 1 Priority

102 200 111 108 150 201 101 Data 7 6 5 4 3 2 1 0

P1 P2

Page 48: CE 311 Data Structure and Algorithm - Bangkok …bucroccs.bu.ac.th/courses/documents/CRCC10/Ch5.pdfค ว (Queue) •ค วเป นโครงสร างขอ ม ลเช

48

• แทรก ขอมลใหม {130,1} ทชอง p2

• Array[p1].priority = x.priority

0 1 2 3 4 5 6 7

Data 101 201 150 108 111 200 102

Priority 1 1 1 2 2 2 3

P1 P2

3 2 2 2 1 1 1 1 Priority

102 200 111 108 130 150 201 101 Data 7 6 5 4 3 2 1 0

P1 P2

Page 49: CE 311 Data Structure and Algorithm - Bangkok …bucroccs.bu.ac.th/courses/documents/CRCC10/Ch5.pdfค ว (Queue) •ค วเป นโครงสร างขอ ม ลเช

49

Function Insert

int Insert(ElementType X,Queue Q)

{ if ( IsFull(Q) ) {printf(“Queue Overflow\n”);

return 0;} else /* ไมเตม Q */

{ int p1,p2; p1 = Q->Rear; p2 = Succ(Q->Rear,Q); /* เลอนขอมลทPriorityสงกวาไปขวามอ */

while (p2!=Q->Front)

{ if (Q->Array[p1].priority >

X.priority)

Q->Array[p2] = Q-> Array[p1] ;

else break;

p1 = Pred(p1,Q);

p2 = Pred(p2,Q);

}

Q->Array[p2] = X ;

Q->Rear = Succ(Q->Rear,Q);

Q->Size++;

Return 1;

} /*จบ else Qไมเตม*/

} /*จบ function */

Page 50: CE 311 Data Structure and Algorithm - Bangkok …bucroccs.bu.ac.th/courses/documents/CRCC10/Ch5.pdfค ว (Queue) •ค วเป นโครงสร างขอ ม ลเช

50

ปญหา

• การใช Array กบ Priority Queue

ตองเลอนขอมลไปทางขวาเพอแทรกขอมลใหม • โดย Queue เรยงตามล าดบ priority

Enqueue ไมเปน

Constant Time

Page 51: CE 311 Data Structure and Algorithm - Bangkok …bucroccs.bu.ac.th/courses/documents/CRCC10/Ch5.pdfค ว (Queue) •ค วเป นโครงสร างขอ ม ลเช

51

วธแกไข

• สรางควเปนจ านวนเทากบระดบความส าคญ

0 1 2 3

ล าดบ 0 100 200 Q[0]

ล าดบ 1 202 150 Q[1]

ล าดบ 2 300 400 500 Q[2]

ล าดบ 3 120 Q[3]

Prior

ity

4 queue ขอมลในแตละควล าดบเทากน

Page 52: CE 311 Data Structure and Algorithm - Bangkok …bucroccs.bu.ac.th/courses/documents/CRCC10/Ch5.pdfค ว (Queue) •ค วเป นโครงสร างขอ ม ลเช

52

• แยกเกบขอมลทมความส าคญตางกนในควตาง ๆ • การ Enqueue. Dequeue ในแตละคว

Q[i] กระท าเชนเดยวกบ ควปกต (Fi-Fo)

Page 53: CE 311 Data Structure and Algorithm - Bangkok …bucroccs.bu.ac.th/courses/documents/CRCC10/Ch5.pdfค ว (Queue) •ค วเป นโครงสร างขอ ม ลเช

53

• ถาตองการเพมขอมล {560,1}

• EnQueue(560, Q[1]);

• การ DeQueue เรมจาก priority 0,1,2,3

Page 54: CE 311 Data Structure and Algorithm - Bangkok …bucroccs.bu.ac.th/courses/documents/CRCC10/Ch5.pdfค ว (Queue) •ค วเป นโครงสร างขอ ม ลเช

54

Priority Queue Liked List Implementation

• คลายกบกรณ Queue ธรรมดา

• การจดล าดบขอมลใน list ตาม priority

– การ Dequeue เหมอนเดม (ลบ node แรกใน PQ )

– การ Enqueue ตอง Find node ทม priority ต ากวา แลวแทรก node ใหมขางหนา (คนจาก Front ไปทางขวา)

Page 55: CE 311 Data Structure and Algorithm - Bangkok …bucroccs.bu.ac.th/courses/documents/CRCC10/Ch5.pdfค ว (Queue) •ค วเป นโครงสร างขอ ม ลเช

55

Priority Queue Liked List Implementation

• ไมจดล าดบขอมลใน List

– การ Enqueue เหมอนใน Queue ธรรมดา

– การ Dequeue ตองคนหา Node แรกทม priority สงทสด แลวจงลบ Node นนออก

Page 56: CE 311 Data Structure and Algorithm - Bangkok …bucroccs.bu.ac.th/courses/documents/CRCC10/Ch5.pdfค ว (Queue) •ค วเป นโครงสร างขอ ม ลเช

Queue Application

1. Categorizing Data

2. Evaluate Prefix

3. Breath-first Traversal

(จะกลาวถงในบท โครงสรางขอมลทรและไบนารทร)

Page 57: CE 311 Data Structure and Algorithm - Bangkok …bucroccs.bu.ac.th/courses/documents/CRCC10/Ch5.pdfค ว (Queue) •ค วเป นโครงสร างขอ ม ลเช

• การจดกลมของขอมลใหม โดยมใหมการเปลยนแปลงล าดบของขอมลเดม

• ตวอยางเชนตองการจดขอมลตอไปนเปน 4 กลม 3 22 12 6 10 34

65 29 9 30 81 4 5 19 20 57 44 99

1. นอยกวา 10

2. 10-19

3. 20-29

4. 30 ขนไป

3 6 9 4 5

12 10 9

22 29 20

34 65 30 81 57 44 99

1. Categorizing Data

Page 58: CE 311 Data Structure and Algorithm - Bangkok …bucroccs.bu.ac.th/courses/documents/CRCC10/Ch5.pdfค ว (Queue) •ค วเป นโครงสร างขอ ม ลเช

วธแกปญหา

• ใช Queue ทงหมด 4 ตว

• อานขอมลทละตว และใสลงใน Queue ทเหมาะสม

• เมอขอมลหมดจงน าขอมลออกจาก Queue

Page 59: CE 311 Data Structure and Algorithm - Bangkok …bucroccs.bu.ac.th/courses/documents/CRCC10/Ch5.pdfค ว (Queue) •ค วเป นโครงสร างขอ ม ลเช

2. Evaluate Prefix

• ใสแตละ Token ลงใน Queue

• ถาเปน Operator และตามดวย Operand อก 2 ตว ให

ค านวณคากอนแลวใสคาทไดลง Queue

• เมอขอมลหมดกใหเรมท าใหมจนกวาจะได

ผลลพธสดทาย

Page 60: CE 311 Data Structure and Algorithm - Bangkok …bucroccs.bu.ac.th/courses/documents/CRCC10/Ch5.pdfค ว (Queue) •ค วเป นโครงสร างขอ ม ลเช

ตวอยาง

จงหาผลลพธของนพจน Prefix ตอไปน

- + * 9 + 2 8 * + 4 8 6 3

Result = 159

- + * 9 10 * 12 6 3

- + 90 72 3

- 162 3

Page 61: CE 311 Data Structure and Algorithm - Bangkok …bucroccs.bu.ac.th/courses/documents/CRCC10/Ch5.pdfค ว (Queue) •ค วเป นโครงสร างขอ ม ลเช

แบบฝกหด

1. จงเปลยนนพจน Infix ตอไปนเปน Prefix

(50 - 2 * (2 + 3) - 5 * 4) * 2

2. จงค านวณหาคาของนพจน Prefix ทไดในขอ 1

Page 62: CE 311 Data Structure and Algorithm - Bangkok …bucroccs.bu.ac.th/courses/documents/CRCC10/Ch5.pdfค ว (Queue) •ค วเป นโครงสร างขอ ม ลเช

Quiz

Page 63: CE 311 Data Structure and Algorithm - Bangkok …bucroccs.bu.ac.th/courses/documents/CRCC10/Ch5.pdfค ว (Queue) •ค วเป นโครงสร างขอ ม ลเช

Quiz

Page 64: CE 311 Data Structure and Algorithm - Bangkok …bucroccs.bu.ac.th/courses/documents/CRCC10/Ch5.pdfค ว (Queue) •ค วเป นโครงสร างขอ ม ลเช

Q & A