class BinarySearchTree
{
node head;
/*
from 时 代 J a v a 公 众 号
*/
static class node
{
int data;
node left;
node right;
node(int d)
{
data = d;
left = null;
right = null;/**来自 NowJava.com - 时代Java**/
}
}
public void Insert(int value)
{
node temp = new node(value);
if(head == null)
head = temp;
else
{
node current;
current = head;
while(current != null)
{
if(value < current.data)
{
if(current.left != null)
current = current.left;
else
{
current.left = temp;
return ;
}
}
else
{
if(current.right != null)
current = current.right;
else
{
current.right = temp;
return ;
}
}
}
}
}
public void Search(int value)
{
node current;
current = head;
while(current != null)
{
if(value < current.data)
current = current.left;
else if(value > current.data)
current = current.right;
else
{
System.out.println("Element " + value + " Found");
return ;
}
}
System.out.println("Element " + value + " not Found");
}
public int Min_Value(node head)
{
while(head.left != null)
{
head = head.left;
}
return head.data;
}
public node Delete_Key(node head, int value)
{
if(head == null)
return head;
if(value < head.data)
head.left = Delete_Key(head.left, value);
else if(value > head.data)
head.right = Delete_Key(head.right, value);
else
{
if(head.left == null)
return head.right;
else if(head.right == null)
return head.left;
head.data = Min_Value(head.right);
head.right = Delete_Key(head.right, head.data);
}
return head;
}
public void Delete(int value)
{
head = Delete_Key(head, value);
}
/**代码未完, 请加载全部代码(NowJava.com).**/
本文系作者在时代Java发表,未经许可,不得转载。如有侵权,请联系nowjava@qq.com删除。