Java实现AVL平衡树

Java实现AVL平衡树,第1张

Java实现AVL平衡

代码如下:

package AVLTree;

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

public class AVLTree {
    private class TreeNode
    {
        private int data;
        TreeNode left;
        TreeNode right;
        private int height;

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

    private TreeNode AVLTreeRoot;

    public AVLTree()
    {
        AVLTreeRoot = null;
    }


    public int heightTree(TreeNode root)
    {
        if (root==null) return 0;

        int l = heightTree(root.left);
        int r = heightTree(root.right);

        return (l>r?l:r)+1;
    }

    private TreeNode singleLeftRotation(TreeNode root)
    {
        TreeNode p = root.left;
        root.left = p.right;
        p.right = root;
        root.height = heightTree(root);
        p.height = heightTree(p);
        return p;
    }

    private TreeNode singleRightRotation(TreeNode root)
    {
        TreeNode p = root.right;
        root.right = p.left;
        p.left = root;
        root.height = heightTree(root);
        p.height = heightTree(p);
        return p;
    }

    private TreeNode doubleLeftRightRotation(TreeNode root)
    {
        root.left = singleRightRotation(root.left);
        return singleLeftRotation(root);
    }

    private TreeNode doubleRightLeftRotation(TreeNode root)
    {
        root.right = singleLeftRotation(root.right);
        return singleRightRotation(root);
    }

    public TreeNode insertTree(int e,TreeNode root)
    {
        if (root==null)
        {
            root = new TreeNode(e);
        }
        else if (e < root.data)
        {
            root.left = insertTree(e,root.left);
            if (heightTree(root.left)-heightTree(root.right)==2)
            {
                if (e < root.left.data)
                {
                    root = singleLeftRotation(root);
                }
                else
                {
                    root = doubleLeftRightRotation(root);
                }
            }
        }
        else if (e > root.data)
        {
            root.right = insertTree(e,root.right);
            if (heightTree(root.left)-heightTree(root.right)==-2)
            {
                if (e > root.right.data)
                {
                    root = singleRightRotation(root);
                }
                else
                {
                    root = doubleRightLeftRotation(root);
                }
            }
        }

        root.height = heightTree(root);
        return root;
    }


    public boolean createTree(int n)
    {
        Scanner sc = new Scanner(System.in);
        for (int i = 0;i arrays = new ArrayList<>();
        Queue q = new linkedList<>();
        if (AVLTreeRoot==null)
        {
            System.out.println(arrays);
            return ;
        }
        q.add(AVLTreeRoot);
        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);
    }

}

测试类如下:

package AVLTree;

public class TestAVLTree {
    public static void main(String[] args)
    {
        AVLTree bt = new AVLTree();
        bt.createTree(8);
        bt.levelOrder();
    }
}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5522990.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-13
下一篇 2022-12-13

发表评论

登录后才能评论

评论列表(0条)

保存