Tree Traversals A traversal is a way of walking the tree structure Some common traversals: –pre-order traversal –in-order traversal –post-order traversal.

Slides:



Advertisements
Similar presentations
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 20: Binary Trees.
Advertisements

CS Fall 2012, Lab 08 Haohan Zhu. Boston University Slideshow Title Goes Here CS Fall 2012, Lab /17/2015 Tree - Data Structure  Basic.
SUNY Oneonta Data Structures and Algorithms Visualization Teaching Materials Generation Group Binary Search Tree A running demonstration of binary search.
Lecture 16: Tree Traversal.
CS 261 – Recitation 9 & 10 Graphs & Final review
Binary Trees Terminology A graph G = is a collection of nodes and edges. An edge (v 1,v 2 ) is a pair of vertices that are directly connected. A path,
Binary Tree Terminology Linear versus hierarchical data Tree – connected graph with no cycles Child Parent Descendant Sibling Ancestor Leaf vs. internal.
Lists A list is a finite, ordered sequence of data items. Two Implementations –Arrays –Linked Lists.
Fall 2007CS 2251 Iterators and Tree Traversals. Fall 2007CS 2252 Binary Trees In a binary tree, each node has at most two subtrees A set of nodes T is.
Trees CMSC 433 Chapter 8.1 Nelson Padua-Perez Bill Pugh.
Preorder Traversal with a Stack Push the root onto the stack. While the stack is not empty n pop the stack and visit it.
Binary Trees Like a list, a (binary) tree can be empty or non-empty. In class we will explore a state-based implementation, similar to the LRStruct You.
Data Structures Using C++1 Chapter 11 Binary Trees.
Data Structures Using C++1 Chapter 11 Binary Trees.
1 Chapter 18 Trees Objective To learn general trees and recursion binary trees and recursion tree traversal.
Department of Computer Engineering Faculty of Engineering, Prince of Songkla University 1 8 – Trees.
Trees Chapter 8. 2 Tree Terminology A tree consists of a collection of elements or nodes, organized hierarchically. The node at the top of a tree is called.
Traversing a Binary Tree 10/23/ Traversing Binary Tree There are 3 ways of traversing a binary tree T having root R. 1. Preorder Traversing Steps:
1 Trees Tree nomenclature Implementation strategies Traversals –Depth-first –Breadth-first Implementing binary search trees.
Chapter 19: Binary Trees. Objectives In this chapter, you will: – Learn about binary trees – Explore various binary tree traversal algorithms – Organize.
CS Data Structures Chapter 5 Trees. Chapter 5 Trees: Outline  Introduction  Representation Of Trees  Binary Trees  Binary Tree Traversals 
1 Joe Meehean. A A B B D D I I C C E E X X A A B B D D I I C C E E X X  Terminology each circle is a node pointers are edges topmost node is the root.
Binary Trees Chapter Definition And Application Of Binary Trees Binary tree: a nonlinear linked list in which each node may point to 0, 1, or two.
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.
Trees Chapter 8. 2 Tree Terminology A tree consists of a collection of elements or nodes, organized hierarchically. The node at the top of a tree is called.
Starting at Binary Trees
Binary Search Tree Traversal Methods. How are they different from Binary Trees?  In computer science, a binary tree is a tree data structure in which.
Dynamic Data Structures Stacks, Queues and Binary Trees hold dynamic data.
Trees : Part 1 Section 4.1 (1) Theory and Terminology (2) Preorder, Postorder and Levelorder Traversals.
Computer Science 112 Fundamentals of Programming II Introduction to Trees.
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
IKI 10100I: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100I: Data.
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.
Data Structures Using Java1 Chapter 10 Binary Trees.
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.
Tree Implementations Chapter 24 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions with Java,
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.
Binary Tree.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 20: Binary Trees.
1 Joe Meehean. A A B B D D I I C C E E X X A A B B D D I I C C E E X X  Terminology each circle is a node pointers are edges topmost node is the root.
CMSC 202, Version 5/02 1 Trees. CMSC 202, Version 5/02 2 Tree Basics 1.A tree is a set of nodes. 2.A tree may be empty (i.e., contain no nodes). 3.If.
Graph Traversal Text Weiss, § 9.6 Depth-First Search Think Stack Breadth-First Search Think Queue.
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 Trees : Part 1 Reading: Section 4.1 Theory and Terminology Preorder, Postorder and Levelorder Traversals.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 20: Binary Trees.
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture13.
Iterators. Iterator  An iterator is any object that allows one to step through each element in a list (or, more generally, some collection).
1 Trees. 2 Trees Trees. Binary Trees Tree Traversal.
Recursive Objects (Part 4)
Trees ---- Soujanya.
Tree.
Section 8.1 Trees.
Chapter 20: Binary Trees.
CS223 Advanced Data Structures and Algorithms
8.2 Tree Traversals Chapter 8 - Trees.
Chapter 21: Binary Trees.
Abstract Data Structures
Trees Definitions Implementation Traversals K-ary Trees
CSE 373, Copyright S. Tanimoto, 2002 Binary Trees -
CSE 373, Copyright S. Tanimoto, 2001 Binary Trees -
Trees.
Chapter 20: Binary Trees.
Binary Trees.
Data Structures Using C++ 2E
Binary Tree Iterators Tree Traversals: preorder, inorder, postorder
Presentation transcript:

Tree Traversals A traversal is a way of walking the tree structure Some common traversals: –pre-order traversal –in-order traversal –post-order traversal

(Depth-first) traversal path Fred WilmaBetty Barney Pebbles

Each node is reached three times Fred WilmaBetty Barney Pebbles

Pre-order traversal (1) visit node before children Fred WilmaBetty Barney Pebbles Fred  Betty  Barney  Wilma  Pebbles

In-order traversal (2) visit node between children Fred WilmaBetty Barney Pebbles Barney  Betty  Fred  Pebbles  Wilma

Post-order traversal (3) visit node after children Fred WilmaBetty Barney Pebbles Barney  Betty  Pebbles  Wilma  Fred

Example Fred WilmaBetty Barney Pebbles Pre-order traversal: Fred Betty Barney Wilma Pebbles In-order traversal: Barney Betty Fred Pebbles Wilma Post-order traversal: Barney Betty Pebbles Wilma Fred

Tree iterators We can define tree iterators which follow the same traversal path. Unlike the visitors, iterators stop at each node: they must remember where they are! Let us consider first an in-order iterator.

Behavior of an inOrderIterator > java.util.Iterator it = bst.inOrderIterator(); > System.out.println("it.hasNext() ==> "+it.hasNext()); it.hasNext() ==> true Fred WilmaBetty Barney Pebbles

Behavior of an inOrderIterator > java.util.Iterator it = bst.inOrderIterator(); > System.out.println("it.hasNext() ==> "+it.hasNext()); it.hasNext() ==> true > System.out.println("it.next() ==> " + it.next()); it.next() ==> Barney > System.out.println("it.hasNext() ==> "+it.hasNext()); it.hasNext() ==> true Fred WilmaBetty Barney Pebbles

Behavior of an inOrderIterator > java.util.Iterator it = bst.inOrderIterator(); > System.out.println("it.hasNext() ==> "+it.hasNext()); it.hasNext() ==> true > System.out.println("it.next() ==> " + it.next()); it.next() ==> Barney > System.out.println("it.hasNext() ==> "+it.hasNext()); it.hasNext() ==> true > System.out.println("it.next() ==> " + it.next()); it.next() ==> Betty > System.out.println("it.hasNext() ==> "+it.hasNext()); it.hasNext() ==> true Fred WilmaBetty Barney Pebbles

Behavior of an inOrderIterator > java.util.Iterator it = bst.inOrderIterator(); > System.out.println("it.hasNext() ==> "+it.hasNext()); it.hasNext() ==> true > System.out.println("it.next() ==> " + it.next()); it.next() ==> Barney > System.out.println("it.hasNext() ==> "+it.hasNext()); it.hasNext() ==> true > System.out.println("it.next() ==> " + it.next()); it.next() ==> Betty > System.out.println("it.hasNext() ==> "+it.hasNext()); it.hasNext() ==> true > System.out.println("it.next() ==> " + it.next()); it.next() ==> Fred > System.out.println("it.hasNext() ==> "+it.hasNext()); it.hasNext() ==> true Fred WilmaBetty Barney Pebbles

Behavior of an inOrderIterator > java.util.Iterator it = bst.inOrderIterator(); > System.out.println("it.hasNext() ==> "+it.hasNext()); it.hasNext() ==> true > System.out.println("it.next() ==> " + it.next()); it.next() ==> Barney > System.out.println("it.hasNext() ==> "+it.hasNext()); it.hasNext() ==> true > System.out.println("it.next() ==> " + it.next()); it.next() ==> Betty > System.out.println("it.hasNext() ==> "+it.hasNext()); it.hasNext() ==> true > System.out.println("it.next() ==> " + it.next()); it.next() ==> Fred > System.out.println("it.hasNext() ==> "+it.hasNext()); it.hasNext() ==> true > System.out.println("it.next() ==> " + it.next()); it.next() ==> Pebbles > System.out.println("it.hasNext() ==> "+it.hasNext()); it.hasNext() ==> true Fred WilmaBetty Barney Pebbles

