Advertisement
Guest User

IntTree Chip

a guest
Jan 28th, 2020
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.89 KB | None | 0 0
  1. package hw3;
  2.  
  3. import java.util.LinkedList;
  4.  
  5. /* ***********************************************************************
  6. * A simple BST with int keys and no values
  7. *
  8. * Complete each function below.
  9. * Write each function as a separate recursive definition (do not use more than one helper per function).
  10. * Depth of root==0.
  11. * Height of leaf==0.
  12. * Size of empty tree==0.
  13. * Height of empty tree=-1.
  14. *
  15. * TODO: complete the functions in this file.
  16. * DO NOT change the Node class.
  17. * DO NOT change the name or type of any function (the first line of the function)
  18. *
  19. * Restrictions:
  20. * - no loops (you cannot use "while" "for" etc...)
  21. * - only one helper function per definition
  22. * - no fields (static or non static). Only local variables
  23. *************************************************************************/
  24. public class IntTree {
  25. private Node root;
  26. private static class Node {
  27. public final int key;
  28. public Node left, right;
  29. public Node(int key) { this.key = key; }
  30. }
  31.  
  32. public void printInOrder() {
  33. printInOrder(root);
  34. }
  35.  
  36. private void printInOrder(Node n) {
  37. if (n == null)
  38. return;
  39. printInOrder(n.left);
  40. System.out.println(n.key);
  41. printInOrder(n.right);
  42. }
  43.  
  44. // the number of nodes in the tree
  45. public int size() {
  46. // TODO
  47. return sizeHelper(root);
  48. }
  49. private int sizeHelper(Node tree) {
  50. int leftT = sizeHelper(tree.left);
  51. int rightT = sizeHelper(tree.right);
  52. return leftT + rightT + 1;
  53. }
  54.  
  55. // Recall the definitions of height and depth.
  56. // in the BST with level order traversal "41 21 61 11 31",
  57. // node 41 has depth 0, height 2
  58. // node 21 has depth 1, height 1
  59. // node 61 has depth 1, height 0
  60. // node 11 has depth 2, height 0
  61. // node 31 has depth 2, height 0
  62. // height of the whole tree is the height of the root
  63.  
  64.  
  65. // 20 points
  66. /* Returns the height of the tree.
  67. * For example, the BST with level order traversal 50 25 100 12 37 150 127
  68. * should return 3.
  69. *
  70. * Note that the height of the empty tree is defined to be -1.
  71. */
  72. public int height() {
  73. // TODO
  74. return heightHelper(root);
  75. }
  76. private int heightHelper(Node tree) {
  77. if (tree==null) {
  78. return -1;
  79. }
  80. else {
  81. int treeL = heightHelper(tree.left);
  82. int treeR = heightHelper(tree.right);
  83. if (treeL > treeR) {
  84. return treeL + 1;
  85. }else {
  86. return treeR + 1;
  87. }
  88. }
  89. }
  90.  
  91.  
  92. // 20 points
  93. /* Returns the number of nodes with odd keys.
  94. * For example, the BST with level order traversal 50 25 100 12 37 150 127
  95. * should return 3 (25, 37, and 127).
  96. */
  97. public int sizeOdd() {
  98. // TODO
  99. return sizeOddHelper(root);
  100. }
  101. private int sizeOddHelper(Node tree) {
  102. if (tree == null) {
  103. return 0;
  104. }
  105. else {
  106. int treeL = sizeOddHelper(tree.left);
  107. int treeR = sizeOddHelper(tree.right);
  108. if (tree.key % 2 != 0) {
  109. return treeL + treeR + 1;
  110. }
  111. else {
  112. return treeL + treeR;
  113. }
  114. }
  115. }
  116.  
  117.  
  118. // 20 points
  119. /* Returns true if this tree and that tree "look the same." (i.e. They have
  120. * the same keys in the same locations in the tree.)
  121. * Note that just having the same keys is NOT enough. They must also be in
  122. * the same positions in the tree.
  123. */
  124. public boolean treeEquals(IntTree that) {
  125. // TODO
  126. return treeEqualsHelper(that.root, root);
  127. }
  128. private boolean treeEqualsHelper(Node that, Node tree) {
  129. if (that == null && tree == null) {
  130. return true;
  131. }
  132. if (that == null && tree != null) {
  133. return false;
  134. }
  135. if (that != null && tree == null) {
  136. return false;
  137. }
  138.  
  139. else {
  140. boolean tempLeft = treeEqualsHelper(that.left, tree.left);
  141. boolean tempRight = treeEqualsHelper(that.right, tree.right);
  142.  
  143. if (that.key == tree.key) {
  144. return tempLeft || tempRight || true;
  145. }
  146. else {
  147. return true;
  148. }
  149. }
  150.  
  151. }
  152.  
  153.  
  154.  
  155.  
  156. // 10 points
  157. /* Returns the number of nodes in the tree, at exactly depth k.
  158. * For example, given BST with level order traversal 50 25 100 12 37 150 127
  159. * the following values should be returned
  160. * t.sizeAtDepth(0) == 1 [50]
  161. * t.sizeAtDepth(1) == 2 [25, 100]
  162. * t.sizeAtDepth(2) == 3 [12, 37, 150]
  163. * t.sizeAtDepth(3) == 1 [127]
  164. * t.sizeAtDepth(k) == 0 for k >= 4
  165. */
  166. public int sizeAtDepth(int k) {
  167. // TODO
  168. return sizeAtDepthHelper(k, root);
  169. }
  170. private int sizeAtDepthHelper(int k, Node tree) {
  171. int tempLeft = sizeAtDepthHelper(k--, tree.left);
  172. int tempRight = sizeAtDepthHelper(k--, tree.right);
  173. if (k==0) {
  174. return num;
  175. }else {
  176. return 1 + tempLeft + tempRight;
  177. }
  178. }
  179.  
  180. // 10 points
  181. /*
  182. * Returns the number of nodes in the tree "above" depth k.
  183. * Do not include nodes at depth k.
  184. * For example, given BST with level order traversal 50 25 100 12 37 150 127
  185. * the following values should be returned
  186. * t.sizeAboveDepth(0) == 0
  187. * t.sizeAboveDepth(1) == 1 [50]
  188. * t.sizeAboveDepth(2) == 3 [50, 25, 100]
  189. * t.sizeAboveDepth(3) == 6 [50, 25, 100, 12, 37, 150]
  190. * t.sizeAboveDepth(k) == 7 for k >= 4 [50, 25, 100, 12, 37, 150, 127]
  191. */
  192. public int sizeAboveDepth(int k) {
  193. // TODO
  194. return -1;
  195. }
  196.  
  197. // 10 points
  198. /* Returns true if for every node in the tree has the same number of nodes
  199. * to its left as to its right.
  200. * For example, the BST with level order traversal 50 25 100 12 37 150 127
  201. * is NOT perfectly balanced. Although most of the nodes (including the root) have the same number of nodes
  202. * to the left as to the right, the nodes with 100 and 150 do not and so the tree is not perfeclty balanced.
  203. *
  204. * HINT: In the helper function, change the return type to int and return -1 if the tree is not perfectly balanced
  205. * otherwise return the size of the tree. If a recursive call returns the size of the subtree, this will help
  206. * you when you need to determine if the tree at the current node is balanced.
  207. */
  208. public boolean isPerfectlyBalanced() {
  209. // TODO
  210. return false;
  211. }
  212.  
  213.  
  214.  
  215. /*
  216. * Mutator functions
  217. * HINT: This is easier to write if the helper function returns Node, rather than void
  218. * similar to recursive mutator methods on lists.
  219. */
  220.  
  221. // 10 points
  222. /* Removes all subtrees with odd roots (if node is odd, remove it and its descendants)
  223. * A node is odd if it has an odd key.
  224. * If the root is odd, then you should end up with the empty tree.
  225. * For example, when called on a BST
  226. * with level order traversal 50 25 100 12 37 150 127
  227. * the tree will be changed to have level order traversal 50 100 150
  228. */
  229. public void removeOddSubtrees () {
  230. Node temp = removeOddSubtreesHelper(root);
  231. root = temp;
  232. }
  233. private Node removeOddSubtreesHelper(Node tree) {
  234. if (tree == null) {
  235. return tree;
  236. }
  237. else{
  238. Node tempLeft = removeOddSubtreesHelper(tree.left);
  239. Node tempRight = removeOddSubtreesHelper(tree.right);
  240. if (tree.key % 2 != 0) {
  241.  
  242. }
  243. }
  244. return tree;
  245. }
  246.  
  247.  
  248.  
  249. /* ***************************************************************************
  250. * Some methods to create and display trees
  251. *****************************************************************************/
  252.  
  253. // Do not modify "put"
  254. public void put(int key) { root = put(root, key); }
  255. private Node put(Node n, int key) {
  256. if (n == null) return new Node(key);
  257. if (key < n.key) n.left = put(n.left, key);
  258. else if (key > n.key) n.right = put(n.right, key);
  259. return n;
  260. }
  261. // This is what contains looks like
  262. public boolean contains(int key) { return contains(root, key); }
  263. private boolean contains(Node n, int key) {
  264. if (n == null) return false;
  265. if (key < n.key) return contains(n.left, key);
  266. else if (key > n.key) return contains(n.right, key);
  267. return true;
  268. }
  269. // Do not modify "copy"
  270. public IntTree copy () {
  271. IntTree that = new IntTree ();
  272. for (int key : levelOrder())
  273. that.put (key);
  274. return that;
  275. }
  276. // Do not modify "levelOrder"
  277. public Iterable<Integer> levelOrder() {
  278. LinkedList<Integer> keys = new LinkedList<Integer>();
  279. LinkedList<Node> queue = new LinkedList<Node>();
  280. queue.add(root);
  281. while (!queue.isEmpty()) {
  282. Node n = queue.remove();
  283. if (n == null) continue;
  284. keys.add(n.key);
  285. queue.add(n.left);
  286. queue.add(n.right);
  287. }
  288. return keys;
  289. }
  290. // Do not modify "toString"
  291. public String toString() {
  292. StringBuilder sb = new StringBuilder();
  293. for (int key: levelOrder())
  294. sb.append (key + " ");
  295. return sb.toString ();
  296. }
  297.  
  298. public void prettyPrint() {
  299. if (root == null)
  300. System.out.println("<EMPTY>");
  301. else
  302. prettyPrintHelper(root, "");
  303. }
  304.  
  305. private void prettyPrintHelper(Node n, String indent) {
  306. if (n != null) {
  307. System.out.println(indent + n.key);
  308. indent += " ";
  309. prettyPrintHelper(n.left, indent);
  310. prettyPrintHelper(n.right, indent);
  311. }
  312. }
  313.  
  314. public static void main(String[] args) {
  315. IntTree s = new IntTree();
  316. s.put(15);
  317. s.put(11);
  318. s.put(21);
  319. s.put(8);
  320. s.put(13);
  321. s.put(16);
  322. s.put(18);
  323. s.printInOrder();
  324. }
  325. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement