Skip to content

Instantly share code, notes, and snippets.

@kp96
Last active December 7, 2015 20:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kp96/08de83fc1e4111613e08 to your computer and use it in GitHub Desktop.
Save kp96/08de83fc1e4111613e08 to your computer and use it in GitHub Desktop.
Simple implementation of binary serach tree data structure with ints.
import java.util.Scanner;
public class BSTree {
private TreeNode root;
public BSTree() {
this.root = null;
}
public TreeNode getTreeRoot() {
return this.root;
}
public void insert(int val) {
this.root = insertRecursive(this.root, val);
}
public void delete(int val) {
this.root = deleteRecursive(this.root, val);
}
public boolean contains(int key) {
return containsRecursive(this.root, key);
}
private boolean containsRecursive(TreeNode root, int val) {
if(root == null)
return false;
else if(val < root.val)
return containsRecursive(root.left, val);
else if(val > root.val)
return containsRecursive(root.right, val);
else
return true;
}
private TreeNode deleteRecursive(TreeNode root, int val) {
if(root == null)
return null;
else if(val < root.val)
root.left = deleteRecursive(root.left, val);
else if(val > root.val)
root.right = deleteRecursive(root.right, val);
else {
if(root.left == null) {
TreeNode temp = root.right;
root = null;
return temp;
}
else if(root.right == null) {
TreeNode temp = root.left;
root = null;
return temp;
}
else {
int key = minValue(root.right);
root.val = key;
deleteRecursive(root.right, key);
}
}
return root;
}
private int minValue(TreeNode root) {
while(root.left != null)
root = root.left;
return root.val;
}
private TreeNode insertRecursive(TreeNode root, int val) {
if(root == null)
root = new TreeNode(val);
else if(val < root.val)
root.left = insertRecursive(root.left , val);
else
root.right = insertRecursive(root.right, val);
return root;
}
public void traverseInorder() {
inorder(this.root);
}
public void traversePreorder() {
preorder(this.root);
}
public void traversePostorder() {
postorder(this.root);
}
private void postorder(TreeNode root) {
if(root == null)
return;
else {
postorder(root.left);
postorder(root.right);
System.out.print(root.val + " ");
}
}
private void preorder(TreeNode root) {
if(root == null)
return;
else {
System.out.print(root.val + " ");
preorder(root.left);
preorder(root.right);
}
}
private void inorder(TreeNode root) {
if(root == null)
return;
else {
inorder(root.left);
System.out.print(root.val + " ");
inorder(root.right);
}
}
private static class TreeNode {
int val;
TreeNode left;
TreeNode right;
public TreeNode() {
this.val = 0;
this.left = this.right = null;
}
public TreeNode(int val) {
this.val = val;
this.left = this.right = null;
}
public boolean isLeaf() {
if(this.left == null && this.right == null)
return true;
return false;
}
}
}
class Test {
public static void main(String[] args) {
BSTree bsTree = new BSTree();
int choice;
Scanner sc = new Scanner(System.in);
outer:while(true) {
System.out.println("Enter 1> Insertion 2> Delete 3> Traverse 4> Quit");
choice = sc.nextInt();
switch(choice) {
case 1: System.out.print("Num> ");
bsTree.insert(sc.nextInt());
break;
case 2: System.out.print("Num> ");
bsTree.delete(sc.nextInt());
break;
case 3: bsTree.traverseInorder();
System.out.println();
break;
case 4: break outer;
}
}
}
}
// iterative version
// public void insert(int val) {
// TreeNode cur = this.root;
// if(cur == null) {
// cur = new TreeNode(val);
// this.root = cur;
// }
// else {
// TreeNode prev = cur;
// while(cur != null) {
// prev = cur;
// if(val < cur.val)
// cur = cur.left;
// else
// cur = cur.right;
// }
// if(val < prev.val)
// prev.left = new TreeNode(val);
// else
// prev.right = new TreeNode(val);
// }
// }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment