Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Tree Implementations Chapter 24 Copyright ©2012 by Pearson Education, Inc. All rights reserved."— Presentation transcript:

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

2 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

3 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

4 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

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

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

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

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

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

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

11 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-AListing 24-A Private class InorderIterator, Listing 24-4Listing 24-4 Copyright ©2012 by Pearson Education, Inc. All rights reserved

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

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

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

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

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

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

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

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

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

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


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

Similar presentations


Ads by Google