java二叉查找树的基本操作

import java.io.*;
import java.util.Scanner;
public class BinarySortTree{
    private BinarySortTree leftSon;    // 左子树
    private BinarySortTree rightSon;   // 右子树
    private int value;          // 节点的值

    public BinarySortTree(){}

    public BinarySortTree(int value){
        this.value = value;
    }
    
    // 建立二叉查找树
    public void buildBinaryTree(int data[]){
        if(data.length==0){
            return ;
        }
        this.value = data[0];
        for(int i = 1;i<data.length;i++){
            this.addNode(data[i]);
        }
    }

    // 添加节点
    public void addNode(int value){
         if(this.value >value){
             if(this.leftSon == null){
                this.leftSon = new BinarySortTree(value);
            }else{
                this.leftSon.addNode(value);
            }
        }else{
             if(this.rightSon == null){
                this.rightSon = new BinarySortTree(value);
            }else{
                this.rightSon.addNode(value);
            }
        }
    }

    // 删除节点
    public boolean delNode(int value){
        if(this.isExitNode(value)){
            deleteNode(value);
            return true;
        }else{
            return false;
        }
    }

    private void deleteNode(int value){
        BinarySortTree root = this;
        BinarySortTree currentNode = this;
        BinarySortTree parentNode = this;
        boolean isLeftNode = true;  // 标记currentNode为左节点
        while(currentNode.value != value){
            parentNode = currentNode;
            if(currentNode.value > value){
                currentNode = currentNode.leftSon;
                isLeftNode = true;  // currentNode 是parentNode的左节点
            }else{
                currentNode = currentNode.rightSon;
                isLeftNode = false; // currentNode 是parentNode的右节点
            }
        }
        // 此时currentNode为删除节点,parentNode是它的父节点
        // 删除currentNode节点有3种情况
        // currentNode有0个子节点;有1个子节点;有2个子节点
        // 第一种情况,currentNode有0个子节点
        if(currentNode.leftSon == null && currentNode.rightSon == null){
            if(currentNode == this){    // 该节点为根节点,则删除整棵树(只有一个根节点)
                root = null;
            }else{
                if(isLeftNode){                 // 删除currentNode,即parentNode的右节点
                    parentNode.leftSon = null;
                }else{                          // 删除currentNode,即parentNode的右节点
                    parentNode.rightSon = null;
                }
            }
        }else{
        // 第二种情况,currentNode有1个子节点
            if(currentNode.leftSon == null){    // currentNode有1个右节点
                if(currentNode == this){       // 该节点为根节点,则将根节点指向右节点
                    root = currentNode.rightSon;
                }else{
                    if(isLeftNode){             // 删除currentNode,即parentNode的左节点
                        parentNode.leftSon = currentNode.rightSon;
                    }else{                      // 删除currentNode,即parentNode的右节点
                        parentNode.rightSon = currentNode.rightSon;
                    }
                }
            }else{
                if(currentNode.rightSon == null){  // currentNode有1个左节点
                    if(currentNode == this){
                        root = currentNode.leftSon;
                    }else{
                        if(isLeftNode){
                            parentNode.leftSon = currentNode.leftSon;
                        }else{
                            parentNode.rightSon = currentNode.leftSon;
                        }
                    }
                }else{
                    // 第三种情况,currentNode有2个子节点
                    BinarySortTree successor = findSuccessor(currentNode);
                    if(currentNode == this){
                        root = successor;
                    }else{
                        if(isLeftNode){
                            parentNode.leftSon = successor;
                        }else{
                            parentNode.rightSon = successor;
                        }
                    }
                }
            }
        }    
    }
    // 从delNode的右儿子中寻找继承者人(继承人的value必需大于delNode的左子树节点的value,而且,还要
    // 小于delNode的右子树节点的value,因此,必需从delNode的右子树中寻找继承人)
    public BinarySortTree findSuccessor(BinarySortTree delNode){
        BinarySortTree parent = delNode;
        BinarySortTree successor = delNode;         // 继承人
        BinarySortTree current = delNode.rightSon;
        while(current != null){
            parent = successor;
            successor = current;
            current = current.leftSon;
        }
        if(successor != delNode.rightSon){        // 如果继承人不是delNode的亲儿子
                // parent的左儿子(原来是successor),现在指向successor的右儿子,successor没有左儿子
            parent.leftSon = successor.rightSon; 
            successor.rightSon = delNode.rightSon; // 继承人的右儿子,指向删除节点的右儿子
            //successor.leftSon = delNode.leftSon;    // 继承人的左儿子,指向删除节点的做左儿子
        }
        successor.leftSon = delNode.leftSon;
        return successor;
    }
    // 前序遍历
    public void preTraversl(){
        System.out.print(this.value + ",");
        if(this.leftSon != null){
            this.leftSon.preTraversl();
        }
        if(this.rightSon != null){
            this.rightSon.preTraversl();
        }
    }
    
    // 中序遍历
    public void midTraversl(){
        if(this.leftSon != null){
            this.leftSon.midTraversl();
        }
        System.out.print(this.value + ",");
        if(this.rightSon != null){
            this.rightSon.midTraversl();
        } 
    }

    // 后序遍历
    public void backTraversl(){
        if(this.leftSon != null){
            this.leftSon.midTraversl();
        }
        if(this.rightSon != null){
            this.rightSon.backTraversl();
        }
        System.out.print(this.value + ",");
    }
    
    // 给整型数组赋值
    public void setIntArraData(int data[]){
        int len = data.length;
        for(int i =0;i<len;i++){
            data[i] = (int)(Math.random()*100);
        }
    }
    
    // 查找树中是否存在值为value的节点,存在,返回true;不存在,返回false
    public boolean isExitNode(int value){
        if(this.value == value){
            return true;
        }
        if(this.value > value){
            if(this.leftSon == null){
                return false;
            }else{
                return this.leftSon.isExitNode(value);
            }
        }else{
            if(this.rightSon == null){
                return false;
            }else{
                return this.rightSon.isExitNode(value);
            }
        }
    }


    public static void main(String args[]){
        int data [] = {8,4,2,1,3,6,5,7,11,9,10,12}; 
        BinarySortTree bst = new BinarySortTree();
//        int data [] = new int[20];
//        bst.setIntArraData(data);
        bst.buildBinaryTree(data);
        System.out.print("前序遍历:");
        bst.preTraversl();
        System.out.println();
        System.out.print("中序遍历:");
        bst.midTraversl();
        System.out.println();
        System.out.print("后序遍历:");
        bst.backTraversl();
        System.out.println();
        System.out.println("请输入要删除的节点value值:");
        Scanner scanner = new Scanner(System.in);
        int value = scanner.nextInt();
        if(bst.isExitNode(value)){
            System.out.println("OK,存在这样的节点,value ="+ value);
        }else{
            System.out.println("Sorry,不存在这样的节点");
        }
        if(bst.delNode(value)){
            System.out.println("删除节点"+ value +"成功");
            System.out.print("前序遍历:");
            bst.preTraversl();
            System.out.println();
            System.out.print("中序遍历:");
            bst.midTraversl();
            System.out.println();
            System.out.print("后序遍历:");
            bst.backTraversl();
            System.out.println();
            if(bst.isExitNode(value)){
                System.out.println("OK,存在这样的节点,value ="+ value);
            }else{
                System.out.println("Sorry,节点" + value + "已经被删除");
            }
        }else{
            System.out.println("二叉树中不存在这样的value = "+value+"的节点!");
        }
    }
}
    原文作者:二叉查找树
    原文地址: https://blog.csdn.net/qwe2010123/article/details/38885319
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