package com.tree;
import com.tree.BitNode;
/**
*
* 二叉搜索树:一个节点的左子节点的关键字小于这个节点。右子节点的关键字大于或等于这个父节点
*
* 注意:不适合插入同样关键字的节点
与平衡二叉树比較:不适合插入有序序列
*/
public class SearchBinTree {
public static void main(String[] args) {
BitNode root = new BitNode();
root.data = 6;
int[] arr = { 1, 3, 9, 7, 0, 4 };
for (int i = 0; i < arr.length; i++) {
insert(root, arr[i]);
}
BinTree.levelTraverse(root);
BitNode successor = getSuccessor(root);
System.out.println("successor:" + successor.data);
System.out.println("delete : " + delete(root, 1));
BinTree.levelTraverse(root);
}
public static void insert(BitNode root, int data) {
BitNode node = new BitNode();
node.data = data;
if (root == null) {
root = node;
} else {
if (data < root.data) {
if (root.lchild == null) {
root.lchild = node;
} else {
insert(root.lchild, data);
}
} else {
if (root.rchild == null) {
root.rchild = node;
} else {
insert(root.rchild, data);
}
}
}
}
public static BitNode getMinNode(BitNode root) {
BitNode current, min = null;
current = root;
while (current != null) {
min = current;
current = current.lchild;
}
return min;
}
public static BitNode getMaxNode(BitNode root) {
BitNode current, max = null;
current = root;
while (current != null) {
max = current;
current = current.rchild;
}
return max;
}
public static BitNode find(BitNode root, int data) {
BitNode current = root;
while (current.data != data) {
if (data < current.data)
current = current.lchild;
else
current = current.rchild;
if (current == null)
return null;
}
return current;
}
public static BitNode getSuccessor(BitNode delNode) {
BitNode successorParent = delNode;
BitNode successor = delNode;
BitNode current = delNode.rchild;
while (current != null) {
successorParent = successor;
successor = current;
current = current.lchild;
}
if (successor != delNode.rchild) {
successorParent.lchild = successor.rchild;
successor.rchild = delNode.rchild;
}
return successor;
}
public static boolean delete(BitNode root, int data) {
BitNode current = root;
BitNode parent = root;
boolean isLeftChild = true;
while (current.data != data) {
parent = current;
if (data < current.data) {
isLeftChild = true;
current = current.lchild;
} else {
isLeftChild = false;
current = current.rchild;
}
if (current == null) {
return false;
}
}
if (current.lchild == null && current.rchild == null) {
if (current == root) {
root = null;
} else if (isLeftChild) {
parent.lchild = null;
} else {
parent.rchild = null;
}
} else if (current.rchild == null) {
if (current == root) {
root = current.lchild;
} else if (isLeftChild) {
parent.rchild = current.lchild;
} else {
parent.rchild = current.lchild;
}
} else if (current.lchild == null) {
if (current == root) {
root = current.rchild;
} else if (isLeftChild) {
parent.lchild = current.rchild;
} else {
parent.rchild = current.rchild;
}
} else {
BitNode successor = getSuccessor(current);
if(current == root){
root = successor;
} else if(isLeftChild){
parent.lchild = successor;
} else {
parent.rchild = successor;
}
successor.lchild = current.lchild;
}
return true;
}
}