Advertisement
Guest User

Untitled

a guest
May 5th, 2016
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.81 KB | None | 0 0
  1. package hw6;
  2.  
  3. import java.util.*;
  4.  
  5. //import solution.hw6.TreeNode;
  6.  
  7.  
  8. public class TreeNode implements Comparable<TreeNode>
  9. {
  10. private String name;
  11. private TreeNode parent;
  12. private TreeSet<TreeNode> children;
  13.  
  14.  
  15. TreeNode(String name)
  16. {
  17. this.name = name;
  18. this.children = new TreeSet<>();
  19. }
  20.  
  21.  
  22. // Creates and returns a TreeNode.
  23. TreeNode addChild(String childName)
  24. {
  25. TreeNode child = new TreeNode(childName);
  26. children.add(child);
  27. parent = child.getParent();
  28. return child;
  29. }
  30.  
  31.  
  32. String getName()
  33. {
  34. return name;
  35. }
  36.  
  37.  
  38. TreeSet<TreeNode> getChildren()
  39. {
  40. return children;
  41. }
  42.  
  43.  
  44. TreeNode getParent()
  45. {
  46. return parent;
  47. }
  48.  
  49.  
  50. // The root has depth zero. All its children have depth 1. All their
  51. // children have depth 2. Etc.
  52. int getDepth()
  53. {
  54. int depth = 0;
  55. for (TreeNode node : children) {
  56. if(node.getParent() != null) {
  57. depth++;
  58. }
  59. }
  60. return depth;
  61. }
  62.  
  63.  
  64. // Call Rank.values() to get an array of Ranks, in the order they appear in Rank.java.
  65. // Assume this method will never be called on the root node. Any child of the root has
  66. // rank = Rank.KINGDOM, and so on.
  67. Rank getRank()
  68. {
  69. Rank.values();
  70. Rank rank = null;
  71. for (TreeNode node : children) {
  72. if (node.getDepth() == 1) {
  73. rank = Rank.KINGDOM;
  74. } else if (node.getDepth() == 2) {
  75. rank = Rank.PHYLUM;
  76. } else if (node.getDepth() == 3) {
  77. rank = Rank.CLASS;
  78. } else if (node.getDepth() == 4) {
  79. rank = Rank.ORDER;
  80. } else if (node.getDepth() == 5) {
  81. rank = Rank.FAMILY;
  82. } else if (node.getDepth() == 6) {
  83. rank = Rank.GENUS;
  84. } else if (node.getDepth() == 7) {
  85. rank = Rank.SPECIES;
  86. }
  87. }
  88. return rank;
  89.  
  90. }
  91.  
  92.  
  93. // If this node has a child node with the specified name, returns that
  94. // child node. Otherwise returns null;
  95. TreeNode getChildWithName(String childName)
  96. {
  97. TreeNode child = new TreeNode(childName);
  98. for (TreeNode node : children) {
  99. if (child.name == node.name) {
  100. return child;
  101. }
  102. }
  103. return null;
  104. }
  105.  
  106.  
  107. // Return the hashCode of the name variable.
  108. public int hashCode()
  109. {
  110. return name.hashCode();
  111. }
  112.  
  113.  
  114. // Just compare this node's name to that node's name.
  115. public int compareTo(TreeNode that)
  116. {
  117. return this.name.compareTo(that.name);
  118. }
  119.  
  120.  
  121. // Be compatible and simple.
  122. public boolean equals(Object x)
  123. {
  124. return this.compareTo((TreeNode)x) == 0;
  125. }
  126.  
  127.  
  128. public static void main(String[] args)
  129. {
  130. System.out.println("Will test TreeNode");
  131. TreeNode n1 = new TreeNode("Darth");
  132. n1.addChild("Leia");
  133. TreeNode n2 = n1.getChildWithName("Leia");
  134. assert n2 != null;
  135. assert n2.name.equals("Leia");
  136. assert n2.getParent() == n1;
  137. assert n1.getDepth() == 0;
  138. assert n2.getDepth() == 1;
  139. System.out.println("All ok");
  140. }
  141. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement