|
| 1 | +/* Author : Suraj Kumar |
| 2 | + Github : https://github.com/skmodi649 |
| 3 | + */ |
| 4 | + |
| 5 | +/* PROBLEM DESCRIPTION : |
| 6 | + There is a Binary Search Tree given, and we are supposed to find a random node in the given binary tree. |
| 7 | + */ |
| 8 | + |
| 9 | +/* ALGORITHM : |
| 10 | + Step 1: START |
| 11 | + Step 2: First create a binary tree using the steps mentioned in the first approach |
| 12 | + Step 3: Now use a method inOrder() that takes a node as input parameter to traverse through the |
| 13 | + binary tree in inorder fashion as also store the values in a ArrayList simultaneously. |
| 14 | + Step 4: Now define a method getrandom() that takes a node as input parameter, in this first call |
| 15 | + the inOrder() method to store the values in the arraylist, then find the size of the binary tree and now just generate a random number between 0 to n-1. |
| 16 | + Step 5: After generating the number display the value of the ArrayList at the generated index |
| 17 | + Step 6: STOP |
| 18 | + */ |
| 19 | + |
| 20 | + |
| 21 | +import java.util.ArrayList; |
| 22 | + |
| 23 | +// Using auxiliary array to find the random node in a given binary tree |
| 24 | +class Node { |
| 25 | + int item; |
| 26 | + Node left, right; |
| 27 | + |
| 28 | + public Node(int key) { |
| 29 | + item = key; |
| 30 | + left = right = null; |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +public class TreeRandomNode { |
| 35 | + |
| 36 | + // Using an arraylist to store the inorder traversal of the given binary tree |
| 37 | + static ArrayList<Integer> list = new ArrayList<>(); |
| 38 | + // root of Tree |
| 39 | + Node root; |
| 40 | + |
| 41 | + TreeRandomNode() { |
| 42 | + root = null; |
| 43 | + } |
| 44 | + |
| 45 | + // Now lets find the inorder traversal of the given binary tree |
| 46 | + static void inOrder(Node node) { |
| 47 | + if (node == null) |
| 48 | + return; |
| 49 | + |
| 50 | + // traverse the left child |
| 51 | + inOrder(node.left); |
| 52 | + |
| 53 | + list.add(node.item); |
| 54 | + // traverse the right child |
| 55 | + inOrder(node.right); |
| 56 | + } |
| 57 | + |
| 58 | + public void getrandom(Node val) |
| 59 | + { |
| 60 | + inOrder(val); |
| 61 | + // getting the count of node of the binary tree |
| 62 | + int n = list.size(); |
| 63 | + int min = 0; |
| 64 | + int max = n - 1; |
| 65 | + //Generate random int value from 0 to n-1 |
| 66 | + int b = (int)(Math.random()*(max-min+1)+min); |
| 67 | + // displaying the value at the generated index |
| 68 | + int random = list.get(b); |
| 69 | + System.out.println("Random Node : " + random); |
| 70 | + |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | + |
| 75 | +/* Explanation of the Approach : |
| 76 | + (a) Form the required binary tree |
| 77 | + (b) Now use the inOrder() method to get the nodes in inOrder fashion and also store them in the given arraylist 'list' |
| 78 | + (c) Using the getRandom() method generate a random number between 0 to n-1, then get the value at the generated random number |
| 79 | + from the arraylist using get() method and finally display the result. |
| 80 | + */ |
| 81 | + |
| 82 | + |
| 83 | +/* OUTPUT : |
| 84 | + First output : |
| 85 | + Random Node : 15 |
| 86 | + Second output : |
| 87 | + Random Node : 99 |
| 88 | + */ |
| 89 | + |
| 90 | +/* Time Complexity : O(n) |
| 91 | + Auxiliary Space Complexity : O(1) |
| 92 | + */ |
| 93 | + |
0 commit comments