Behavior of an inOrderIterator > java.util.Iterator it = bst.inOrderIterator(); > System.out.println("it.hasNext() ==> "+it.hasNext()); it.hasNext() ==> true > System.out.println("it.next() ==> " + it.next()); it.next() ==> Barney > System.out.println("it.hasNext() ==> "+it.hasNext()); it.hasNext() ==> true > System.out.println("it.next() ==> " + it.next()); it.next() ==> Betty > System.out.println("it.hasNext() ==> "+it.hasNext()); it.hasNext() ==> true > System.out.println("it.next() ==> " + it.next()); it.next() ==> Fred > System.out.println("it.hasNext() ==> "+it.hasNext()); it.hasNext() ==> true > System.out.println("it.next() ==> " + it.next()); it.next() ==> Pebbles > System.out.println("it.hasNext() ==> "+it.hasNext()); it.hasNext() ==> true > System.out.println("it.next() ==> " + it.next()); it.next() ==> Wilma > System.out.println("it.hasNext() ==> "+it.hasNext()); it.hasNext() ==> false Fred WilmaBetty Barney Pebbles

Implementation? How is state of iterator maintained?

Implementation: Using a stack! On creation of the iterator, references to all nonEmpty trees along the left edge of the tree are pushed onto the stack hasNext() is implemented to return !_stack.isEmpty()

Behavior of an inOrderIterator > java.util.Iterator it = bst.inOrderIterator(); > System.out.println("it.hasNext() ==> "+it.hasNext()); it.hasNext() ==> true Fred WilmaBetty Barney Pebbles

Implementation: The next() method pops an item off the stack, walks down its right child’s left edge (pushing BRS references onto the stack along the way)

Behavior of an inOrderIterator > java.util.Iterator it = bst.inOrderIterator(); > System.out.println("it.hasNext() ==> "+it.hasNext()); it.hasNext() ==> true > System.out.println("it.next() ==> " + it.next()); it.next() ==> Barney > System.out.println("it.hasNext() ==> "+it.hasNext()); it.hasNext() ==> true Fred WilmaBetty Barney Pebbles

Behavior of an inOrderIterator > java.util.Iterator it = bst.inOrderIterator(); > System.out.println("it.hasNext() ==> "+it.hasNext()); it.hasNext() ==> true > System.out.println("it.next() ==> " + it.next()); it.next() ==> Barney > System.out.println("it.hasNext() ==> "+it.hasNext()); it.hasNext() ==> true > System.out.println("it.next() ==> " + it.next()); it.next() ==> Betty > System.out.println("it.hasNext() ==> "+it.hasNext()); it.hasNext() ==> true Fred WilmaBetty Barney Pebbles

Behavior of an inOrderIterator > java.util.Iterator it = bst.inOrderIterator(); > System.out.println("it.hasNext() ==> "+it.hasNext()); it.hasNext() ==> true > System.out.println("it.next() ==> " + it.next()); it.next() ==> Barney > System.out.println("it.hasNext() ==> "+it.hasNext()); it.hasNext() ==> true > System.out.println("it.next() ==> " + it.next()); it.next() ==> Betty > System.out.println("it.hasNext() ==> "+it.hasNext()); it.hasNext() ==> true > System.out.println("it.next() ==> " + it.next()); it.next() ==> Fred > System.out.println("it.hasNext() ==> "+it.hasNext()); it.hasNext() ==> true Fred WilmaBetty Barney Pebbles

Behavior of an inOrderIterator > java.util.Iterator it = bst.inOrderIterator(); > System.out.println("it.hasNext() ==> "+it.hasNext()); it.hasNext() ==> true > System.out.println("it.next() ==> " + it.next()); it.next() ==> Barney > System.out.println("it.hasNext() ==> "+it.hasNext()); it.hasNext() ==> true > System.out.println("it.next() ==> " + it.next()); it.next() ==> Betty > System.out.println("it.hasNext() ==> "+it.hasNext()); it.hasNext() ==> true > System.out.println("it.next() ==> " + it.next()); it.next() ==> Fred > System.out.println("it.hasNext() ==> "+it.hasNext()); it.hasNext() ==> true > System.out.println("it.next() ==> " + it.next()); it.next() ==> Pebbles > System.out.println("it.hasNext() ==> "+it.hasNext()); it.hasNext() ==> true Fred WilmaBetty Barney Pebbles

Behavior of an inOrderIterator > java.util.Iterator it = bst.inOrderIterator(); > System.out.println("it.hasNext() ==> "+it.hasNext()); it.hasNext() ==> true > System.out.println("it.next() ==> " + it.next()); it.next() ==> Barney > System.out.println("it.hasNext() ==> "+it.hasNext()); it.hasNext() ==> true > System.out.println("it.next() ==> " + it.next()); it.next() ==> Betty > System.out.println("it.hasNext() ==> "+it.hasNext()); it.hasNext() ==> true > System.out.println("it.next() ==> " + it.next()); it.next() ==> Fred > System.out.println("it.hasNext() ==> "+it.hasNext()); it.hasNext() ==> true > System.out.println("it.next() ==> " + it.next()); it.next() ==> Pebbles > System.out.println("it.hasNext() ==> "+it.hasNext()); it.hasNext() ==> true > System.out.println("it.next() ==> " + it.next()); it.next() ==> Wilma > System.out.println("it.hasNext() ==> "+it.hasNext()); it.hasNext() ==> false Fred WilmaBetty Barney Pebbles