Skip to content

Instantly share code, notes, and snippets.

@tgvdinesh
Last active November 15, 2016 15:46
Show Gist options
  • Save tgvdinesh/c0a7b4b7fb44aa0a5fd5c50183e0ee87 to your computer and use it in GitHub Desktop.
Save tgvdinesh/c0a7b4b7fb44aa0a5fd5c50183e0ee87 to your computer and use it in GitHub Desktop.
Binary search Tree data structure implementation in Java
package com.oracle.java;
public class BinarySearchTree {
private int treeHeight(Node root) {
if (root == null) return 0;
return (1 + Math.max(treeHeight(root.left), treeHeight(root.right)));
}
private boolean isBalancedNaive(Node node) {
if (root == null) return true;
int heightDifference = treeHeight(node.left) - treeHeight(node.right);
return Math.abs(heightDifference) <= 1 && isBalancedNaive(node.left) && isBalancedNaive(node.right);
}
private static Node root;
private BinarySearchTree() {
root = null;
}
private boolean find(int id) {
Node current = root;
while (current != null) {
if (current.data == id) {
return true;
} else if (current.data > id) {
current = current.left;
} else {
current = current.right;
}
}
return false;
}
private boolean delete(int id) {
Node parent = root;
Node current = root;
boolean isLeftChild = false;
while (current.data != id) {
parent = current;
if (current.data > id) {
isLeftChild = true;
current = current.left;
} else {
isLeftChild = false;
current = current.right;
}
if (current == null) {
return false;
}
}
//if i am here that means we have found the node
//Case 1: if node to be deleted has no children
if (current.left == null && current.right == null) {
if (current == root) {
root = null;
}
if (isLeftChild) {
parent.left = null;
} else {
parent.right = null;
}
}
//Case 2 : if node to be deleted has only one child
else if (current.right == null) {
if (current == root) {
root = current.left;
} else if (isLeftChild) {
parent.left = current.left;
} else {
parent.right = current.left;
}
} else if (current.left == null) {
if (current == root) {
root = current.right;
} else if (isLeftChild) {
parent.left = current.right;
} else {
parent.right = current.right;
}
} else {
//now we have found the minimum element in the right sub tree
Node successor = getSuccessor(current);
if (current == root) {
root = successor;
} else if (isLeftChild) {
parent.left = successor;
} else {
parent.right = successor;
}
successor.left = current.left;
}
return true;
}
private Node getSuccessor(Node deleteNode) {
Node successor = null;
Node successorParent = null;
Node current = deleteNode.right;
while (current != null) {
successorParent = successor;
successor = current;
current = current.left;
}
//check if successor has the right child, it cannot have left child for sure
// if it does have the right child, add it to the left of successorParent.
// successorParent
if (successor != deleteNode.right) {
successorParent.left = successor.right;
successor.right = deleteNode.right;
}
return successor;
}
private void insert(int id) {
Node newNode = new Node(id);
if (root == null) {
root = newNode;
return;
}
Node current = root;
Node parent;
while (true) {
parent = current;
if (id < current.data) {
current = current.left;
if (current == null) {
parent.left = newNode;
return;
}
} else {
current = current.right;
if (current == null) {
parent.right = newNode;
return;
}
}
}
}
private void display(Node root) {
if (root != null) {
display(root.left);
System.out.print(" " + root.data);
display(root.right);
}
}
public static void main(String arg[]) {
BinarySearchTree b = new BinarySearchTree();
b.insert(3);
b.insert(8);
b.insert(1);
b.insert(4);
b.insert(6);
b.insert(2);
b.insert(10);
b.insert(9);
b.insert(20);
b.insert(25);
b.insert(15);
b.insert(16);
System.out.println("Original Tree : ");
b.display(BinarySearchTree.root);
System.out.println("\n Tree height (Sum of longest path edges from root to leaf) :");
System.out.println(b.treeHeight(BinarySearchTree.root));
System.out.println("\n Is the tree balanced (or) balance naive? ");
System.out.println(b.isBalancedNaive(BinarySearchTree.root));
System.out.println("");
System.out.println("Check whether Node with value 4 exists : " + b.find(4));
System.out.println("Delete Node with no children (2) : " + b.delete(2));
b.display(root);
System.out.println("\n Delete Node with one child (4) : " + b.delete(4));
b.display(root);
System.out.println("\n Delete Node with Two children (10) : " + b.delete(10));
b.display(root);
System.out.println("\n Tree height");
System.out.println(b.treeHeight(BinarySearchTree.root));
}
}
class Node {
int data;
Node left;
Node right;
Node(int data) {
this.data = data;
left = null;
right = null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment