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.

Slides:



Advertisements
Similar presentations
©Brooks/Cole, 2003 Chapter 12 Abstract Data Type.
Advertisements

CS 261 – Recitation 9 & 10 Graphs & Final review
Main Index Contents 11 Main Index Contents Week 6 – Binary Trees.
1 Trees Tree nomenclature Implementation strategies Traversals –Depth-first –Breadth-first Implementing binary trees Reading: L&C 9.1 – 9.7.
CS 171: Introduction to Computer Science II
©Brooks/Cole, 2003 Chapter 12 Abstract Data Type.
DictionaryADT and Trees. Overview What is the DictionaryADT? What are trees? Implementing DictionaryADT with binary trees Balanced trees DictionaryADT.
CS 307 Fundamentals of Computer Science 1 Abstract Data Types many slides taken from Mike Scott, UT Austin.
Lists A list is a finite, ordered sequence of data items. Two Implementations –Arrays –Linked Lists.
CS102 – Data Structures Lists, Stacks, Queues, Trees, Hash Collections Framework David Davenport Spring 2002.
CS 104 Introduction to Computer Science and Graphics Problems Data Structure & Algorithms (4) Data Structures 11/18/2008 Yang Song.
©Brooks/Cole, 2003 Chapter 12 Abstract Data Type.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L12 (Chapter 20) Lists, Stacks,
Chapter 12 C Data Structures Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 12 – Data Structures Outline 12.1Introduction.
Chapter 12 Trees. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Define trees as data structures Define the terms.
Trees Chapter 23 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Tree Traversals & Maps Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
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.
Trees & Graphs Chapter 25 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X.
1 Trees Tree nomenclature Implementation strategies Traversals –Depth-first –Breadth-first Implementing binary search 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.
Jan 12, 2012 Introduction to Collections. 2 Collections A collection is a structured group of objects Java 1.2 introduced the Collections Framework Collections.
Topic 14 The BinaryTree ADT Objectives Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms.
Chapter 19 Implementing Trees and Priority Queues Fundamentals of Java.
CISC220 Fall 2009 James Atlas Lecture 13: Trees. Skip Lists.
Chapter 19 Implementing Trees and Priority Queues Fundamentals of Java.
2013-T2 Lecture 22 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, and Thomas.
Chapter 10-A Trees Modified
1 Chapter 17 Object-Oriented Data Structures. 2 Objectives F To describe what a data structure is (§17.1). F To explain the limitations of arrays (§17.1).
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.
Binary trees Binary search trees Expression trees Heaps Data Structures and Algorithms in Java, Third EditionCh06 – 1.
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.
Data Structures Chapter 6. Data Structure A data structure is a representation of data and the operations allowed on that data. Examples: 1.Array 2.Record.
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.
Chapter 12 Abstract Data Type. Understand the concept of an abstract data type (ADT). Understand the concept of a linear list as well as its operations.
1/14/20161 BST Operations Data Structures Ananda Gunawardena.
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.
Trees Ellen Walker CPSC 201 Data Structures Hiram College.
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.
Foundation of Computing Systems Lecture 4 Trees: Part I.
COSC 2P03 Week 21 Stacks – review A Last-In First-Out (LIFO) structure Basic Operations: –push : insert data item onto top of stack –pop : remove data.
Graph Traversal Text Weiss, § 9.6 Depth-First Search Think Stack Breadth-First Search Think Queue.
CSE 373 Data Structures and Algorithms Lecture 9: Set ADT / Trees.
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.
Chapter 10 Trees © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture13.
DS.T.1 Trees Chapter 4 Overview Tree Concepts Traversals Binary Trees Binary Search Trees AVL Trees Splay Trees B-Trees.
The Tree ADT.
Chapter 12 – Data Structures
Trees Chapter 15.
Trees Chapter 11 (continued)
Fundamentals of Programming II Introduction to Trees
Recursive Objects (Part 4)
Trees Chapter 11 (continued)
Tree.
Section 8.1 Trees.
Map interface Empty() - return true if the map is empty; else return false Size() - return the number of elements in the map Find(key) - if there is an.
Alyce Brady CS 470: Data Structures CS 510: Computer Algorithms
Lesson Objectives Aims
Binary Tree Traversals
Chapter 10 1 – Binary Trees Tree Structures (3 slides)
Chapter 20: Binary Trees.
Trees.
Binary Tree Implementation And Applications
CS2005 Week 8 Lectures Maps & Binary Trees.
Binary Tree Iterators Tree Traversals: preorder, inorder, postorder
Presentation transcript:

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 a binary tree if either of the following is true –T is empty –Its root node has two subtrees, TL and TR, such that TL and TR are binary trees

Fall 2007CS 2253 Tree Traversals Often we want to visit all the nodes of a tree and do something with each –A tree traversal is a path that visits every node in the tree Two ways to traverse a tree –Depth first - Inorder, Preorder and Postorder traversals – Breadth-first - Level order traversal

Fall 2007CS 2254 Depth-First Tree Traversals Preorder Tree Traversal –Visit root node, traverse TL, traverse TR Inorder Tree Traversal –Traverse TL, visit root node, traverse TR Postorder Tree Traversal –Traverse TL, Traverse TR, visit root node

Fall 2007CS 2255 Level-Order Traversal Visit all nodes at one level of the tree before going on to the next level In the tree below, a level-order traversal gives a-b-c-d-e-f-g-h-i-j-k

Fall 2007CS 2256 Implementing Traversals How do we write methods to traverse a tree? –For depth-first traversals, we can use recursive methods But what if we want an iterator which gives only one element at a time? –For level-order traversals, algorithm isn't so obvious

Fall 2007CS 2257 Common Features of Collections Collection interface specifies a set of common methods Fundamental features include: –Collections grow as needed –Collections hold references to objects –Collections have at least two constructors

Fall 2007CS 2258 Collection Interface add( E element) addAll( Collection coll) clear() contains( Object e) containsAll( Collection coll) isEmpty() Iterator iterator() remove( Object e) removeAll( Collection coll) retainAll( Collection coll) size() toArray()

Fall 2007CS 2259 Collections API Implementation Hash Table Resizable Array Balanced Tree Linked List Hash Table + Linked List SetHashSetTreeSetLinkedHashSet ListArrayListLinkedList DequeArrayDequeLinkedList MapHashMapTreeMapLinkedHashMap

Fall 2007CS Iterable Interface Contains the method –Iterator iterator() The Collection interface extends the Iterable interface, so all classes that implement the Collection interface must provide an iterator method

Fall 2007CS The Iterator Interface The interface Iterator is defined as part of API package java.util Iterator methods –boolean hasNext() –E next() –void remove()

Fall 2007CS Implementing Iterators For linear collections, implementing an iterator just requires keeping track of where you are in the collection with a reference variable or an index. For trees, the problem is a little more complicated –You can't interrupt a recursive method

Fall 2007CS Depth-first searches Use a stack to keep track of where you are in the recursive process Setting up the iterator push the root node on the stack Algorithm for next pre-order pop the top node process the node push right child push left child

Fall 2007CS Depth first iterators For the other traversals, each node needs to be pushed multiple times –need to keep track of how many times it has been pushed For in-order searches the node gets pushed twice and processed on the second pop For post-order traversals the node gets pushed three times

Fall 2007CS Level-Order traversals In this case, we can use a queue Initializing the iterator enqueue the root Algorithm for next dequeue the first element enqueue the left child enqueue the right child process the removed node

Fall 2007CS Visitor Design Pattern This is a common way of handling the process of performing an action on each element of a collection The Visitor interface has a single method –visit( E element) The collection class can have traversal methods that take a Visitor as a parameter