Advertisement
Guest User

Untitled

a guest
Oct 20th, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.70 KB | None | 0 0
  1. public class Node {
  2. Node left, right = null;
  3. String Binary = "";
  4. int bValue = -1;;
  5. }
  6.  
  7. int sizeofstrings = 0;
  8. Node root = new Node();
  9. int size = 0;
  10. int TrieLength;
  11. int trackBit = 0;
  12.  
  13. public boolean insert(BinaryTrie trie, String a) { //this is the insert function
  14. if (trie.lexiordered.contains(a)) {
  15. return false;
  16. } else {
  17.  
  18. sizeofstrings++;
  19. insertTheString(trie.root, a);
  20. return true;
  21. }
  22.  
  23. }
  24.  
  25. public void insertTheString(Node root, String a) {
  26.  
  27. if (size == 0) {
  28. TrieLength = a.length();
  29. } // if this is the first string to be added, set the length of strings to be the
  30. // length of the first string
  31. if (root.Binary.equals("") && size == 0) {
  32.  
  33. root.Binary = a;
  34. size++;
  35. } // if this is the root of the trie and it's empty, then add the string
  36. else if (root.Binary.equals("") && root.bValue == 0) {// if it's empty and is a path
  37.  
  38. char newString[] = a.toCharArray();
  39. if (newString[trackBit] == '0') {
  40.  
  41. trackBit++;
  42. if (root.left == null) { // if the left node doesn't exist, then that means we can put the binary string
  43. // there
  44. Node newNode = new Node();
  45. root.left = newNode;
  46. root.left.Binary = a; // put the new string in the left node
  47. trackBit = 0; // trackBit goes to 0
  48. return;
  49. } else {
  50. insertTheString(root.left, a);
  51.  
  52. return;
  53. }
  54. }
  55. if (newString[trackBit] == '1') {
  56.  
  57. trackBit++;
  58. if (root.right == null) { // if the right node doesn't exist, then that means we can put the binary
  59. // string there
  60. Node newNode = new Node();
  61. root.right = newNode;
  62. root.right.Binary = a; // put the new string in the right node
  63. trackBit = 0;// trackBit goes to 0
  64. return;
  65.  
  66. } else {
  67. insertTheString(root.right, a);
  68. return;
  69.  
  70. }
  71. }
  72.  
  73. } else if (!root.Binary.equals("") && root.bValue == -1) {// if the node has a string in it
  74. char newString[] = a.toCharArray(); // converting the string to be inserted to a char array
  75. char oldString[] = root.Binary.toCharArray(); // converting the string that's already in the node to an
  76. // array
  77.  
  78. // this goes over the two strings and compares them
  79. if (newString[trackBit] == oldString[trackBit] && newString[trackBit] == '1') {
  80. Node newNode = new Node();
  81. root.right = newNode;
  82. root.right.Binary = root.Binary;
  83. root.Binary = "";
  84. root.bValue = 0;
  85. trackBit++;
  86. insertTheString(root.right, a);
  87. return;
  88. }
  89. if (newString[trackBit] == oldString[trackBit] && newString[trackBit] == '0') {
  90.  
  91. Node newNode = new Node();
  92. root.left = newNode;
  93. root.left.Binary = root.Binary;
  94. root.Binary = "";
  95. root.bValue = 0;
  96.  
  97. trackBit++;
  98.  
  99. insertTheString(root.left, a);
  100. return;
  101. }
  102. if (newString[trackBit] != oldString[trackBit]) { // here happens insertion of strings
  103. if (newString[trackBit] == '0') {
  104. Node newNode = new Node();
  105. root.left = newNode;
  106. root.left.Binary = a;
  107. root.bValue = 0;// this marks the node that it's a path now, and cannot be given any strings
  108.  
  109. Node newNode2 = new Node();
  110. root.right = newNode2;
  111. root.right.Binary = root.Binary;
  112. root.Binary = "";
  113. trackBit = 0;
  114.  
  115. return;
  116. } else if (newString[trackBit] == '1') {
  117.  
  118. Node newNode = new Node();
  119. root.right = newNode;
  120. root.right.Binary = a;// put the new string in the right node
  121. root.bValue = 0;// this marks the node that it's a path now, and cannot be given any strings
  122. Node newNode2 = new Node();
  123. root.left = newNode2;
  124. root.left.Binary = root.Binary;
  125. root.Binary = "";
  126. trackBit = 0;
  127.  
  128. return;
  129. }
  130.  
  131. }
  132.  
  133. }
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement