-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestclass.java
More file actions
88 lines (74 loc) · 2.1 KB
/
Copy pathTestclass.java
File metadata and controls
88 lines (74 loc) · 2.1 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/*Create a Binary Search Tree from list A containing N elements. Insert elements in the same order as given. Print the pre-order traversal
of the subtree with root node data equal to Q (inclusive of Q), separating each element by a space.
Input:
First line contains a single integer N – number of elements.
Second line contains N space-separated integers.
Third line contains a single integer Q – the element whose subtree is to be printed in pre-order form.
Output:
Print K space-separated integers – where K is the number of elements in the subtree of Q (inclusive of Q)
Constraints:
SAMPLE INPUT
4
2 1 3 4
3
SAMPLE OUTPUT
3
4 */
import java.util.*;
class Node {
int data;
Node left, right;
public Node(int data) {
this.data = data;
this.left = this.right = null;
}
}
class BST {
Node root;
public void insert(int data) {
root = insertRec(root, data);
}
private Node insertRec(Node root, int data) {
if (root == null) {
return new Node(data);
}
if (data < root.data) {
root.left = insertRec(root.left, data);
} else {
root.right = insertRec(root.right, data);
}
return root;
}
public Node find(Node root, int data) {
if (root == null || root.data == data) {
return root;
}
if (data < root.data) {
return find(root.left, data);
}
return find(root.right, data);
}
public void preorder(Node root) {
if (root != null) {
System.out.print(root.data + " ");
preorder(root.left);
preorder(root.right);
}
}
}
public class Testclass{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
BST bst = new BST();
for (int i = 0; i < N; i++) {
bst.insert(sc.nextInt());
}
int Q = sc.nextInt();
Node subtreeRoot = bst.find(bst.root, Q);
if (subtreeRoot != null) {
bst.preorder(subtreeRoot);
}
sc.close();
}
}