二叉搜索树(BST)的java实现

一.什么是二叉搜索树?

简单来说,它是一个加了限定条件的二叉树。
它可以使最坏情况下的平均搜索的时间复杂度降低到O(log n).
条件是:
1.一个节点的左子树只能包含键值小于该节点的键值的节点。
2.一个节点的右子树只能包含键值大于该节点的键值的节点。
3.左右子树也都必须是二叉搜索树。

例如:
在这里插入图片描述

二.java代码的实现

1.节点的定义:

class BinarySearchTreeNode{
    private int data;
    private BinarySearchTreeNode left;
    private BinarySearchTreeNode right;
    public int getData()
    {
        return data;
    }

    public void setData(int data)
    {
        this.data=data;
    }

    public BinarySearchTreeNode getLeft()
    {
        return left;
    }
    public void setLeft(BinarySearchTreeNode left)
    {
        this.left=left;
    }

    public BinarySearchTreeNode getRight()
    {
        return right;
    }
    public void setRight(BinarySearchTreeNode right)
    {
        this.right=right;
    }
}

二.操作方法的定义:
1.查找元素:时间复杂度为O(n),空间复杂度为O(1)
2.查找最小,最大元素:时间复杂度为O(n),空间复杂度为O(1)
3.插入元素:时间复杂度为O(n),空间复杂度为O(n)。可用迭代改进
4.删除元素:时间复杂度为O(n),空间复杂度为O(n)。可用迭代改进

class CaoZuo1{
    //寻找元素
    BinarySearchTreeNode Find(BinarySearchTreeNode root,int data)
    {
        if(root==null) return null;
        while (root!=null)
        {
            if(data==root.getData())
                return root;
            else if(data>root.getData())
                root=root.getRight();
            else root=root.getLeft();
        }
        return null;
    }

    //寻找树中最小元素
    BinarySearchTreeNode FindMin(BinarySearchTreeNode root)
    {
        if(root==null) return null;
        while(root.getLeft()!=null)
            root=root.getLeft();
        return root;
    }
    //寻找树中最大元素
    BinarySearchTreeNode FindMax(BinarySearchTreeNode root)
    {
        if(root==null) return null;
        while(root.getRight()!=null)
            root=root.getRight();
        return root;
    }

    //插入元素
    BinarySearchTreeNode Insert(BinarySearchTreeNode root ,int data)
    {
        if(root==null){
            root=new BinarySearchTreeNode();
            if(root==null)
            {
                System.out.println("Memory Error");
                return null;
            }
            else {
                root.setData(data);
            }
        }
        else{
            if(data<root.getData())
                root.setLeft(Insert(root.getLeft(),data));
            else if(data>root.getData())
                root.setRight(Insert(root.getRight(),data));
        }
        return root;
    }

    //删除元素
    BinarySearchTreeNode Delete(BinarySearchTreeNode root,int data)
    {
        BinarySearchTreeNode temp;

        root=Find(root,data);
        if(root==null)
        {
            System.out.println("没有该元素");
        }

        else {
            if(root.getLeft()!=null&&root.getRight()!=null) {
                temp = FindMax(root.getLeft());
                root.setData(temp.getData());
                root.setLeft(Delete(root.getLeft(), root.getData()));
            }
            else {
                temp=root;
                if(root.getLeft()==null)
                    root=root.getRight();
                if(root.getRight()==null)
                    root=root.getLeft();
            }
        }
        return root;
    }
}

测试代码:

public class TwoSearchTree {

    public static void main(String[] args) {

        CaoZuo1 text=new CaoZuo1();
        BinarySearchTreeNode a=new BinarySearchTreeNode();
        a.setData(10);
        for(int i=1;i<10;i++)
        {
            text.Insert(a,i);
        }
        a=text.Delete(a,1);
        a=text.FindMin(a);
        System.out.println(a.getData());

    }
}

结果:
在这里插入图片描述

发布了73 篇原创文章 · 获赞 1 · 访问量 2449

猜你喜欢

转载自blog.csdn.net/c1776167012/article/details/105067356