Skip to content

Commit c7ccfd6

Browse files
Implemented BST with insert and traversal methods
1 parent bb6385e commit c7ccfd6

File tree

2 files changed

+496
-0
lines changed

2 files changed

+496
-0
lines changed
Lines changed: 278 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,278 @@
1+
package com.thealgorithms.tree;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
import java.util.NoSuchElementException;
6+
7+
public class BST {
8+
9+
/**
10+
* Node class represents a node in the BST.
11+
* 0->0->0
12+
* 0=Nodes\
13+
* each node contains divided into 3 section
14+
* Key-> Actual value,
15+
* left-> store the address of left tree,
16+
* Right-> store the address of right tree
17+
*/
18+
private static class Node {
19+
int key;
20+
Node left, right;
21+
22+
Node(int key) {
23+
this.key = key;
24+
left = right = null;
25+
}
26+
}
27+
28+
// Root of the BST
29+
// the first of Node tree
30+
//0->
31+
private Node root;
32+
33+
/** Create an empty BST. */
34+
public BST() {
35+
root = null;
36+
}
37+
38+
/* ===========================
39+
* INSERT
40+
* =========================== */
41+
42+
/**
43+
* Insert a value into the BST. Duplicate values are ignored (no-op).
44+
*
45+
* @param value value to insert
46+
*/
47+
public void insert(int value) {
48+
root = insertRecursive(root, value);
49+
}
50+
51+
// Helper recursive method for insertion.
52+
private Node insertRecursive(Node node, int value) {
53+
// If we reached a null position, create and return a new node.
54+
if (node == null) {
55+
return new Node(value);
56+
}
57+
58+
// If value is less, go left; if greater, go right; if equal, do nothing.
59+
if (value < node.key) {
60+
node.left = insertRecursive(node.left, value);
61+
} else if (value > node.key) {
62+
node.right = insertRecursive(node.right, value);
63+
} // else duplicate -> ignore
64+
65+
return node; // return current (possibly updated) subtree root
66+
}
67+
68+
/* ===========================
69+
* SEARCH
70+
* =========================== */
71+
72+
/**
73+
* Search the BST for a value.
74+
*
75+
* @param value value to search
76+
* @return true if value exists in the BST, false otherwise
77+
*/
78+
public boolean search(int value) {
79+
return searchRecursive(root, value);
80+
}
81+
82+
private boolean searchRecursive(Node node, int value) {
83+
if (node == null) {
84+
return false; // reached leaf, not found
85+
}
86+
if (value == node.key) {
87+
return true; // found exact match
88+
} else if (value < node.key) {
89+
return searchRecursive(node.left, value); // search left subtree
90+
} else {
91+
return searchRecursive(node.right, value); // search right subtree
92+
}
93+
}
94+
95+
/* ===========================
96+
* DELETE
97+
* =========================== */
98+
99+
/**
100+
* Delete a value from the BST. If the value is not present, tree remains unchanged.
101+
*
102+
* @param value value to delete
103+
*/
104+
public void delete(int value) {
105+
root = deleteRecursive(root, value);
106+
}
107+
108+
// Helper for deletion, handles three cases:
109+
// 1) node is a leaf -> just remove
110+
// 2) node has one child -> replace node with child
111+
// 3) node has two children -> replace node with successor (smallest in right subtree)
112+
private Node deleteRecursive(Node node, int value) {
113+
if (node == null) {
114+
return null; // value not found
115+
}
116+
117+
if (value < node.key) {
118+
// target is in left subtree
119+
node.left = deleteRecursive(node.left, value);
120+
} else if (value > node.key) {
121+
// target is in right subtree
122+
node.right = deleteRecursive(node.right, value);
123+
} else {
124+
// node.key == value -> delete this node
125+
// Case 1 & 2: node has 0 or 1 child
126+
if (node.left == null) {
127+
// return right child (could be null) to replace node
128+
return node.right;
129+
} else if (node.right == null) {
130+
// return left child to replace node
131+
return node.left;
132+
}
133+
134+
// Case 3: node has two children
135+
// Find the inorder successor (smallest in the right subtree)
136+
Node successor = findMinNode(node.right);
137+
// Copy successor's value to this node
138+
node.key = successor.key;
139+
// Delete the successor node from right subtree
140+
node.right = deleteRecursive(node.right, successor.key);
141+
}
142+
143+
return node;
144+
}
145+
146+
/**
147+
* Find the minimum key in the BST.
148+
*
149+
* @return the smallest integer key in the tree
150+
* @throws NoSuchElementException if the tree is empty
151+
*/
152+
public int findMin() {
153+
if (root == null) {
154+
throw new NoSuchElementException("BST is empty");
155+
}
156+
return findMinNode(root).key;
157+
}
158+
159+
// Helper to find node with minimum key in a subtree (leftmost node)
160+
private Node findMinNode(Node node) {
161+
Node current = node;
162+
// go as far left as possible
163+
while (current.left != null) {
164+
current = current.left;
165+
}
166+
return current;
167+
}
168+
169+
/**
170+
* Find the maximum key in the BST.
171+
*
172+
* the largest integer key in the tree
173+
* NoSuchElementException if the tree is empty
174+
*/
175+
public int findMax() {
176+
if (root == null) {
177+
throw new NoSuchElementException("BST is empty");
178+
}
179+
Node current = root;
180+
// go as far right as possible
181+
while (current.right != null) {
182+
current = current.right;
183+
}
184+
return current.key;
185+
}
186+
187+
188+
/**
189+
* Print inorder traversal (Left, Node, Right).
190+
* InOrder -> Left, key, Right
191+
*/
192+
public void printInorder() {
193+
System.out.print("Inorder: ");
194+
printInorderRecursive(root);
195+
System.out.println();
196+
}
197+
198+
private void printInorderRecursive(Node node) {
199+
if (node == null) return;
200+
printInorderRecursive(node.left); // left
201+
System.out.print(node.key + " "); // node
202+
printInorderRecursive(node.right); // right
203+
}
204+
205+
/**
206+
* Print preorder traversal (Node, Left, Right).
207+
* PreOrder -> key,Left, Right
208+
*/
209+
public void printPreorder() {
210+
System.out.print("Preorder: ");
211+
printPreorderRecursive(root);
212+
System.out.println();
213+
}
214+
215+
private void printPreorderRecursive(Node node) {
216+
if (node == null) return;
217+
System.out.print(node.key + " "); // node
218+
printPreorderRecursive(node.left); // left
219+
printPreorderRecursive(node.right); // right
220+
}
221+
222+
/**
223+
* Print postorder traversal (Left, Right, Node).
224+
*PreOrder -> key,Left, Right
225+
*/
226+
public void printPostorder() {
227+
System.out.print("Postorder: ");
228+
printPostorderRecursive(root);
229+
System.out.println();
230+
}
231+
232+
private void printPostorderRecursive(Node node) {
233+
if (node == null) return;
234+
printPostorderRecursive(node.left); // left
235+
printPostorderRecursive(node.right); // right
236+
System.out.print(node.key + " "); // node
237+
}
238+
239+
public List<Integer> inorderList() {
240+
List<Integer> result = new ArrayList<>();
241+
inorderToList(root, result);
242+
return result;
243+
}
244+
245+
private void inorderToList(Node node, List<Integer> out) {
246+
if (node == null) return;
247+
inorderToList(node.left, out);
248+
out.add(node.key);
249+
inorderToList(node.right, out);
250+
}
251+
252+
253+
public List<Integer> preorderList() {
254+
List<Integer> result = new ArrayList<>();
255+
preorderToList(root, result);
256+
return result;
257+
}
258+
259+
private void preorderToList(Node node, List<Integer> out) {
260+
if (node == null) return;
261+
out.add(node.key);
262+
preorderToList(node.left, out);
263+
preorderToList(node.right, out);
264+
}
265+
266+
public List<Integer> postorderList() {
267+
List<Integer> result = new ArrayList<>();
268+
postorderToList(root, result);
269+
return result;
270+
}
271+
272+
private void postorderToList(Node node, List<Integer> out) {
273+
if (node == null) return;
274+
postorderToList(node.left, out);
275+
postorderToList(node.right, out);
276+
out.add(node.key);
277+
}
278+
}

0 commit comments

Comments
 (0)