Skip to content

Commit 47e3b3d

Browse files
committed
BST
1 parent ff41742 commit 47e3b3d

File tree

3 files changed

+289
-5
lines changed

3 files changed

+289
-5
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ This repository contains various data structures and provide insights about them
3434
**5. Trees**
3535
- [ ] Introduction
3636
- [X] [Binary Tree](../master/src/com/deepak/data/structures/Tree/BinaryTree.java)
37-
- [ ] Binary Search Tree
37+
- [X] Binary Search Tree(../master/src/com/deepak/data/structures/Tree/BinarySearchTree.java)
3838
- [ ] AVL Tree
3939
- [ ] Splay Tree
4040
- [ ] B Tree
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,276 @@
1+
package com.deepak.data.structure.Tree;
2+
3+
import java.util.LinkedList;
4+
import java.util.Queue;
5+
6+
/**
7+
* It is same as binary tree i.e contains at max 2 nodes
8+
* Left node contains value <= root and right node contains
9+
* value > root
10+
*
11+
* @author Deepak
12+
*/
13+
public class BinarySearchTree {
14+
15+
/* We will maintain root and size of the tree.
16+
* Size here means number of nodes in the tree */
17+
private Node root;
18+
19+
/**
20+
* Add Node to the tree
21+
*/
22+
public void addNode(int value) {
23+
if (root == null) {
24+
root = new Node(value);
25+
} else {
26+
addNode(root, value);
27+
}
28+
}
29+
30+
private Node addNode(Node root, int value) {
31+
if (root == null) {
32+
return null;
33+
}
34+
if (value <= root.value) { /* We will insert left */
35+
if (root.left != null) {
36+
addNode(root.left, value);
37+
} else {
38+
root.left = new Node(value);
39+
}
40+
} else { /* We will insert right */
41+
if (root.right != null) {
42+
addNode(root.right, value);
43+
} else {
44+
root.right = new Node(value);
45+
}
46+
}
47+
return root;
48+
}
49+
50+
public int size() {
51+
return size(root);
52+
}
53+
54+
private int size(Node root) {
55+
if (root == null) {
56+
return 0;
57+
}
58+
return (size(root.left)) + 1 + (size(root.right));
59+
}
60+
61+
public boolean isEmpty() {
62+
return size() == 0;
63+
}
64+
65+
public Node getRoot() {
66+
if (isEmpty()) {
67+
return null;
68+
}
69+
return root;
70+
}
71+
72+
public boolean isRoot(Node node) {
73+
return node == root;
74+
}
75+
76+
public Node findParent(Node node) {
77+
return findParent(node.value, root, null);
78+
}
79+
80+
private Node findParent(int value, Node root, Node parent) {
81+
if (root == null) {
82+
return null;
83+
}
84+
if (value != root.value) {
85+
parent = findParent(value, root.left, root);
86+
if (parent == null) {
87+
parent = findParent(value, root.right, root);
88+
}
89+
}
90+
return parent;
91+
}
92+
93+
public boolean hasParent(Node node) {
94+
return findParent(node) != null;
95+
}
96+
97+
public boolean hasLeftNode(Node node) {
98+
return node.left != null;
99+
}
100+
101+
public boolean hasRightNode(Node node) {
102+
return node.right != null;
103+
}
104+
105+
public Node findLeft(Node node) {
106+
if (hasLeftNode(node)) {
107+
return node.left;
108+
}
109+
return null;
110+
}
111+
112+
public Node findRight(Node node) {
113+
if (hasRightNode(node)) {
114+
return node.right;
115+
}
116+
return null;
117+
}
118+
119+
public boolean isLeafNode(Node node) {
120+
return !hasLeftNode(node) && !hasRightNode(node);
121+
}
122+
123+
public static int getDepth(Node node) {
124+
if (node == null) {
125+
return 0;
126+
}
127+
int left = getDepth(node.left);
128+
int right = getDepth(node.right);
129+
return left > right ? left + 1 : right + 1;
130+
}
131+
132+
public boolean contains(int value) {
133+
return search(value) != null;
134+
}
135+
136+
public Node search(int value) {
137+
Node node = root;
138+
while (node != null && node.value != value) {
139+
if (value <= node.value) {
140+
node = node.left;
141+
} else {
142+
node = node.right;
143+
}
144+
}
145+
return node;
146+
}
147+
148+
public Node delete(int value) {
149+
return delete(root, value);
150+
}
151+
152+
/* There are 3 cases here,
153+
* 1. Node to be removed has no child
154+
* 2. Node to be removed has one child
155+
* 3. Node to be removed has two child */
156+
private Node delete(Node root, int value) {
157+
/* Base case, when tree is empty */
158+
if (root == null) {
159+
return root;
160+
}
161+
/* Now, go down the tree */
162+
if (value < root.value) {
163+
root.left = delete(root.left, value);
164+
} else if (value > root.value) {
165+
root.right = delete(root.right, value);
166+
} else { /* If key is same as the root key, this is the node to be deleted */
167+
/* Node with only one child or no child */
168+
if (root.left == null) {
169+
return root.right;
170+
} else if (root.right == null) {
171+
return root.left;
172+
}
173+
int minv = root.value;
174+
while (root.left != null)
175+
{
176+
minv = root.left.value;
177+
root = root.left;
178+
}
179+
root.value = minv;
180+
root.right = delete(root.right, root.value);
181+
}
182+
return root;
183+
}
184+
185+
public int getMinimum() {
186+
Node node = root;
187+
while (node.left != null) {
188+
node = node.left;
189+
}
190+
return node.value;
191+
}
192+
193+
public int getMaximum() {
194+
Node node = root;
195+
while (node.right != null) {
196+
node = node.right;
197+
}
198+
return node.value;
199+
}
200+
201+
/* All of these are DFS */
202+
/* Left -> Root -> Right */
203+
public void traverseInOrder(Node node) {
204+
if (node == null) {
205+
return;
206+
}
207+
traverseInOrder(node.left);
208+
System.out.println(node.value + " ");
209+
traverseInOrder(node.right);
210+
}
211+
212+
/* Root -> Left -> Right */
213+
public void traversePreOrder(Node node) {
214+
if (node == null) {
215+
return;
216+
}
217+
System.out.println(node.value + " ");
218+
traversePreOrder(node.left);
219+
traversePreOrder(node.right);
220+
}
221+
222+
/* Left -> Root -> Right */
223+
public void traversePostOrder(Node node) {
224+
if (node == null) {
225+
return;
226+
}
227+
traversePostOrder(node.left);
228+
System.out.println(node.value + " ");
229+
traversePostOrder(node.right);
230+
}
231+
232+
/* This is BFS */
233+
/* Level by Level */
234+
public void traverseLevelOrder(Node root) {
235+
Queue<Node> firstQ = new LinkedList<>();
236+
firstQ.add(root);
237+
238+
Queue<Queue<Node>> mainQ = new LinkedList<>();
239+
mainQ.add(firstQ);
240+
241+
while (!mainQ.isEmpty()) {
242+
Queue<Node> levelQ = mainQ.remove();
243+
Queue<Node> nextLevelQ = new LinkedList<>();
244+
for (Node x : levelQ) {
245+
System.out.print(x.value + " ");
246+
if (x.left != null) nextLevelQ.add(x.left);
247+
if (x.right != null) nextLevelQ.add(x.right);
248+
}
249+
if (!nextLevelQ.isEmpty()) mainQ.add(nextLevelQ);
250+
System.out.println();
251+
}
252+
}
253+
254+
/**
255+
* Node class for BST
256+
*
257+
* @author Deepak
258+
*/
259+
public class Node {
260+
261+
private Node left;
262+
private Node right;
263+
private int value;
264+
265+
public Node(int value) {
266+
this.value = value;
267+
}
268+
269+
@Override
270+
public String toString() {
271+
return "[Node + [Value = " + value + "]";
272+
}
273+
274+
}
275+
276+
}

src/com/deepak/data/structure/Tree/BinaryTree.java

+12-4
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,26 @@ public static void main(String[] args) {
1414
System.out.println("Printing the Tree");
1515
printByLevel(root);
1616
System.out.println();
17-
tree.addNode(7);
18-
tree.addNode(12);
17+
tree.addNode(17);
1918
tree.addNode(13);
20-
tree.addNode(8);
21-
tree.addNode(15);
2219
tree.addNode(9);
20+
tree.addNode(4);
21+
tree.addNode(16);
22+
tree.addNode(3);
23+
tree.addNode(8);
2324
System.out.println("Size of tree after insertion => " + tree.size());
2425
System.out.println("Printing the Tree");
2526
System.out.println();
2627
printByLevel(root);
2728
System.out.println();
2829
System.out.println(getDepth(root));
30+
System.out.println();
31+
System.out.println("Inorder");
32+
printInOrder(root);
33+
System.out.println("\nPreorder");
34+
printPreorder(root);
35+
System.out.println("\nPostorder");
36+
printPostorder(root);
2937
}
3038

3139
static Node root;

0 commit comments

Comments
 (0)