Skip to content

Commit 56cb1d9

Browse files
authored
Updated BinarySearchTree.java with comments
1 parent dfd84f4 commit 56cb1d9

1 file changed

Lines changed: 24 additions & 18 deletions

File tree

Data Structure/Trees/Binary Trees/BinarySearchTree.java

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@
44
import java.util.*;
55
import java.io.*;
66

7+
// initializing node class
78
class Node {
89
int data;
910
Node left, right;
10-
Node(int x)
11+
Node(int x) // Node contructor
1112
{
1213
data = x;
1314
left = right = null;
@@ -17,27 +18,31 @@ class Node {
1718
public class BinaryTree {
1819

1920
static int count = 0;
20-
public static Node insert(Node root, int x)
21+
// insertion of element using recursion
22+
public static Node insert(Node root, int data)
2123
{
2224
if (root == null)
23-
return new Node(x);
24-
if (x < root.data)
25-
root.left = insert(root.left, x);
26-
else if (x > root.data)
27-
root.right = insert(root.right, x);
25+
return new Node(data);
26+
if (data < root.data) // if the data is less than node data go left
27+
root.left = insert(root.left, data);
28+
29+
else if (data > root.data) // if data is greater than node data go right
30+
root.right = insert(root.right, data);
31+
2832
return root;
2933
}
3034

3135
public static Node kthSmallest(Node root, int k)
3236
{
3337
if (root == null)
3438
return null;
35-
Node left = kthSmallest(root.left, k);
39+
Node left = kthSmallest(root.left, k); // at this line we go to leftmost node, so that we can start counting nth smallest from thatnode (leftmost)
3640

3741
if (left != null)
38-
return left;
39-
count++;
40-
if (count == k)
42+
return left;
43+
count++;
44+
45+
if (count == k) // if we find the element at k return k;
4146
return root;
4247

4348
return kthSmallest(root.right, k);
@@ -46,8 +51,8 @@ public static Node kthSmallest(Node root, int k)
4651
public static void printKthSmallest(Node root, int k)
4752
{
4853
count = 0;
54+
Node res = kthSmallest(root, k); // get the smallest element
4955

50-
Node res = kthSmallest(root, k);
5156
if (res == null)
5257
System.out.println("can't print as k is greater than nodes");
5358
else
@@ -56,15 +61,16 @@ public static void printKthSmallest(Node root, int k)
5661

5762
public static void main (String[] args) {
5863
Scanner take = new Scanner(System.in);
59-
Node root = null;
60-
System.out.print("enter number of elements in the list: ");
61-
int size = take.nextInt();
64+
Node root = null; // initialising root node
65+
66+
System.out.print("enter number of elements in the list: ");
67+
int size = take.nextInt();
6268

63-
while(size-- !=0)
64-
root = insert(root, take.nextInt());
69+
while(size-- !=0)
70+
root = insert(root, take.nextInt()); // calling insert method to insert the element in the tree
6571
System.out.print("enter k: ");
6672
int k = take.nextInt();
67-
printKthSmallest(root, k);
73+
printKthSmallest(root, k); //calling this function to find the kth smallest element
6874

6975

7076
}

0 commit comments

Comments
 (0)