二叉树的实现:
比较有意思的是二叉树的删除
public class Node {
int iData;
double fData;
Node leftChild;
Node rightChild;
public void displayNode(){
}
}
public class Tree {
private Node root;
public Node find(int key){
Node current=root;
while (current!=null){
if (current.iData==key){
break;
}
if (current.iData<key){
current=current.rightChild;
}else {
current=current.leftChild;
}
}
return current;
}
public void insert(int key,double value){
Node newNode=new Node();
newNode.iData=key;
newNode.fData=value;
Node current=root;
if (root==null){
root=newNode;
return;
}
while (current!=null){
Node parent=current;
if (key<current.iData){
current=current.leftChild;
if (current==null){
parent.leftChild=newNode;
return;
}
}else {
current=current.rightChild;
if (current==null){
parent.rightChild=newNode;
return;
}
}
}
}
/**
*中序遍历法
*/
public void inDisplay(Node node){
if (node!=null){
inDisplay(node.leftChild);
System.out.println(node.iData+":"+node.fData);
inDisplay(node.rightChild);
}
}
public void delete(int id){
if (this.root!=null){
Node node=root;
Node fNode=null;
while (node.iData!=id){
fNode=node;
if (node.iData>id){
node=node.leftChild;
}else {
node=node.rightChild;
}
if (node==null){
return;
}
}
if (fNode==null){
//需要删除的节点是根节点
//看有几个子节点。
if (node.leftChild==null && node.rightChild==null) {
//0个,则删除成功后,树是空的
this.root=null;
return;
}
if (node.rightChild==null) {
//1个
this.root.leftChild=node.leftChild;
}
if (node.leftChild==null){
//1个
this.root.rightChild=node.rightChild;
}
if (node.leftChild!=null && node.rightChild!=null){
//2个,则将右侧树的最小叶子节点晋升为根节点
Node current=node.rightChild;
Node fn=node;
while (current.leftChild!=null) {
fn=current;
current = current.leftChild;
}
if (fn==node){
//最小子节点的父节点是即将被删除的节点
this.root=current;
current.leftChild=node.leftChild;
}else {
if (current.rightChild != null) {
fn.leftChild = current.rightChild;
}//上面先把目标树的右侧树的最小节点从原始树中独立出来,
else {
fn.leftChild = null;//把最小节点冲原始树中独立出来
}
this.root = current;
current.leftChild = node.leftChild;
current.rightChild = node.rightChild;
}
}
}else {//需要删除的不是根节点
boolean left=false;
if (fNode.leftChild==node){
left=true;//待删除节点是父节点的左节点
}
if (node.leftChild==null &&node.rightChild==null){
if (left){
fNode.leftChild=null;
}else {
fNode.rightChild=null;
}
return;
}
if (node.rightChild==null){
if (left){
fNode.leftChild=node.leftChild;
}else {
fNode.rightChild=node.leftChild;
}
}
if (node.leftChild==null){
if (left){
fNode.leftChild=node.rightChild;
}else {
fNode.rightChild=node.rightChild;
}
}
if (node.leftChild!=null && node.rightChild!=null){
Node current=node.rightChild;
Node fn=node;
while (current.leftChild!=null) {
fn=current;
current = current.leftChild;
}
if (fn==node){
//最小子节点的父节点是即将被删除的节点.把current嫁接到fNode的下面
if (left){
fNode.leftChild=current;
current.leftChild=node.leftChild;
}else {
fNode.rightChild=current;
current.leftChild=node.leftChild;
}
}else {
fn.leftChild=current.rightChild;//剥离current节点
//current替换node
if (left){
fNode.leftChild=current;
}else {
fNode.rightChild=current;
}
current.leftChild=node.leftChild;
current.rightChild=node.rightChild;
}
}
}
}
}
/**
* 获取树最大的节点
* @return
*/
public Node max(){
Node node=this.root;
if (node!=null){
while (node.rightChild!=null)
node=node.rightChild;
}
return node;
}
/**
* 获取树最小的节点
* @return
*/
public Node min(Node rn){
Node node=rn;
if (node!=null){
while (node.leftChild!=null)
node=node.leftChild;
}
return node;
}
public static void main(String[] args) {
Tree theTree=new Tree();
theTree.insert(50,1.3);
theTree.insert(25,1.7);
theTree.insert(75,1.9);
theTree.insert(60,1.3);
theTree.insert(15,1.7);
theTree.insert(85,1.9);
Node found=theTree.find(25);
if (found!=null){
System.out.println("找到 key = 25 的节点");
}
theTree.inDisplay(theTree.root);
System.out.println(theTree.max().iData);
System.out.println(theTree.min(theTree.root).iData);
theTree.delete(50);
theTree.inDisplay(theTree.root);
}
}