CSC 205 Java Programming II Lecture 26 Traversing Binary Tree.

Slides:



Advertisements
Similar presentations
COSC2007 Data Structures II Chapter 10 Trees II. 2 Topics ADT Binary Tree (BT) Operations Tree traversal BT Implementation Array-based LL-based Expression.
Advertisements

Chapter 7. Binary Search Trees
SUNY Oneonta Data Structures and Algorithms Visualization Teaching Materials Generation Group Binary Search Tree A running demonstration of binary search.
CSC 205 – Java Programming II Lecture 35 April 17, 2002.
Binary Trees Chapter 6. Linked Lists Suck By now you realize that the title to this slide is true… By now you realize that the title to this slide is.
© 2006 Pearson Addison-Wesley. All rights reserved11 B-1 Chapter 11 (continued) Trees.
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 Trees.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L12 (Chapter 20) Lists, Stacks,
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 19 Binary.
Marc Smith and Jim Ten Eyck
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 19: Binary Trees.
Binary Search Trees Chapter 7 Objectives
By : Budi Arifitama Pertemuan ke Objectives Upon completion you will be able to: Create and implement binary search trees Understand the operation.
Properties: -Each node has a value -The left subtree contains only values less than the parent node’s value -The right subtree contains only values greater.
CSC 205 Java Programming II Lecture 25 Binary Tree.
Chapter 11 A Trees. © 2004 Pearson Addison-Wesley. All rights reserved 11 A-2 Terminology A tree consists of vertices and edges, –An edge connects to.
Recursion Bryce Boe 2013/11/18 CS24, Fall Outline Wednesday Recap Lab 7 Iterative Solution Recursion Binary Tree Traversals Lab 7 Recursive Solution.
Course: Programming II - Abstract Data Types Slide Number 1 The ADT Binary Tree Definition The ADT Binary Tree is a finite set of nodes which is either.
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.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver. 5.0.
© 2011 Pearson Addison-Wesley. All rights reserved 11 B-1 Chapter 11 (continued) Trees.
Chapter 19: Binary Trees Java Programming: Program Design Including Data Structures Program Design Including Data Structures.
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.
 Trees Data Structures Trees Data Structures  Trees Trees  Binary Search Trees Binary Search Trees  Binary Tree Implementation Binary Tree Implementation.
Chapter 11 B Trees. © 2004 Pearson Addison-Wesley. All rights reserved 11 B-2 The ADT Binary Search Tree A deficiency of the ADT binary tree which is.
COSC2007 Data Structures II Chapter 11 Trees IV. 2 Topics ADT BST Implementations Efficiency TreeSort Save/Restore into/from file General Trees.
Binary trees Binary search trees Expression trees Heaps Data Structures and Algorithms in Java, Third EditionCh06 – 1.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Modified for use at Midwestern State University Chapter.
Topics Definition and Application of Binary Trees Binary Search Tree Operations.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 25 Trees, Iterators,
© 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.
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.
CSC 205 Java Programming II Lecture 28 BST Implementation.
1 Lecture 21: Binary Search Tree delete etc. operations Lecturer: Santokh Singh CompSci 105 SS 2005 Principles of Computer Science.
Binary Search Tree. Tree  A nonlinear data structure consisting of nodes, each of which contains data and pointers to other nodes.  Each node has only.
ADT Binary Search Tree Ellen Walker CPSC 201 Data Structures Hiram College.
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.
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 Trees.
Copyright © 2012 Pearson Education, Inc. Chapter 20: Binary Trees.
Data Structures: A Pseudocode Approach with C, Second Edition 1 Chapter 7 Objectives Create and implement binary search trees Understand the operation.
COSC2007 Data Structures II Chapter 11 Trees II. 2 Topics ADT Binary Tree (BT) Operations Tree traversal BT Implementation Array-based LL-based Expression.
Concepts of Algorithms CSC-244 Unit 19 & 20 Binary Search Tree (BST) Shahid Iqbal Lone Computer College Qassim University K.S.A.
Binary Tree Implementation. Binary Search Trees (BST) Nodes in Left subtree has smaller values Nodes in right subtree has bigger values.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 20: Binary Trees.
(c) University of Washington20c-1 CSC 143 Binary Search Trees.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 20: Binary Trees.
(c) University of Washington20-1 CSC 143 Java Trees.
Binary Search Trees Chapter 7 Objectives
Trees Chapter 11 (continued)
Trees Chapter 11 (continued)
Binary Search Tree (BST)
Chapter 11 Trees © 2011 Pearson Addison-Wesley. All rights reserved.
Csc 2720 Instructor: Zhuojun Duan
Section 8.1 Trees.
Chapter 20: Binary Trees.
Chapter 21: Binary Trees.
Binary Search Trees Chapter 7 Objectives
Stacks with Dynamic Memory
CSC 143 Java Trees.
Chapter 20: Binary Trees.
Chapter 11 Trees © 2011 Pearson Addison-Wesley. All rights reserved.
CSC 205 – Java Programming II
CSC 205 – Java Programming II
Data Structures Using C++ 2E
Chapter 11 Trees © 2011 Pearson Addison-Wesley. All rights reserved.
Binary Tree Iterators Tree Traversals: preorder, inorder, postorder
Presentation transcript:

CSC 205 Java Programming II Lecture 26 Traversing Binary Tree

TreeNode & BinaryTree root

Reference-Based Repres’n Needs a TreeNode class UML class diagram TreeNode -item:Object -left:TreeNode -right:TreeNode +TreeNode(item:Object) +TreeNode(item:Object,left:TreeNode, right:TreeNode) +setLeft(left:TreeNode) +getLeft():TreeNode +isLeaf():boolean

The BinaryTree Class BinaryTreeBasis #root:TreeNode +BinaryTreeBasis() +BinaryTreeBasis(root:TreeNode) +isEmpty():boolean +makeEmpty() +getRoot():TreeNode BinaryTree +BinaryTree () +BinaryTree (root:TreeNode) +BinaryTree (root:TreeNode, leftTree:BinaryTree, rightTree:BinaryTree) +setRoot(root:TreeNode) +attachLeft(left:TreeNode) +detachLeft() +attachLeftSubtree(lTree:BinaryTree) +detachLeftSubtree(lTree:BinaryTree)

Typical BinaryTree Methods public void attachLeft(Object newItem) { if (!isEmpty() && root.getLeft() == null) { // assertion: nonempty tree; no left child root.setLeft(new TreeNode(newItem, null, null)); } // end if } // end attachLeft

public void attachLeftSubtree(BinaryTree leftTree) throws TreeException { if (isEmpty()) { throw new TreeException("TreeException: Empty tree"); } else if (root.getLeft() != null) { // a left subtree already exists; it should have been // deleted first throw new TreeException("TreeException: " + "Cannot overwrite left subtree"); } else { // assertion: nonempty tree; no left child root.setLeft(leftTree.root); // don't want to leave multiple entry points into // our tree leftTree.makeEmpty(); } // end if } // end attachLeftSubtree

Example – Animal Guess Are you a mammal? Are you bigger than a catDo you live underwater? KangarooMouseTroutRobin Yes No root

Tree Traversal Traversal: visit each node in a tree Visit root Visit every node in the left subtree Visit every node in the right subtree Three different traversal algorithms Preorder: visit root before all other nodes Postorder: visit root after all other nodes Inorder: visit nodes in left subtree, then the root, and then nodes in the right tree

Examples Related concepts Preorder Postorder Inorder Jane Bob Tom AlanErikNile

Tree Traversal Using Iterator An Iterator class needs to implement hasNext() next() and optionally, remove() To enforce the ordering, items in a tree will be put in a queue One of the three orders should be chosen before the iterator may be used Changes will not be reflected in the iterator, since the remove method is not implemented here

Typical TreeIterator Methods public class TreeIterator implements java.util.Iterator { private BinaryTreeBasis binTree; private TreeNode currentNode; private QueueInterface queue; public TreeIterator(BinaryTreeBasis bTree) { binTree = bTree; currentNode = null; // empty queue indicates no traversal type // currently selected or end of current // traversal has been reached queue = new QueueReferenceBased(); } // end constructor

Typical TreeIterator Methods – cont’d public boolean hasNext() { return !queue.isEmpty(); } // end hasNext public Object next() throws java.util.NoSuchElementException { try { currentNode = (TreeNode)queue.dequeue(); return currentNode.getItem(); } // end try catch (QueueException e) { throw new java.util.NoSuchElementException(); } // end catch } // end next

Typical TreeIterator Methods – cont’d public void setPreorder() { queue.dequeueAll(); preorder(binTree.root); } // end setPostorder private void preorder(TreeNode treeNode) { if (treeNode != null) { queue.enqueue(treeNode); preorder(treeNode.getLeft()); preorder(treeNode.getRight()); } // end if } // end preorder

Binary Search Tree Three properties for any node n in a BST n’s value is greater than all values in its left subtree n’s value is less than all values in its right sub tree Both left and right subtrees are BSTs