public class BST {
private TreeNode root;
public TreeNode getRoot() {
return root;
}
public void setRoot(TreeNode root) {
this.root = root;
}
static class TreeNode {
public int value;
public TreeNode left;
public TreeNode right;
public TreeNode(int value){
this.value = value;
}
}
public TreeNode get(int key){
TreeNode current = root;
while (current != null && current.value != key){
if(key < current.value){
current = current.left;
} else if(key > current.value){
current = current.right;
}
}
return current == null ? null : current;
}
public void insert(int key){
if(root == null){
root = new TreeNode(key);
return;
}
TreeNode current = root;
TreeNode parent = null;
while(true) {
parent = current;
if(key < parent.value){
current = parent.left;
if(current == null){
parent.left = new TreeNode(key);
return;
}
}
else if(key > parent.value ){
current = parent.right;
if(current == null){
parent.right = new TreeNode(key);
return;
}
}
else{
return;
}
}
}
public boolean delete(int key){
TreeNode parent = root;
TreeNode current = root;
boolean isLeftChild = false;
while(current != null && current.value != key){
parent = current;
if(current.value > key){
isLeftChild = true;
current = current.left;
} else {
isLeftChild = false;
current = current.right;
}
}
if(current == null){
return false;
}
if(current.left == null && current.right == null){
if(current == root){
root = null;
} else if(isLeftChild) {
parent.left = null;
} else {
parent.right = null;
}
}
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 {
TreeNode 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 TreeNode getSuccessor(TreeNode node){
TreeNode successor = null;
TreeNode successorParent = null;
TreeNode current = node.right;
while(current != null){
successorParent = successor;
successor = current;
current = current.left;
}
if(successor != node.right){
successorParent.left = successor.right;
successor.right = node.right;
}
return successor;
}
public static void preOrderTraversal(TreeNode root){
if(root == null){
return;
}
System.out.println(root.value);
preOrderTraversal(root.left);
preOrderTraversal(root.right);
}
public static void inOrderTraversal(TreeNode root){
if(root == null){
return;
}
inOrderTraversal(root.left);
System.out.println(root.value);
inOrderTraversal(root.right);
}
public static void postOrderTraversal(TreeNode root){
if(root == null){
return;
}
postOrderTraversal(root.left);
postOrderTraversal(root.right);
System.out.println(root.value);
}
public static void main(String[] args) {
BST bst = new BST();
bst.insert(50);
bst.insert(36);
bst.insert(88);
bst.insert(77);
BST.inOrderTraversal(bst.getRoot());
System.out.println();
BST.preOrderTraversal(bst.getRoot());
System.out.println();
BST.postOrderTraversal(bst.getRoot());
}