Traversing a Binary Tree 10/23/081. 2 Traversing Binary Tree There are 3 ways of traversing a binary tree T having root R. 1. Preorder Traversing Steps:

Slides:



Advertisements
Similar presentations
CS Fall 2012, Lab 08 Haohan Zhu. Boston University Slideshow Title Goes Here CS Fall 2012, Lab /17/2015 Tree - Data Structure  Basic.
Advertisements

1 Jake’s Pizza Shop Owner Jake Manager Chef Brad Carol Waitress Waiter Cook Helper Joyce Chris Max Len.
Chapter 4: Trees General Tree Concepts Binary Trees Lydia Sinapova, Simpson College Mark Allen Weiss: Data Structures and Algorithm Analysis in Java.
Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A12 – Binary Trees.
Tree Traversal. Traversal Algorithms preorder inorder postorder.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L12 (Chapter 20) Lists, Stacks,
Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A12 – Binary Trees.
Binary Trees. Linear data structures Here are some of the data structures we have studied so far: –Arrays –Singly-linked lists and doubly-linked lists.
1 CS308 Data Structures An application of binary trees: Binary Expression Trees.
Tree Traversal. Traversal Algorithms preorder inorder postorder.
Preorder Traversal with a Stack Push the root onto the stack. While the stack is not empty n pop the stack and visit it.
Three Types of Depth-First Search Alyce Brady CS 470: Data Structures CS 510: Computer Algorithms.
1 Chapter 18 Trees Objective To learn general trees and recursion binary trees and recursion tree traversal.
“There is never enough time to do everything, but there is always enough time to do the most important thing.” – Brian Tracy Thought for the Day.
CHAPTER 71 TREE. Binary Tree A binary tree T is a finite set of one or more nodes such that: (a) T is empty or (b) There is a specially designated node.
Review Binary Tree Binary Tree Representation Array Representation Link List Representation Operations on Binary Trees Traversing Binary Trees Pre-Order.
Trees CS212 & CS-240 D.J. Foreman. What is a Tree A tree is a finite set of one or more nodes such that: –There is a specially designated node called.
Trees. Containers we have studied so far are linear. To represent nonlinear, i.e. hierarchal data we use trees. Nonlinear Containers root node leaf edge.
Binary Trees 2 Overview Trees. Terminology. Traversal of Binary Trees. Expression Trees. Binary Search Trees.
Chapter 19: Binary Trees Java Programming: Program Design Including Data Structures Program Design Including Data Structures.
ACM/JETT Workshop - August 4-5, 2005 Using Visualization Tools To Teach Data Structures and Algorithms Java applets by Dr. R. Mukundan, University of Canterbury,
Starting at Binary Trees
Computer Science: A Structured Programming Approach Using C Trees Trees are used extensively in computer science to represent algebraic formulas;
Lecture – Searching a Tree Neil Ghani University of Strathclyde.
Trees Isaac Sheff. Nodes Building blocks of trees “Parent” node may have “Child” nodes Can be both parent and child Can’t be its own ancestor Can’t have.
Search: Binary Search Trees Dr. Yingwu Zhu. Linear Search Collection of data items to be searched is organized in a list x 1, x 2, … x n Assume == and.
Trees 2: Section 4.2 and 4.3 Binary trees. Binary Trees Definition: A binary tree is a rooted tree in which no vertex has more than two children
NURUL HASLINDA NGAH BP-37 SEMESTER II 2013/2014.  At the end of this chapter you’ll be learnt about:  Binary tree  Binary search tree  Traversing.
Rudiments of Trees a Joshua presentation DATA STRUCTURES.
Binary Trees In computer science, a binary tree is a tree data structure in which each node has at most two children, which are referred to as the left.
Huffman’s Algorithm 11/02/ Weighted 2-tree A weighted 2-tree T is an extended binary tree with n external nodes and each of the external nodes is.
Binary Search Trees (BST)
Lecture 17: Trees and Networks I Discrete Mathematical Structures: Theory and Applications.
Traversing a tree means visiting each node in a specified order. There are different ways to traverse a tree. Tree Traversing Depth first search Pre-orderIn-orderPost-order.
1 Trees 2 Binary trees Section Binary Trees Definition: A binary tree is a rooted tree in which no vertex has more than two children –Left and.
Copyright © 2012 Pearson Education, Inc. Chapter 20: Binary Trees.
AL-HUSEEN BIN TALAL UNIVERSITY College of Engineering Department of Computer Engineering Algorithms and Data Structures Binary Tree Course No.:
Copyright © 2005 Pearson Addison-Wesley. All rights reserved Balancing Binary Trees There are many approaches to balancing binary trees One method.
1 Trees General Trees  Nonrecursive definition: a tree consists of a set of nodes and a set of directed edges that connect pairs of nodes.
Binary Tree Implementation. Binary Search Trees (BST) Nodes in Left subtree has smaller values Nodes in right subtree has bigger values.
Lecture 7: Searching a Tree Neil Ghani University of Strathclyde.
Search: Binary Search Trees Dr. Yingwu Zhu. Review: Linear Search Collection of data items to be searched is organized in a list x 1, x 2, … x n – Assume.
1 Trees General Trees  Nonrecursive definition: a tree consists of a set of nodes and a set of directed edges that connect pairs of nodes.
1 Nell Dale Chapter 8 Binary Search Trees Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus C++ Plus Data Structures.
Fundamentals of Algorithms MCS - 2 Lecture # 17. Binary Search Trees.
Trees Binary Trees Extended Binary Trees. Tree A Tree consist of Nodes connected by Edges. Node is represented by Circle Edges as Lines or Arrows A Tree.
Printing a search tree in order
Recursive Objects (Part 4)
Trees.
Trees ---- Soujanya.
Binary Search Tree (BST)
Tree.
Section 8.1 Trees.
Chapter 20: Binary Trees.
CS223 Advanced Data Structures and Algorithms
Chapter 21: Binary Trees.
Abstract Data Structures
Principles of Computing – UFCFA3-30-1
CSE 373, Copyright S. Tanimoto, 2002 Binary Trees -
Binary Tree Traversals
Chapter 9 Binary Trees.
Section 9.3 by Andrew Watkins
2018, Fall Pusan National University Ki-Joune Li
CSE 373, Copyright S. Tanimoto, 2001 Binary Trees -
if the tree is empty, do nothing,
Chapter 20: Binary Trees.
Binary Tree Traversal.
General Tree Concepts Binary Trees
Trees 2: Dynamic Binary Tree Navigation and Iteration
Binary Tree Iterators Tree Traversals: preorder, inorder, postorder
Presentation transcript:

Traversing a Binary Tree 10/23/081

2 Traversing Binary Tree There are 3 ways of traversing a binary tree T having root R. 1. Preorder Traversing Steps: (a) Process the root R (b) Traverse the left subtree of R in preorder. (c) Traverse the right subtree of R in preorder. Example: A BC DEFG H I J Preorder Traversal of T A, B, D, H, E, C, F, I, J, G Figure: Binary Tree T

10/23/ Inorder Traversing Steps: (a) Traverse the left subtree of R in inorder. (b) Traverse the root R. (c) Traverse the right subtree of R in inorder. Example: A BC DEFG H I J Inorder Traversal of T D, H, B, E, A, I, F, J, C, G Figure: Binary Tree T

10/23/ Postorder Traversing Steps: (a) Traverse the left subtree of R in postorder. (b) Traverse the right subtree of R in postorder. (c) Traverse the root R. Example: A BC DEFG H I J Postorder Traversal of T H, D, E, B, I, J, F, G, C, A Figure: Binary Tree T

10/23/085 Traversal Algorithms Using Stacks Preorder Traversal Using Stack Algorithm: Preorder_Traverse(Tree, Root, Stack) (1) Set Stack[0]=Null and Top=1 and Ptr=Root (2) Repeat steps (3) to (5) until Ptr ≠ NULL (3) Process Ptr->Info. (4) if Ptr->Right ≠ NULL then set Stack[Top]=Ptr->Right and Top=Top+1 (5) If Ptr->Left ≠ NULL then set Ptr=Ptr->Left else Set Ptr=Stack[Top] and Top=Top-1 (6) Exit.

