Advertisement
Guest User

Untitled

a guest
Jan 24th, 2017
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.90 KB | None | 0 0
  1. public class Tree {
  2. class IntNode {
  3.  
  4. // Do not change these variables and the constructor. Do not add further variables and constructors here.
  5. private int elem;
  6. private IntNode left;
  7. private IntNode right;
  8.  
  9. private IntNode(int elem) {
  10. this.elem = elem;
  11. }
  12.  
  13. void add(int elem){
  14. if(elem< this.elem){ //it should be in the left part
  15. if(this.left==null){
  16. this.left=new IntNode(elem);
  17. }else{
  18. this.left.add(elem);
  19. }
  20. }else { //right subtree -> e >= this.elem
  21. if(this.right==null){
  22. this.right=new IntNode(elem);
  23. }else {
  24. this.right.add(elem);
  25. }
  26. }
  27. }
  28.  
  29. private IntNode copyNode(){
  30. IntNode helpNode = new IntNode(this.elem);
  31. if(this.left!=null){
  32. helpNode.add(this.left.elem);
  33. }
  34. if(this.right!=null){
  35. helpNode.add(this.right.elem);
  36. }
  37. return helpNode;
  38. }
  39.  
  40. int countLeaves(){
  41. int count = 0;
  42. if(this.left== null || this.right==null){
  43. return 1;
  44. }
  45. if(this.left!=null){
  46. count+= this.left.countLeaves();
  47. }
  48. if(this.right!=null){
  49. count+=this.right.countLeaves();
  50. }
  51. return count;
  52. }
  53.  
  54. String toStringPrefix(){ //node, left, right
  55. String s = Integer.toString(elem);
  56. if(this.left!=null){
  57. s += " " + this.left + " ";
  58. }
  59. if(this.right!= null){
  60. s += this.right;
  61. }
  62. return s;
  63. }
  64.  
  65. String toStringPostfix(){ //left,right, node
  66. String s = "";
  67. if(this.left!=null){
  68. s = this.left + " ";
  69. }
  70. if(this.right!= null){
  71. s+= " " + this.right + " ";
  72. }
  73. s += Integer.toString(elem);
  74. return s;
  75. }
  76.  
  77. @Override
  78. public String toString(){ //inorder - left, node, right
  79. String s = Integer.toString(elem);
  80. if(this.left!=null){
  81. s = this.left + " " + s;
  82. }
  83.  
  84. if(this.right!= null){
  85. s+= " " + this.right ;
  86. }
  87. return s;
  88. }
  89.  
  90. int maxDepth(){
  91. if(this.left== null && this.right==null){
  92. return 0;
  93. }
  94. int l = 0;
  95. int r = 0;
  96.  
  97. if(this.left!= null){
  98. l = this.left.maxDepth();
  99. }
  100. if(this.right!=null){
  101. r = this.right.maxDepth();
  102. }
  103.  
  104. return Math.max(l ,r) + 1 ;
  105. }
  106.  
  107. IntNode find(int elem){ //check
  108. IntNode helpNode = this;
  109. if(helpNode.elem == elem){
  110. return helpNode;
  111. }
  112. else if(elem < helpNode.elem){ //maybe the element is in the left subtree
  113. helpNode = helpNode.left;
  114. }else {
  115. helpNode = helpNode.right;
  116. }
  117. return helpNode;
  118. }
  119.  
  120. /*
  121. SortedList toSortedList(){
  122. SortedList helpList = new SortedList();
  123. if(this.left!=null){
  124. helpList = this.left.toSortedList();
  125. }
  126. helpList.addSorted(this.elem);
  127. if(this.right!= null){
  128. SortedList listRight = this.right.toSortedList();
  129. SortedList.Node current = listRight.getHead();
  130. while(current!= null){
  131. helpList.addSorted(current.getElem());
  132. current = current.getNext();
  133. }
  134. }
  135.  
  136. return helpList;
  137. } */
  138.  
  139. // hashCode sollte hier zwar implementiert sein, geht aber nicht in die Beurteilung ein.
  140. @Override
  141. public int hashCode() {
  142. return 11111;
  143. }
  144.  
  145. } // End of the definition of Node
  146.  
  147. // The root of the tree. Do not change or add object variables.
  148. private IntNode root = null;
  149.  
  150. public boolean empty(){
  151. return this.root==null;
  152. }
  153.  
  154. public void add(int elem){
  155. if(empty()){
  156. this.root = new IntNode(elem);
  157. }else {
  158. this.root.add(elem);
  159. }
  160. }
  161.  
  162. public void remove(int elem){
  163.  
  164. }
  165.  
  166. public IntNode find(int elem){ //contains method
  167. //whether a Node is in the Tree or not
  168. //TODO returns the node where you found the element or null otherwise
  169. if(empty()){
  170. return null;
  171. }else {
  172. return root.find(elem);
  173. }
  174. }
  175.  
  176. public int countLeaves(){
  177. //TODO returns the number of leaf-nodes in the whole tree
  178. if(empty()){
  179. return 0;
  180. }
  181. else {
  182. return this.root.countLeaves();
  183. }
  184.  
  185. }
  186.  
  187. public int maxDepth(){
  188. //TODO returns the maximum depth of a tree (root depth = 0)
  189. if(empty()){
  190. return 0;
  191. } else {
  192. return this.root.maxDepth();
  193. }
  194. }
  195.  
  196.  
  197. @Override
  198. public String toString(){
  199. //TODO print INfix notation
  200. if(empty()) {
  201. return "";
  202. }
  203. return this.root.toString();
  204. }
  205.  
  206.  
  207. public String toStringPrefix(){
  208. //TODO print PREfix notation
  209. if(empty()) {
  210. return "";
  211. }
  212. return this.root.toStringPrefix();
  213. }
  214.  
  215.  
  216. public String toStringPostfix(){
  217. //TODO print POSTfix notation
  218. if(empty()){
  219. return "";
  220. }
  221. return this.root.toStringPostfix();
  222. }
  223.  
  224. /*
  225. public SortedList toSortedList(){
  226. //TODO convert to a SortedList
  227. return root.toSortedList();
  228. }*/
  229.  
  230. public IntNode copyTree(){
  231. if(empty()){
  232. return null;
  233. }else {
  234. return this.root.copyNode();
  235. }
  236. }
  237.  
  238.  
  239.  
  240. // Just for testing, not used for assessment (geht nicht in die Beurteilung ein).
  241. public static void main(String[] args) {
  242. Tree myTree = new Tree();
  243. myTree.add(20);
  244. myTree.add(10);
  245. myTree.add(5);
  246. myTree.add(15);
  247. myTree.add(30);
  248. myTree.add(25);
  249. myTree.add(35);
  250. myTree.add(55);
  251.  
  252.  
  253. System.out.println(myTree);
  254. System.out.println();
  255. System.out.println(myTree.toStringPostfix());
  256. System.out.println();
  257. System.out.println(myTree.toStringPrefix());
  258. System.out.println();
  259. System.out.println(myTree.maxDepth());
  260.  
  261. myTree.copyTree();
  262. System.out.println(myTree);
  263. }
  264. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement