diff --git a/bin/trees/FamilyTree$TreeNode.class b/bin/trees/FamilyTree$TreeNode.class index 0c64bab..7ba63e2 100644 Binary files a/bin/trees/FamilyTree$TreeNode.class and b/bin/trees/FamilyTree$TreeNode.class differ diff --git a/bin/trees/FamilyTree.class b/bin/trees/FamilyTree.class index 2999acb..dc5e392 100644 Binary files a/bin/trees/FamilyTree.class and b/bin/trees/FamilyTree.class differ diff --git a/bin/trees/LanguagesTree.txt b/bin/trees/LanguagesTree.txt new file mode 100644 index 0000000..138d661 --- /dev/null +++ b/bin/trees/LanguagesTree.txt @@ -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 \ No newline at end of file diff --git a/src/trees/FamilyTree.java b/src/trees/FamilyTree.java index 0d72bd2..18fc2e4 100644 --- a/src/trees/FamilyTree.java +++ b/src/trees/FamilyTree.java @@ -7,21 +7,21 @@ public class FamilyTree { - private static class TreeNode { - private String name; - private TreeNode parent; - private ArrayList children; + private static class TreeNode { + private T data; + private TreeNode parent; + private ArrayList> 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 childNode) { // Add childNode to this node's children list. Also // set childNode's parent to this node. children.add(childNode); @@ -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 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 child: children) { - TreeNode result = child.getNodeWithName(targetName); + TreeNode result = child.getNodeWithData(targetData); if (result != null) return result; } @@ -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 collectAncestorsToList() { - ArrayList ancestors = new ArrayList<>(); - TreeNode current = this.parent; + ArrayList> collectAncestorsToList() { + ArrayList> ancestors = new ArrayList<>(); + TreeNode current = this.parent; while (current != null) { ancestors.add(current); current = current.parent; @@ -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 childNode : children) s += childNode.toStringWithIndent(indent); return s; } } - private TreeNode root; + private TreeNode root; // // Displays a file browser so that user can select the family tree file. @@ -122,12 +122,12 @@ 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 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. } @@ -135,7 +135,7 @@ private void addLine(String line) throws TreeException // 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 node = new TreeNode<>(name); parentNode.addChild(node); } } @@ -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 getMostRecentCommonAncestor(String name1, String name2) throws TreeException { // Get nodes for input names. - TreeNode node1 = root.getNodeWithName(name1); // node whose name is name1 + TreeNode 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 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 ancestorsOf1 = node1.collectAncestorsToList(); - ArrayList ancestorsOf2 = node2.collectAncestorsToList(); + ArrayList> ancestorsOf1 = node1.collectAncestorsToList(); + ArrayList> 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 n1: ancestorsOf1) if (ancestorsOf2.contains(n1)) return n1; @@ -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 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) { diff --git a/src/trees/LanguagesTree.txt b/src/trees/LanguagesTree.txt new file mode 100644 index 0000000..138d661 --- /dev/null +++ b/src/trees/LanguagesTree.txt @@ -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 \ No newline at end of file