Tree Traversal Pseudocode PQ

Duration: 7 min

This video lesson is available to enrolled students.

Return to /learn/BTSC-LAB-ASSISTANT/data-structures/tree/traversals-construction/asset-tree-traversal-pseudocode-pq after enrolling

Inside: a video lesson and guided study material.

Module outline

  1. About Course: BTSC About Course
  2. Basics of Computer Science: Basics of COA, Number System, Number Representation
  3. Operating System: Introduction to OS, Process Management, CPU Scheduling, Process Synchronization, Threads & Process Creation, Deadlock, Memory Management, Virtual Memory
  4. Data Structures & Algorithms: Introduction to DS, Array, Stack, Queue, Linked List, Tree, Graphs, Hashing, Algorithm Analysis, Time Complexity Analysis, Sorting Algorithms
  5. Database Management Systems(DBMS): Basics of DBMS, ER Diagram, Relational Model & Functional Dependencies, Keys & Integrity Constraints, Normalization (1NF - BCNF), Relational Algebra, SQL, Transaction Management, Concurrency Control, Database Recovery
  6. Networking and Internet: Introduction to CN, Data Communication, Net Layer: IPv4 & Proto, Net Layer: IP Addressing, Application Layer, Hardware Basics, Network Access Technologies, Network Security
  7. Cyber Security Basics: Cyber Threats and Malware Attacks, Role of Internet and Web Services, Electronic Data Interchange and E-Commerce, Cyber Safety and Security
  8. C Programming (Programming Fundamentals): C Fundamentals, Control Flow, Functions, Arrays & Pointers, Storage Classes, Structures & Enums, DMA, Macros, Scoping & File Handling
  9. C++, OOPs & Java (Programming Fundamentals): Java, OOPS with C++
  10. Python (Programming Fundamentals): Introduction to Python, Tokens, Variables, Data Types, I/O, Operators & Expressions, Control Flow & Looping, Collections & Strings, Functions & Modules
  11. Web Development Basics: Internet Fundamentals, Web Design & Development, Protocols & Services, Web Publishing & Hosting, Server Side & Security, Emerging Technologies, JavaScript-Basics, HTML Basics
  12. Software Engineering: Fundamentals of Software Engineering, Software Requirements and Quality Assurance, Software Design, Estimation and Metrics, Software Testing
  13. Software and Hardware Tools: MS Office Applications, Unix OS, Linux OS
  14. Emerging Trends. AI & Social Ethics: Artificial Intelligence, Emerging Tech Trends, Society, Law & Ethics
  15. Full Mock Test:
AI summary & chapters

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

  1. 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. 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.

  3. 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.

Loading lesson…