Copyright ©2012 by Pearson Education, Inc. All rights reserved

Slides:



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

A Bag Implementation that Links Data Chapter 3 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
© 2006 Pearson Addison-Wesley. All rights reserved11 B-1 Chapter 11 (continued) Trees.
DictionaryADT and Trees. Overview What is the DictionaryADT? What are trees? Implementing DictionaryADT with binary trees Balanced trees DictionaryADT.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 26 Binary Search Trees.
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 Trees.
Trees Chapter 25 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Tree Implementations Chapter Nodes in a Binary Tree The most common way uses a linked structure with each node represent each element in a binary.
Trees Chapter 23 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
A Binary Search Tree Implementation Chapter 25 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Tree Implementations Chapter 24 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Graph Implementations Chapter 29 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Marc Smith and Jim Ten Eyck
A Binary Search Tree Implementation Chapter 27 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Chapter 18 - basic definitions - binary trees - tree traversals Intro. to Trees 1CSCI 3333 Data Structures.
Lecture Objectives  To learn how to use a tree to represent a hierarchical organization of information  To learn how to use recursion to process trees.
Trees & Graphs Chapter 25 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X.
Trees Chapter 15 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
Lecture Objectives  To learn how to use a tree to represent a hierarchical organization of information  To learn how to use recursion to process trees.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 10: Trees Data Abstraction & Problem Solving with C++
Data Structures : Project 5 Data Structures Project 5 – Expression Trees and Code Generation.
© 2011 Pearson Addison-Wesley. All rights reserved 11 B-1 Chapter 11 (continued) Trees.
Chapter 19 Implementing Trees and Priority Queues Fundamentals of Java.
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.
Tree Implementations Chapter 16 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
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.
© 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.
A Heap Implementation Chapter 26 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Trees Chapter Chapter Contents Tree Concepts Hierarchical Organizations Tree Terminology Traversals of a Tree Traversals of a Binary Tree Traversals.
Trees Chapter 23 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 Trees.
Copyright © 2012 Pearson Education, Inc. Chapter 20: Binary Trees.
Tree Implementations Chapter 24 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions with Java,
Tree Implementations Chapter Chapter Contents The Nodes in a Binary Tree An Interface for a Node An implementation of BinaryNode An Implementation.
Data Abstraction and Problem Solving with JAVA Walls and Mirrors Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Data Abstraction and Problem.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 20: Binary Trees.
Copyright © 2012 Pearson Education, Inc. Chapter 10 Advanced Topics.
Chapter 10 Trees © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 20: Binary Trees.
Recursion Chapter 7 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Planning & System installation
Planning & System installation
Trees Chapter 15.
Trees Chapter 11 (continued)
Planning & System installation
Trees Chapter 11 (continued)
Trees CSE 2320 – Algorithms and Data Structures Vassilis Athitsos
Planning & System installation
Chapter 11 Trees © 2011 Pearson Addison-Wesley. All rights reserved.
A Bag Implementation that Links Data
Chapter 16 Tree Implementations
A Binary Search Tree Implementation
Tree Implementations (plus briefing about Iterators)
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Trees.
Slides by Steve Armstrong LeTourneau University Longview, TX
Chapter 16 Tree Implementations
Trees (part 2) CSE 2320 – Algorithms and Data Structures
A List Implementation That Links Data
Copyright ©2012 by Pearson Education, Inc. All rights reserved
General Trees & Binary Trees
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Copyright ©2012 by Pearson Education, Inc. All rights reserved
A Heap Implementation Chapter 26 Adapted from Pearson Education, Inc.
Chapter 20: Binary Trees.
Chapter 11 Trees © 2011 Pearson Addison-Wesley. All rights reserved.
A Binary Search Tree Implementation
Chapter 11 Trees © 2011 Pearson Addison-Wesley. All rights reserved.
A List Implementation That Links Data
Presentation transcript:

Copyright ©2012 by Pearson Education, Inc. All rights reserved Tree Implementations Chapter 24 Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved Contents The Nodes in a Binary Tree An Interface for a Node An Implementation of BinaryNode An Implementation of the ADT Binary Tree Creating a Basic Binary Tree The Method privateSetTree Accessor and Mutator Methods Computing the Height and Counting Nodes Traversals Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved Contents An Implementation of an Expression Tree General Trees A Node for a General Tree Using a Binary Tree to Represent a General Tree Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved Objectives Describe necessary operations on node within binary tree Implement class of nodes for binary tree Implement class of binary trees Implement an expression tree by extending class of binary trees Describe necessary operations on a node within general tree Use binary tree to represent general tree Copyright ©2012 by Pearson Education, Inc. All rights reserved

Figure 24-1 A node in a binary tree Copyright ©2012 by Pearson Education, Inc. All rights reserved

An Interface for a Node Note code for node interface, Listing 24-1 An implementation of BinaryNode, Listing 24-2 Creating a basic binary tree First draft of the class, Listing 24-3 Note: Code listing files must be in same folder as PowerPoint files for links to work Copyright ©2012 by Pearson Education, Inc. All rights reserved

Figure 24-2 The binary tree treeA shares nodes with treeB and treeC Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved Q 1 In the previous method copy, are the casts to BinaryNode<T> necessary? Explain. Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved Q 1 In the previous method copy, are the casts to BinaryNode<T> necessary? Explain. Yes. The fields left and right of BinaryNode (see Segment 24.3) have BinaryNode<T> as their data type, but the return type of the method copy is BinaryNodeInterface<T>. Copyright ©2012 by Pearson Education, Inc. All rights reserved

Figure 24-3 treeA has identical subtrees Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved Question 2 At the end of the implementation of privateSetTree, can you set rightTree to null instead of invoking clear? Explain. Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved Question 2 At the end of the implementation of privateSetTree, can you set rightTree to null instead of invoking clear? Explain. No. Setting rightTree to null affects only the local copy of the reference argument rightTree. An analogous comment applies to leftTree . Copyright ©2012 by Pearson Education, Inc. All rights reserved

Traversing Recursively Inorder traversal Public method for user, calls private method Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved Question 3 Trace the method inorderTraverse with the binary tree in Figure 24-4. What data is displayed? Question 4 Implement a recursive method preorder Traverse that displays the data in a binary tree in preorder. Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved Question 3 Trace the method inorderTraverse with the binary tree in Figure 24-4. What data is displayed? Question 4 Implement a recursive method preorder Traverse that displays the data in a binary tree in preorder. The data in the objects d , b, e , a, f , g, and c is displayed on separate lines. public void preorderTraverse() { preorderTraverse(root); } private void preorderTraverse(BinaryNodeInterface<T> node) { if (node != null) { System.out.println(node.getData()); preorderTraverse(node.getLeftChild()); preorderTraverse(node.getRightChild()); Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved Figure 24-4 A binary tree Copyright ©2012 by Pearson Education, Inc. All rights reserved

Traversals with An Iterator Iterator traversal provides more flexibility Class BinaryTree must implement methods in interface TreeIteratorInterface Possible to use a stack to do inorder traversal Note example, Listing 24-A Private class InorderIterator, Listing 24-4 Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved Figure 24-5 Using a stack to perform an inorder traversal of a binary tree Copyright ©2012 by Pearson Education, Inc. All rights reserved

Figure 24-6 Using a stack to traverse a binary tree in (a) preorder; Copyright ©2012 by Pearson Education, Inc. All rights reserved

Figure 24-6 Using a stack to traverse a binary tree in (b) postorder; Copyright ©2012 by Pearson Education, Inc. All rights reserved

Figure 24-7 Using a queue to traverse a binary tree in level order Copyright ©2012 by Pearson Education, Inc. All rights reserved

Implementation of an Expression Tree Note interface, Listing 24-5 Derive from BinaryTree, Listing 24-6 Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved Question 6 Trace the method evaluate for the expression tree in Figure 23-14c of the previous chapter. What value is returned? Assume that a is 3, b is 4, and c is 5. Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved Question 6 Trace the method evaluate for the expression tree in Figure 23-14c of the previous chapter. What value is returned? Assume that a is 3, b is 4, and c is 5. 27 Copyright ©2012 by Pearson Education, Inc. All rights reserved

Node for a General Tree Figure 24-8 A node for a general tree Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved Node for a General Tree Interface, Listing 24-7 Copyright ©2012 by Pearson Education, Inc. All rights reserved

Using a Binary Tree to Represent a General Tree Figure 24-9 (a) A general tree; (b) an equivalent binary tree; Copyright ©2012 by Pearson Education, Inc. All rights reserved

Figure 24-9 (c) a more conventional view of the same binary tree Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved Q 7 What binary tree can represent the general tree in Fig. 23-1 of the previous chapter? Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved Q 7 What binary tree can represent the general tree in Fig. 23-1 of the previous chapter? Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved End Chapter 24 Copyright ©2012 by Pearson Education, Inc. All rights reserved