Advertisement
Guest User

Untitled

a guest
Nov 20th, 2019
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.19 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. public int visited;
  7.  
  8.  
  9. public class Node{
  10. private Delegate data;
  11. private Node left;
  12. private Node right;
  13.  
  14. public Node(Delegate data){
  15. this.data = data;
  16. right = null;
  17. left = null;
  18. }
  19.  
  20. }
  21. public DelegateTree(){
  22. root = null;
  23. numEntries = 0;
  24. }
  25.  
  26. @Override
  27. public void clearDB() {
  28. root = null;
  29. }
  30.  
  31. public boolean Contains(Node node, String name){
  32. boolean got = false;
  33. if (node == null){
  34. System.out.println("Name not found");
  35. return false;
  36. }
  37. else if(name.compareTo(node.data.getName()) == 0){
  38. System.out.println("Person has been found:");
  39. return true;
  40. }
  41. 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.
  42. return Contains(node.right, name);
  43. }
  44. 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.
  45. return Contains(node.left, name);
  46. }
  47. return false;
  48. }
  49.  
  50.  
  51.  
  52. @Override
  53. public boolean containsName(String name) {
  54. assert !name.equals("");
  55. boolean containing = Contains(root,name);
  56. if(containing){
  57. return true;
  58. }
  59. else{
  60. return false;
  61. }
  62. }
  63.  
  64. public Node findPos(Node node, String name){
  65. if (node == null){
  66. return null;
  67. }
  68. else if(name.compareTo(node.data.getName()) == 0){
  69. return node;
  70. }
  71. else if(name.compareTo(node.data.getName()) > 0){
  72. node.right = findPos(node.right, name);
  73. }
  74. else if(name.compareTo(node.data.getName()) < 0){
  75. node.left = findPos(node.left, name);
  76. }
  77. return null;
  78. }
  79.  
  80.  
  81. @Override
  82. public Delegate get(String name) {
  83. System.out.println("Getting name..");
  84. Node find = findPos(root, name);
  85. if (find == null){
  86. System.out.println("Name not found" + name);
  87. return null;
  88. }
  89. System.out.println("Name is found:" + name);
  90. Delegate temp = find.data;
  91. return temp;
  92. }
  93.  
  94. @Override
  95. public int size() {
  96. return numEntries;
  97. }
  98.  
  99. @Override
  100. public boolean isEmpty() {
  101. return size() == 0;
  102. }
  103.  
  104. @Override
  105. public Delegate put(Delegate delegate) {
  106. assert delegate == null;
  107. visited = 1;
  108. Node temp = insert(root, delegate);
  109. if (numEntries == 0){
  110. root = temp;
  111. }
  112. numEntries++;
  113. return delegate;
  114.  
  115. }
  116.  
  117. public Node insert(Node node, Delegate data){
  118. String name = data.getName();
  119. if (node == null){
  120. node = new Node(data);
  121. System.out.println(name);
  122. }
  123. 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.
  124. node.right = insert(node.right, data);
  125. visited++;
  126. }
  127. 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.
  128. node.left = insert(node.left, data);
  129. visited++;
  130. }
  131. return node;
  132. }
  133.  
  134.  
  135. public boolean delete(Node node, Delegate data){
  136. Node current = root;
  137. Node parent = root;
  138. boolean leftChild = false;
  139. while(current){
  140. parent = current;
  141. if(data < current.data){
  142. // Move to the left if searched value is less
  143. current = current.left;
  144. leftChild = true;
  145. }
  146. else{
  147. // Move to the right if searched value is >=
  148. current = current.right;
  149. leftChild = false;
  150. }
  151. if(current == null){
  152. return false;
  153. }
  154. }
  155. // if reached here means node to be deleted is found
  156.  
  157. // Leaf node deletion case
  158. if(current.left == null && current.right == null){
  159. System.out.println("Leaf node deletion case");
  160. // if root node is to be deleted
  161. if(current == root){
  162. root = null;
  163. }
  164. // left child
  165. else if(leftChild){
  166. parent.left = null;
  167. }
  168. // right child
  169. else{
  170. parent.right = null;
  171. }
  172. }
  173. // Node to be deleted has one child case
  174. // Node to be deleted has right child
  175. else if(current.left == null){
  176. System.out.println("One right child deletion case");
  177. // if root node is to be deleted
  178. if(current == root){
  179. root = current.right;
  180. }
  181. // if deleted node is left child
  182. else if(leftChild){
  183. parent.left = current.right;
  184. }
  185. // if deleted node is right child
  186. else{
  187. parent.right = current.right;
  188. }
  189. }
  190. // Node to be deleted has left child
  191. else if(current.right == null){
  192. System.out.println("One left child deletion case");
  193. if(current == root){
  194. root = current.left;
  195. }
  196. // if deleted node is left child
  197. else if(leftChild){
  198. parent.left = current.left;
  199. }
  200. // if deleted node is right child
  201. else{
  202. parent.right = current.left;
  203. }
  204. }
  205. // Node to be deleted has two children case
  206. else{
  207. System.out.println("Two children deletion case");
  208. // find in-order heir of the node to be deleted
  209. Node heir = findHeir(current);
  210. if(current == root){
  211. root = heir;
  212. }
  213. // if deleted node is left child
  214. else if(leftChild){
  215. parent.left = heir;
  216. }
  217. // if deleted node is right child
  218. else{
  219. parent.right = heir;
  220. }
  221. heir.left = current.left;
  222. }
  223. return true;
  224. }
  225. // Method to find the in-order heir of the deleted node
  226. /*private Node findHeir(Node node){
  227. Node successor = node;
  228. Node successorParent = node;
  229. // Start from the right child of the node to be deleted
  230. Node current = node.right;
  231. while(current != null){
  232. successorParent = successor;
  233. successor = current;
  234. current = current.left;
  235. }
  236. // When In-order heir is in the left subtree
  237. // perform two ref changes here as we have
  238. // access to successorParent
  239. if(successor != node.right){
  240. successorParent.left = successor.right;
  241. // applicable only when heir is not right child
  242. // so doing here
  243. successor.right = node.right;
  244. }
  245. return successor;
  246. }*/
  247.  
  248.  
  249.  
  250.  
  251. @Override
  252. public Delegate remove(String name) {
  253.  
  254. }
  255.  
  256. public static void TraversalInOrder(Node node) {
  257. if (node != null) {
  258. TraversalInOrder(node.left);
  259. System.out.println(node.data.getName());
  260. TraversalInOrder(node.right);
  261. }
  262. }
  263. @Override
  264. public void displayDB() {
  265. TraversalInOrder(root);
  266. }
  267. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement