forked from shweta-laha/Hacktoberfest
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinarySearchTree.java
More file actions
71 lines (58 loc) · 1.47 KB
/
Copy pathBinarySearchTree.java
File metadata and controls
71 lines (58 loc) · 1.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// Question:
// Write a Java program to identify the kth smallest element in the binary search tree where k =8.
import java.util.*;
import java.io.*;
class Node {
int data;
Node left, right;
Node(int x)
{
data = x;
left = right = null;
}
}
public class BinaryTree {
static int count = 0;
public static Node insert(Node root, int x)
{
if (root == null)
return new Node(x);
if (x < root.data)
root.left = insert(root.left, x);
else if (x > root.data)
root.right = insert(root.right, x);
return root;
}
public static Node kthSmallest(Node root, int k)
{
if (root == null)
return null;
Node left = kthSmallest(root.left, k);
if (left != null)
return left;
count++;
if (count == k)
return root;
return kthSmallest(root.right, k);
}
public static void printKthSmallest(Node root, int k)
{
count = 0;
Node res = kthSmallest(root, k);
if (res == null)
System.out.println("can't print as k is greater than nodes");
else
System.out.println(k+"-th Smallest Element is " + res.data);
}
public static void main (String[] args) {
Scanner take = new Scanner(System.in);
Node root = null;
System.out.print("enter number of elements in the list: ");
int size = take.nextInt();
while(size-- !=0)
root = insert(root, take.nextInt());
System.out.print("enter k: ");
int k = take.nextInt();
printKthSmallest(root, k);
}
}