Tree Implementations Chapter 24 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions with Java,

Slides:



Advertisements
Similar presentations
Chapter 4: Trees General Tree Concepts Binary Trees Lydia Sinapova, Simpson College Mark Allen Weiss: Data Structures and Algorithm Analysis in Java.
Advertisements

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.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L12 (Chapter 20) Lists, Stacks,
© 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.
Chapter 12 Trees. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Define trees as data structures Define the terms.
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.
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.
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.
© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures for Java William H. Ford William R. Topp Chapter 17 Binary.
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.
Data Structures and Abstractions with Java, 4e Frank Carrano
CS Data Structures Chapter 5 Trees. Chapter 5 Trees: Outline  Introduction  Representation Of Trees  Binary Trees  Binary Tree Traversals 
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 10: Trees Data Abstraction & Problem Solving with C++
© 2011 Pearson Addison-Wesley. All rights reserved 11 B-1 Chapter 11 (continued) Trees.
Chapter 19 Implementing Trees and Priority Queues Fundamentals of Java.
Chapter 19 Implementing Trees and Priority Queues Fundamentals of Java.
Data Structures and Algorithms Lecture (BinaryTrees) Instructor: Quratulain.
An Introduction to Sorting Chapter 8 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions with.
Searching Chapter 18 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions with Java, 4e Frank.
Tree Implementations Chapter 16 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
Computer Science 112 Fundamentals of Programming II Introduction to Trees.
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.
IKI 10100I: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100I: Data.
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.
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 Trees.
Copyright © 2012 Pearson Education, Inc. Chapter 20: Binary Trees.
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.
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.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 20: Binary Trees.
A Bag Implementation that Links Data Chapter 3 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions.
Stacks Chapter 5 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions with Java, 4e Frank Carrano.
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.
A Binary Search Tree Implementation Chapter 25 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions.
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture13.
Chapter 10 Trees © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
Trees Chapter 15.
Trees Chapter 11 (continued)
Fundamentals of Programming II Introduction to Trees
Recursive Objects (Part 4)
Trees Chapter 11 (continued)
Planning & System installation
Chapter 11 Trees © 2011 Pearson Addison-Wesley. All rights reserved.
Tree.
Section 8.1 Trees.
Chapter 16 Tree Implementations
Binary Tree Applications
Tree Implementations (plus briefing about Iterators)
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Trees.
Slides by Steve Armstrong LeTourneau University Longview, TX
Chapter 16 Tree Implementations
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Chapter 20: Binary Trees.
Chapter 11 Trees © 2011 Pearson Addison-Wesley. All rights reserved.
Chapter 11 Trees © 2011 Pearson Addison-Wesley. All rights reserved.
Chapter 11 Trees © 2011 Pearson Addison-Wesley. All rights reserved.
Binary Tree Iterators Tree Traversals: preorder, inorder, postorder
Presentation transcript:

Tree Implementations Chapter 24 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions with Java, 4e Frank Carrano

Nodes in a Binary Tree FIGURE 24-1 A node in a binary tree © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Nodes in a Binary Tree LISTING 24-1 The class BinaryNode © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Nodes in a Binary Tree LISTING 24-1 The class BinaryNode © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Nodes in a Binary Tree LISTING 24-1 The class BinaryNode © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Nodes in a Binary Tree LISTING 24-1 The class BinaryNode © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Nodes in a Binary Tree LISTING 24-1 The class BinaryNode © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Nodes in a Binary Tree LISTING 24-1 The class BinaryNode © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Creating a Basic Binary Tree Interface for a class of binary trees © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Creating a Basic Binary Tree LISTING 24-2 A first draft of the class BinaryTree © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Creating a Basic Binary Tree LISTING 24-2 A first draft of the class BinaryTree © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Creating a Basic Binary Tree LISTING 24-2 A first draft of the class BinaryTree © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

The Method privateSetTree FIGURE 24-2 The binary tree treeA shares nodes with treeB and treeC © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

The Method privateSetTree Definition of the method copy in the class BinaryNode © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

The Method privateSetTree Method privateSetTree can invoke copy to copy the nodes from the two given subtrees © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

The Method privateSetTree Another approach, more problems FIGURE 24-3 treeA has identical subtrees © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

The Method privateSetTree The second solution: 1.If left subtree exists and not empty, attach root node to r as left child. 2.Create root node r containing given data. 3.If right subtree exists, not empty, and distinct from left subtree, attach root node to r as a right child. But if right and left subtrees are same, attach copy of right subtree to r instead. © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

The Method privateSetTree 4.If the left subtree exists and differs from the tree object used to call privateSetTree, set the subtree’s data field root to null. 5.If right subtree exists and differs from the tree object used to call privateSetTree, set subtree’s data field root to null. © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

The Method privateSetTree An implementation of privateSetTree © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Accessor and Mutator Methods © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Accessor and Mutator Methods © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Computing the Height and Counting Nodes © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Methods within BinaryNode. © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Methods within BinaryNode. © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Traversals Traversing a binary tree recursively © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Traversals FIGURE 24-4 A binary tree © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Traversals That Use An Iterator Method getInorderIterator can be implemented within BinaryTree © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Traversals That Use An Iterator FIGURE 24-5 Using a stack to perform an inorder traversal of a binary tree © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Traversals That Use An Iterator Iterative version … © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Private Class InorderIterator LISTING 24-3 The private inner class InorderIterator © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Private Class InorderIterator LISTING 24-3 The private inner class InorderIterator © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Postorder, and Level-order Traversals FIGURE 24-6 Using a stack to traverse a binary tree in (a) preorder © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Postorder, and Level-order Traversals FIGURE 24-6 Using a stack to traverse a binary tree in (b) postorder © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Postorder, and Level-order Traversals FIGURE 24-7 Using a queue to traverse a binary tree in level order © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Implementation of an Expression Tree LISTING 24-4 An interface for an expression tree © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Implementation of an Expression Tree LISTING 24-5 The class ExpressionTree © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Implementation of an Expression Tree LISTING 24-5 The class ExpressionTree © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Implementation of an Expression Tree LISTING 24-5 The class ExpressionTree © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

A Node for a General Tree FIGURE 24-8 A node for a general tree © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

A Node for a General Tree LISTING 24-6 An interface for a node in a general tree © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Using a Binary Tree to Represent a General Tree FIGURE 24-9 (a) A general tree; (b) an equivalent binary tree; (c) a more conventional view of the same binary tree © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

End Chapter 24 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.