Binary Tree Traversals Lecture 38 Fri, Apr 28, 2006 1/16/2019 Binary Trees
Topics Binary tree traversals Binary tree iterators Pre-order traversals In-order traversals Post-order traversals Level-order traversals Binary tree iterators Pre-order iterators In-order iterators Post-order iterators Level-order iterators 1/16/2019 Binary Trees
Binary Tree Traversals Four Standard Traversals Pre-order Traversal. In-order Traversal. Post-order Traversal. Level-order Traversal. 1/16/2019 Binary Trees
Pre-order Traversal At each node, Also called an VLR traversal. First visit the node, Then perform a pre-order traversal of the left subtree, Then perform a pre-order traversal of the right subtree. Also called an VLR traversal. Visit - Left - Right. 1/16/2019 Binary Trees
Pre-Order Traversal 10 20 30 40 50 60 70 1/16/2019 Binary Trees
Pre-Order Traversal 10 20 30 40 50 60 70 10 1/16/2019 Binary Trees
Pre-Order Traversal 10 20 30 40 50 60 70 10 20 1/16/2019 Binary Trees
Pre-Order Traversal 10 20 30 40 50 60 70 10 20 40 1/16/2019 Binary Trees
Pre-Order Traversal 10 20 30 40 50 60 70 10 20 40 50 1/16/2019 Binary Trees
Pre-Order Traversal 10 20 30 40 50 60 70 10 20 30 40 50 1/16/2019 Binary Trees
Pre-Order Traversal 10 20 30 40 50 60 70 10 20 30 40 50 60 1/16/2019 Binary Trees
Pre-Order Traversal 10 20 30 40 50 60 70 10 20 30 40 50 60 70 1/16/2019 Binary Trees
Pre-Order Traversal Order: 10, 20, 40, 50, 30, 60, 70 10 20 30 40 50 1/16/2019 Binary Trees
In-order Traversal At each node, Also called an LVR traversal. First, perform an in-order traversal of the left subtree, Then visit the node, Then perform an in-order traversal of the right subtree. Also called an LVR traversal. Left - Visit - Right. 1/16/2019 Binary Trees
In-Order Traversal 10 20 30 40 50 60 70 1/16/2019 Binary Trees
In-Order Traversal 10 20 30 40 50 60 70 40 1/16/2019 Binary Trees
In-Order Traversal 10 20 30 40 50 60 70 20 40 1/16/2019 Binary Trees
In-Order Traversal 10 20 30 40 50 60 70 20 40 50 1/16/2019 Binary Trees
In-Order Traversal 10 20 30 40 50 60 70 10 20 40 50 1/16/2019 Binary Trees
In-Order Traversal 10 20 30 40 50 60 70 10 20 40 50 60 1/16/2019 Binary Trees
In-Order Traversal 10 20 30 40 50 60 70 10 20 30 40 50 60 1/16/2019 Binary Trees
In-Order Traversal 10 20 30 40 50 60 70 10 20 30 40 50 60 70 1/16/2019 Binary Trees
In-Order Traversal Order: 40, 20, 50, 10, 60, 30, 70 10 20 30 40 50 60 1/16/2019 Binary Trees
Post-order Traversal At each node, Also called an LRV traversal. First perform a post-order traversal of the left subtree, Then perform a post-order traversal of the right subtree, Then visit the node. Also called an LRV traversal. Left - Right - Visit. 1/16/2019 Binary Trees
Post-Order Traversal 10 20 30 40 50 60 70 1/16/2019 Binary Trees
Post-Order Traversal 10 20 30 40 50 60 70 40 1/16/2019 Binary Trees
Post-Order Traversal 10 20 30 40 50 60 70 40 50 1/16/2019 Binary Trees
Post-Order Traversal 10 20 30 40 50 60 70 20 40 50 1/16/2019 Binary Trees
Post-Order Traversal 10 20 30 40 50 60 70 20 40 50 60 1/16/2019 Binary Trees
Post-Order Traversal 10 20 30 40 50 60 70 20 40 50 60 70 1/16/2019 Binary Trees
Post-Order Traversal 10 20 30 40 50 60 70 20 30 40 50 60 70 1/16/2019 Binary Trees
Post-Order Traversal 10 20 30 40 50 60 70 10 20 30 40 50 60 70 1/16/2019 Binary Trees
Post-Order Traversal Order: 40, 50, 20, 60, 70, 30, 10 10 20 30 40 50 1/16/2019 Binary Trees
Level-order Traversal Traverse the levels of the tree from top to bottom. Within each level, visit the nodes from left to right. 1/16/2019 Binary Trees
Level-Order Traversal 10 20 30 40 50 60 70 1/16/2019 Binary Trees
Level-Order Traversal 10 20 30 40 50 60 70 10 1/16/2019 Binary Trees
Level-Order Traversal 10 20 30 40 50 60 70 10 20 1/16/2019 Binary Trees
Level-Order Traversal 10 20 30 40 50 60 70 10 20 30 1/16/2019 Binary Trees
Level-Order Traversal 10 20 30 40 50 60 70 10 20 30 40 1/16/2019 Binary Trees
Level-Order Traversal 10 20 30 40 50 60 70 10 20 30 40 50 1/16/2019 Binary Trees
Level-Order Traversal 10 20 30 40 50 60 70 10 20 30 40 50 60 1/16/2019 Binary Trees
Level-Order Traversal 10 20 30 40 50 60 70 10 20 30 40 50 60 70 1/16/2019 Binary Trees
Level-Order Traversal 10 20 30 40 50 60 70 10 20 30 40 50 60 70 Order: 10, 20, 30, 40, 50, 60, 70 1/16/2019 Binary Trees
Binary Tree Iterators A binary tree iterator systematically visits each node of a binary tree. The four standard iterators correspond to the four standard traversal methods. Pre-order iterator In-order iterator Post-order iterator Level-order iterator 1/16/2019 Binary Trees
Pre-order Iterator Visit the nodes in a pre-order traversal. Use a stack to store unvisited nodes. Begin with the root node. Visit the node. If there is a right subtree, push the pointer to it. If there is a left subtree, move to the left subtree and continue recursively. If there is no left subtree, pop a node and continue recursively. 1/16/2019 Binary Trees
Pre-order Iterator 10 20 30 40 50 60 70 10 10 10 20 20 20 30 30 30 40 50 60 70 1/16/2019 Binary Trees
In-order Iterator Visit the nodes in an in-order traversal. Use a stack to store unvisited nodes. Move from the root to the “leftmost” node, pushing nodes along the way, etc. 1/16/2019 Binary Trees
In-order Iterator 10 20 30 40 50 60 70 10 10 20 20 30 30 40 50 60 70 1/16/2019 Binary Trees
Post-order Iterator Visit the nodes in a post-order traversal. Use a stack to store unvisited nodes. Move to the “leftmost” leaf, pushing nodes along the way, etc. 1/16/2019 Binary Trees
Post-order Iterator 10 20 30 40 50 60 70 10 10 20 20 30 30 40 50 60 70 1/16/2019 Binary Trees
Level-order Iterator Visit the nodes in a level-order traversal. Use a queue to store unvisited nodes. Begin with the root node. Enqueue the node’s children, left first. Visit the node. Dequeue a node and continue recursively until the queue is empty. 1/16/2019 Binary Trees
Level-order Iterator 10 20 30 40 50 60 70 10 20 20 30 30 40 40 50 50 60 60 70 70 1/16/2019 Binary Trees