Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified bin/trees/FamilyTree$TreeNode.class
Binary file not shown.
Binary file modified bin/trees/FamilyTree.class
Binary file not shown.
13 changes: 13 additions & 0 deletions bin/trees/LanguagesTree.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Indo-European:Anatolian,Indo-Iranian,Greek,Italic,Celtic,Germanic,Armenian,Tocharian,Balto-Slavic,Albanian
Anatolian:Hittite,Luwian,Lycian,Lydian
Indo-Iranian:Indo-Aryan,Iranian,Nuristani
Indo-Aryan:Sanskrit,Hindi,Urdu,Bengali,Punjabi
Iranian:Persian,Kurdish,Pashto
Greek:Ancient Greek,Modern Greek
Italic:Latin,Romance
Romance:Italian,Spanish,French,Portuguese,Romanian
Celtic:Irish,Scottish Gaelic,Welsh,Breton
Germanic:English,German,Dutch,Swedish,Norwegian,Danish,Icelandic
Balto-Slavic:Baltic,Slavic
Baltic:Lithuanian,Latvian
Slavic:Russian,Polish,Czech,Ukrainian,Serbian,Bulgarian
62 changes: 31 additions & 31 deletions src/trees/FamilyTree.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,21 @@

public class FamilyTree {

private static class TreeNode {
private String name;
private TreeNode parent;
private ArrayList<TreeNode> children;
private static class TreeNode<T> {
private T data;
private TreeNode<T> parent;
private ArrayList<TreeNode<T>> children;

TreeNode(String name) {
this.name = name;
TreeNode(T data) {
this.data = data;
children = new ArrayList<>();
}

String getName() {
return name;
T getData() {
return data;
}

void addChild(TreeNode childNode) {
void addChild(TreeNode<T> childNode) {
// Add childNode to this node's children list. Also
// set childNode's parent to this node.
children.add(childNode);
Expand All @@ -30,15 +30,15 @@ void addChild(TreeNode childNode) {

// Searches subtree at this node for a node
// with the given name. Returns the node, or null if not found.
TreeNode getNodeWithName(String targetName) {
TreeNode<T> getNodeWithData(T targetData) {
// Does this node have the target name?
if (this.name.equals(targetName))
if (this.data.equals(targetData))
return this;

// No, recurse. Check all children of this node.
for (TreeNode child: children)
for (TreeNode<T> child: children)
{
TreeNode result = child.getNodeWithName(targetName);
TreeNode<T> result = child.getNodeWithData(targetData);
if (result != null)
return result;
}
Expand All @@ -50,9 +50,9 @@ TreeNode getNodeWithName(String targetName) {
// Returns a list of ancestors of this TreeNode, starting with this node’s
// parent and
// ending with the root. Order is from recent to ancient.
ArrayList<TreeNode> collectAncestorsToList() {
ArrayList<TreeNode> ancestors = new ArrayList<>();
TreeNode current = this.parent;
ArrayList<TreeNode<T>> collectAncestorsToList() {
ArrayList<TreeNode<T>> ancestors = new ArrayList<>();
TreeNode<T> current = this.parent;
while (current != null) {
ancestors.add(current);
current = current.parent;
Expand All @@ -65,15 +65,15 @@ public String toString() {
}

private String toStringWithIndent(String indent) {
String s = indent + name + "\n";
String s = indent + data + "\n";
indent += " ";
for (TreeNode childNode : children)
for (TreeNode<T> childNode : children)
s += childNode.toStringWithIndent(indent);
return s;
}
}

private TreeNode root;
private TreeNode<String> root;

//
// Displays a file browser so that user can select the family tree file.
Expand Down Expand Up @@ -122,20 +122,20 @@ private void addLine(String line) throws TreeException
// Find parent node. If root is null then the tree is empty and the
// parent node must be constructed. Otherwise the parent node should be
// somewhere in the tree.
TreeNode parentNode;
TreeNode<String> parentNode;
if (root == null)
parentNode = root = new TreeNode(parent);
parentNode = root = new TreeNode<>(parent);
else
{
parentNode = root.getNodeWithName(parent); //There's a method in Node that searches for a named node.
parentNode = root.getNodeWithData(parent); //There's a method in Node that searches for a named node.
//??? If the parent node wasn't found, there must have been something wrong in the
//data file. Throw an exception.
}

// Add child nodes to parentNode.
//?? For each name in childrenArray, create a new node and add that node to parentNode.
for (String name : childrenArray){
TreeNode node = new TreeNode(name);
TreeNode<String> node = new TreeNode<>(name);
parentNode.addChild(node);
}
}
Expand All @@ -148,26 +148,26 @@ private void addLine(String line) throws TreeException
// of the root is 0. The
// depth of the root's immediate children is 1, and so on.
//
TreeNode getMostRecentCommonAncestor(String name1, String name2) throws TreeException
TreeNode<String> getMostRecentCommonAncestor(String name1, String name2) throws TreeException
{
// Get nodes for input names.
TreeNode node1 = root.getNodeWithName(name1); // node whose name is name1
TreeNode<String> node1 = root.getNodeWithData(name1); // node whose name is name1
if (node1 == null)
{
throw new TreeException("there is no node with name of " + name1);
}
TreeNode node2 = root.getNodeWithName(name2); // node whose name is name2
TreeNode<String> node2 = root.getNodeWithData(name2); // node whose name is name2
if (node2 == null){
throw new TreeException("there is no node with name of " + name2);
}

// Get ancestors of node1 and node2.
ArrayList<TreeNode> ancestorsOf1 = node1.collectAncestorsToList();
ArrayList<TreeNode> ancestorsOf2 = node2.collectAncestorsToList();
ArrayList<TreeNode<String>> ancestorsOf1 = node1.collectAncestorsToList();
ArrayList<TreeNode<String>> ancestorsOf2 = node2.collectAncestorsToList();

// Check members of ancestorsOf1 in order until you find a node that is also
// an ancestor of 2.
for (TreeNode n1: ancestorsOf1)
for (TreeNode<String> n1: ancestorsOf1)
if (ancestorsOf2.contains(n1))
return n1;

Expand All @@ -183,8 +183,8 @@ public static void main(String[] args) {
try {
FamilyTree tree = new FamilyTree();
System.out.println("Tree:\n" + tree + "\n**************\n");
TreeNode ancestor = tree.getMostRecentCommonAncestor("Bilbo", "Frodo");
System.out.println("Most recent common ancestor of Bilbo and Frodo is " + ancestor.getName());
TreeNode<String> ancestor = tree.getMostRecentCommonAncestor("Bilbo", "Frodo");
System.out.println("Most recent common ancestor of Bilbo and Frodo is " + ancestor.getData());
} catch (IOException x) {
System.out.println("IO trouble: " + x.getMessage());
} catch (TreeException x) {
Expand Down
13 changes: 13 additions & 0 deletions src/trees/LanguagesTree.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Indo-European:Anatolian,Indo-Iranian,Greek,Italic,Celtic,Germanic,Armenian,Tocharian,Balto-Slavic,Albanian
Anatolian:Hittite,Luwian,Lycian,Lydian
Indo-Iranian:Indo-Aryan,Iranian,Nuristani
Indo-Aryan:Sanskrit,Hindi,Urdu,Bengali,Punjabi
Iranian:Persian,Kurdish,Pashto
Greek:Ancient Greek,Modern Greek
Italic:Latin,Romance
Romance:Italian,Spanish,French,Portuguese,Romanian
Celtic:Irish,Scottish Gaelic,Welsh,Breton
Germanic:English,German,Dutch,Swedish,Norwegian,Danish,Icelandic
Balto-Slavic:Baltic,Slavic
Baltic:Lithuanian,Latvian
Slavic:Russian,Polish,Czech,Ukrainian,Serbian,Bulgarian