Traversing Trees. Traversing is the systematic way of accessing, or ‘visiting’ all the nodes in a tree. Consider the three operations: V: Visit a node.

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

SUNY Oneonta Data Structures and Algorithms Visualization Teaching Materials Generation Group Binary Search Tree A running demonstration of binary search.
1 Tree Traversal Section 9.3 Longin Jan Latecki Temple University Based on slides by Paul Tymann, Andrew Watkins, and J. van Helden.
Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A12 – Binary Trees.
Tree Traversal. Traversal Algorithms preorder inorder postorder.
Binary Tree B G E D I H F c A Binary tree
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 Trees.
Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A12 – Binary Trees.
Tree Traversal. Traversal Algorithms preorder inorder postorder.
Department of Computer Eng. & IT Amirkabir University of Technology (Tehran Polytechnic) Data Structures Lecturer: Abbas Sarraf Trees.
10. Binary Trees A. Introduction: Searching a linked list.
Data Structures Chapter 10 Binary Trees Andreas Savva.
Three Types of Depth-First Search Alyce Brady CS 470: Data Structures CS 510: Computer Algorithms.
Types of Binary Trees Introduction. Types of Binary Trees There are several types of binary trees possible each with its own properties. Few important.
Lecture 17 Non-Linear data structures Richard Gesick.
CS Data Structures Chapter 5 Trees. Chapter 5 Trees: Outline  Introduction  Representation Of Trees  Binary Trees  Binary Tree Traversals 
Emma Price 1.  To be able to:  Explain what a binary tree is.  To traverse a binary tree using the three different methods. 2.
CISC220 Fall 2009 James Atlas Lecture 13: Trees. Skip Lists.
Binary Trees 2 Overview Trees. Terminology. Traversal of Binary Trees. Expression Trees. Binary Search Trees.
Tree Data Structures. Introductory Examples Willliam Willliam BillMary Curt Marjorie Richard Anne Data organization such that items of information are.
 Trees Data Structures Trees Data Structures  Trees Trees  Binary Search Trees Binary Search Trees  Binary Tree Implementation Binary Tree Implementation.
Trees by Dr. Bun Yue Professor of Computer Science CSCI 3333 Data Structures.
1 10. Binary Trees Read Sec A. Introduction: Searching a linked list. 1. Linear Search /* Linear search a list for a particular item */ 1. Set.
Chapter 6 (cont’) Binary Tree. 6.4 Tree Traversal Tree traversal is the process of visiting each node in the tree exactly one time. This definition does.
Binary trees Binary search trees Expression trees Heaps Data Structures and Algorithms in Java, Third EditionCh06 – 1.
Computer Science 112 Fundamentals of Programming II Introduction to Trees.
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.
Min Chen School of Computer Science and Engineering Seoul National University Data Structure: Chapter 6.
Trees By P.Naga Srinivasu M.tech,(MBA). Basic Tree Concepts A tree consists of finite set of elements, called nodes, and a finite set of directed lines.
1 Chapter 7 Objectives Upon completion you will be able to: Create and implement binary search trees Understand the operation of the binary search tree.
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.
Data Structures Azhar Maqsood School of Electrical Engineering and Computer Sciences (SEECS-NUST) Binary Trees.
Foundation of Computing Systems Lecture 4 Trees: Part I.
Lecture 7: Searching a Tree Neil Ghani University of Strathclyde.
Chapter 7 Trees_ Part2 TREES. Depth and Height 2  Let v be a node of a tree T. The depth of v is the number of ancestors of v, excluding v itself. 
1 Trees. 2 Trees Trees. Binary Trees Tree Traversal.
Binary Search Trees Chapter 7 Objectives
Traversal From CSCE 3110 Data Structures & Algorithm Analysis
Chapter 10 Trees © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
Printing a search tree in order
Fundamentals of Programming II Introduction to Trees
Recursive Objects (Part 4)
Data Structures Binary Trees 1.
Paul Tymann and Andrew Watkins
Binary Search Tree (BST)
Tree.
Section 8.1 Trees.
CS223 Advanced Data Structures and Algorithms
Alyce Brady CS 470: Data Structures CS 510: Computer Algorithms
Binary Trees.
Abstract Data Structures
Data Structures CSCI 132, Spring 2014 Lecture 35 Binary Trees
Binary Trees.
Binary Trees.
Paul Tymann, Andrew Watkins,
Principles of Computing – UFCFA3-30-1
Binary Tree Traversals
Section 9.3 by Andrew Watkins
Binary Tree Chapter 8 (cont’) Part2.
2018, Fall Pusan National University Ki-Joune Li
Paul Tymann, Andrew Watkins,
if the tree is empty, do nothing,
CS210- Lecture 9 June 20, 2005 Announcements
Trees.
Chapter 20: Binary Trees.
Binary Trees.
Data Structures Using C++ 2E
A Binary Tree is a tree in which each node has at most 2 children
Binary Tree Iterators Tree Traversals: preorder, inorder, postorder
Presentation transcript:

Traversing Trees

Traversing is the systematic way of accessing, or ‘visiting’ all the nodes in a tree. Consider the three operations: V: Visit a node L: (Recursively) traverse the left subtree of a node R: (Recursively) traverse the right subtree of a node Traversing a Tree

Tree Traversing We can traverse a tree in six different orders: LVR VLR LRV VRL RVL RLV

Preorder Traversal Algorithm preorder( T, v ) perform the visit action for node v for each child w of v do recursively traverse the subtree rooted at w by calling preorder( T, w ) void preorderPrint(const Tree& T, const Position& v ) { cout << v.element() << " "; PositionIterator children = T.children(v); while( children.hasNext() ) { preorderPrint( T, children.next() ) }

Preorder Traversal One way to make sure that we visit every node is with VLR ( a node is visited before its descendants ) Start by visiting the root Then traverse each subtree T v v v Visit the node Then, visit the left sub tree recursively Done with or No left sub tree? Then, visit the right sub tree recursively Done with or No right sub tree? Then go back to the calling function

Postorder Traversal Algorithm postorder( T, v ) for each child w of v do recursively traverse the subtree rooted at w by calling postorder( T, w ) perform the visit action for node v void postorderPrint(const Tree& T, const Position& v ){ PositionIterator children = T.children(v); while( children.hasNext() ) { postorderPrint( T, children.next() ) } cout << v.element() << " "; }

Postorder Traversal T Another way to make sure that we visit every node is with LRV, where you start by visiting each subtree, and then visit the node ( a node is visited after its descendants ) NOW, visit the node Then go back to the calling function Visit the left sub tree recursively Done with or No left sub tree? Then, visit the right sub tree recursively Done with or No right sub tree?

Inorder Traversal Algorithm inorder( T, v ) { if v is an internal node then inorder ( T, T.leftChild(v) ) perform the visit action for node v if v is an internal node then inorder ( T, T.rightChild(v) ) }

Inorder Traversal T Another way to make sure that we visit every node is with LVR (a node is visited after its left descendants but before its right ones) NOW, visit the node Visit the left sub tree recursively Done with or No left sub tree? Then, visit the right sub tree recursively Done with or No right sub tree? Then go back to the calling function