public class SearchTree {
private TreeNode root;
public TreeNode getRoot() {
return root;
}
public void setRoot(TreeNode root) {
this.root = root;
}
public SearchTree(){
}
/**
*
* 创建查找二叉树,添加结点
*/
public TreeNode put(int data){
TreeNode node = null;
TreeNode parent = null;
if(root == null){
node = new TreeNode(0,data);
root = node;
return node;
}
node = root;
while(node != null){
parent = node;
if(data > node.data){
node = node.rightTreeNode;
}else if (data < node.data){
node = node.leftTreeNode;
}else {
return node;
}
}
node = new TreeNode(0,data);
if(data < parent.data){
parent.leftTreeNode = node;
}else{
parent.rightTreeNode = node;
}
node.parent = parent;
return node;
}
private void deleteNode(int key) throws Exception{
TreeNode node = searchNode(key);
if(node == null)
throw new Exception("该结点无法找到");
else
delete(node);
}
private TreeNode searchNode(int key){
TreeNode node = root;
if(node == null) {
return null;
}else{
while(node != null && key != node.data){
if(key < node.data){
node = node.leftTreeNode;
}else{
node = node.rightTreeNode;
}
}
}
return node;
}
private void delete(TreeNode node) throws Exception {
if (node == null){
throw new Exception("该结点为空,无法找到");
}else{
TreeNode parent = node.parent;
//被删除的结点无左右孩子
if(node.leftTreeNode == null && node.rightTreeNode == null){
if(parent.leftTreeNode == node)
parent.leftTreeNode = null;
else
parent.rightTreeNode = null;
return;
}
//被删除结点有左无右
if(node.leftTreeNode != null && node.rightTreeNode == null){
if(parent.leftTreeNode == node){
parent.leftTreeNode = node.leftTreeNode;
}else if (parent.rightTreeNode == node){
parent.rightTreeNode = node.leftTreeNode;
}
return;
}
//被删除结点有右无左
if(node.leftTreeNode == null && node.rightTreeNode != null){
if(parent.leftTreeNode == node){
parent.leftTreeNode = node.rightTreeNode;
}else if(parent.rightTreeNode == node){
parent.rightTreeNode = node.rightTreeNode;
}
return ;
}
//被删除的结点既有左孩子又有右孩子
TreeNode next = getNextNode(node);
node.data = next.data;
delete(next);
}
}
//查找一个结点的后继结点
private TreeNode getNextNode(TreeNode node){
if(node == null){
return null;
}
if(node.rightTreeNode != null) {
//找某结点最小关键字结点
return getMinTreeNode(node.rightTreeNode);
}else{
TreeNode parent = node.parent;
while(parent != null && node == parent.rightTreeNode){
node = parent;
parent = parent.parent;
}
return parent;
}
}
private TreeNode getMinTreeNode(TreeNode node){
if(node == null)
return null;
while(node.leftTreeNode != null){
node = node.leftTreeNode;
}
return node;
}
class TreeNode{
private int index;
private TreeNode leftTreeNode;
private TreeNode rightTreeNode;
private TreeNode parent;
private int data;
public TreeNode(int index,int data){
this.data = data;
this.index = index;
this.leftTreeNode = null;
this.rightTreeNode = null;
this.parent = null;
}
public int getIndex() {
return index;
}
public void setIndex(int index) {
this.index = index;
}
public TreeNode getLeftTreeNode() {
return leftTreeNode;
}
public void setLeftTreeNode(TreeNode leftTreeNode) {
this.leftTreeNode = leftTreeNode;
}
public TreeNode getRightTreeNode() {
return rightTreeNode;
}
public void setRightTreeNode(TreeNode rightTreeNode) {
this.rightTreeNode = rightTreeNode;
}
public TreeNode getParent() {
return parent;
}
public void setParent(TreeNode parent) {
this.parent = parent;
}
public int getData() {
return data;
}
public void setData(int data) {
this.data = data;
}
}
}