1 Binary Tree Traversals Binary Tree Traversal classification BreadthFirst traversal DepthFirst traversal Accept method of BinaryTree class Binary Tree.

Slides:



Advertisements
Similar presentations
Trees 2 and Doubly Linked Lists As you arrive: Please snarf today’s code.
Advertisements

Binary Tree Traversals
Traversals A systematic method to visit all nodes in a tree Binary tree traversals: Pre-order: root, left, right In-order: left, root, right Post-order:
Lecture 16: Tree Traversal.
Main Index Contents 11 Main Index Contents Week 6 – Binary Trees.
1 Binary Search Trees II Chapter 6. 2 Objectives You will be able to use a binary search tree template with your own classes.
CS 171: Introduction to Computer Science II
DictionaryADT and Trees. Overview What is the DictionaryADT? What are trees? Implementing DictionaryADT with binary trees Balanced trees DictionaryADT.
Binary Trees 2 Prof. Sin-Min Lee Department of Computer Science.
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.
Graph Traversals Depth-First Traversal. The Algorithm.
Graph Traversals Depth-First Traversals. –Algorithms. –Example. –Implementation. Breadth-First Traversal. –The Algorithm. –Example. –Implementation. Review.
Razdan CST230http://dcst2.east.asu.edu/~razdan/cst230/ Razdan with contribution from others 1 Chapter 9 Trees Anshuman Razdan Div of Computing Studies.
1 Binary Tree Traversals Binary Tree Traversal classification Tree traversal animations DepthFirst traversal abstraction Accept method of AbstractTree.
Tree Implementations Chapter 24 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Tree Traversals A traversal is a way of walking the tree structure Some common traversals: –pre-order traversal –in-order traversal –post-order traversal.
CS 240: Data Structures Monday, July 30 th Binary Search Trees.
CS21, Tia Newhall Binary Search Trees (BST) 1.Hierarchical data structure with a single pointer to root node 2.Each node has at most two child nodes (a.
Tree Traversals & Maps Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
CS261 Data Structures Tree Traversals. Goals Euler Tours Recursive Implementation Tree Sort Algorithm.
Joseph Lindo Trees Sir Joseph Lindo University of the Cordilleras.
Department of Computer Engineering Faculty of Engineering, Prince of Songkla University 1 8 – Trees.
Thought for the Day “To become truly great, one has to stand with people, not above them.” – Charles de Montesquieu.
1 TK1924 Program Design & Problem Solving Session 2011/2012 L8: 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.
2013-T2 Lecture 22 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, and Thomas.
Binary Trees 2 Overview Trees. Terminology. Traversal of Binary Trees. Expression Trees. Binary Search Trees.
Lecture – Searching a Tree Neil Ghani University of Strathclyde.
Data Structure Chapter# 5 Tree Course Instructor AMEER JAMAL SHAH.
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.
Week 7 - Friday.  What did we talk about last time?  Trees in general  Binary search trees.
Computer Science 112 Fundamentals of Programming II Introduction to Trees.
CSE 3358 NOTE SET 10 Data Structures and Algorithms.
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. 1 Chapter 19 Binary Search 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.
Discrete Mathematics Chapter 5 Trees.
Binary Tree. Some Terminologies Short review on binary tree Tree traversals Binary Search Tree (BST)‏ Questions.
1 Lecture 21: Binary Search Tree delete etc. operations Lecturer: Santokh Singh CompSci 105 SS 2005 Principles of Computer Science.
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.
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.
Tree Implementations Chapter 24 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions with Java,
Binary Tree Implementation. Binary Search Trees (BST) Nodes in Left subtree has smaller values Nodes in right subtree has bigger values.
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.
Data Structures Azhar Maqsood School of Electrical Engineering and Computer Sciences (SEECS-NUST) Binary Trees.
U n i v e r s i t y o f H a i l 1 ICS 202  2011 spring  Data Structures and Algorithms 
Foundation of Computing Systems Lecture 4 Trees: Part I.
More Binary Search Trees, Project 2 Bryce Boe 2013/08/06 CS24, Summer 2013 C.
Hello Everyone!!! 1. Tree And Graphs 2 Features of Trees  Tree Nodes Each node have 0 or more children A node have must one parent  Binary tree Tree.
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. 
Fundamentals of Algorithms MCS - 2 Lecture # 17. Binary Search Trees.
CC 215 DATA STRUCTURES TREES TRAVERSALS Dr. Manal Helal - Fall 2014 Lecture 9 AASTMT Engineering and Technology College 1.
Fundamentals of Programming II Introduction to Trees
Recursive Objects (Part 4)
Lecture No.13 Data Structures Dr. Sohail Aslam
Week 6 - Wednesday CS221.
Binary Search Tree (BST)
Tree.
Section 8.1 Trees.
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Trees.
Abstract Data Structures
Trees.
Binary Trees.
Binary Tree Traversals
Binary Tree Traversals
CSC 143 Java Trees.
Chapter 20: Binary Trees.
Binary Tree Traversals
Binary Tree Iterators Tree Traversals: preorder, inorder, postorder
Presentation transcript:

1 Binary Tree Traversals Binary Tree Traversal classification BreadthFirst traversal DepthFirst traversal Accept method of BinaryTree class Binary Tree Iterator

2 Tree Traversal (Definition) The process of systematically visiting all the nodes in a tree and performing some computation at each node in the tree is called a tree traversal. There are two methods in which to traverse a tree: 1.Breadth-First Traversal. 2.Depth-First Traversal: Preorder traversal Inorder traversal (for binary trees only) Postorder traversal

3 Breadth-First Traversal The BinaryTree class breadthFirstTraversal method: public void breadthFirstTraversal(Visitor visitor){ QueueAsLinkedList queueaslinkedlist = new QueueAsLinkedList(); if(!isEmpty()) queueaslinkedlist.enqueue(this); while(!queueaslinkedlist.isEmpty() && !visitor.isDone()){ BinaryTree tree = (BinaryTree)queueaslinkedlist.dequeue(); visitor.visit(tree.getKey()); if (!tree.getLeft().isEmpty()) queueaslinkedlist.enqueue(tree.getLeft()); if (!tree.getRight().isEmpty()) queueaslinkedlist.enqueue(tree.getRight()); }

4 Breadth-First Traversal H D B A C E G I KMO N L J F OMKIGECANJFBLDH

5 Depth-First Traversals CODEfor each Node:Name public void preorderTraversal(Visitor v){ if(!isEmpty() && ! v.isDone()){ v.visit(getKey()); getLeft().preorderTraversal(v); getRight().preorderTraversal(v); } Visit the node Visit the left subtree, if any. Visit the right subtree, if any. Preorder (N-L-R) public void inorderTraversal(Visitor v){ if(!isEmpty() && ! v.isDone()){ getLeft().inorderTraversal(v); v.visit(getKey()); getRight().inorderTraversal(v); } Visit the left subtree, if any. Visit the node Visit the right subtree, if any. Inorder (L-N-R) public void postorderTraversal(Visitor v){ if(!isEmpty() && ! v.isDone()){ getLeft().postorderTraversal(v) ; getRight().postorderTraversal(v); v.visit(getKey()); } Visit the left subtree, if any. Visit the right subtree, if any. Visit the node Postorder (L-R-N)

6 Depth-first Preorder Traversal H D B A C E G I KMO N L J F OMNKIJLGEFCABDH N-L-R

7 Depth-first Inorder Traversal H D B A C E G I KMO N L J F L-N-R ONMLKJIHGFEDCBA Note: An inorder traversal of a BST visits the keys sorted in increasing order.

8 Depth-first Postorder Traversal H D B A C E G I KMO N L J F L-R-N HLNOMJKIDFGEBCA

9 Traversals The following code illustrates how to display the contents of a Binary tree using each traversal method. Visitor v = new PrintingVisitor() ; BinaryTree t = new BinaryTree() ; //... t.breadthFirstTraversal(v) ; t.postorderTraversal(v) ; t.inorderTraversal(v) ; t.postorderTraversal(v) ;

10 The accept method of the BinaryTree class Usually the accept method of a container is allowed to visit the elements of the container in any order. A depth-first tree traversal visits the nodes in either preoder or postorder and for Binary trees inorder traversal is also possible. The BinaryTree class accept method does a preorder traversal: public void accept(Visitor visitor) { preorderTraversal(visitor) ; }

11 Binary Tree Iterator The BinaryTree class provides a tree iterator that does a preorder traversal. The iterator is implemented as an inner class: private class BinaryTreeIterator implements Iterator{ Stack stack; public BinaryTreeIterator(){ stack = new StackAsLinkedList(); if(!isEmpty())stack.push(BinaryTree.this); } public boolean hasNext(){return !stack.isEmpty();} public Object next(){ if(stack.isEmpty())throw new NoSuchElementException(); BinaryTree tree = (BinaryTree)stack.pop(); if (!tree.getRight().isEmpty()) stack.push(tree.getRight()); if (!tree.getLeft().isEmpty()) stack.push(tree.getLeft()); return tree.getKey(); }

12 Using a Binary Tree Iterator The iterator() method of the BinaryTree class returns a new instance of the BinaryTreeIterator inner class each time it is called: The following program fragment shows how to use a tree iterator: public Iterator iterator(){ return new BinaryTreeIterator(); } BinaryTree tree = new BinaryTree() ; //... Iterator i = tree.iterator() ; while(i.hasNext(){ Object obj = e.next() ; System.out.print(obj + " ") ; }