Binary Tree Traversals

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

Trees 2 and Doubly Linked Lists As you arrive: Please snarf today’s code.
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.
Computer Science C++ High School Level By Guillermo Moreno.
1 Trees Tree nomenclature Implementation strategies Traversals –Depth-first –Breadth-first Implementing binary trees Reading: L&C 9.1 – 9.7.
DictionaryADT and Trees. Overview What is the DictionaryADT? What are trees? Implementing DictionaryADT with binary trees Balanced trees DictionaryADT.
1 Binary Tree Traversals Binary Tree Traversal classification BreadthFirst traversal DepthFirst traversal Accept method of BinaryTree class Binary Tree.
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.
Binary Tree B G E D I H F c A Binary tree
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.
Graph Traversals General Traversal Algorithm Depth-First Traversals. –Algorithms. –Example. –Implementation. Breadth-First Traversal. –The Algorithm. –Example.
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 & 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.
Chapter 18 - basic definitions - binary trees - tree traversals Intro. to Trees 1CSCI 3333 Data Structures.
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.
2013-T2 Lecture 22 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, and Thomas.
Tree Traversal.
Lecture – Searching a Tree Neil Ghani University of Strathclyde.
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.
Computer Science 112 Fundamentals of Programming II Introduction to Trees.
COMP 103 Traversing Trees. 2 RECAP-TODAY RECAP  Building and Traversing Trees TODAY  Traversing Trees  Chapter 16, especially 16.2.
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.
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,
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.
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 
More Binary Search Trees, Project 2 Bryce Boe 2013/08/06 CS24, Summer 2013 C.
Chapter 10 Trees © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
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)
Week 6 - Wednesday CS221.
Binary Search Tree (BST)
Trees 2 CSC 172 SPRING 2004 LECTURE 15.
Section 8.1 Trees.
Trees.
Binary Trees.
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Abstract Data Structures
Trees.
Tree data structure.
Binary Trees.
Binary Trees.
Principles of Computing – UFCFA3-30-1
Chapter 9 Binary Trees.
Binary Tree Traversals
CSC 143 Java Trees.
Chapter 20: Binary Trees.
Trees.
Data Structures Using C++ 2E
Binary Tree Traversals
Binary Tree Iterators Tree Traversals: preorder, inorder, postorder
Presentation transcript:

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

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: Breadth-First Traversal. Depth-First Traversal: Preorder traversal Inorder traversal (for binary trees only) Postorder traversal

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()); }

Breadth-First Traversal C E G I K M O N L J F O M K I G E C A N J F B L D H

Depth-First Traversals CODE for 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){ getLeft().inorderTraversal(v); getRight().inorderTraversal(v); Visit the left subtree, if any. Visit the node Inorder (L-N-R) public void postorderTraversal(Visitor v){ getLeft().postorderTraversal(v) ; getRight().postorderTraversal(v); Postorder (L-R-N)

Depth-first Preorder Traversal N-L-R H D B A C E G I K M O N L J F O M N K I J L G E F C A B D H

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

Depth-first Postorder Traversal L-R-N H D B A C E G I K M O N L J F H L N O M J K I D F G E B C A

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.preorderTraversal(v) ; t.inorderTraversal(v) ; t.postorderTraversal(v) ;

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) ; }

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();

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 + " ") ; }