Advertisement
Guest User

Untitled

a guest
Nov 20th, 2019
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.80 KB | None | 0 0
  1. public class DelegateTree implements IDelegateDB {
  2. public String name;
  3. private static int tableSize = 10;
  4. private int numEntries;
  5. private static Node root;
  6.  
  7.  
  8. public class Node{
  9. private Delegate data;
  10. private Node left;
  11. private Node right;
  12.  
  13. public Node(Delegate data){
  14. this.data = data;
  15. right = null;
  16. left = null;
  17. }
  18.  
  19. }
  20. public DelegateTree(){
  21. root = null;
  22. numEntries = 0;
  23. }
  24.  
  25. @Override
  26. public void clearDB() {
  27. root = null;
  28. }
  29.  
  30. public boolean Contains(Node node, String name){
  31. boolean got = false;
  32. if (node == null){
  33. System.out.println("Name not found");
  34. return false;
  35. }
  36. else if(name.compareTo(node.data.getName()) == 0){
  37. System.out.println("Name has been found");
  38. return true;
  39. }
  40. else if(name.compareTo(node.data.getName()) > 0){ //Looks at two nodes and if the correct path is greater than the opposite node, it will run this code since the correct node is greater.
  41. return Contains(node.right, name);
  42. }
  43. else if(name.compareTo(node.data.getName()) < 0){ //Looks at two nodes from the root and if the node in the correct path is greater than the opposite node, it will run this code since the correct node is greater.
  44. return Contains(node.left, name);
  45. }
  46. return false;
  47. }
  48.  
  49.  
  50.  
  51. @Override
  52. public boolean containsName(String name) {
  53.  
  54. }
  55.  
  56.  
  57. public Node findPos(Node node, String name){
  58. if (node == null){
  59. return null;
  60. }
  61. else if(name.compareTo(node.data.getName()) == 0){
  62. return node;
  63. }
  64. else if(name.compareTo(node.data.getName()) > 0){
  65. node.right = findPos(node.right, name);
  66. }
  67. else if(name.compareTo(node.data.getName()) < 0){
  68. node.left = findPos(node.left, name);
  69. }
  70. return null;
  71. }
  72.  
  73.  
  74. @Override
  75. public Delegate get(String name) {
  76. System.out.println("Getting name..");
  77. Node find = findPos(root, name);
  78. if (find == null){
  79. System.out.println("Name not found" + name);
  80. return null;
  81. }
  82. System.out.println("Name is found:" + name);
  83. Delegate temp = find.data;
  84. return temp;
  85. }
  86.  
  87. @Override
  88. public int size() {
  89. return numEntries;
  90. }
  91.  
  92. @Override
  93. public boolean isEmpty() {
  94. return size() == 0;
  95. }
  96.  
  97. @Override
  98. public Delegate put(Delegate delegate) {
  99. assert delegate == null;
  100. Node temp = insert(root, delegate);
  101. if (numEntries == 0){
  102. root = temp;
  103. }
  104. return delegate;
  105.  
  106. }
  107.  
  108. public Node insert(Node node, Delegate data){
  109. String name = data.getName();
  110. if (node == null){
  111. node = new Node(data);
  112. System.out.println(name);
  113. }
  114. else if(name.compareTo(node.data.getName()) > 0){ //Looks at two nodes and if the correct path is greater than the opposite node, it will run this code since the correct node is greater.
  115. node.right = insert(node.right, data);
  116. }
  117. else if(name.compareTo(node.data.getName()) < 0){//Looks at two nodes from the root and if the node in the correct path is greater than the opposite node, it will run this code since the correct node is greater.
  118. node.left = insert(node.left, data);
  119. }
  120. return node;
  121. }
  122.  
  123.  
  124. @Override
  125. public Delegate remove(String name) {
  126. }
  127.  
  128.  
  129. @Override
  130. public void displayDB() {
  131.  
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement