Faldi767

Binary Tree

Nov 4th, 2017
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.62 KB | None | 0 0
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package binarytree;
  7.  
  8. /**
  9. *
  10. * @author Faldi
  11. */
  12. class Node {
  13. int data;
  14. Node left;
  15. Node right;
  16. Node parent;
  17.  
  18. public Node(int i) {
  19. data = i;
  20. }
  21. private Node root;
  22. public void cekroot() {
  23. System.out.println("Nilai root : " + root.data);
  24. }
  25.  
  26. public boolean isempty() {
  27. return root == null;
  28. }
  29.  
  30. public void insert(int i) {
  31. Node temp = new Node(i);
  32. Node y = null;
  33. Node x = root;
  34.  
  35. if(isempty()) {
  36. System.out.println("nilai " + i + " menjadi root");
  37. root = temp;
  38. } else {
  39. while(x != null) {
  40. y = x;
  41. if(i<x.data) {
  42. x = x.left;
  43. } else {
  44. x = x.right;
  45. }
  46. }
  47. if(i<y.data) {
  48. y.left = temp;
  49. System.out.println("nilai " + i + " masuk sebelah kiri " + y.data);
  50. } else {
  51. y.right = temp;
  52. System.out.println("nilai " + i + " masuk sebelah kanan " + y.data);
  53. }
  54.  
  55. }
  56. }
  57.  
  58. public void searching(int i) {
  59. Node temp = root;
  60. boolean hasil = false;
  61. while(temp != null) {
  62. if(temp.data == i) {
  63. hasil = true;
  64. break;
  65. }
  66. if(i<temp.data) {
  67. temp = temp.left;
  68. } else {
  69. temp = temp.right;
  70. }
  71. }
  72. if(hasil) {
  73. System.out.println("Data ditemukan");
  74. } else {
  75. System.out.println("Maaf data tidak ditemukan");
  76. }
  77. }
  78.  
  79. public void findmin() {
  80. Node temp = root;
  81. while(temp.left != null) {
  82. temp = temp.left;
  83. }
  84. System.out.println("nilai minimum : " + temp.data);
  85. }
  86.  
  87. public void findmax() {
  88. Node temp = root;
  89. while(temp.right != null) {
  90. temp = temp.right;
  91. }
  92. System.out.println("nilai maksimum : " + temp.data);
  93. }
  94.  
  95. public void uruttree(Node i) {
  96. Node temp = i;
  97. if(temp != null) {
  98. uruttree(temp.left);
  99. System.out.println(temp.data);
  100. uruttree(temp.right);
  101. }
  102. }
  103.  
  104. public void urutroot() {
  105. System.out.println("Mengurutkan Tree : ");
  106. uruttree(root);
  107. }
  108.  
  109. public Node minvalue(Node i) {
  110. Node temp = i;
  111. while(temp.left != null) {
  112. temp = temp.left;
  113. }
  114. return temp;
  115. }
  116.  
  117. public void transplanted(Node del, Node reply) {
  118. if(del.parent == null) {
  119. root = reply;
  120. } else if(del == del.parent.left) {
  121. del.parent.left = reply;
  122. } else {
  123. del.parent.right = reply;
  124. }
  125. if(reply != null) {
  126. reply.parent = del.parent;
  127. }
  128. }
  129.  
  130. public void delete(int i) {
  131. Node y = null;
  132. Node x = root;
  133. while((x != null) && (x.data != i)) {
  134. y = x;
  135. if(i < x.data) {
  136. x = x.left;
  137. } else {
  138. x = x.right;
  139. }
  140. }
  141. if(x == null) {
  142. System.out.println("data tidak ditemukan");
  143. } else {
  144. x.parent = y;
  145. }
  146. if(x.left == null) {
  147. transplanted(x,x.right);
  148. } else if(x.right == null) {
  149. transplanted(x,x.left);
  150. } else {
  151. Node min = minvalue(x.right);
  152. if(x.right != min) {
  153. transplanted(min,min.right);
  154. min.right = x.right;
  155. min.right.parent = min;
  156.  
  157. transplanted(x,min);
  158. min.left = x.left;
  159. min.left.parent = min;
  160. }
  161. System.out.println("nilai " + i + " telah dihapus");
  162. }
  163. }
  164. }
  165. public class BinaryTree {
  166.  
  167. /**
  168. * @param args the command line arguments
  169. */
  170. public static void main(String[] args) {
  171. Node a = new Node(1);
  172. a.insert(2);
  173. a.cekroot();
  174. a.insert(4);
  175. a.insert(6);
  176. a.insert(9);
  177. a.insert(10);
  178. a.insert(5);
  179. a.findmax();
  180. a.findmin();
  181. a.urutroot();
  182. a.searching(9);
  183. }
  184.  
  185. }
Add Comment
Please, Sign In to add comment