Add select random tree node algorithm (#2906)
Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Co-authored-by: Andrii Siriak <siryaka@gmail.com>
This commit is contained in:
parent
857c4aafb2
commit
e4fa83bd29
@ -133,6 +133,7 @@
|
||||
* [PrintTopViewofTree](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/trees/PrintTopViewofTree.java)
|
||||
* [RedBlackBST](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/trees/RedBlackBST.java)
|
||||
* [SegmentTree](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/trees/SegmentTree.java)
|
||||
* [TreeRandomNode](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/trees/TreeRandomNode.java)
|
||||
* [TreeTraversal](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/trees/TreeTraversal.java)
|
||||
* [TrieImp](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/trees/TrieImp.java)
|
||||
* [ValidBSTOrNot](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/trees/ValidBSTOrNot.java)
|
||||
@ -369,6 +370,7 @@
|
||||
* [GnomeSort](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/sorts/GnomeSort.java)
|
||||
* [HeapSort](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/sorts/HeapSort.java)
|
||||
* [InsertionSort](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/sorts/InsertionSort.java)
|
||||
* [LinkList Sort](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/sorts/LinkList_Sort.java)
|
||||
* [MergeSort](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/sorts/MergeSort.java)
|
||||
* [MergeSortNoExtraSpace](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/sorts/MergeSortNoExtraSpace.java)
|
||||
* [MergeSortRecursive](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/sorts/MergeSortRecursive.java)
|
||||
@ -404,14 +406,12 @@
|
||||
* [Upper](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/strings/Upper.java)
|
||||
* [WordLadder](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/strings/WordLadder.java)
|
||||
* test
|
||||
* java
|
||||
* com
|
||||
* thealgorithms
|
||||
* maths
|
||||
* [KaprekarNumbersTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/KaprekarNumbersTest.java)
|
||||
* [PascalTriangleTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/PascalTriangleTest.java)
|
||||
* [PronicNumberTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/PronicNumberTest.java)
|
||||
* others
|
||||
* [ArrayLeftRotationTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/others/ArrayLeftRotationTest.java)
|
||||
* [LinkList Sort test](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/others/LinkList_Sort_test.java)
|
||||
* searches
|
||||
* [QuickSelectTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/QuickSelectTest.java)
|
||||
|
@ -0,0 +1,93 @@
|
||||
/* Author : Suraj Kumar
|
||||
Github : https://github.com/skmodi649
|
||||
*/
|
||||
|
||||
/* PROBLEM DESCRIPTION :
|
||||
There is a Binary Search Tree given, and we are supposed to find a random node in the given binary tree.
|
||||
*/
|
||||
|
||||
/* ALGORITHM :
|
||||
Step 1: START
|
||||
Step 2: First create a binary tree using the steps mentioned in the first approach
|
||||
Step 3: Now use a method inOrder() that takes a node as input parameter to traverse through the
|
||||
binary tree in inorder fashion as also store the values in a ArrayList simultaneously.
|
||||
Step 4: Now define a method getrandom() that takes a node as input parameter, in this first call
|
||||
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.
|
||||
Step 5: After generating the number display the value of the ArrayList at the generated index
|
||||
Step 6: STOP
|
||||
*/
|
||||
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
// Using auxiliary array to find the random node in a given binary tree
|
||||
class Node {
|
||||
int item;
|
||||
Node left, right;
|
||||
|
||||
public Node(int key) {
|
||||
item = key;
|
||||
left = right = null;
|
||||
}
|
||||
}
|
||||
|
||||
public class TreeRandomNode {
|
||||
|
||||
// Using an arraylist to store the inorder traversal of the given binary tree
|
||||
static ArrayList<Integer> list = new ArrayList<>();
|
||||
// root of Tree
|
||||
Node root;
|
||||
|
||||
TreeRandomNode() {
|
||||
root = null;
|
||||
}
|
||||
|
||||
// Now lets find the inorder traversal of the given binary tree
|
||||
static void inOrder(Node node) {
|
||||
if (node == null)
|
||||
return;
|
||||
|
||||
// traverse the left child
|
||||
inOrder(node.left);
|
||||
|
||||
list.add(node.item);
|
||||
// traverse the right child
|
||||
inOrder(node.right);
|
||||
}
|
||||
|
||||
public void getrandom(Node val)
|
||||
{
|
||||
inOrder(val);
|
||||
// getting the count of node of the binary tree
|
||||
int n = list.size();
|
||||
int min = 0;
|
||||
int max = n - 1;
|
||||
//Generate random int value from 0 to n-1
|
||||
int b = (int)(Math.random()*(max-min+1)+min);
|
||||
// displaying the value at the generated index
|
||||
int random = list.get(b);
|
||||
System.out.println("Random Node : " + random);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/* Explanation of the Approach :
|
||||
(a) Form the required binary tree
|
||||
(b) Now use the inOrder() method to get the nodes in inOrder fashion and also store them in the given arraylist 'list'
|
||||
(c) Using the getRandom() method generate a random number between 0 to n-1, then get the value at the generated random number
|
||||
from the arraylist using get() method and finally display the result.
|
||||
*/
|
||||
|
||||
|
||||
/* OUTPUT :
|
||||
First output :
|
||||
Random Node : 15
|
||||
Second output :
|
||||
Random Node : 99
|
||||
*/
|
||||
|
||||
/* Time Complexity : O(n)
|
||||
Auxiliary Space Complexity : O(1)
|
||||
*/
|
||||
|
Loading…
Reference in New Issue
Block a user