Cover of The Art of Computer Programming by Donald E. Knuth - Business and Economics Book

From "The Art of Computer Programming"

Author: Donald E. Knuth
Publisher: Addison-Wesley Professional
Year: 2014
Category: Computers

🎧 Free Preview Complete

You've listened to your free 10-minute preview.
Sign up free to continue listening to the full summary.

🎧 Listen to Summary

Free 10-min Preview
0:00
Speed:
10:00 free remaining
Chapter 6: Trees
Key Insight 3 from this chapter

Binary Tree Traversal and Threaded Trees

Key Insight

Traversing a binary tree involves systematically examining each node exactly once, which effectively linearizes the tree's nodes into a sequence. Three primary recursive methods exist: 'preorder,' which visits the root, then the left subtree (preorder), then the right subtree (preorder); 'inorder,' which visits the left subtree (inorder), then the root, then the right subtree (inorder), often referred to as 'symmetric order'; and 'postorder,' which visits the left subtree (postorder), then the right subtree (postorder), then the root. These traversal orders are fundamental to most computer algorithms dealing with trees. For example, for a tree with root A and subtrees, a preorder traversal might yield 'A, B, H, J, C, D, E, G, F', while an inorder traversal would be 'H, B, J, A, D, C, G, E, F'.

Recursive definitions for traversal need to be adapted for computer implementation, often utilizing an auxiliary stack. Algorithm T, an example for inorder traversal, initializes an empty stack and a pointer P to the tree. It iteratively pushes P onto the stack and moves P to its left child until P is null. Then, it pops a node from the stack, 'visits' it (performs the intended activity), and moves P to its right child, repeating the process until the stack is empty. This algorithm's correctness can be rigorously proven by induction on the number of nodes, demonstrating that it correctly traverses the tree in inorder and restores the stack state. Preorder traversal algorithms are similar and slightly simpler, while postorder traversal is generally more complex to implement without recursion.

A conventional binary tree representation often results in many null links, wasting memory. The 'threaded tree' representation addresses this by replacing null links with 'threads' that point to other nodes, specifically the inorder predecessor or successor of the node. Each node requires two additional one-bit fields, 'LTAG' and 'RTAG', to distinguish between ordinary links and threads. This ingenious method eliminates the need for an explicit stack during traversal. For instance, Algorithm S efficiently finds the symmetric (inorder) successor of a node P: if P's right link is a thread, it directly points to the successor; otherwise, it traverses left down the right subtree to find the leftmost node. This is a significant advantage over unthreaded representations, where finding a successor without a stack is impossible due to the absence of upward pointers.

📚 Continue Your Learning Journey — No Payment Required

Access the complete The Art of Computer Programming summary with audio narration, key takeaways, and actionable insights from Donald E. Knuth.