10/23/08Shaily Kabir,Dept. of CSE, DU6 A BC DEFG H I J Example: 1.Initially Ptr := A and Stack: Ø 2.Proceed down the left-most path rooted at Ptr = A i. Process A, Push C onto Stack. Stack: Ø, C ii. Process B, Push E onto Stack. Stack: Ø, C, E iii. Process D, Push H onto Stack. Stack: Ø, C, E, H 3. Pop the Stack and Set Ptr := H. Stack: Ø, C, E 4. Proceed down the left-most path rooted at Ptr = H i. Process H Figure: Binary Tree T

10/23/ Pop the Stack and Set Ptr := E and Stack: Ø, C 6. Proceed down the left-most path rooted at Ptr = E i. Process E 7. Pop the Stack and Set Ptr := C and Stack: Ø 8. Proceed down the left-most path rooted at Ptr = C i. Process C, Push G onto Stack. Stack: Ø, G ii. Process F, Push J onto Stack. Stack: Ø, G, J iii. Process I 9. Pop the Stack and Set Ptr := J and Stack: Ø, G 10. Proceed down the left-most path rooted at Ptr = J i. Process J 11. Pop the Stack and Set Ptr := G and Stack: Ø 12. Proceed down the left-most path rooted at Ptr = G i. Process G 13. Pop the Stack and set Ptr := Ø and Exit. Preorder Traversal of T: A, B, D, H, E, C, F, I, J, G

10/23/08Shaily Kabir,Dept. of CSE, DU8 2. Inorder Traversal Using Stack Algorithm: Inorder_Traverse(Tree, Root, Stack) (1) Set Stack[0]=NULL and Top=1 and Ptr=Root (2) Repeat while Ptr ≠ NULL (a) Set Stack[Top]=Ptr and Top=Top+1 (b) Set PTR=Ptr->Left (3) Set Ptr=Stack[Top] and Top := Top -1 (4) Repeat steps 5 to 7 while Ptr ≠ NULL (5) Process Ptr->Info (6) If Ptr->Right ≠NULL then set Ptr=Ptr->Right and go to step 2. (7) Set Ptr=Stack[Top] and Top=Top-1 (8) Exit

10/23/08Shaily Kabir,Dept. of CSE, DU9 A BC DEFG H I J Example: 1.Initially Ptr := A and Stack: Ø 2.Proceed down the left-most path rooted at Ptr = A, pushing A, B, D onto Stack. Stack: Ø, A, B, D 1.Pop the Stack and Set Ptr := D. Stack: Ø, A, B 2.Process D. Set Ptr := H. Proceed down the left-most path rooted at Ptr = H, pushing H onto Stack. Stack: Ø, A, B, H 3.Pop the Stack and Set Ptr := H. Stack: Ø, A, B 6. Process H. 1.Pop the Stack and Set Ptr := B. Stack: Ø, A 2.Process B. Set Ptr:= E.Proceed down the left-most path rooted at Ptr = E, pushing E onto Stack. Stack: Ø, A, E Figure: Binary Tree T

10/23/08Shaily Kabir,Dept. of CSE, DU Pop the Stack and Set Ptr := E. Stack: Ø, A 11. Process E. 12. Pop the Stack and Set Ptr := A. Stack: Ø 13. Process A. Set Ptr:= C. Proceed down the left-most path rooted at Ptr = C, pushing C, F, I onto Stack. Stack: Ø, C, F, I 14. Pop the Stack. Set Ptr := I. Stack: Ø, C, F 15. Process I. 16. Pop the Stack. Set Ptr := F. Stack: Ø, C, F 17. Process F. Set Ptr := J. Proceed down the left-most path rooted at Ptr = J, pushing J onto Stack. Stack: Ø, C, J 18. Pop the Stack. Set Ptr := J. Stack: Ø, C 19. Process J. 20. Pop the Stack. Set Ptr := C. Stack: Ø 21. Process C. Set Ptr := G. Proceed down the left-most path rooted at Ptr = G, pushing G onto Stack. Stack: Ø, G 22. Pop the Stack. Set Ptr := G. Stack: Ø 23. Process G. 24. Pop the Stack. Set Ptr := Ø and Exit. Inorder Traversal of T: D, H, B, E, A, I, F, J, C, G

10/23/08Shaily Kabir,Dept. of CSE, DU11 Assignment Write an algorithm that will traverse a binary tree in postorder traversal using stack. Discuss the algorithm using example.

10/23/08Shaily Kabir,Dept. of CSE, DU12 END!!!