Binary Tree Iterators Tree Traversals: preorder, inorder, postorder

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

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.
CS 104 Introduction to Computer Science and Graphics Problems Data Structure & Algorithms (4) Data Structures 11/18/2008 Yang Song.
Binary Tree B G E D I H F c A Binary tree
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.
1 General Trees & Binary Trees CSC Trees Previous data structures (e.g. lists, stacks, queues) have a linear structure. Linear structures represent.
Tree Traversals A traversal is a way of walking the tree structure Some common traversals: –pre-order traversal –in-order traversal –post-order traversal.
Preorder Traversal with a Stack Push the root onto the stack. While the stack is not empty n pop the stack and visit it.
By : Budi Arifitama Pertemuan ke Objectives Upon completion you will be able to: Create and implement binary search trees Understand the operation.
1 Chapter 25 Trees Iterators Heaps Priority Queues.
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:
Lecture 17 Non-Linear data structures Richard Gesick.
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.
CISC220 Fall 2009 James Atlas Lecture 13: Trees. Skip Lists.
For Monday Read Weiss, chapter 7, sections 1-3. Homework –Weiss, chapter 4, exercise 6. Make sure you include parentheses where appropriate.
Binary Trees 2 Overview Trees. Terminology. Traversal of Binary Trees. Expression Trees. Binary Search Trees.
Tree (new ADT) Terminology:  A tree is a collection of elements (nodes)  Each node may have 0 or more successors (called children)  How many does a.
Chapter 19: Binary Trees Java Programming: Program Design Including Data Structures Program Design Including Data Structures.
Starting at Binary Trees
 Trees Data Structures Trees Data Structures  Trees Trees  Binary Search Trees Binary Search Trees  Binary Tree Implementation Binary Tree Implementation.
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.
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.
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.
Copyright © 2012 Pearson Education, Inc. Chapter 20: Binary Trees.
COSC 2P03 Week 21 Tree Traversals – reminder Breadth-first traversal: starting from root, visit all nodes on each level in turn, from left to right Depth-first.
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.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 20: Binary Trees.
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.
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.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 20: Binary Trees.
DS.T.1 Trees Chapter 4 Overview Tree Concepts Traversals Binary Trees Binary Search Trees AVL Trees Splay Trees B-Trees.
SUYASH BHARDWAJ FACULTY OF ENGINEERING AND TECHNOLOGY GURUKUL KANGRI VISHWAVIDYALAYA, HARIDWAR.
TREES From root to leaf. Trees  A tree is a non-linear collection  The elements are in a hierarchical arrangement  The elements are not accessible.
Binary Search Trees Chapter 7 Objectives
Chapter 12 – Data Structures
Binary Tree ADT: Properties
Chapter 25 Binary Search Trees
Lecture No.14 Data Structures Dr. Sohail Aslam
Fundamentals of Programming II Introduction to Trees
Recursive Objects (Part 4)
Data Structures Binary Trees 1.
Binary search tree. Removing a node
Binary Search Tree (BST)
Tree.
Section 8.1 Trees.
Trees.
Tree Traversals – reminder
Chapter 20: Binary Trees.
CS223 Advanced Data Structures and Algorithms
Tree Traversals – reminder
Chapter 21: Binary Trees.
Binary Trees.
General Trees & Binary Trees
Binary Tree Traversal Methods
Abstract Data Structures
Trees.
Binary Trees.
Binary Trees.
Trees Definitions Implementation Traversals K-ary Trees
Binary Tree Traversal Methods
Binary Tree Traversals
General Trees & Binary Trees
2018, Fall Pusan National University Ki-Joune Li
if the tree is empty, do nothing,
Trees.
Chapter 20: Binary Trees.
Trees.
Non-Linear data structures
Presentation transcript:

Binary Tree Iterators Tree Traversals: preorder, inorder, postorder public interface java.util.Iterator<E> boolean hasNext() E next() void remove() /*optional – removes the last E returned by the Iterator in the underlying Collection */ Bailey 11.6

Binary Tree Iterators Implementations often maintain a linear structure that keeps track of the state of tht iterator. java.util.Stack<E> boolean empty() E pop() E push(E)

Inorder Iterator When is each node visited? After all of the nodes in its left subtree have been visited and before Any of the nodes of the right subtree

Inorder Iterator – 1 8 6 12 4 11 17 2 5 10 20 1 3 The first element visited in an in-order traversal is the left-most descendant of the root. To initialize the iterator: push each of the nodes from the root down to the leftmost descendant on the stack.

Inorder Iterator – 2 BT<E> currrent = root; 8 6 12 4 11 17 2 5 10 20 1 3 tos BT<E> currrent = root; while (current != null) { stack.push(current); current = current.left; } 1 2 4 6 8

Inorder Iterator – 3 next() What do we know about current in the tree? Returns the current value Increments the iterator BTN<E> current = stack.pop(); E value = current.getValue(); What do we know about current in the tree? All left children have been visited It may or may not have a right subtree

Inorder Iterator - 4 Two cases: If current has no right subtree, we are done. The next node to be considered is at the top of the stack. If current has a right subtree: Those nodes haven't been visited They are not on the stack ...so... Push the right child and each of the right child's left descendants on the stack.

Inorder Iterator – 5 current 8 before: 12 tos: empty 11 17 10 20 tos after: 10 11 12