-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
165 additions
and
1 deletion.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |