Advertisement
Guest User

Untitled

a guest
Mar 20th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.52 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.Collections;
  3.  
  4. public class BSTDictionary<E, K> implements Dictionary<Object, SortableString>{
  5. private BSTNode root;
  6. private BSTNode curr;
  7.  
  8. /*
  9. * Basic constructor, sets root to null
  10. */
  11. public BSTDictionary() {
  12. root = null;
  13. }
  14.  
  15. /*
  16. * Searches the tree for the given key
  17. * @param key the key that dictates the node that is to be found
  18. * @return returns the element of the node with the given key or null if not found
  19. */
  20. public Object search(SortableString key) {
  21. boolean found = false;
  22. curr = root;
  23. while(!found && curr != null) { //while loop to search until found or end of tree
  24. if(key.compareTo(curr.getKey()) < 0) {//less than | key < curr
  25. curr = curr.getLeft();
  26.  
  27. }
  28. else if(key.compareTo(curr.getKey()) > 0) {//greater than | key > curr
  29. curr = curr.getRight();
  30.  
  31. }
  32. else if (key.compareTo(curr.getKey()) == 0) {//if node is found
  33. found = true;
  34. }
  35. }
  36. if(!found)
  37. {
  38. return null;
  39. }
  40. else {
  41. return curr.getElement();
  42. }
  43. }
  44.  
  45. /*
  46. * creates a node with the provided key and element and adds it to the tree
  47. *
  48. * @param key the key that dictates the node that is to be found
  49. * @param element the element of the node
  50. */
  51. public void insert(SortableString key, java.lang.Object element){
  52. boolean inserted = false;
  53. curr = root;
  54. //System.out.println("the key is: " + key + " The element is: " + element); //prints the key and element created
  55. if(key == null) {
  56. return;
  57. }
  58. else if(root == null) {//case if node inserted is the first one
  59. root = new BSTNode(key, element, null, null);
  60. }
  61. else {
  62. while(!inserted) { //while loop to navigate until the right spot is found
  63. if(key.compareTo(curr.getKey()) < 0) {//less than | key < curr
  64.  
  65. if(curr.getLeft() == null) {// checks if there is a node left of the parent
  66. curr.setLeft(new BSTNode(key, element, null, null));//sets a new node to the left of the parent with given params
  67. inserted = true;
  68. //System.out.println("added left");
  69. }
  70. else {
  71. curr = curr.getLeft();//moves to the left and repeats loop
  72. }
  73. }
  74. else if(key.compareTo(curr.getKey()) > 0) {//greater than | key > curr
  75. if(curr.getRight() == null) {//checks of right slot is empty
  76. curr.setRight(new BSTNode(key, element, null, null));//sets a new node to the right of the parent
  77. inserted = true;
  78. // System.out.println("added right");
  79. }
  80. else {
  81. curr = curr.getRight();//moves to the right and repeats loop
  82. }
  83. }
  84. else {//element is already in the tree if it reaches here
  85. inserted = true;
  86. }
  87. }
  88.  
  89. }
  90. }
  91.  
  92. /*
  93. * Deletes the specified key from the tree
  94. *
  95. * @param key the key to be deleted
  96. */
  97. public void delete(SortableString key) {
  98. // System.out.println("============");
  99. // System.out.println("Deleting: " + key);
  100. BSTNode parent = root;//lags 1 node behind the curr node
  101. curr = root;
  102.  
  103. if(root == null) {//checks if tree is empty
  104. // System.out.println("tree is empty");
  105. return;
  106. }
  107. boolean found = false;
  108. while(!found && curr != null) {//loops until end of tree or until the key is found
  109. if(key.compareTo(curr.getKey()) < 0) {//less than | key < curr
  110. parent = curr;
  111. curr = curr.getLeft();//moves the curr node along the tree
  112.  
  113. }
  114. else if(key.compareTo(curr.getKey()) > 0) {//greater than | key > curr
  115. parent = curr;
  116. curr = curr.getRight();//moves the curr node along the tree
  117.  
  118. }
  119. else if (key.compareTo(curr.getKey()) == 0) {//checks if key is found
  120. // System.out.println("found: " + curr.getKey());
  121. found = true;
  122. }
  123. }
  124.  
  125.  
  126. if(curr == null) {//checks if
  127. // System.out.println("key is not in the tree: " + key);
  128. }
  129. //case 1 - no children
  130. else if(curr.getLeft() == null && curr.getRight() == null){
  131. // System.out.println("case 1");
  132. curr = parent;
  133.  
  134. if(curr.getLeft() != null) {
  135. if(curr.getLeft().getKey().compareTo(key) == 0) {
  136. // System.out.println("Deleted: " + curr.getLeft().getKey());
  137. curr.setLeft(null);
  138. }
  139. }
  140. if(curr.getRight() != null) {
  141. if(curr.getRight().getKey().compareTo(key) == 0) {
  142. // System.out.println("Deleted: " + curr.getRight().getKey());
  143. curr.setRight(null);
  144. }
  145. }
  146. }
  147.  
  148.  
  149. //case 2 - one child
  150.  
  151. else if(curr.getLeft() != null && curr.getRight() == null){ //has a left leaf
  152. // System.out.println("case 2L");
  153.  
  154. BSTNode temp;
  155. temp = curr.getLeft();
  156. if(curr == root){
  157. root = curr.getLeft();
  158. }
  159. else {
  160. // System.out.println("Deleted: " + curr.getKey());
  161. curr = parent;
  162. if(curr.getLeft() != null) {
  163. if(curr.getLeft().getKey() == key) {
  164. curr.setLeft(temp);
  165. }
  166. }
  167. else if(curr.getRight() != null) {
  168. if(curr.getRight().getKey() == key) {
  169. curr.setRight(temp);
  170. }
  171. }
  172. }
  173. }
  174.  
  175. else if(curr.getLeft() == null && curr.getRight() != null){//has a right leaf
  176. // System.out.println("case 2R");
  177. BSTNode temp;
  178. temp = curr.getRight();
  179. // System.out.println("Deleted: " + curr.getKey());
  180. if(curr == root){
  181. root = curr.getRight();
  182. }
  183. else {
  184. curr = parent;
  185. if(curr.getLeft() != null) {
  186. if(curr.getLeft().getKey() == key) {
  187. curr.setLeft(temp);
  188. }
  189. }
  190. else if(curr.getRight() != null) {
  191. if(curr.getRight().getKey() == key) {
  192. curr.setRight(temp);
  193. }
  194. }
  195. }
  196. }
  197.  
  198. //case 3 - 2 children
  199. else if(curr.getLeft() != null && curr.getRight() != null){//has 2 children
  200. // System.out.println("case 3");
  201. BSTNode tempR, tempL;
  202. tempR = curr.getRight();
  203. tempL = curr.getLeft();
  204. // System.out.println("Deleted: " + curr.getKey());
  205. curr = curr.getRight();
  206.  
  207.  
  208. while(curr.getLeft() != null) {
  209. curr = curr.getLeft();
  210. }
  211.  
  212. if(parent.getLeft() != null && parent.getLeft().getKey() == key) {
  213. parent.setLeft(curr);
  214. curr.setLeft(tempL);
  215. curr.setRight(tempR);
  216. }
  217. else if( parent.getRight() != null && parent.getRight().getKey() == key) {
  218. parent.setRight(curr);
  219. curr.setLeft(tempL);
  220. curr.setRight(tempR);
  221. }
  222. }
  223. }
  224.  
  225.  
  226. public void printTree() {
  227. ArrayList<String> elements = new ArrayList<String>();
  228. elements = traverse(root, elements);
  229. Collections.sort(elements);
  230. for(String s: elements) {
  231. System.out.println(s + "");
  232. }
  233.  
  234. }
  235.  
  236. private ArrayList traverse(BSTNode root,ArrayList list){
  237. if (root.getLeft() != null){
  238. traverse(root.getLeft(), list);
  239. }
  240. list.add(root.getElement());
  241. if (root.getRight() != null){
  242. traverse(root.getRight(), list);
  243. }
  244. return list;
  245. }
  246.  
  247.  
  248.  
  249. public int depth() {
  250. return maxDepth(root);
  251. }
  252.  
  253.  
  254. private int maxDepth(BSTNode node) {
  255. if (node == null) {
  256. return 0;
  257. } else {
  258. // compute the depth of each subtree
  259. int leftDepth = maxDepth(node.getLeft());
  260. int rightDepth = maxDepth(node.getRight());
  261. // find the larger one
  262. if (leftDepth > rightDepth )
  263. return (leftDepth + 1);
  264. else
  265. return (rightDepth + 1);
  266. }
  267. }
  268.  
  269.  
  270. public BSTNode find(Sortable key) {
  271. curr = root;
  272. boolean found = false;
  273. while(!found) {
  274. if(key.compareTo(curr.getKey()) < 0) {//less than | key < curr
  275. curr = curr.getLeft();
  276. }
  277. else if(key.compareTo(curr.getKey()) < 0) {//greater than | key > curr
  278. curr = curr.getRight();
  279. }
  280. else {
  281.  
  282. return curr;
  283. }
  284. }
  285. return null;
  286. }
  287.  
  288.  
  289. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement