二叉搜索树的搜索、插入、遍历和删除(Java)

首先定义二叉树的节点数据结构

package com.xiaokai.search;

/**
 * Created by Administrator on 2016/7/31.
 */
public class TreeNode {
     public int value;

    public TreeNode(int value) {
        this.value = value;
    }
    public TreeNode leftTreeNode;
    public TreeNode rightTreeNode;
}

二叉搜索树代码:

package com.xiaokai.search;

/**
 * Created by Administrator on 2016/7/31.
 */
public class BinarySearchTree {
    TreeNode root;

    public BinarySearchTree(TreeNode root) {
        this.root = root;
    }

    /**
     * 根据value搜索节点 即从根节点出发,遇大向左转,遇小向右转
     * @param value
     * @return
     */
    public TreeNode searchByValue(int value){
        TreeNode node = root;
        if (node == null){
            return null;
        }
        while (node.value!=value){
            if (node.value>value){
                node = node.leftTreeNode;
            }else{
                node = node.rightTreeNode;
            }
            if (node == null){
                return null;
            }
        }
        return node;
    }

    public TreeNode getRoot() {
        return root;
    }

    //插入节点,同样道理,遇大向左,遇小向右,找到空节点补上
    public void insertByValue(int value){
        TreeNode insertNode = new TreeNode(value);
        if (root==null){
            root = insertNode;
            return;
        }
        TreeNode currentNode = root;
        TreeNode parentNode;
        while(currentNode != null){
            parentNode = currentNode;
            if (currentNode.value > value){
                currentNode = currentNode.leftTreeNode;
                if (currentNode==null){
                    parentNode.leftTreeNode=insertNode;
                }
            }else{
                currentNode = currentNode.rightTreeNode;
                if (currentNode==null){
                    parentNode.rightTreeNode=insertNode;
                }
            }
        }
    }
    //获取最大值,一路向右
    public int getMaxValue(){
        TreeNode node = root;
        while(node.rightTreeNode!=null){
            node = node.rightTreeNode;
        }
        return node.value;
    }
    //获取最小值,一路向左
    public int getMinValue(){
        TreeNode node = root;
        while(node.leftTreeNode!=null){
            node = node.leftTreeNode;
        }
        return node.value;
    }
    //遍历打印函数
    private  void display(TreeNode node){
        if (node !=null){
            display(node.leftTreeNode);
            System.out.print(node.value+"  ");
            display(node.rightTreeNode);
        }
    }
    public void display(){
        display(root);
    }
    //删除节点,首先搜到要删除的点,然后根据节点特点分三种情况,1.无左右子节点,直接删掉,2.有一路子树,补上父节点,3.有两路子树,从中选出替换节点补上,需要一系列调整
    public boolean deleteNode(int value){
        TreeNode currentNode = root;
        TreeNode parentNode = root;
        boolean isLeftNode = true;
        while(currentNode.value != value){
            parentNode = currentNode;
            if (value < currentNode.value){
                isLeftNode = true;
                currentNode = currentNode.leftTreeNode;
            }else {
                isLeftNode = false;
                currentNode = currentNode.rightTreeNode;
            }
        }
        if (currentNode == null){
            return  false;
        }
        if (currentNode.leftTreeNode == null && currentNode.rightTreeNode == null){
            if (currentNode == root){
                root = null;
            }else if (isLeftNode){
                parentNode.leftTreeNode = null;
            }else {
                parentNode.rightTreeNode = null;
            }
        }else if (currentNode.leftTreeNode == null){
            if (currentNode == root){
                root = currentNode.rightTreeNode;
            }else if (isLeftNode){
                parentNode.leftTreeNode = currentNode.rightTreeNode;
            }else {
                parentNode.rightTreeNode = currentNode.rightTreeNode;
            }
        }else if (currentNode.rightTreeNode == null){
            if (currentNode == root){
                root = currentNode.leftTreeNode;
            }else if (isLeftNode){
                parentNode.leftTreeNode = currentNode.leftTreeNode;
            }else {
                parentNode.rightTreeNode = currentNode.leftTreeNode;
            }
        }else{
            TreeNode successor = findSuccessor(currentNode);
            if (currentNode == root){
                root = successor;
            }else if (isLeftNode){
                parentNode.leftTreeNode = successor;
            }else {
                parentNode.rightTreeNode = successor;
            }
            successor.leftTreeNode = currentNode.leftTreeNode;
        }
        return true;
    }
    //找到删除节点的替换点,即右叶子节点或者右树的最左子节点,最左子节点若有右子节点部该父节点左节点,并把删除节点的右树赋给替换点的右子节点。
    private TreeNode findSuccessor(TreeNode node) {
        TreeNode parentNode = node;
        TreeNode successor = node;
        TreeNode currentNode = node.rightTreeNode;
        while(currentNode!=null){
            parentNode = successor;
            successor = currentNode;
            currentNode=currentNode.leftTreeNode;
        }
        if (successor != node.rightTreeNode){
            parentNode.leftTreeNode = successor.rightTreeNode;
            successor.rightTreeNode = node.rightTreeNode;
        }

        return successor;
    }

}

测试类:

package com.xiaokai.search;


import org.junit.Test;
/**
 * Created by Administrator on 2016/7/31.
 */
public class BinarySearchTreeTest {
    @Test
    public void binarySearchTest() throws Exception {
        TreeNode node = new TreeNode(26);
        BinarySearchTree binarySearchTree = new BinarySearchTree(node);

        binarySearchTree.insertByValue(26);
        binarySearchTree.insertByValue(13);
        binarySearchTree.insertByValue(70);
        binarySearchTree.insertByValue(7);
        binarySearchTree.insertByValue(20);
        binarySearchTree.insertByValue(3);
        binarySearchTree.insertByValue(10);
        binarySearchTree.insertByValue(15);
        binarySearchTree.insertByValue(24);
        binarySearchTree.insertByValue(40);
        binarySearchTree.insertByValue(80);
        binarySearchTree.insertByValue(30);
        binarySearchTree.insertByValue(60);
        binarySearchTree.insertByValue(75);
        binarySearchTree.insertByValue(84);

        binarySearchTree.display();
        System.out.println();

        binarySearchTree.deleteNode(3);
        binarySearchTree.display();
        System.out.println();

        binarySearchTree.deleteNode(7);
        binarySearchTree.display();
        System.out.println();

        binarySearchTree.deleteNode(70);
        binarySearchTree.display();
        System.out.println();
        System.out.println(binarySearchTree.getMaxValue());
        System.out.println(binarySearchTree.getMinValue());
    }

}

输出结果:

3  7  10  13  15  20  24  26  26  30  40  60  70  75  80  84  
7  10  13  15  20  24  26  26  30  40  60  70  75  80  84  
10  13  15  20  24  26  26  30  40  60  70  75  80  84  
10  13  15  20  24  26  26  30  40  60  75  80  84  
84
10
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值