Skip to content

Commit 7566e42

Browse files
committed
Add BST - Binary Search Tree
1 parent ba3409e commit 7566e42

3 files changed

Lines changed: 88 additions & 1 deletion

File tree

src/main/java/org/alda/Main.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,11 @@
1919
import org.alda.structure.stack.array.StackArray;
2020
import org.alda.structure.stack.linkedList.IStackLinkedList;
2121
import org.alda.structure.stack.linkedList.StackLinkedList;
22+
import org.alda.structure.tree.bst.BinarySearchTree;
2223

2324
public class Main {
2425
public static void main(String[] args) {
25-
testPriorityQueue();
26+
testBST();
2627
}
2728
public static void testSimpleLinkedList() {
2829
ISimpleLinkedList<Integer> intList = new SimpleLinkedList<Integer>();
@@ -206,4 +207,17 @@ public static void testPriorityQueue(){
206207
System.out.println(item2);
207208
System.out.println(item3);
208209
}
210+
211+
public static void testBST(){
212+
BinarySearchTree<Integer> bst = new BinarySearchTree<>();
213+
bst.insert(5);
214+
bst.insert(3);
215+
bst.insert(7);
216+
bst.insert(1);
217+
bst.insert(20);
218+
bst.insert(4);
219+
220+
System.out.println(bst.inorderTraversal());
221+
System.out.println(bst.search(4));
222+
}
209223
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package org.alda.structure.tree.bst;
2+
3+
import org.alda.common.Comparer;
4+
5+
import java.util.ArrayList;
6+
7+
public class BinarySearchTree<T extends Comparable<T>> {
8+
Node<T> root;
9+
10+
public BinarySearchTree() {
11+
root = null;
12+
}
13+
14+
public void insert(T key) {
15+
root = _insertRecursive(root, key);
16+
}
17+
18+
protected Node<T> _insertRecursive(Node<T> root, T key){
19+
if (root == null) {
20+
return new Node<>(key);
21+
}
22+
if (key.compareTo(root.key) < 0) {
23+
root.left = _insertRecursive(root.left, key);
24+
} else if (key.compareTo(root.key) > 0) {
25+
root.right = _insertRecursive(root.right, key);
26+
}
27+
return root;
28+
}
29+
30+
public Node<T> search(T key) {
31+
return _searchRecursive(root, key);
32+
}
33+
34+
protected Node<T> _searchRecursive(Node<T> root, T key){
35+
if (root == null || root.key.equals(key)) {
36+
return root;
37+
}
38+
if (key.compareTo(root.key) < 0) {
39+
return _searchRecursive(root.left, key);
40+
}
41+
return _searchRecursive(root.right, key);
42+
}
43+
44+
public ArrayList<T> inorderTraversal() {
45+
ArrayList<T> result = new ArrayList<>();
46+
_inorderTraversal(root, result);
47+
return result;
48+
}
49+
50+
protected void _inorderTraversal(Node<T> root, ArrayList<T> result) {
51+
if (root != null) {
52+
_inorderTraversal(root.left, result);
53+
result.add(root.key);
54+
_inorderTraversal(root.right, result);
55+
}
56+
}
57+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package org.alda.structure.tree.bst;
2+
3+
/**
4+
* Node for binary search tree
5+
* @param <T>
6+
*/
7+
public class Node<T> {
8+
T key;
9+
Node<T> left;
10+
Node<T> right;
11+
public Node(T key){
12+
this.key = key;
13+
left = null;
14+
right = null;
15+
}
16+
}

0 commit comments

Comments
 (0)