Advertisement
Guest User

Untitled

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