Data structure: balanced binary tree


balanced binary tree

I. Overview

A balanced binary tree, also known as an AVL tree, is a special binary sorting tree in which the height difference between the left subtree and the right subtree of each node does not exceed 1. The definition of a balanced binary tree can be summarized as the following two points:

  1. The absolute value of the height difference between the left subtree and the right subtree does not exceed 1.
  2. Both the left subtree and the right subtree are balanced binary trees.

The balance factor is an important concept in balanced binary trees. It represents the height of the left subtree of a node minus the height of the right subtree. According to the definition of a balanced binary tree, the balance factor of each node can only be -1, 0 or 1.

When a new node is inserted into a balanced binary sorting tree, it may cause imbalance, that is, a node with a balance factor whose absolute value is greater than 1. Adjustments to the tree may be necessary to restore balance. Common adjustment methods include rotation and rebalancing.

There are four types of rotation: left-hand, right-hand, left-right, and right-left. Rotation operation is a commonly used method in balancing binary tree adjustment. Its purpose is to satisfy the definition of balanced binary tree by changing the position of nodes.

Overall, a balanced binary tree is an important data structure with good search and insertion performance. It improves the efficiency of operations by keeping the tree balanced.

Introduction :

  • A balanced binary tree is a self-balancing binary search tree in which the height difference between its left subtree and right subtree does not exceed 1.
  • The basic operations of a balanced binary tree include insertion, deletion and search. The time complexity of these operations is O(log n), where n is the number of nodes in the tree.
  • The main application scenarios of balanced binary trees include computer science, data structures and algorithms.

Illustration :

The following is an illustration of a simple balanced binary tree:

      50
     / \
    30  70
   / \  / \
  10 40 60 80

In this balanced binary tree, the height difference between the left subtree and the right subtree of each node does not exceed 1.

Example :

Here is a simple example of implementing a balanced binary tree in Java:

public class AVLTree {
    
    
    class Node {
    
    
        int key;
        Node left, right;
        int height;

        public Node(int item) {
    
    
            key = item;
            left = right = null;
            height = 1;
        }
    }

    Node root;

    AVLTree(int key) {
    
    
        root = new Node(key);
    }

    AVLTree() {
    
    
        root = null;
    }

    int height(Node node) {
    
    
        if (node == null) return 0;
        return node.height;
    }

    Node insert(Node node, int key) {
    
    
        if (node == null) return new Node(key);
        if (key < node.key) {
    
    
            node.left = insert(node.left, key);
        } else if (key > node.key) {
    
    
            node.right = insert(node.right, key);
        } else {
    
     // Duplicate keys not allowed
            return node;
        }
        node.height = 1 + Math.max(height(node.left), height(node.right));
        return node;
    }
}

In this example, an inner class is defined Nodeto represent the nodes of a balanced binary tree. Each node has a key keyand two child nodes , leftas rightwell as an attribute representing the height of the node height. heightTwo methods and are also defined insert. heightmethod is used to calculate the height of a node, while insertmethod is used to insert a new node into a balanced binary tree.

2. Add data

In Java, a balanced binary tree is a common data structure that allows efficient organization and search of data. A balanced binary tree is a self-balancing binary search tree in which the absolute value of the height difference between the left subtree and the right subtree of any node does not exceed 1. AVL trees and red-black trees are two common types of balanced binary trees.

Here is an example showing how to add data in a balanced binary tree:

First, you need to create a class that represents the nodes of a balanced binary tree:

// 定义平衡二叉树的节点类
public class TreeNode {
    
    
    int value;
    TreeNode left;
    TreeNode right;

    // 构造方法
    public TreeNode(int value) {
    
    
        this.value = value;
        this.left = null;
        this.right = null;
    }
}

Then, you can create a class that represents a balanced binary tree and add a method to add data:

// 定义平衡二叉树类
public class AVLTree {
    
    
    TreeNode root;

    // 构造方法
    public AVLTree() {
    
    
        root = null;
    }

    // 添加数据的方法
    public void add(int value) {
    
    
        root = addRecursive(root, value);
    }

    // 递归添加数据的方法
    private TreeNode addRecursive(TreeNode current, int value) {
    
    
        if (current == null) {
    
    
            // 如果当前节点为空,则创建一个新节点作为根节点
            return new TreeNode(value);
        }

        // 根据值的大小决定添加到左子树还是右子树
        if (value < current.value) {
    
    
            current.left = addRecursive(current.left, value);
        } else if (value > current.value) {
    
    
            current.right = addRecursive(current.right, value);
        } else {
    
    
            // 如果值已经存在于树中,则不做任何操作
            return current;
        }

        // 更新当前节点的高度为左子树和右子树的最大高度+1
        current.height = 1 + Math.max(getHeight(current.left), getHeight(current.right));

        // 获取当前节点的平衡因子,检查是否失衡
        int balance = getBalance(current);

        // 如果当前节点失衡,则根据情况进行旋转操作
        if (balance > 1) {
    
    
            // 如果左子树的平衡因子大于1,则进行右旋操作
            return rightRotate(current);
        } else if (balance < -1) {
    
    
            // 如果右子树的平衡因子小于-1,则进行左旋操作
            return leftRotate(current);
        } else {
    
    
            // 如果平衡因子为0,则不做任何操作,返回当前节点指针
            return current;
        }
    }

    // 计算节点的高度的方法
    private int getHeight(TreeNode node) {
    
    
        if (node == null) {
    
    
            return 0;
        } else {
    
    
            return node.height;
        }
    }

    // 计算节点的平衡因子的方法(左子树高度-右子树高度)
    private int getBalance(TreeNode node) {
    
    
        if (node == null) {
    
    
            return 0;
        } else {
    
    
            return getHeight(node.left) - getHeight(node.right);
        }
    }
}

3. Delete data

In Java, a balanced binary tree is a common data structure that allows efficient organization and search of data. A balanced binary tree is a self-balancing binary search tree in which the absolute value of the height difference between the left subtree and the right subtree of any node does not exceed 1. AVL trees and red-black trees are two common types of balanced binary trees.

Here is an example showing how to delete data in a balanced binary tree:

First, you need to create a class that represents the nodes of a balanced binary tree:

// 定义平衡二叉树的节点类
public class TreeNode {
    
    
    int value;
    TreeNode left;
    TreeNode right;

    // 构造方法
    public TreeNode(int value) {
    
    
        this.value = value;
        this.left = null;
        this.right = null;
    }
}

Then, you can create a class that represents a balanced binary tree and add a method to remove data:

// 定义平衡二叉树类
public class AVLTree {
    
    
    TreeNode root;

    // 构造方法
    public AVLTree() {
    
    
        root = null;
    }

    // 删除数据的方法
    public void remove(int value) {
    
    
        root = removeRecursive(root, value);
    }

    // 递归删除数据的方法
    private TreeNode removeRecursive(TreeNode current, int value) {
    
    
        if (current == null) {
    
    
            // 如果当前节点为空,则不做任何操作,返回null
            return null;
        }

        // 根据值的大小决定从左子树还是右子树中删除数据
        if (value < current.value) {
    
    
            current.left = removeRecursive(current.left, value);
        } else if (value > current.value) {
    
    
            current.right = removeRecursive(current.right, value);
        } else {
    
    
            // 如果值等于当前节点的值,则根据子节点的情况进行处理
            if (current.left == null && current.right == null) {
    
    
                // 如果当前节点没有子节点,则直接删除当前节点,返回null
                return null;
            } else if (current.left == null) {
    
    
                // 如果当前节点只有右子节点,则直接删除当前节点,并返回右子节点作为新的节点
                return current.right;
            } else if (current.right == null) {
    
    
                // 如果当前节点只有左子节点,则直接删除当前节点,并返回左子节点作为新的节点
                return current.left;
            } else {
    
    
                // 如果当前节点有两个子节点,则找到右子树中的最小节点作为新的节点,并删除该最小节点
                TreeNode minNode = findMinNode(current.right);
                current.value = minNode.value;
                current.right = removeRecursive(current.right, minNode.value);
            }
        }

        // 更新当前节点的高度为左子树和右子树的最大高度+1
        current.height = 1 + Math.max(getHeight(current.left), getHeight(current.right));

        // 获取当前节点的平衡因子,检查是否失衡
        int balance = getBalance(current);

        // 如果当前节点失衡,则根据情况进行旋转操作
        if (balance > 1) {
    
    
            // 如果左子树的平衡因子大于1,则进行右旋操作
            return rightRotate(current);
        } else if (balance < -1) {
    
    
            // 如果右子树的平衡因子小于-1,则进行左旋操作
            return leftRotate(current);
        } else {
    
    
            // 如果平衡因子为0,则不做任何操作,返回当前节点指针
            return current;
        }
    }

    // 计算节点的高度的方法
    private int getHeight(TreeNode node) {
    
    
        if (node == null) {
    
    
            return 0;
        } else {
    
    
            return node.height;
        }
    }

    // 计算节点的平衡因子的方法(左子树高度-右子树高度)
    private int getBalance(TreeNode node) {
    
    
        if (node == null) {
    
    
            return 0;
        } else {
    
    
            return getHeight(node.left) - getHeight(node.right);
        }
    }
    // 找到右子树中的最小节点的方法(递归)
    private TreeNode findMinNode(TreeNode node) {
    
    
        if (node == null) {
    
    
            return null;
        } else if (node.left == null) {
    
    
            return node;
        } else {
    
    
            return findMinNode(node.left);
        }
    }
}

Guess you like

Origin blog.csdn.net/m0_62617719/article/details/132926700