Recursion
แนะนำใหม่
15:34
การค้นหา & การเรียงลำดับ
สมุดโทรศัพท์ที่มีชื่อหนึ่งล้านชื่อ หากคุณตรวจสอบทีละชื่อ อาจต้องเปรียบเทียบถึงหนึ่งล้านครั้ง แต่คุณรู้เทคนิคอยู่แล้ว: เปิดมันใน…
บรรยายภาษาอังกฤษ · สลับคำบรรยายภาษาอังกฤษและภาษาจีนได้
ในบทเรียนนี้
| English | ความหมาย |
|---|---|
| recursive/rɪˈkɜːsɪv/ | แบบเรียกซ้ำ |
| loop/luːp/ | A structure that repeats a sequence of instructions. |
| base case/beɪs keɪs/ | กรณีพื้นฐาน |
| recursive case/rɪˈkɜːsɪv keɪs/ | กรณีเรียกซ้ำ |
| recursive call/rɪˈkɜːsɪv kɔːl/ | A function call made by the same function while it is running. |
| trace/treɪs/ | To follow an algorithm step by step and record its values. |
| call stack/kɔːl stæk/ | |
| recursion/rɪˈkɜːʃn/ | The use of a rule inside its own definition to express repetition. |
| function/ˈfʌŋkʃn/ | A mapping that assigns exactly one output to each allowed input. |
| Winding up | The stage when recursive calls are made until a base case is reached. |
| Unwinding | The stage when recursive calls return their results to the calls waiting for them. |
| stack/stæk/ | A last-in, first-out data structure in which items are added and removed at the same end. |
| parameters/pəˈræmɪtəz/ | Values that specify a model or function, or named inputs that a subroutine receives. |
| local variables/ˈləʊkl ˈveərɪəblz/ | A variable whose name is accessible only within a particular function, block or other limited scope. |
| stack frame/stæk freɪm/ | เฟรมสแต็ก |
| return address/rɪˈtɜːn əˈdres/ | The location where execution should resume after a called routine finishes, saved as part of managing the call. |
| pushed | Added to the top of the call stack when a function is called. |
| popped | Removed from the top of the call stack when a function returns. |
| ADT | A description of data and its allowed operations, separate from how it is stored. |
| memory/ˈmeməri/ | The part of a computer that holds data and programs currently in use. |
| stack overflow/stæk ˌəʊvəˈfləʊ/ | สแต็กโอเวอร์โฟลว์ |
| Deep recursion | Even a correct recursion of a million levels needs a million frames, so it can exhaust memory where a loop would use none. |
| Fibonacci | A sequence starting with zero and one, where each later number is the sum of the previous two. |
| memoisation/ˌmeməʊaɪˈzeɪʃn/ | การจำแนกผลลัพธ์ |
| iteration/ˌɪtəˈreɪʃn/ | Repeating a sequence of instructions or calculations. |
| self-similar | Describes a problem that contains a smaller version of itself. |
| tree traversal | The process of visiting each node in a tree in a particular order. |
| divide and conquer/dɪˈvaɪd ænd ˈkɒŋkə/ | A method that divides a problem into smaller problems and combines their solutions. |
| binary search/ˈbaɪnəri sɜːtʃ/ | A search of ordered data that repeatedly compares with the middle item and discards half of the remaining search range. |
| merge sort/mɜːdʒ sɔːt/ | A sorting algorithm that divides data into smaller parts, sorts them, and joins them in order. |
| arithmetic overflow | A result that is too large or small for the available number of bits. |
| infinite recursion | When a function keeps calling itself because it never reaches a stopping point. |
นิยามที่อ้างอิงถึงตัวเอง
- เราจะนิยามว่าบรรพบุรุษคืออะไรได้อย่างไร? พ่อแม่ของคุณคือบรรพบุรุษ ดังนั้นบรรพบุรุษของพ่อแม่คุณก็ถือเป็นบรรพบุรุษเช่นกัน สองประโยคนี้สร้างสายโซ่ที่ไม่จำกัดขอบเขต และประโยคหลังได้นำคำที่กำลังนิยามมาใช้อ้างอิงด้วย
- นี่ไม่ใช่การให้เหตุผลแบบวงกลม เพราะประโยคแรกกำหนดจุดหยุดของสายโซ่นั้นไว้ หากไม่มีจุดหยุดนี้ การนิยามจะวนลูปไปเรื่อยๆ โดยไม่สิ้นสุด
- โปรแกรมสามารถเขียนได้เช่นเดียวกัน และสำหรับปัญหาที่มีลักษณะเป็นสายโซ่แบบนี้ วิธีแก้แบบ recursive จะสั้นกว่าการใช้ loop อย่างมีนัยสำคัญ
- บทเรียนนี้จะกล่าวถึง base case และ recursive case, วิธีการติดตามการเรียกซ้ำ, และสิ่งที่ call stack กำลังทำงานอยู่เบื้องหลัง

สองกรณีหลัก
FUNCTION Factorial(n : INTEGER) RETURNS INTEGER
IF n = 0 OR n = 1 THEN
RETURN 1 // base case
ELSE
RETURN n * Factorial(n - 1) // recursive case
ENDIF
ENDFUNCTION
- Base case คือเวอร์ชันของปัญหาที่มีขนาดเล็กพอที่จะตอบได้โดยตรงโดยไม่ต้องมีการเรียกเพิ่มเติม มันคือสิ่งที่หยุดการเรียกซ้ำ
- Recursive case จะเรียกฟังก์ชันอีกครั้งโดยส่ง อินพุต ที่ เล็กลง เข้าไป เพื่อเคลื่อนเข้าใกล้ base case
- ทั้งสองกรณีจำเป็นต่อการทำงาน การเรียกซ้ำที่ไม่มี base case จะไม่ pernahหยุด; ส่วนกรณีที่มีอินพุตไม่หดตัวลง จะไม่เคยไปถึง base case ได้

Every recursive algorithm must have a base case because:
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
The base case is the condition that ends the chain of calls; without it the recursion runs forever.
Recursion is a natural fit for self-similar problems (trees, divide-and-conquer), but each call adds a stack frame — so without a base case it overflows the stack.
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
A simple counting loop is cleaner for plain iteration; recursion shines when the problem contains smaller copies of itself.
What must a recursive routine have to terminate? Select ทั้งหมด that apply.
ความยาก 2/5 · 140 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
A base case alone is not enough: if the input never shrinks, the base case is never reached and frames pile up until the stack overflows.
ตัวอย่างฝึกปฏิบัติ: ติดตามการเรียกซ้ำ
- ติดตาม
Factorial(4). - การเรียกซ้อนกัน (Winding up):
Factorial(4)ต้องใช้4 * Factorial(3), ซึ่งต้องใช้3 * Factorial(2), ซึ่งต้องใช้2 * Factorial(1)ยังไม่ได้มีการคูณอะไรเลย; ทุกการเรียกกำลังรอคอย - Base case:
Factorial(1)คืนค่า 1 โดยไม่มีการเรียกอะไรเพิ่ม - การจบการทำงาน (Unwinding): ค่า
2 * 1 = 2ถูกส่งกลับมาก่อน ตามด้วย3 * 2 = 6, แล้วตามด้วย4 * 6 = 24 - แสดงทั้งสองทิศทาง การติดตามที่ดูเฉพาะทางลง หรือกลับมาทางเดียวเพียงอย่างเดียว จะเสียคะแนนไปครึ่งหนึ่ง
Recursion unwinds from the leaves up
Step through fib(4) in the order the calls actually finish: the leaves (base cases) resolve first, then each parent combines its children. Notice fib(2) is computed twice — that repeated work is why naive recursion is slow.
What does Factorial(4) return?
ความยาก 2/5 · 140 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
4 × 3 × 2 × 1 = 24.
สิ่งที่เครื่องกำลังทำ
- ทุก call ต้องมี สำเนา ของ parameters และ local variables ของมันเอง เนื่องจาก
Factorial(3)และFactorial(2)เป็น call ที่แตกต่างกันซึ่งมีค่าnที่ไม่เหมือนกัน - สำเนานี้ resides อยู่ภายใน stack frame บน call stack: มีหนึ่ง frame ต่อ call ที่กำลังดำเนินการ เก็บ parameters, locals และ return address ไว้
- Frame จะถูก push เข้าไปใน stack ทุกครั้งที่มีการ call และ pop ออกเมื่อกลับคืน นี่คือเหตุผลว่าทำไมค่าจึงกลับมาในลำดับย้อนกลับจากลำดับที่ถูกเรียก: call stack เป็น stack พอดี ตาม ADT จากหัวข้อ 10
Put the events of evaluating Factorial(4) in order.
ความยาก 3/5 · 200 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
Nothing is multiplied on the way down; every call waits. The multiplications all happen as the stack unwinds.
สิ่งที่เกิดขึ้นผิดพลาด
- ไม่มี base case หรือ base_case ที่ไม่สามารถเข้าถึงได้: การเรียกซ้ำจะไม่หยุด Frames จะสะสมเพิ่มขึ้น และ call stack จะหมดพื้นที่จัดเก็บ ข้อมูลนี้เรียกว่า stack overflow
- Deep recursion: แม้การเรียกซ้ำที่ถูกต้องแต่มีความลึกล้านระดับก็ต้องใช้ frames ล้าน帧 ดังนั้นจึงอาจทำให้หน่วยความจำหมด ในขณะที่การใช้ loop จะไม่ใช้หน่วยความจำเสริมเลย
- งานซ้ำซ้อน: ฟิโบนัตซีแบบเรียกใช้ซ้ำแบบหยาบๆ คำนวณค่าเดิมซ้ำหลายครั้งแบบเอกซ์โพเนนเชียล แก้ไขด้วยลูปหรือใช้ การจำค่า, โดยบันทึกผลลัพธ์แต่ละค่าในครั้งแรกที่คำนวณ

Match each recursion term to what it means.
ความยาก 2/5 · 140 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
A recursion needs a base case to stop and a recursive case to shrink the problem; each call adds a stack frame.
A stack frame for a function call holds:
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
Each frame stores that call's parameters, locals and where to resume — so calls don't trample each other.
Each call in progress keeps its parameters and locals in its own ____ on the call stack.
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
Pushed on call, popped on return. That is why values come back in the reverse of the order the calls were made.
การเลือก Recursive หรือ Iteration
- Recursion เหมาะกับปัญหาที่มีลักษณะ self-similar คือปัญหาที่ประกอบด้วยส่วนย่อยที่เป็นสำเนาของตัวเอง: การ traverses tree, Divide and Conquer เช่น binary search และ merge sort, และสายโซ่บรรพบุรุษข้างต้น
- การวนซ้ำเหมาะสำหรับทุกกรณี และใช้ หน่วยความจำเพิ่มเติม สำหรับการซ้ำ ไม่มี
- ทุกสิ่งที่ใช้การเรียกใช้ซ้ำสามารถเขียนเป็นการวนลูปได้และในทางกลับกันก็จริงเช่นกัน การเลือกขึ้นอยู่กับว่าอะไรสื่อถึงความชัดเจนของปัญหามากกว่า เมื่อเทียบกับต้นทุนหน่วยความจำของสแต็ก

A recursive routine crashes with a stack overflow. Which explanation is correct?
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
Stack overflow is about frames, not arithmetic. A number too large for its register is an arithmetic overflow, a different thing entirely.
ตัวอย่างฝึกปฏิบัติ: ระบุความเสี่ยง
- นักเรียนเขียน routine แบบ recursive แล้วเกิด crash ด้วย stack overflow. ให้สาเหตุที่เป็นไปได้สองประการ
- ไม่มี base case, หรือ base_case ไม่สามารถเข้าถึงได้เนื่องจากอินพุตไม่ลดขนาดลงในทุก call ทำให้ calls ดำเนินต่อไปตลอดและ frames สะสมเพิ่มขึ้น
- การเรียกซ้ำถูกต้องแต่ ลึกเกินไป: ทุก call จำนวนมากยังคง stack frame ของมันเอง ทำให้ call stack หมดพื้นที่จัดเก็บ ก่อนที่จะถึง base case
- สาเหตุทั้งสองเกี่ยวข้องกับ frames ที่สะสมขึ้นมา บอกว่าอะไรสะสมขึ้นและทำไมมันจึงไม่หยุด
คะแนนที่หลุดหายไป
- การเรียกซ้ำต้องการ ทั้ง base case และอินพุตที่ลดขนาดลง การระบุแค่ base case เป็นเพียงครึ่งหนึ่งของเงื่อนไข
- ในการติดตาม (trace), แสดงการเรียกที่ m้วนขึ้น (winding up) และค่าที่ ม้วนลง (unwinding). ทั้งสองทิศทางให้คะแนน
- แต่ละ call มี parameters และ locals ของมันเอง อยู่ใน stack frame ของมันเอง นี่คือเหตุผลว่าทำไม recursion จึงใช้หน่วยความจำในขณะที่ iteration ไม่ได้
- Stack overflow คือการหมดพื้นที่จัดเก็บของ stack จาก frames ที่มากเกินไป ไม่ใช่ arithmetic overflow
คุณเข้าใจแล้ว
- routine แบบ recursive ต้องการ base case ที่แก้ได้โดยตรงและ recursive case ที่เรียกตัวเองด้วย อินพุต ที่ เล็กลง
- ติดตามในทั้งสองทิศทาง: การเรียก m้วนขึ้นสู่ base case แล้วค่าจะม้วนลงกลับมา
- แต่ละ call มี stack frame ของมันเองบน call stack, ถูก push เข้าเมื่อมีการ call และ pop ออกเมื่อกลับคืน นี่คือเหตุผลว่าทำไม recursion จึงใช้หน่วยความจำ
- ความเสี่ยง: หากไม่มีกรณีฐานที่เข้าถึงได้จะนำไปสู่การเรียกซ้ำแบบไม่มีที่สิ้นสุดและ Stack overflow การเรียกซ้ำที่ลึกเกินไปจะใช้หน่วยความจำหมด และการทำงานซ้ำซ้อนควรใช้ลูปหรือ Memoisation
เกิดปัญหาอะไรขึ้น? เฉพาะทีมเว็บไซต์เท่านั้นที่เห็นข้อมูลนี้