Tree Traversal Pseudocode PQ
Duration: 7 min
This video lesson is available to enrolled students.
AI Summary
An AI-generated summary of this video lecture.
The video lecture analyzes a recursive C function named `what` designed to traverse a binary tree. The instructor begins by displaying the code, which contains three `printf` statements and two recursive calls to `what` for the left and right children. He draws a sample binary tree on the whiteboard with a root node of value 1, a left child 2 (which has a left child 4), and a right child 3. The instructor then performs a step-by-step trace of the function execution. He demonstrates that for every node visited, the data is printed three times: once before the left child recursion, once between the left and right child recursions, and once after the right child recursion. By following the call stack, he derives the final output sequence: 1 2 4 4 4 2 2 1 3 3 3 1. The lesson emphasizes understanding the order of operations in recursive tree traversals.
Chapters
0:00 – 2:00 00:00-02:00
The video opens with the code snippet for the function `void what(struct Bnode *t)`. The instructor introduces the problem of determining the function's behavior. He draws a binary tree on the right side of the screen, starting with a root node labeled 1. He adds a left child labeled 2 and a right child labeled 3. He further extends the tree by adding a left child labeled 4 to node 2. The instructor begins tracing the function by writing `what(1)` on the board, indicating the initial call with the root node. He underlines the first line of code inside the `if (t)` block, which is `printf("%d', t->data);`, explaining that this will print the data of the current node.
2:00 – 5:00 02:00-05:00
The instructor continues the trace by underlining the next line, `what(t->LC);`, which represents a recursive call to the left child. He writes `what(2)` below `what(1)` to show the next step in the recursion. Inside the call for node 2, he explains that the data 2 is printed first. Then, the function calls `what(t->LC)` again, moving to node 4. For node 4, the data 4 is printed. Since node 4 has no left child, `what(NULL)` is called and returns immediately. The function then prints 4 again (the second `printf`), calls `what(t->RC)` (which is NULL and returns), and finally prints 4 a third time. The instructor writes the sequence `4 4 4` on the board to represent the output for node 4. Back in node 2, after the left child returns, the second `printf` prints 2 again. The right child call is NULL, so it returns, and the final `printf` prints 2 a third time.
5:00 – 7:17 05:00-07:17
The trace returns to the root node 1. After the left subtree (node 2) finishes, the second `printf` in the root function executes, printing 1 again. Next, the function calls `what(t->RC)`, moving to the right child, node 3. The instructor explains that node 3 follows the same pattern: print 3, call left (NULL), print 3, call right (NULL), print 3. This results in the sequence `3 3 3`. Finally, the function returns to the root node 1 one last time, where the final `printf` statement executes, printing 1 for the third time. The instructor writes the complete output sequence on the board: `1 2 4 4 4 2 2 1 3 3 3 1`. He concludes by summarizing that the function prints each node's data three times in a specific order: pre-order, in-order, and post-order relative to the recursive calls.
The lecture provides a clear, visual method for tracing recursive functions on binary trees. By breaking down the code into its constituent parts—printing data and making recursive calls—the instructor shows how the execution flow determines the output. The key learning point is that the position of `printf` statements relative to the recursive calls dictates the traversal order. In this specific case, the function performs a custom traversal where every node is visited and printed three times: before descending left, between left and right descents, and after descending right. This reinforces the concept that recursive functions execute code sequentially, and the 'return' from a recursive call resumes execution at the line immediately following the call.