Pabl0o0

Untitled

May 17th, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.74 KB | None | 0 0
  1. package lab8;
  2.  
  3. import java.util.Arrays;
  4. import java.util.LinkedList;
  5. import java.util.List;
  6.  
  7. public class drzewo_BST {
  8.  
  9. //klasa wewnętrzna węzła
  10. private static class Node {
  11. int key;
  12. Node left, right, parent = null;
  13. public String value;
  14. Node(int key){
  15. this.key = key;
  16. }
  17. }
  18.  
  19. public static Node root = null; // korzeń drzewa
  20.  
  21.  
  22. public static void insert(int key){
  23. if (root==null)
  24. root = new Node(key);
  25. else {
  26. Node actual = root;
  27. Node parent = null;
  28. while (actual != null){
  29. parent = actual;
  30. actual = (actual.key > key) ? actual.left : actual.right;
  31. }
  32. if (parent.key > key){
  33. parent.left = new Node(key);
  34. parent.left.parent = parent;
  35. }
  36. else {
  37. parent.right = new Node(key);
  38. parent.right.parent = parent;
  39. }
  40. }
  41. }
  42.  
  43.  
  44. public static void inOrder(Node node){
  45. if(node != null){
  46. inOrder(node.left);
  47. System.out.print(node.key+ ", ");
  48. inOrder(node.right);
  49. }
  50. }
  51.  
  52.  
  53. public static void preOrder(Node node){
  54. if (node == null)
  55. return;
  56. System.out.print(node.key + ", ");
  57. preOrder(node.left);
  58. preOrder(node.right);
  59. }
  60.  
  61.  
  62. public static void postOrder(Node node) {
  63. if (node == null)
  64. return;
  65. postOrder(node.left);
  66. postOrder(node.right);
  67. System.out.print(node.key + ", ");
  68. }
  69.  
  70.  
  71. public static Node search(int key) throws TreeException {
  72. Node actual = root;
  73. while(actual != null && actual.key != key)
  74. actual = (actual.key > key) ? actual.left : actual.right;
  75. if(actual == null)
  76. throw new TreeException("Key not found :-(");
  77. return actual;
  78. }
  79.  
  80.  
  81. private static class TreeException extends Throwable {
  82. TreeException() {}
  83. TreeException(String msg) { super(msg); }
  84. }
  85.  
  86.  
  87. private static Node min(Node node) {
  88. while(node.left != null)
  89. node = node.left;
  90. return node;
  91. }
  92.  
  93.  
  94. private static Node max(Node node) {
  95. while(node.right != null)
  96. node = node.right;
  97. return node;
  98. }
  99.  
  100.  
  101. static int i = -1;
  102. public static int height(Node node){
  103. if(node != null){
  104. if(node.left != null || node.right != null){
  105. i++;
  106. }
  107. int a = height(node.left);
  108. int b = height(node.right);
  109. if(a>b)
  110. i+=a;
  111. }
  112. return i;
  113. }
  114.  
  115.  
  116. public static int nodeNumber(Node node) {
  117. if (node == null) {
  118. return 0;
  119. }
  120. return 1 + nodeNumber(node.left) + nodeNumber(node.right);
  121. }
  122.  
  123.  
  124. public static int leavesNumber(Node node) {
  125. if (node == null)
  126. return 0;
  127.  
  128. if (node.left == null && node.right == null)
  129. return 1;
  130. else {
  131. return leavesNumber(node.left) + leavesNumber(node.right);
  132. }
  133. }
  134.  
  135.  
  136. public static int insideLeavesNumber(){
  137. return nodeNumber(root) - leavesNumber(root);
  138. }
  139.  
  140. static int x = 0;
  141. public static int outsideLeavesNumber(Node node) {
  142. if (node == null){
  143. x++;
  144. return 0;
  145. }
  146. if (node.left == null && node.right == null)
  147. x++;
  148. else {
  149. return outsideLeavesNumber(node.left) + outsideLeavesNumber(node.right);
  150. }
  151. return x;
  152. }
  153.  
  154.  
  155.  
  156. public static void printLevelOrder(List<Node> level) {
  157. List<Node> nextLevel = new LinkedList<Node>();
  158. for (Node node : level) {
  159. System.out.print(node.key + ", ");
  160. if (node.left != null) {
  161. nextLevel.add(node.left);
  162. }
  163. if (node.right != null) {
  164. nextLevel.add(node.right);
  165. }
  166. }
  167. if (!nextLevel.isEmpty()) {
  168. printLevelOrder(nextLevel);
  169. }
  170. }
  171.  
  172.  
  173. public static void printLevelOrder() {
  174. if (root != null) {
  175. printLevelOrder(Arrays.asList(root));
  176. }
  177. }
  178.  
  179.  
  180. public static Node successor(int key) throws TreeException {
  181. Node node = search(key);
  182. if (node.right != null) {
  183. node = node.right;
  184. while (node.left != null)
  185. node = node.left;
  186. return node;
  187. } else if (node.right == null && node != root && node != max(root)) {
  188. Node parent = node.parent;
  189. while (parent != root && parent.key < node.key)
  190. parent = parent.parent;
  191. return parent;
  192. } else
  193. System.out.println("Nie znaleziono nastepnika");
  194. return null;
  195. }
  196.  
  197.  
  198.  
  199. public static Node predecessor(int key) throws TreeException {
  200. Node node = search(key);
  201. if(node.left != null) {
  202. node = node.left;
  203. while(node.right != null)
  204. node = node.right;
  205. return node;
  206. }
  207. else if(node.left == null && node != root && node != min(root)) {
  208. Node parent = node.parent;
  209. while(parent != root && parent.key > node.key)
  210. parent = parent.parent;
  211. return parent;
  212. }
  213. else
  214. throw new TreeException("Not Found Predecessor");
  215. }
  216.  
  217. public static Node remove(int key) throws TreeException {
  218. Node node = search(key);
  219. Node parent = node.parent;
  220. Node tmp;
  221. if (node.left != null && node.right != null) {
  222. tmp = remove(successor(key).key);
  223. tmp.left = node.left;
  224. if (tmp.left != null)
  225. tmp.left.parent = tmp;
  226. tmp.right = node.right;
  227. if (tmp.right != null)
  228. tmp.right.parent = tmp;
  229. } else
  230. tmp = (node.left != null) ? node.left : node.right;
  231. if (tmp != null)
  232. node.parent = parent;
  233. if (parent == null)
  234. root = tmp;
  235. else if (parent.left == node)
  236. parent.left = tmp;
  237. else
  238. parent.right = tmp;
  239. return node;
  240. }
  241.  
  242.  
  243.  
  244. public static void main(String args[]) throws TreeException{
  245. insert(10);
  246. insert(5);
  247. insert(4);
  248. insert(8);
  249. insert(9);
  250. insert(15);
  251. insert(13);
  252.  
  253. System.out.print("InOrder:");
  254. inOrder(root);
  255. System.out.println();
  256. System.out.print("PreOrder:");
  257. preOrder(root);
  258. System.out.println();
  259. System.out.print("PostOrder:");
  260. postOrder(root);
  261. System.out.println();
  262. System.out.println("Result of searching for key: " + search(4).key);
  263.  
  264. System.out.println("Min. key = "+ min(root).key);
  265. System.out.println("Max. key = "+ max(root).key);
  266. System.out.println("Tree height = " + height(root));
  267. System.out.println("number of nodes = " + nodeNumber(root));
  268. System.out.println("number of leaves = " + leavesNumber(root));
  269. System.out.println("number of inside leaves = " + insideLeavesNumber());
  270. System.out.println("number of outside leaves = " + outsideLeavesNumber(root));
  271. System.out.print("Printing in level order: ");
  272. printLevelOrder();
  273. System.out.println();
  274. System.out.println("Successor of 9 = " +successor(9).key);
  275. System.out.println("Predecessor of 10 = " +predecessor(10).key);
  276. remove(8);
  277. System.out.print("InOrder:");
  278. inOrder(root);
  279. // System.out.println("Result of searching for key: " + search(8).key);
  280.  
  281.  
  282.  
  283. }
  284.  
  285. }
Advertisement
Add Comment
Please, Sign In to add comment