-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathBST.java
99 lines (83 loc) · 2.37 KB
/
BST.java
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
89
90
91
92
93
94
95
96
97
98
99
class Node {
int key;
Node left, right;
public Node(int item) {
key = item;
left = right = null;
}
}
class BinarySearchTree {
Node root;
// Constructor
public BinarySearchTree() {
root = null;
}
// Insert a new key
void insert(int key) {
root = insertRec(root, key);
}
// A recursive function to insert a new key
Node insertRec(Node root, int key) {
// If the tree is empty, return a new node
if (root == null) {
root = new Node(key);
return root;
}
// Otherwise, recur down the tree
if (key < root.key)
root.left = insertRec(root.left, key);
else if (key > root.key)
root.right = insertRec(root.right, key);
// return the unchanged node pointer
return root;
}
// This method mainly calls inorderRec()
void inorder() {
inorderRec(root);
}
// A utility function to do inorder traversal of BST
void inorderRec(Node root) {
if (root != null) {
inorderRec(root.left);
System.out.print(root.key + " ");
inorderRec(root.right);
}
}
// Search a key in BST
boolean search(int key) {
return searchRec(root, key);
}
// A recursive function to search a key in BST
boolean searchRec(Node root, int key) {
// Base Cases: root is null or key is present at root
if (root == null) {
return false;
}
if (root.key == key) {
return true;
}
// Key is greater than root's key
if (key > root.key) {
return searchRec(root.right, key);
}
// Key is smaller than root's key
return searchRec(root.left, key);
}
// Main method to test the implementation
public static void main(String[] args) {
BinarySearchTree bst = new BinarySearchTree();
bst.insert(50);
bst.insert(30);
bst.insert(20);
bst.insert(40);
bst.insert(70);
bst.insert(60);
bst.insert(80);
// Print in-order traversal of the BST
System.out.println("Inorder traversal:");
bst.inorder();
// Search for a key
int keyToSearch = 40;
System.out.println("\nSearching for " + keyToSearch + ": " + bst.search(keyToSearch));
}
}