Avl tree 仅在主根中的AVL树中进行平衡

Avl tree 仅在主根中的AVL树中进行平衡,avl-tree,Avl Tree,我正在创建AVL二叉树,其中唯一的问题是根不会改变其位置或获得平衡,除了所有其他子对象的根或叶等 任何帮助都将不胜感激 逻辑层文件 public class Node { public int data; public Node left, right; public Node(int data) { this.data = data; left = null;

我正在创建AVL二叉树,其中唯一的问题是根不会改变其位置或获得平衡,除了所有其他子对象的根或叶等

任何帮助都将不胜感激

逻辑层文件

 public class Node
    {
        public int data;
        public Node left, right;

        public Node(int data)
        {
            this.data = data;
            left = null;
            right = null;
        }
    }

 public class BinaryTree
    {
        public Node root;
        public BinaryTree()
        {
            root = null;
        }
        public int height(Node temp)
        {
            int h = 0;
            if (temp != null)
            {
                int l_height = height(temp.left);
                int r_height = height(temp.right);
                int max_height = Math.Max(l_height, r_height);
                h = max_height + 1;
            }
            return h;
        }
        public int diff(Node temp)
        {
            int l_height = height(temp.left);
            int r_height = height(temp.right);
            int b_factor = l_height - r_height;
            return b_factor;
        }

        Node rr_rotation(Node parent)
        {
            Node temp;
            temp = parent.right;
            parent.right = temp.left;
            temp.left = parent;
            return temp;
        }
        Node ll_rotation(Node parent)
        {
            Node temp;
            temp = parent.left;
            parent.left = temp.right;
            temp.right = parent;
            return temp;
        }
        Node lr_rotation(Node parent)
        {
            Node temp;
            temp = parent.left;
            parent.left = rr_rotation(temp);
            return ll_rotation(parent);
        }
        Node rl_rotation(Node parent)
        {
            Node temp;
            temp = parent.right;
            parent.right = ll_rotation(temp);
            return rr_rotation(parent);
        }
        Node balance(Node temp)
        {
            int bal_factor = diff(temp);
            if (bal_factor > 1)
            {
                if (diff(temp.left) > 0)
                    temp = ll_rotation(temp);
                else
                    temp = lr_rotation(temp);
            }
            else if (bal_factor < -1)
            {
                if (diff(temp.right) > 0)
                    temp = rl_rotation(temp);
                else
                    temp = rr_rotation(temp);
            }
            return temp;
        }

        public Node addNode(int data)  //  It only add the Root(that is 55 in the fig)
        {
            Node newNode = new Node(data);
            if (root == null)
            {
                root = newNode;

            }
            return root;
        }
        public Node insertNode(Node root, int newNode)  //But I want to make this should add root node.
        {

            if (root == null)            (I think here is some problem)
            {
                root = new Node(newNode);
                root.data = newNode;
                root.left = null;
                root.right = null;
                return root;
            }


            if (newNode < root.data)
            {
                root.left = insertNode(root.left, newNode);
                root = balance(root);
            }
            else if (newNode >= root.data)
            {
                root.right = insertNode(root.right, newNode);
                root = balance(root);
            }
            return root;
        }
    }
 public partial class PresentationLayer : Form
    {
        BinaryTree obj = new BinaryTree();
        int a, b;

        public PresentationLayer()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)  //add node button
        {
            int num = 0;
            bool result = int.TryParse(textBox1.Text, out num);

            if (result)
            {
                a = Int32.Parse(textBox1.Text);
                obj.addNode(a);    //It creates root
               textBox1.Hide();
               button1.Hide();
            }
        }
           private void button2_Click_1(object sender, EventArgs e)   //insert button
        {
            int num = 0;
            bool result = int.TryParse(textBox2.Text, out num);

            if (result)
            {
                b = Int32.Parse(textBox2.Text);
                Node abc = new Node(b);
                obj.insertNode(obj.root, b);   //It is not creating root
                textBox2.Clear();
            }
        }
    }
}

任何帮助都将不胜感激

您应该将案例添加到
ll\u rotation
rr\u rotation
函数中,以便在根旋转时进行处理。您应该能够检查参数
parent
是否与
root
是同一节点,如果是,请将
root
设置为引用新旋转的节点

例如,根据:

向左旋转以成为:

               Q
              / \
             /   \
            P     C
           / \
          A   B

如果调用
ll\u rotation(p)
,其中
p
root
是同一节点,则函数应将
Q
指定为新的
root

@user3662005如果此答案对您有效,请随意:)
               Q
              / \
             /   \
            P     C
           / \
          A   B