Skip to content

Commit

Permalink
Added Binary search tree
Browse files Browse the repository at this point in the history
  • Loading branch information
NMKrastev committed Aug 17, 2022
1 parent 7d7aa85 commit 1035b26
Show file tree
Hide file tree
Showing 9 changed files with 165 additions and 1 deletion.
1 change: 1 addition & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion 85-BreadthFirstSearch/85-BreadthFirstSearch.iml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager">
<output url="file://$MODULE_DIR$/out/production/" />
<output url="file://$MODULE_DIR$/out/production" />
<output-test url="file://$MODULE_DIR$/../out/test/85-BreadthFirstSearch" />
<exclude-output />
<content url="file://$MODULE_DIR$">
Expand Down
13 changes: 13 additions & 0 deletions 86-BinarySearchTree/86-BinarySearchTree.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager">
<output url="file://$MODULE_DIR$/out/production/" />
<output-test url="file://$MODULE_DIR$/../out/test/86-BinarySearchTree" />
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
Binary file not shown.
Binary file added 86-BinarySearchTree/out/production/Main.class
Binary file not shown.
Binary file added 86-BinarySearchTree/out/production/Node.class
Binary file not shown.
108 changes: 108 additions & 0 deletions 86-BinarySearchTree/src/BinarySearchTree.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
public class BinarySearchTree {

Node root;

public void insert(Node node) {

root = insertHelper(root, node);
}

private Node insertHelper(Node root, Node node) {

int data = node.data;

if (root == null) {
root = node;
return root;
} else if (data < root.data) {
root.left = insertHelper(root.left, node);
} else {
root.right = insertHelper(root.right, node);
}
return root;
}

public void display() {

displayHelper(root);
}

private void displayHelper(Node root) {

if (root != null) {
/*If we want ascending order we start from the left side of the root,
* otherwise we start from the right side*/
displayHelper(root.left);
System.out.println(root.data);
displayHelper(root.right);
}
}

public boolean search(int data) {

return searchHelper(root, data);
}

private boolean searchHelper(Node root, int data) {

if (root == null) {
return false;
} else if (root.data == data) {
return true;
} else if (root.data > data) {
return searchHelper(root.left, data);
} else {
return searchHelper(root.right, data);
}
}

public void remove(int data) {

if (search(data)) {
removeHelper(root, data);
} else {
System.out.println(data + " could not be found");
}
}

public Node removeHelper(Node root, int data) {

if (root == null) {
return root;
} else if (data < root.data) {
root.left = removeHelper(root.left, data);
} else if (data > root.data) {
root.right = removeHelper(root.right, data);
} else { // node found
if (root.left == null && root.right == null) {
root = null;
} else if (root.right != null) { //find a successor to replace this node
root.data = successor(root);
root.right = removeHelper(root.right, root.data);
} else { //find predecessor to replace this node
root.data = predecessor(root);
root.left = removeHelper(root.left, root.data);
}
}

return root;
}

private int successor(Node root) { //find the least value below the right child of this root node
root = root.right;

while (root.left != null) {
root = root.left;
}
return root.data;
}

private int predecessor(Node root) { //find the greatest value below the left child of this root node
root = root.left;

while (root.right != null) {
root = root.right;
}
return root.data;
}
}
32 changes: 32 additions & 0 deletions 86-BinarySearchTree/src/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
public class Main {
public static void main(String[] args) {

//Binary Search Tree - A tree data structure, where each node is greater than it's left child,
//but less than it's right.

//benefit: easy to locate a node when they are in this order

//time complexity: best case O(log n)
// worst case O(n)

//space complexity: O(n)

BinarySearchTree tree = new BinarySearchTree();

tree.insert(new Node(5));
tree.insert(new Node(1));
tree.insert(new Node(9));
tree.insert(new Node(7));
tree.insert(new Node(3));
tree.insert(new Node(6));
tree.insert(new Node(4));
tree.insert(new Node(8));
tree.insert(new Node(2));

tree.remove(4);
tree.display();

//System.out.println(tree.search(10));

}
}
10 changes: 10 additions & 0 deletions 86-BinarySearchTree/src/Node.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
public class Node {

int data;
Node left;
Node right;

public Node(int data) {
this.data = data;
}
}

0 comments on commit 1035b26

Please sign in to comment.