自己实现一个简单的AVL树

需求

自己实现一个简单的AVL-Tree。

包含功能

  • insert
  • remove
  • findMax
  • findMin
  • toString

  基本功能与之前实现的BinarySearchTree相同,不过avl树需要保证树的平衡,每一个节点的左右子树的高度差不超过1.所以在每次插入或删除的时候要对树进行调整。

代码

import java.util.NoSuchElementException;
import java.util.StringJoiner;

public class AvlTree<T extends Comparable<? super T>> {
    private AvlNode<T> root;
    private static final int ALLOWED_IMBLANCE = 1;

    public AvlTree() {
    }

    public AvlTree(T[] arr) {
        for (T t : arr) {
            insert(t);
        }
    }

    public void makeEmpty() {
        root = null;
    }

    public boolean isEmpty() {
        return root == null;
    }

    public void insert(T t) {
        root = insert(t, root);
    }

    public T findMax() {
        return findMax(root).element;
    }

    public T findMin() {
        return findMin(root).element;
    }

    public void remove(T t) {
        root = remove(t, root);
    }

    private AvlNode<T> findMax(AvlNode<T> node) {
        if (node == null)
            throw new NoSuchElementException();
        if (node.right != null)
            return findMax(node.right);
        return node;
    }

    private AvlNode<T> findMin(AvlNode<T> node) {
        if (node == null)
            throw new NoSuchElementException();
        while (node.left != null)
            node = node.left;
        return node;
    }

    private int height(AvlNode<T> node) {
        return node == null ? -1 : node.height;
    }

    private int heightDiff(AvlNode<T> a, AvlNode<T> b) {
        return height(a) - height(b);
    }

    private AvlNode<T> insert(T t, AvlNode<T> node) {
        if (node == null)
            return new AvlNode<T>(t);
        int cmpRes = t.compareTo(node.element);
        if (cmpRes < 0)
            node.left = insert(t, node.left);
        else if (cmpRes > 0)
            node.right = insert(t, node.right);
        return balance(node);
    }

    private AvlNode<T> remove(T t, AvlNode<T> node) {
        if (node == null)
            return node;
        int cmpRes = t.compareTo(node.element);
        if (cmpRes < 0) {
            node.left = remove(t, node.left);
        } else if (cmpRes > 0) {
            node.right = remove(t, node.right);
        } else if (node.left != null && node.right != null) {
            node.element = findMin(node.right).element;
            node.right = remove(node.element, node.right);
        } else
            node = node.left != null ? node.left : node.right;
        return balance(node);
    }

    private AvlNode<T> balance(AvlNode<T> node) {
        if (node != null) {
            if (heightDiff(node.left, node.right) > ALLOWED_IMBLANCE) {
                if (heightDiff(node.left.left, node.left.right) >= 0)
                    node = rotateWithLeftChild(node);
                else
                    node = doubleWithLeftChild(node);
            } else if (heightDiff(node.right, node.left) > ALLOWED_IMBLANCE) {
                if (heightDiff(node.right.right, node.right.left) >= 0)
                    node = rotateWithRightChild(node);
                else
                    node = doubleWithRightChild(node);
            }
            node.height = Math.max(height(node.left), height(node.right)) + 1;
        }
        return node;
    }

    private AvlNode<T> rotateWithLeftChild(AvlNode<T> n1) {
        AvlNode<T> n2 = n1.left;
        n1.left = n2.right;
        n2.right = n1;
        n1.height = Math.max(height(n1.left), height(n1.right)) + 1;
        n2.height = Math.max(height(n2.left), n1.height) + 1;
        return n2;
    }

    private AvlNode<T> rotateWithRightChild(AvlNode<T> n1) {
        AvlNode<T> n2 = n1.right;
        n1.right = n2.left;
        n2.left = n1;
        n1.height = Math.max(height(n1.left), height(n1.right)) + 1;
        n2.height = Math.max(height(n2.right), n1.height) + 1;
        return n2;
    }

    private AvlNode<T> doubleWithLeftChild(AvlNode<T> node) {
        node.left = rotateWithRightChild(node.left);
        return rotateWithLeftChild(node);
    }

    private AvlNode<T> doubleWithRightChild(AvlNode<T> node) {
        node.right = rotateWithLeftChild(node.right);
        return rotateWithRightChild(node);
    }

    @Override
    public String toString() {
        StringJoiner joiner = new StringJoiner(" ", "[", "]");
        listAll(this.root, joiner);
        return joiner.toString();
    }

    private void listAll(AvlNode<T> node, StringJoiner joiner) {
        showNode(node);
        if (node.left != null)
            listAll(node.left, joiner);
        joiner.add(node.element.toString());
        if (node.right != null)
            listAll(node.right, joiner);
    }

    private void showNode(AvlNode<T> node) {
        System.out.printf("本节点:%s [左子:%s,右子:%s]\n", nodeValue(node), nodeValue(node.left), nodeValue(node.right));
    }

    private String nodeValue(AvlNode<T> node) {
        if (node == null)
            return "null";
        return node.element.toString();
    }

    private static class AvlNode<T> {
        T element;
        AvlNode<T> left;
        AvlNode<T> right;
        int height;

        public AvlNode(T element) {
            this(element, null, null);
        }

        public AvlNode(T element, AvlNode<T> left, AvlNode<T> right) {
            this.element = element;
            this.left = left;
            this.right = right;
            this.height = 0;
        }
    }
}
好的,我可以帮您实现一个 C AVL树。以下是代码示例: ``` #include <stdio.h> #include <stdlib.h> typedef struct AVLNode { int data; int height; struct AVLNode *left; struct AVLNode *right; } AVLNode; int max(int a, int b) { return (a > b) ? a : b; } int height(AVLNode *node) { if (node == NULL) { return ; } return node->height; } AVLNode *newNode(int data) { AVLNode *node = (AVLNode *) malloc(sizeof(AVLNode)); node->data = data; node->height = 1; node->left = NULL; node->right = NULL; return node; } AVLNode *rightRotate(AVLNode *y) { AVLNode *x = y->left; AVLNode *T2 = x->right; x->right = y; y->left = T2; y->height = max(height(y->left), height(y->right)) + 1; x->height = max(height(x->left), height(x->right)) + 1; return x; } AVLNode *leftRotate(AVLNode *x) { AVLNode *y = x->right; AVLNode *T2 = y->left; y->left = x; x->right = T2; x->height = max(height(x->left), height(x->right)) + 1; y->height = max(height(y->left), height(y->right)) + 1; return y; } int getBalance(AVLNode *node) { if (node == NULL) { return ; } return height(node->left) - height(node->right); } AVLNode *insert(AVLNode *node, int data) { if (node == NULL) { return newNode(data); } if (data < node->data) { node->left = insert(node->left, data); } else if (data > node->data) { node->right = insert(node->right, data); } else { return node; } node->height = 1 + max(height(node->left), height(node->right)); int balance = getBalance(node); if (balance > 1 && data < node->left->data) { return rightRotate(node); } if (balance < -1 && data > node->right->data) { return leftRotate(node); } if (balance > 1 && data > node->left->data) { node->left = leftRotate(node->left); return rightRotate(node); } if (balance < -1 && data < node->right->data) { node->right = rightRotate(node->right); return leftRotate(node); } return node; } void preOrder(AVLNode *root) { if (root != NULL) { printf("%d ", root->data); preOrder(root->left); preOrder(root->right); } } int main() { AVLNode *root = NULL; root = insert(root, 10); root = insert(root, 20); root = insert(root, 30); root = insert(root, 40); root = insert(root, 50); root = insert(root, 25); printf("Preorder traversal of the constructed AVL tree is: \n"); preOrder(root); return ; } ``` 这是一个简单AVL实现,包括插入和前序遍历。您可以根据需要进行修改和扩展。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值