Advertisement
Guest User

Untitled

a guest
Sep 22nd, 2017
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.71 KB | None | 0 0
  1. import java.util.HashMap;
  2.  
  3. public class Tree {
  4. private Node root;
  5.  
  6. public Tree(){
  7.  
  8. root = new Node();
  9. }
  10.  
  11.  
  12. public int distinct(String k){
  13. char letter;
  14. int distinct = 0;
  15. Node node = null;
  16. HashMap<Character, Node> children = root.children;
  17.  
  18. for(int i = 0; i < k.length(); i++){
  19. letter = k.charAt(i);
  20. if(children.containsKey(letter)){
  21. node = children.get(letter);
  22. children = node.children;
  23. }
  24. }
  25.  
  26. if(node != null){
  27. if(node.value > 0) distinct++;
  28. for(HashMap.Entry<Character, Node> child : children.entrySet()){
  29. if(child.getValue().value > 0) distinct++;
  30. distinct += distinct(child.getValue());
  31. }
  32. }
  33.  
  34. return distinct;
  35. }
  36.  
  37.  
  38. private int distinct(Node node){
  39. int distinct = 0;
  40.  
  41. if(node != null){
  42. for(HashMap.Entry<Character, Node> child : node.children.entrySet()){
  43. if(child.getValue().value > 0) distinct++;
  44. distinct += distinct(child.getValue());
  45. }
  46. }
  47.  
  48. return distinct;
  49. }
  50.  
  51. public int count(String k){
  52. char letter;
  53. int count = 0;
  54. Node node = null;
  55. HashMap<Character, Node> children = root.children;
  56.  
  57. for(int i = 0; i < k.length(); i++){
  58. letter = k.charAt(i);
  59. if(children.containsKey(letter)){
  60. node = children.get(letter);
  61. children = node.children;
  62. if(i == k.length()-1) count += node.value;
  63. }
  64.  
  65. }
  66.  
  67. if(node != null){
  68. for(HashMap.Entry<Character, Node> child : children.entrySet()){
  69. count += child.getValue().value;
  70. count += count(child.getValue());
  71. }
  72. }
  73.  
  74. return count;
  75. }
  76.  
  77. private int count(Node node){
  78. int count = 0;
  79.  
  80. if(node != null){
  81. for(HashMap.Entry<Character, Node> child : node.children.entrySet()){
  82. count += child.getValue().value;
  83. count += count(child.getValue());
  84. }
  85. }
  86.  
  87. return count;
  88. }
  89.  
  90.  
  91. public void put(String k){
  92. HashMap<Character, Node> children = root.children;
  93. for(int i = 0; i < k.length(); i++){
  94. char key = k.charAt(i);
  95. Node diffNode;
  96. if(children.containsKey(key)){
  97. diffNode = children.get(key);
  98. }
  99. else{
  100. diffNode = new Node(key);
  101. children.put(key, diffNode);
  102. }
  103. children = diffNode.children;
  104.  
  105. if(i == k.length()-1) {
  106. diffNode.leaf = true;
  107. diffNode.increment();
  108. }
  109. }
  110. }
  111.  
  112. public Integer get(String k){
  113. char letter;
  114. Integer val = 0;
  115. Node node;
  116. HashMap<Character, Node> children = root.children;
  117.  
  118. for(int i = 0; i < k.length(); i++){
  119. letter = k.charAt(i);
  120. if(children.containsKey(letter)){
  121. node = children.get(letter);
  122. val = node.value;
  123. children = node.children;
  124. }
  125. else return 0;
  126. }
  127. return val;
  128. }
  129.  
  130. private class Node{
  131. char key;
  132. Integer value = 0;
  133. boolean leaf;
  134. HashMap<Character, Node> children = new HashMap<>();
  135.  
  136. public void increment(){
  137. value++;
  138. }
  139.  
  140. public Node(){}
  141. public Node(Character key){
  142. this.key = key;
  143. }
  144. }
  145. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement