From b56dfe6de6f992f27b31ee0cff85645e6bda9306 Mon Sep 17 00:00:00 2001 From: weslayer Date: Fri, 15 Nov 2024 12:20:51 -0800 Subject: [PATCH] da extra credit --- bin/trees/FamilyTree$TreeNode.class | Bin 2623 -> 3141 bytes bin/trees/FamilyTree.class | Bin 4901 -> 5205 bytes bin/trees/LanguagesTree.txt | 13 ++++++ src/trees/FamilyTree.java | 62 ++++++++++++++-------------- src/trees/LanguagesTree.txt | 13 ++++++ 5 files changed, 57 insertions(+), 31 deletions(-) create mode 100644 bin/trees/LanguagesTree.txt create mode 100644 src/trees/LanguagesTree.txt diff --git a/bin/trees/FamilyTree$TreeNode.class b/bin/trees/FamilyTree$TreeNode.class index 0c64bab9b26bc36898ffc24d0e5a672bc4c25a91..7ba63e239c765bd921e924f58e8d532957735597 100644 GIT binary patch literal 3141 zcma)8TUQfT6#h;KOb7!(1Qf9v5jBt~wHHN5tz4wi0@@O>YHvd_!bnJPGNEEGzoYFR z@UeESsx4ist55A({Re$)U;EnEmVW!pkc)sVYcZU2_C9-m-`?LjhhP8M{1L!7J~t3j z=NW}J(3iDZ|Ze2&2sy8PT_&&dysxvsS_ zLacvIRbINn8uPVD40ut0|dVaOmn!P-R8fKC%JbdhIbV4)cb2TU}g z$v}_7eqEY+cu^cgr}!NG zID;YCa>>LP#ubcZt29Te*2*n~sAsvAt~KhC5SVM1%6a;<&U;pNW!72?1kjx==JPyx zV}-0;BFa*xC~~LZ>cG~XgA2P`{?|!pwzl&Sc(&~Nr#Ipi=RMacEGg8>%wHXi;HmEBVKyM*lEop;;Bdw8FPTeVi~iDDsZc><23l4lh>p0eRx@YS5Dy%Bei`G*pIz5^lL zW;>mLa_v1C>{n3C6`e1Z-K;(3$kC5gFW@sGF@;1rGg6Z}V0Llm5{oomEP5r+wbo{B zZ@HMGx*e<-xU0}#y`-JOdU3@b@-g`Ar>ramwIR6k3SZQc54`<=SFA!VZ>sa@P}*ZA+!vLM8SmCw)fY>vj7upB{=$49AX1ovbRk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7iL&Ug>jV*{Vc_~zVwOK2$7 zc!^;G0S$G-Q<$eN)fqK4G-_C;p;coa;uVH3Qa8s8RwvV%hMHpvlA-s6eTh8!mR258 z++@tRwSaAB!;Z>4(w*T-=j{ZSr5tqzoW48q*b#NrKY=^X!Cya)+}c=U0XsK7i8~dv zVr3!NT~T+y6^j~YnmvwY{Rrj<48PtK)wm!Q(6}J#4Y&i|Jnr7D3%~C7JT`UyBGi{l zZm^+l=J7MT8K_E9I8=-5#QX4m)>BDb_fY5HJ$ftdrQ{@|^|%lBv+5hE{{V5iscD*# zw!nu6Y`ohUXM{E{6y9s$nt)$SVlkod^5Spr1UwpYQz(Yl#_K0y>XnIl2tMuwD!7{O z@G!i#@H~1{Xmw8cibj&BuHxrEZhF&m}Q zJx)ozaa(bmI2`;kuE7(Ooct~|<4NR+awQ^6I8W^@#BqYNhLemqhbyPO#x8k{(W;O} z;G95(HjLbaYDj{dqkvw5n4?a^Vg57NC@_~tJC06{eB*gn^4r4MZm1JDHO7K@8-p8m zadg|p<4`Z3oOYZk&V#4$1!`1~7g)})gTjV!h*$w%?06hs3fs}X3YPP=u 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