Advertisement
Guest User

Untitled

a guest
Apr 22nd, 2019
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.25 KB | None | 0 0
  1. import java.util.*;
  2. import java.io.*;
  3.  
  4. public class BST{
  5. private class Node{
  6. int data;
  7. Node left, right;
  8.  
  9. Node(int data){
  10. this.data = data;
  11. left = right = null;
  12. }
  13. }
  14.  
  15. private Node root;
  16. private int sum = 0;
  17. private int p = 0;
  18.  
  19. private void insert(int data){
  20. if(root == null){
  21. root = new Node(data);
  22. return;
  23. }
  24. Node current = root;
  25. Node parent = null;
  26. while(current != null){
  27. parent = current;
  28. if(data < current.data)
  29. current = current.left;
  30. else
  31. current = current.right;
  32. }
  33. if(data < parent.data)
  34. parent.left = new Node(data);
  35. else
  36. parent.right = new Node(data);
  37. }
  38.  
  39. private void levelOrder(){
  40. Queue<Node> queue = new LinkedList<>();
  41. queue.add(root);
  42. while(!queue.isEmpty()){
  43. Node current = queue.poll();
  44. System.out.print(current.data+" ");
  45. if(current.left != null)
  46. queue.add(current.left);
  47. if(current.right != null)
  48. queue.add(current.right);
  49. }
  50. System.out.println();
  51. }
  52.  
  53. private void printLevelByLevel(){
  54. Queue<Node> queueOne = new LinkedList<>();
  55. Queue<Node> queueTwo = new LinkedList<>();
  56. queueOne.add(root);
  57. while(!queueOne.isEmpty() || !queueTwo.isEmpty()){
  58. while(!queueOne.isEmpty()){
  59. Node current = queueOne.poll();
  60. System.out.print(current.data+" ");
  61. if(current.left != null)
  62. queueTwo.add(current.left);
  63. if(current.right != null)
  64. queueTwo.add(current.right);
  65. }
  66. System.out.println();
  67. while(!queueTwo.isEmpty()){
  68. Node current = queueTwo.poll();
  69. System.out.print(current.data+" ");
  70. if(current.left != null)
  71. queueOne.add(current.left);
  72. if(current.right != null)
  73. queueOne.add(current.right);
  74. }
  75. System.out.println();
  76. }
  77. }
  78.  
  79. private void spiralOrder(){
  80. Stack<Node> stackOne = new Stack<>();
  81. Stack<Node> stackTwo = new Stack<>();
  82. stackOne.push(root);
  83. while(!stackOne.isEmpty() || !stackTwo.isEmpty()){
  84. while(!stackOne.isEmpty()){
  85. Node current = stackOne.pop();
  86. System.out.print(current.data+" ");
  87. if(current.left != null)
  88. stackTwo.push(current.left);
  89. if(current.right != null)
  90. stackTwo.push(current.right);
  91. }
  92. System.out.println();
  93. while(!stackTwo.isEmpty()){
  94. Node current = stackTwo.pop();
  95. System.out.print(current.data+" ");
  96. if(current.right != null)
  97. stackOne.push(current.right);
  98. if(current.left != null)
  99. stackOne.push(current.left);
  100. }
  101. System.out.println();
  102. }
  103. }
  104.  
  105. private List<List<Integer>> getVOT(Node root){
  106. List<List<Integer>> list = new ArrayList<>();
  107. Queue<Node> queue = new LinkedList<>();
  108. Map<Integer, List<Integer>> map = new HashMap<>();
  109. LinkedList<Integer> hd = new LinkedList<>();
  110. queue.add(root);
  111. hd.add(0);
  112. int min = 0;
  113. int max = 0;
  114. while(!queue.isEmpty()){
  115. Node current = queue.poll();
  116. int l = hd.poll();
  117. min = Math.min(min, l); max = Math.max(max, l);
  118. if(map.containsKey(l))
  119. map.get(l).add(current.data);
  120. else{
  121. List<Integer> temp = new ArrayList<>();
  122. temp.add(current.data);
  123. map.put(l, temp);
  124. }
  125. if(current.left != null){
  126. queue.add(current.left);
  127. hd.add(l - 1);
  128. }
  129. if(current.right != null){
  130. queue.add(current.right);
  131. hd.add(l + 1);
  132. }
  133. }
  134. for(int i=min; i <= max; i++)
  135. if(map.containsKey(i))
  136. list.add(map.get(i));
  137. return list;
  138. }
  139.  
  140. private List<List<Integer>> getLOT(Node root){
  141. List<List<Integer>> output = new ArrayList<>();
  142. Queue<Node> queue = new LinkedList<>();
  143. LinkedList<Integer> level = new LinkedList<>();
  144. Map<Integer, List<Integer>> map = new HashMap<>();
  145. queue.add(root);
  146. level.add(0);
  147. int max = 0;
  148. while(!queue.isEmpty()){
  149. Node current = queue.poll();
  150. int l = level.poll();
  151. max = Integer.max(l, max);
  152. if(map.containsKey(l))
  153. map.get(l).add(current.data);
  154. else{
  155. List<Integer> temp = new ArrayList<>();
  156. temp.add(current.data);
  157. map.put(l, temp);
  158. }
  159. if(current.left != null){
  160. queue.add(current.left);
  161. level.add(l + 1);
  162. }
  163. if(current.right != null){
  164. queue.add(current.right);
  165. level.add(l + 1);
  166. }
  167. }
  168. for(int i=0; i<=max; i++)
  169. if(map.containsKey(i))
  170. output.add(map.get(i));
  171. return output;
  172. }
  173.  
  174. private List<List<Integer>> getDOT(Node root){
  175. List<List<Integer>> output = new ArrayList<>();
  176. Queue<Node> queue = new LinkedList<>();
  177. LinkedList<Integer> dd = new LinkedList<>();
  178. Map<Integer, List<Integer>> map = new HashMap<>();
  179. int max = 0;
  180. queue.add(root);
  181. dd.add(0);
  182. while(!queue.isEmpty()){
  183. Node current = queue.poll();
  184. int d = dd.poll();
  185. max = Math.max(d, max);
  186. if(map.containsKey(d))
  187. map.get(d).add(current.data);
  188. else{
  189. List<Integer> temp = new ArrayList<>();
  190. temp.add(current.data);
  191. map.put(d, temp);
  192. }
  193. if(current.left != null){
  194. queue.add(current.left);
  195. dd.add(d + 1);
  196. }
  197. if(current.right != null){
  198. queue.add(current.right);
  199. dd.add(d);
  200. }
  201. }
  202. for(int i=0; i<=max; i++)
  203. if(map.containsKey(i))
  204. output.add(map.get(i));
  205. return output;
  206. }
  207.  
  208. private void printDiagonalOrderTraversal(){
  209. List<List<Integer>> output = getDOT(root);
  210. output.forEach(e->{
  211. e.forEach(t->System.out.print(t+" "));
  212. System.out.println();
  213. });
  214. }
  215.  
  216. private void printLevelOrderTraversal(){
  217. List<List<Integer>> output = getLOT(root);
  218. output.forEach(e->{
  219. e.forEach(t->System.out.print(t+" "));
  220. System.out.println();
  221. });
  222. }
  223.  
  224.  
  225. private void printVerticalOrderTraversal(){
  226. List<List<Integer>> output = getVOT(root);
  227. output.forEach(e->{
  228. e.forEach(t->System.out.print(t+" "));
  229. System.out.println();
  230. });
  231. }
  232.  
  233. private void printTopView(){
  234. List<List<Integer>> output = getVOT(root);
  235. output.forEach(e->System.out.print(e.get(0)+" "));
  236. System.out.println();
  237. }
  238.  
  239. private void printBottomView(){
  240. List<List<Integer>> output = getVOT(root);
  241. output.forEach(e->System.out.print(e.get(e.size() - 1)+" "));
  242. System.out.println();
  243. }
  244.  
  245. private void printLeftView(){
  246. List<List<Integer>> output = getLOT(root);
  247. output.forEach(e->System.out.print(e.get(0)+" "));
  248. System.out.println();
  249. }
  250.  
  251. private void printRightView(){
  252. List<List<Integer>> output = getLOT(root);
  253. output.forEach(e->System.out.print(e.get(e.size() - 1)+" "));
  254. System.out.println();
  255. }
  256.  
  257. private void greaterTree(Node root){
  258. if(root != null){
  259. greaterTree(root.right);
  260. sum += root.data;
  261. root.data = sum;
  262. greaterTree(root.left);
  263. }
  264. }
  265.  
  266. private void makeTreeGreater(){
  267. greaterTree(root);
  268. }
  269.  
  270. private Node lca(Node root, int a, int b){
  271. Node current = root;
  272. while(current != null){
  273. if(current.data > Math.max(a, b))
  274. current = current.left;
  275. if(current.data < Math.min(a, b))
  276. current = current.right;
  277. else
  278. break;
  279. }
  280. return current;
  281. }
  282.  
  283. private int getLCA(int a, int b){
  284. return lca(root, a, b).data;
  285. }
  286.  
  287. private boolean isLeaf(Node root){
  288. return root.left == null && root.right == null;
  289. }
  290.  
  291. private boolean getPath(Node root, int sum, List<Integer> output){
  292. if(root == null) return false;
  293. if(isLeaf(root)){
  294. if(root.data == sum){
  295. output.add(root.data);
  296. return true;
  297. }
  298. else return false;
  299. }
  300. if(getPath(root.left, sum - root.data, output)){
  301. output.add(root.data);
  302. return true;
  303. }
  304. if(getPath(root.right, sum - root.data, output)){
  305. output.add(root.data);
  306. return true;
  307. }
  308. return false;
  309. }
  310.  
  311. private List<Integer> getSumPath(int sum){
  312. List<Integer> output = new ArrayList<>();
  313. if(getPath(root, sum, output)){
  314. Collections.reverse(output);
  315. return output;
  316. }
  317. else
  318. return new ArrayList<>();
  319. }
  320.  
  321. private Node getMax(Node root){
  322. Node current = root;
  323. while(current.right != null)
  324. current = current.right;
  325. return current;
  326. }
  327.  
  328. private Node delete(Node root, int data){
  329. if(root == null) return root;
  330. if(data < root.data) root.left = delete(root.left, data);
  331. else if(data > root.data) root.right = delete(root.right, data);
  332. else{
  333. if(isLeaf(root)) root = null;
  334. else if(root.left == null) root = root.right;
  335. else if(root.right == null) root = root.left;
  336. else{
  337. Node temp = getMax(root.left);
  338. root.data = temp.data;
  339. root.left = delete(root.left, temp.data);
  340. }
  341. }
  342. return root;
  343. }
  344.  
  345. private void delete(int data){
  346. root = delete(root, data);
  347. }
  348.  
  349. private Node construct(int[] preorder, int data, int min, int max){
  350. int value = preorder[p];
  351. if(value > min && value < max){
  352. Node root = new Node(data);
  353. p += 1;
  354. if(p < preorder.length){
  355. root.left = construct(preorder, preorder[p], min, data);
  356. root.right = construct(preorder, preorder[p], data, max);
  357. }
  358. return root;
  359. }
  360. else
  361. return root;
  362. }
  363.  
  364. private void construct(int[] preorder){
  365. root = construct(preorder, preorder[0], Integer.MIN_VALUE, Integer.MAX_VALUE);
  366. }
  367.  
  368. public static void main(String[] args){
  369. int[] a = {17, 9, 25, 5, 11, 20, 27, 4, 6, 10, 12, 19, 21, 26, 28};
  370. int[] b = {8, 3, 10, 1, 6, 14, 4, 7, 13};
  371. BST bst = new BST();
  372. int[] preorder = {10, 5, 1, 7, 40, 50};
  373. bst.construct(preorder);
  374. bst.printLevelByLevel();
  375. }
  376. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement