Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Tree Implementations Chapter 24 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions with Java,"— Presentation transcript:

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

2 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.

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

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

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

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

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

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

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

10 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.

11 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.

12 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.

13 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.

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

15 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.

16 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.

17 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.

18 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.

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

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

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

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

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

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

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

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

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

28 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.

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

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

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

32 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.

33 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.

34 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.

35 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.

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

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

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

39 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.

40 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.

41 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.

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


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

Similar presentations


Ads by Google