/**
*
* 简单实现的二叉排序树
*/
public class Tree
{
public Node root;
//插入节点,关键是要找到插入节点的父节点,插入边的方向
public void addNode ( int data, String sdata )
{
Node newNode = new Node(data, sdata);
if ( root == null )
{
root = newNode;
return;
}
Node parent = null; //父节点
Node current = root;
boolean isLeftFlag = true;//插入边方向
//找到叶子节点作为插入父节点
while ( current != null )
{
parent = current;
if ( data > current.data )
{
current = current.rightChild;
isLeftFlag = false;
}
else if ( data < current.data )
{
current = current.leftChild;
isLeftFlag = true;
}
else
{
return; //相同元素排除
}
}
//插入节点
if ( isLeftFlag )
{
parent.leftChild = newNode;
}
else
{
parent.rightChild = newNode;
}
}
//中序遍历
public static void inOrder ( Node node )
{
if ( node != null )
{
inOrder(node.leftChild);
System.out.println(node.data);
inOrder(node.rightChild);
}
}
/**
* 首先确认删除元素的父节点,以及边的方向
*
* 删除节点要考虑 ,节点是否有子节点,
*
* 一个子节点,子节点直接替换
* 有两子节点,用中序后继节点替换
*
* */
public Node deleteNode ( int data )
{
Node parent = root;
Node current = root;
boolean isLeftFlag = true;
while ( current.data != data )
{
parent = current;
if ( data > current.data )
{
current = current.rightChild;
isLeftFlag = false;
}
else
{
current = current.leftChild;
isLeftFlag = true;
}
if ( current == null )
{
return null;
}
}
//没有子节点
if ( current.leftChild == null && current.rightChild == null )
{
if ( root == current )
{
root = null;
}
else
{
if(isLeftFlag)
{
parent.leftChild=null;
}
else
{
parent.rightChild=null;
}
}
}
else if ( current.leftChild == null )//有右子节点
{
if ( current == root )
{
root =current.rightChild;
}
else
{
if(isLeftFlag)
{
parent.leftChild=current.rightChild;
}
else
{
parent.rightChild=current.rightChild;
}
}
}
else if ( current.rightChild == null )//有左子节点
{
if ( current == root )
{
root =current.leftChild;
}
else
{
if(isLeftFlag)
{
parent.leftChild=current.leftChild;
}
else
{
parent.rightChild=current.leftChild;
}
}
}
else
//有两子节点
{
Node parentTempNode = current;
//替换节点 ,右子树最小的节点
Node tempNode = current;
Node currNode = tempNode.rightChild;
while ( currNode != null )
{
parentTempNode=tempNode;
tempNode = currNode;
currNode = currNode.leftChild;
}
//当前删除节点右节点有左子树 ,没有就不用替换
if ( tempNode!= current.rightChild )
{
parentTempNode.leftChild=tempNode.rightChild;//最后一个左节点的右节点 赋值给 上一个节点的左子树
tempNode.rightChild=current.rightChild;
}
tempNode.leftChild=current.leftChild;//左子树不变动
if(root==current)
{
root=tempNode;
}
else
{
if(isLeftFlag)
{
parent.leftChild=tempNode;
}
else
{
parent.rightChild=tempNode;
}
}
}
return current;
}
public Node getNode ( int data )
{
Node current = root;//从根节点开始查找
//如果跟当前节点的值不相等,就一直找下去
while ( current.data != data )
{
if ( data > current.data )
{
current = current.rightChild;//查找右子节点
}
else if ( data < current.data )
{
current = current.leftChild;
}
//找不到
if ( current == null )
{
return null;
}
}
return current;
}
public static void main ( String [] args )
{
Tree tree = new Tree();
tree.addNode(10, "zhangsan");
tree.addNode(8, "lisi");
tree.addNode(5, "wangwu");
tree.addNode(9, "kkey");
System.out.println(tree.getNode(10).sdata);
inOrder(tree.root);
// tree.deleteNode(8) ;
tree.deleteNode(10) ;
System.out.println("--------------------");
inOrder(tree.root);
}
}
/**节点*/
public class Node
{public int data;
public String sdata;
public Node leftChild;
public Node rightChild;
public Node ( int Data,String sdata )
{
this.data = Data;
this.sdata=sdata;
}
}