Advertisement
Guest User

Untitled

a guest
Apr 1st, 2015
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.41 KB | None | 0 0
  1. import java.util.Iterator;
  2.  
  3. public class BSTRefBased extends AbstractBinaryTree
  4. implements Iterable<WordRefs>
  5. {
  6. private TreeNode root;
  7.  
  8.  
  9. public BSTRefBased() {
  10. }
  11.  
  12.  
  13. public BSTRefBased(WordRefs item,
  14. AbstractBinaryTree left,
  15. AbstractBinaryTree right)
  16. {
  17. root = new TreeNode(item, null, null);
  18. if (left != null) {
  19. attachLeftSubtree(left);
  20. }
  21.  
  22. if (right != null) {
  23. attachRightSubtree(right);
  24. }
  25. }
  26.  
  27.  
  28. public boolean isEmpty() {
  29. return (root == null);
  30. }
  31.  
  32.  
  33. public void makeEmpty() {
  34. root = null;
  35. }
  36.  
  37.  
  38. protected TreeNode getRoot() {
  39. return root;
  40. }
  41.  
  42.  
  43. protected void setRoot(TreeNode r) {
  44. this.root = r;
  45. }
  46.  
  47.  
  48. public WordRefs getRootItem() throws TreeException {
  49. if (root == null) {
  50. throw new TreeException("getRootItem() on empty tree");
  51. }
  52.  
  53. return root.item;
  54. }
  55.  
  56.  
  57. public void setRootItem(WordRefs item) {
  58. if (root == null) {
  59. root = new TreeNode(item);
  60. } else {
  61. root.item = item;
  62. }
  63. }
  64.  
  65.  
  66. public void attachLeft(WordRefs item) throws TreeException {
  67. if (isEmpty()) {
  68. throw new TreeException("attachLeft to empty tree");
  69. }
  70.  
  71. if (!isEmpty() && root.left != null) {
  72. throw new TreeException("attachLeft to occupied left child");
  73. }
  74.  
  75. root.left = new TreeNode(item, null, null);
  76.  
  77. return;
  78. }
  79.  
  80.  
  81. public void attachLeftSubtree(AbstractBinaryTree left) {
  82. if (isEmpty()) {
  83. throw new TreeException("attachLeftSubtree to empty tree");
  84. }
  85.  
  86. if (!isEmpty() && root.left != null) {
  87. throw new
  88. TreeException("attachLeftSubtree to occupied right child");
  89. }
  90.  
  91. root.left = left.getRoot();
  92. left.makeEmpty();
  93.  
  94. return;
  95. }
  96.  
  97.  
  98. public void attachRight(WordRefs item) throws TreeException {
  99. if (isEmpty()) {
  100. throw new TreeException("attachRight to empty tree");
  101. }
  102.  
  103. if (!isEmpty() && root.right != null) {
  104. throw new TreeException("attachRight to occupied right child");
  105. }
  106.  
  107. root.right = new TreeNode(item, null, null);
  108.  
  109. return;
  110. }
  111.  
  112.  
  113. public void attachRightSubtree(AbstractBinaryTree right) {
  114. if (isEmpty()) {
  115. throw new TreeException("attachRightSubtree to empty tree");
  116. }
  117.  
  118. if (!isEmpty() && root.right != null) {
  119. throw new
  120. TreeException("attachRightSubtree to occupied right child");
  121. }
  122.  
  123. root.right = right.getRoot();
  124. right.makeEmpty();
  125.  
  126. return;
  127. }
  128.  
  129.  
  130. public AbstractBinaryTree detachLeftSubtree()
  131. throws TreeException
  132. {
  133. if (root == null) {
  134. throw new TreeException("detachLeftSubtree on empty tree");
  135. }
  136.  
  137. BSTRefBased result = new BSTRefBased();
  138. result.setRoot(root.left);
  139. root.left = null;
  140.  
  141. return result;
  142. }
  143.  
  144.  
  145. public AbstractBinaryTree detachRightSubtree()
  146. throws TreeException
  147. {
  148. if (root == null) {
  149. throw new TreeException("detachLeftSubtree on empty tree");
  150. }
  151.  
  152. BSTRefBased result = new BSTRefBased();
  153. result.setRoot(root.right);
  154. root.right = null;
  155.  
  156. return result;
  157. }
  158.  
  159.  
  160. public void insert(String word) {
  161. root=insertItem(root, word);
  162. }
  163.  
  164.  
  165. protected TreeNode insertItem(TreeNode r, String word) {
  166. if(r==null){
  167. r = new TreeNode(new WordRefs(word));
  168. r.left = null;
  169. r.right = null;
  170. }
  171. else if(word.compareTo(r.item.getWord()) < 0){
  172. r.left = insertItem(r.left, word);
  173. } else if (word.compareTo(r.item.getWord()) > 0){
  174. r.left = insertItem(r.right, word);
  175. }
  176. return r;
  177. }
  178.  
  179.  
  180. public WordRefs retrieve(String word) {
  181. TreeNode node = retrieveItem(root, word);
  182. if (node == null) {
  183. return null;
  184. } else {
  185. return new WordRefs(node.item.getWord());
  186. }
  187. }
  188.  
  189.  
  190. protected TreeNode retrieveItem(TreeNode r, String word) {
  191. if (r == null){
  192. return null;
  193. }
  194. if (word.compareTo(r.item.getWord()) < 0){
  195. return retrieveItem(r.left, word);
  196. } else if (word.compareTo(r.item.getWord()) > 0 ){
  197. return retrieveItem(r.right, word);
  198. }
  199. return r;
  200. }
  201.  
  202.  
  203. public void delete(String word) {
  204. root = deleteItem(root, word);
  205. }
  206.  
  207.  
  208. protected TreeNode deleteItem(TreeNode r, String word) {
  209. if (r == null){
  210. return r;
  211. }
  212. if(word.compareTo(r.item.getWord()) < 0){
  213. r.left = deleteItem(r.left, word);
  214. } else if (word.compareTo(r.item.getWord()) > 0){
  215. r.right = deleteItem(r.right, word);
  216. } else if(r.left != null && r.right != null)
  217. {
  218. root = findLeftMost(r.right);
  219. r.right = deleteItem(r.right, word).right;
  220. } else {
  221. return r;
  222. }
  223. return r;
  224. }
  225.  
  226.  
  227. protected TreeNode deleteNode(TreeNode node) {
  228. node = null;
  229. return node;
  230. }
  231.  
  232.  
  233. protected TreeNode findLeftMost(TreeNode node) {
  234. if(node == null){
  235. return null;
  236. } else {
  237. return findLeftMost(node.left);
  238. }
  239. }
  240.  
  241.  
  242. protected TreeNode deleteLeftMost(TreeNode node) {
  243. if (node == null){
  244. return null;
  245. } else if(node.left == null){
  246. return findLeftMost(node.left);
  247. } else {
  248. return deleteNode(node.left);
  249. }
  250. }
  251.  
  252.  
  253. public Iterator<WordRefs> iterator() {
  254. return new BSTIterator(this);
  255. }
  256.  
  257.  
  258. public static void main(String args[]) {
  259. BSTRefBased t;
  260. AbstractBinaryTree tt;
  261. int i;
  262. boolean result;
  263. String message;
  264.  
  265. message = "Test 1: inserting 'humpty' -- ";
  266. t = new BSTRefBased();
  267. try {
  268. t.insert("humpty");
  269. result = t.getRootItem().getWord().equals("humpty");
  270. } catch (Exception e) {
  271. result = false;
  272. }
  273. System.out.println(message + (result ? "passed" : "FAILED"));
  274.  
  275. message = "Test 2: inserting 'humpty', 'dumpty', 'sat' -- ";
  276. t = new BSTRefBased();
  277. try {
  278. t.insert("humpty");
  279. t.insert("dumpty");
  280. t.insert("sat");
  281. result = t.getRootItem().getWord().equals("humpty");
  282. tt = t.detachLeftSubtree();
  283. result &= tt.getRootItem().getWord().equals("dumpty");
  284. tt = t.detachRightSubtree();
  285. result &= tt.getRootItem().getWord().equals("sat");
  286. } catch (Exception e) {
  287. result = false;
  288. }
  289. System.out.println(message + (result ? "passed" : "FAILED"));
  290.  
  291. message = "Test 3: deleting 'humpty' -- ";
  292. t = new BSTRefBased();
  293. try {
  294. t.delete("humpty");
  295. result = t.getRootItem().getWord().equals(null);
  296. } catch (Exception e) {
  297. result = false;
  298. }
  299. System.out.println(message + (result ? "passed" : "FAILED"));
  300.  
  301. message = "Test 4: deleting 'dumpty' 'sat' -- ";
  302. t = new BSTRefBased();
  303. try {
  304. t.delete("dumpty");
  305. t.delete("sat");
  306. result = t.getRootItem().getWord().equals(null);
  307. tt = t.detachLeftSubtree();
  308. result &= tt.getRootItem().getWord().equals(null);
  309. } catch (Exception e) {
  310. result = false;
  311. }
  312. System.out.println(message + (result ? "passed" : "FAILED"));
  313. }
  314. }
  315.  
  316. import java.util.LinkedList;
  317.  
  318. public class BSTIterator implements java.util.Iterator<WordRefs> {
  319. private BSTRefBased t;
  320. private WordRefs currentItem;
  321. private LinkedList<WordRefs> list;
  322.  
  323. public BSTIterator(BSTRefBased t) {
  324. this.t = t;
  325. currentItem = null;
  326. list = new LinkedList<>();
  327. setInorder();
  328. }
  329.  
  330. public boolean hasNext() {
  331. return false;
  332. }
  333.  
  334. public WordRefs next() throws java.util.NoSuchElementException {
  335. return null;
  336. }
  337.  
  338. public void remove() throws UnsupportedOperationException {
  339. throw new UnsupportedOperationException();
  340. }
  341.  
  342. public void setPreorder() {
  343. }
  344.  
  345. public void setInorder() {
  346. }
  347.  
  348. public void setPostorder() {
  349. }
  350.  
  351. private void preorder(TreeNode node) {
  352. if(node == null)
  353. {
  354. return;
  355. }
  356. preorder(node.left);
  357. preorder(node);
  358. preorder(node.right);
  359. }
  360.  
  361. private void inorder(TreeNode node) {
  362. if(node == null)
  363. {
  364. return;
  365. }
  366. inorder(node.left);
  367. inorder(node);
  368. inorder(node.right);
  369. }
  370.  
  371. private void postorder(TreeNode node) {
  372. if(node == null)
  373. {
  374. return;
  375. }
  376. postorder(node.left);
  377. postorder(node);
  378. postorder(node.right);
  379. }
  380. }
  381.  
  382. inorder == left node, the actual node, right node
  383. preorder == the actual node, left node,right node
  384. postorder == left node, right node, the actual node
  385.  
  386. public void setInorder() {
  387. list = new LinkedList<>();
  388. }
  389.  
  390. private void inorder(TreeNode node) {
  391. if(node != null)
  392. {
  393. inorder(node.left); //traverse left first
  394. list.add(node.data); //traverse the actual nodes data
  395. inorder(node.right); // then traverse right
  396. }
  397.  
  398. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement