public class GenealogyTree
extends java.lang.Object
Modifier and Type | Field and Description |
---|---|
static java.lang.String |
LOAD_GENEALOGY_ERROR_MESSAGE |
Constructor and Description |
---|
GenealogyTree() |
Modifier and Type | Method and Description |
---|---|
void |
buildFromFile(java.lang.String filename)
Load a tree from file.
|
StackADT<java.lang.String> |
getAncestorStack(java.lang.String target)
return a Stack that contains the ancestors of the node with matching data
The root is at the bottom of the stack and the matching node is at the top
and the stack contains all ancestors of the matching data node.
|
TreeNode<java.lang.String> |
getRoot() |
void |
printTree()
Print a tree with indent.
|
public static final java.lang.String LOAD_GENEALOGY_ERROR_MESSAGE
public TreeNode<java.lang.String> getRoot()
public StackADT<java.lang.String> getAncestorStack(java.lang.String target)
target
- the data you are trying to findpublic void buildFromFile(java.lang.String filename) throws java.io.IOException
a -> b a -> c a -> d b -> e d -> f d -> gNote: all lines of a file must contain a relationship to be a valid format. Blank lines will cause exceptions. Pseudocode for the work done by this method:
// Create a queue, add each new node to the queue // create a Scanner connect to the file // for each line of the file // read the line // parse the line into parent and child // if root is null // create the root node // add the root's first child // add the root and child to the queue // else Construct other TreeNode // while queue is not empty // get next node from queue without removing it from queue // if "front" node matches the parent // create a TreeNode for the child // add the child node to the current "front" node (its parent) // add the child to the queue // break out of the loop // else dequeue the front node // catch IO exceptions, display error message and rethrow the exception // close the file scanner
filename
- the name of the file with the (professor->student) data pairsjava.io.IOException
- if file not foundpublic void printTree()
a ..b ....e ..c ..d ....f ....g