Skip to content

Commit 8b8e98e

Browse files
Ankush263yanglbme
andauthored
Fix TreeRandomNode Algorithm (TheAlgorithms#3174)
Co-authored-by: Yang Libin <contact@yanglibin.info>
1 parent b0f2180 commit 8b8e98e

File tree

1 file changed

+17
-15
lines changed

1 file changed

+17
-15
lines changed

src/main/java/com/thealgorithms/datastructures/trees/TreeRandomNode.java

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
package com.thealgorithms.datastructures.trees;
2+
3+
14
/* Author : Suraj Kumar
25
Github : https://github.com/skmodi649
36
*/
@@ -11,7 +14,7 @@
1114
Step 2: First create a binary tree using the steps mentioned in the first approach
1215
Step 3: Now use a method inOrder() that takes a node as input parameter to traverse through the
1316
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
17+
Step 4: Now define a method getRandom() that takes a node as input parameter, in this first call
1518
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.
1619
Step 5: After generating the number display the value of the ArrayList at the generated index
1720
Step 6: STOP
@@ -21,17 +24,17 @@ the inOrder() method to store the values in the arraylist, then find the size of
2124
import java.util.ArrayList;
2225

2326
// 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-
}
3327

3428
public class TreeRandomNode {
29+
private class Node {
30+
int item;
31+
Node left, right;
32+
33+
public Node(int key) {
34+
item = key;
35+
left = right = null;
36+
}
37+
}
3538

3639
// Using an arraylist to store the inorder traversal of the given binary tree
3740
static ArrayList<Integer> list = new ArrayList<>();
@@ -44,8 +47,9 @@ public class TreeRandomNode {
4447

4548
// Now lets find the inorder traversal of the given binary tree
4649
static void inOrder(Node node) {
47-
if (node == null)
50+
if (node == null) {
4851
return;
52+
}
4953

5054
// traverse the left child
5155
inOrder(node.left);
@@ -55,15 +59,14 @@ static void inOrder(Node node) {
5559
inOrder(node.right);
5660
}
5761

58-
public void getrandom(Node val)
59-
{
62+
public void getRandom(Node val) {
6063
inOrder(val);
6164
// getting the count of node of the binary tree
6265
int n = list.size();
6366
int min = 0;
6467
int max = n - 1;
6568
//Generate random int value from 0 to n-1
66-
int b = (int)(Math.random()*(max-min+1)+min);
69+
int b = (int) (Math.random() * (max - min + 1) + min);
6770
// displaying the value at the generated index
6871
int random = list.get(b);
6972
System.out.println("Random Node : " + random);
@@ -90,4 +93,3 @@ from the arraylist using get() method and finally display the result.
9093
/* Time Complexity : O(n)
9194
Auxiliary Space Complexity : O(1)
9295
*/
93-

0 commit comments

Comments
 (0)