Presentation is loading. Please wait.

Presentation is loading. Please wait.

Binary Tree Traversals

Similar presentations


Presentation on theme: "Binary Tree Traversals"— Presentation transcript:

1 Binary Tree Traversals
Lecture 38 Fri, Apr 28, 2006 1/16/2019 Binary Trees

2 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

3 Binary Tree Traversals
Four Standard Traversals Pre-order Traversal. In-order Traversal. Post-order Traversal. Level-order Traversal. 1/16/2019 Binary Trees

4 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

5 Pre-Order Traversal 10 20 30 40 50 60 70 1/16/2019 Binary Trees

6 Pre-Order Traversal 10 20 30 40 50 60 70 10 1/16/2019 Binary Trees

7 Pre-Order Traversal 10 20 30 40 50 60 70 10 20 1/16/2019 Binary Trees

8 Pre-Order Traversal 10 20 30 40 50 60 70 10 20 40 1/16/2019
Binary Trees

9 Pre-Order Traversal 10 20 30 40 50 60 70 10 20 40 50 1/16/2019
Binary Trees

10 Pre-Order Traversal 10 20 30 40 50 60 70 10 20 30 40 50 1/16/2019
Binary Trees

11 Pre-Order Traversal 10 20 30 40 50 60 70 10 20 30 40 50 60 1/16/2019
Binary Trees

12 Pre-Order Traversal 10 20 30 40 50 60 70 10 20 30 40 50 60 70 1/16/2019 Binary Trees

13 Pre-Order Traversal Order: 10, 20, 40, 50, 30, 60, 70 10 20 30 40 50
1/16/2019 Binary Trees

14 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

15 In-Order Traversal 10 20 30 40 50 60 70 1/16/2019 Binary Trees

16 In-Order Traversal 10 20 30 40 50 60 70 40 1/16/2019 Binary Trees

17 In-Order Traversal 10 20 30 40 50 60 70 20 40 1/16/2019 Binary Trees

18 In-Order Traversal 10 20 30 40 50 60 70 20 40 50 1/16/2019 Binary Trees

19 In-Order Traversal 10 20 30 40 50 60 70 10 20 40 50 1/16/2019 Binary Trees

20 In-Order Traversal 10 20 30 40 50 60 70 10 20 40 50 60 1/16/2019 Binary Trees

21 In-Order Traversal 10 20 30 40 50 60 70 10 20 30 40 50 60 1/16/2019 Binary Trees

22 In-Order Traversal 10 20 30 40 50 60 70 10 20 30 40 50 60 70 1/16/2019 Binary Trees

23 In-Order Traversal Order: 40, 20, 50, 10, 60, 30, 70 10 20 30 40 50 60
1/16/2019 Binary Trees

24 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

25 Post-Order Traversal 10 20 30 40 50 60 70 1/16/2019 Binary Trees

26 Post-Order Traversal 10 20 30 40 50 60 70 40 1/16/2019 Binary Trees

27 Post-Order Traversal 10 20 30 40 50 60 70 40 50 1/16/2019 Binary Trees

28 Post-Order Traversal 10 20 30 40 50 60 70 20 40 50 1/16/2019
Binary Trees

29 Post-Order Traversal 10 20 30 40 50 60 70 20 40 50 60 1/16/2019
Binary Trees

30 Post-Order Traversal 10 20 30 40 50 60 70 20 40 50 60 70 1/16/2019
Binary Trees

31 Post-Order Traversal 10 20 30 40 50 60 70 20 30 40 50 60 70 1/16/2019
Binary Trees

32 Post-Order Traversal 10 20 30 40 50 60 70 10 20 30 40 50 60 70 1/16/2019 Binary Trees

33 Post-Order Traversal Order: 40, 50, 20, 60, 70, 30, 10 10 20 30 40 50
1/16/2019 Binary Trees

34 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

35 Level-Order Traversal
10 20 30 40 50 60 70 1/16/2019 Binary Trees

36 Level-Order Traversal
10 20 30 40 50 60 70 10 1/16/2019 Binary Trees

37 Level-Order Traversal
10 20 30 40 50 60 70 10 20 1/16/2019 Binary Trees

38 Level-Order Traversal
10 20 30 40 50 60 70 10 20 30 1/16/2019 Binary Trees

39 Level-Order Traversal
10 20 30 40 50 60 70 10 20 30 40 1/16/2019 Binary Trees

40 Level-Order Traversal
10 20 30 40 50 60 70 10 20 30 40 50 1/16/2019 Binary Trees

41 Level-Order Traversal
10 20 30 40 50 60 70 10 20 30 40 50 60 1/16/2019 Binary Trees

42 Level-Order Traversal
10 20 30 40 50 60 70 10 20 30 40 50 60 70 1/16/2019 Binary Trees

43 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

44 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

45 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

46 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

47 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

48 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

49 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

50 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

51 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

52 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


Download ppt "Binary Tree Traversals"

Similar presentations


Ads by Google