Fix TreeRandomNode Algorithm (#3174)
Co-authored-by: Yang Libin <contact@yanglibin.info>
This commit is contained in:
parent
b0f21803d1
commit
8b8e98e89a
@ -1,3 +1,6 @@
|
|||||||
|
package com.thealgorithms.datastructures.trees;
|
||||||
|
|
||||||
|
|
||||||
/* Author : Suraj Kumar
|
/* Author : Suraj Kumar
|
||||||
Github : https://github.com/skmodi649
|
Github : https://github.com/skmodi649
|
||||||
*/
|
*/
|
||||||
@ -11,7 +14,7 @@
|
|||||||
Step 2: First create a binary tree using the steps mentioned in the first approach
|
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
|
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.
|
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
|
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.
|
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 5: After generating the number display the value of the ArrayList at the generated index
|
||||||
Step 6: STOP
|
Step 6: STOP
|
||||||
@ -21,7 +24,9 @@
|
|||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
|
||||||
// Using auxiliary array to find the random node in a given binary tree
|
// Using auxiliary array to find the random node in a given binary tree
|
||||||
class Node {
|
|
||||||
|
public class TreeRandomNode {
|
||||||
|
private class Node {
|
||||||
int item;
|
int item;
|
||||||
Node left, right;
|
Node left, right;
|
||||||
|
|
||||||
@ -31,8 +36,6 @@ class Node {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public class TreeRandomNode {
|
|
||||||
|
|
||||||
// Using an arraylist to store the inorder traversal of the given binary tree
|
// Using an arraylist to store the inorder traversal of the given binary tree
|
||||||
static ArrayList<Integer> list = new ArrayList<>();
|
static ArrayList<Integer> list = new ArrayList<>();
|
||||||
// root of Tree
|
// root of Tree
|
||||||
@ -44,8 +47,9 @@ public class TreeRandomNode {
|
|||||||
|
|
||||||
// Now lets find the inorder traversal of the given binary tree
|
// Now lets find the inorder traversal of the given binary tree
|
||||||
static void inOrder(Node node) {
|
static void inOrder(Node node) {
|
||||||
if (node == null)
|
if (node == null) {
|
||||||
return;
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
// traverse the left child
|
// traverse the left child
|
||||||
inOrder(node.left);
|
inOrder(node.left);
|
||||||
@ -55,8 +59,7 @@ public class TreeRandomNode {
|
|||||||
inOrder(node.right);
|
inOrder(node.right);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void getrandom(Node val)
|
public void getRandom(Node val) {
|
||||||
{
|
|
||||||
inOrder(val);
|
inOrder(val);
|
||||||
// getting the count of node of the binary tree
|
// getting the count of node of the binary tree
|
||||||
int n = list.size();
|
int n = list.size();
|
||||||
@ -90,4 +93,3 @@ public class TreeRandomNode {
|
|||||||
/* Time Complexity : O(n)
|
/* Time Complexity : O(n)
|
||||||
Auxiliary Space Complexity : O(1)
|
Auxiliary Space Complexity : O(1)
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user