Ch14 Ancestor Tree

17
Chapter 14 Ancestor Tree

description

 

Transcript of Ch14 Ancestor Tree

Page 1: Ch14 Ancestor Tree

Chapter 14

Ancestor Tree

Page 2: Ch14 Ancestor Tree

Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 14-2

Chapter Objectives

• Provide a case study example from problem statement through implementation

• Demonstrate how a binary tree can be used to solve a problem

Page 3: Ch14 Ancestor Tree

Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 14-3

Ancestor Trees

• An ancestor tree is a tree that represents a person’s biological heritage

• A theoretical ancestor tree is a perfectly balanced, inverted, binary tree

Page 4: Ch14 Ancestor Tree

Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 14-4

Ancestor Trees• For this case study we will create a graphical

implementation of an ancestor tree with the following functional requirements:– enter a person as a starting point

– enter a parent for any individual in the tree that does not already have two parents listed

– find a person in the tree (or determine they are not in the tree)

– remove an individual from the tree

– remove an ancestral line from the tree

– remove all of the individuals from the tree

– return the height of the tree

Page 5: Ch14 Ancestor Tree

Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 14-5

Ancestor Trees• In addition, we will also keep track of the siblings for

each of the individuals in the tree

• Since the tree itself simply represents parents and a single child, the siblings will have to be stored separately

• One additional long-term goal (programming project 14.6) is to allow the user to save and retrieve their work from a file

Page 6: Ch14 Ancestor Tree

Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 14-6

Ancestor Tree - Design• Our system is made up of four high-level

components– The driver

– The graphical user interface

– The class that we will use to represent an individual

– The ancestor tree implementation itself

Page 7: Ch14 Ancestor Tree

Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 14-7

Ancestor Tree - Design• In this case, our ancestor tree is an obvious

refinement of a binary tree

• Thus, this example fits the concept of re-use based development

• Our BinaryTreeADT that we defined in chapter 12 provides most of the functionality that we will need

• Since one of the long-term goals of this project is to save and retrieve our work from a file, we choose to extend an array implementation of a binary tree

Page 8: Ch14 Ancestor Tree

Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 14-8

FIGURE 14.1 The BinaryTreeADT interface

Page 9: Ch14 Ancestor Tree

Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 14-9

AncestorTree - the Person Class

• The Person class must provide variables to represent– First name

– Last name

– Date of birth

– Date of death

– Occupation

– Address

Page 10: Ch14 Ancestor Tree

Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 14-10

AncestorTree - the Person Class

• The Person class must provide a constructor, a compareTo method (implements Comparable), and a toString method

Page 11: Ch14 Ancestor Tree

Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 14-11

AncestorTree - the AncestorTreeNode Class

• The AncestorTreeNode class is designed to serve as a container for the whatever type of object we are storing in the ancestor tree

• This class provides a double ordered list to keep track of siblings and provides an element variable to store the person (or other animal) represented by this node

• This class also provides a constructor, a method to add a sibling, and a toString method

Page 12: Ch14 Ancestor Tree

Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 14-12

AncestorTree - the AncestorTree Class

• The AncestorTree class extends the ArrayBinaryTree class

• The class provides a variable to represent the height of the tree

• The class provides methods to:– Add an element

– Remove an element

– Remove the left or right ancestral line

– Return the height of the tree

– Remove all of the elements

– Find an element

– Determine if an element exists in the tree

Page 13: Ch14 Ancestor Tree

Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 14-13

AncestorTree - the AncestorTree Class

• Since the AncestorTree class extends the ArrayBinaryTree class, it also inherits all of the methods of that class including iterators and the toString method

Page 14: Ch14 Ancestor Tree

Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 14-14

AncestorTree - the AncestorGUI Class

• The AncestorGUI class provides a graphical, direct-manipulation interface for the ancestor tree

• This allows the user to click directly on the person they wish to edit

Page 15: Ch14 Ancestor Tree

Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 14-15

AncestorTree - the AncestorDemo Class

• The AncestorDemo class serves as the driver for our system

• It simply creates an instance of the AncestorGUI class and calls its display method

Page 16: Ch14 Ancestor Tree

Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 14-16

FIGURE 14.2 User interface design for Ancestor Tree system

Page 17: Ch14 Ancestor Tree

Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 14-17

FIGURE 14.3 UML description of Ancestor Tree system