Java实现BST平衡树

Posted Wecccccccc

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java实现BST平衡树相关的知识,希望对你有一定的参考价值。

代码如下:

package BSTree;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;

public class BSTree {
    private class TreeNode
    {
        int data;
        TreeNode left;
        TreeNode right;

        public TreeNode(int e){
            data = e;
            left = null;
            right = null;
        }
    }


    private TreeNode BSTreeRoot;

    public BSTree()
    {
        BSTreeRoot=null;
    }

    public TreeNode findElem(int e,TreeNode root)
    {
        if (root==null) return null;
        if (e > root.data)
        {
            return findElem(e,root.right);
        }
        else if (e < root.data)
        {
            return findElem(e,root.left);
        }
        else
        {
            return root;
        }
    }

    public TreeNode findMin(TreeNode root)
    {
        if (root==null) return null;
        else if (root.left==null)
        {
            return root;
        }
        else return findMin(root.left);
    }

    public TreeNode findMax(TreeNode root)
    {
        if (root==null) return null;
        else if (root.right==null)
        {
            return root;
        }
        else return findMax(root.left);
    }


    public TreeNode insertTree(int e,TreeNode root)
    {
        if (root==null)
        {
            root = new TreeNode(e);
            root.left = root.right = null;
        }
        else
        {
            if (e > root.data)
            {
                root.right = insertTree(e,root.right);
            }
            else if (e < root.data)
            {
                root.left = insertTree(e,root.left);
            }
        }
        return root;
    }


    public TreeNode deleteTree(int e,TreeNode root)
    {
        if (root==null)
        {
            System.out.println("The elem is found");
            return null;
        }
        else if (e > root.data)
        {
            root.right = deleteTree(e,root.right);
        }
        else if (e < root.data)
        {
            root.left = deleteTree(e,root.left);
        }
        else
        {
            if (root.left!=null && root.right!=null)
            {
                TreeNode tmp = findMin(root.right);
                root.data = tmp.data;
                root.right = deleteTree(root.data,root.right);
            }
            else
            {
                if (root.left==null)
                {
                    root = root.right;
                }
                else if (root.right==null)
                {
                    root = root.left;
                }

            }
        }
        return root;
    }

    public boolean createTree(int n)
    {
        Scanner sc = new Scanner(System.in);
        for (int i = 0;i<n;i++)
        {
            int val = sc.nextInt();
            BSTreeRoot =  insertTree(val,BSTreeRoot);
        }
        return true;
    }


    public void levelOrder()
    {
        ArrayList<Integer> arrays = new ArrayList<>();
        if (BSTreeRoot==null)
        {
            System.out.println(arrays);
            return ;
        }
        Queue<TreeNode> q = new LinkedList<>();

        q.add(BSTreeRoot);
        while(!q.isEmpty())
        {
            TreeNode t = q.poll();
            arrays.add(t.data);
            if (t.left!=null) q.add(t.left);
            if (t.right!=null) q.add(t.right);
        }
        System.out.println(arrays);
        return ;
    }


}

测试类如下:

package BSTree;

public class TestBSTree {
    public static void main(String[] args)
    {
        BSTree bt = new BSTree();
        bt.createTree(5);
        bt.levelOrder();
    }
}

以上是关于Java实现BST平衡树的主要内容,如果未能解决你的问题,请参考以下文章

Java树结构实际应用(平衡二叉树/AVL树)

Treap 实现名次树

[平衡树] aw3786. 二叉排序树(BST)

Splay(伸展树分裂树):平衡二叉搜索树中功能最丰富的树

AVL平衡树(非指针实现)

二叉搜索树 - Java 实现