java avl tree

package com.gxmedu.avltree;


import com.gxmedu.binary_sort_tree.BinarySortTree;

/**
 * @author 郭学明
 * @version 1.0
 */
public class AVLTree {
    public static void main(String[] args) {
        AVLTree t1 = new AVLTree();
        Node node1 = t1.new Node(1);
        Node node2 = t1.new Node(7);
        Node node3 = t1.new Node(3);
        Node node4 = t1.new Node(9);
        Node node5 = t1.new Node(88);
        Node node6 = t1.new Node(92);
        Node node7 = t1.new Node(13);
        Node node8 = t1.new Node(27);
        t1.addNode(node1);
        t1.addNode(node2);
        t1.addNode(node3);
        t1.addNode(node4);
        t1.addNode(node5);
        t1.addNode(node6);
        t1.addNode(node7);
        t1.addNode(node8);
        System.out.println(t1.root);
//        t1.inorder();
//        System.out.println(t1.root.height());
//        System.out.println(t1.root.leftHeight());
//        System.out.println(t1.root.rightHeight());

    }
    private Node root;
    public class Node{
        private int value;
        private Node left;
        private Node right;

        public Node(int value) {
            this.value = value;
        }

        public int getValue() {
            return value;
        }

        public void setValue(int value) {
            this.value = value;
        }

        public Node getLeft() {
            return left;
        }

        public void setLeft(Node left) {
            this.left = left;
        }

        public Node getRight() {
            return right;
        }

        public void setRight(Node right) {
            this.right = right;
        }

        @Override
        public String toString() {
            return "Node{" +
                    "value=" + value +
                    '}';
        }
        public void addNode(Node node){
            if(this.value >= node.value){
                if(this.left == null){
                    this.left = node;
                }else{
                    this.left.addNode(node);
                }
            }else{
                if(this.right == null){
                    this.right = node;
                }else{
                    this.right.addNode(node);
                }
            }
            if(this.leftHeight() - this.rightHeight() > 1 ){
                /**
                 *  右旋要保证左子树a的左子树高度要大于a的右子树,否则要对a的左子树先进行一次左旋
                 */
                if(this.left.rightHeight() > this.left.leftHeight() ){
                    this.left.leftRotate();
                }
                this.rightRotate();
                return;
            }
            if(this.rightHeight() - this.leftHeight() > 1){
                /**
                 *  左旋要保证右子树a的右子树高度要大于a的左子树,否则要对a的右子树进行一次右旋
                 */
                if(this.right.leftHeight() > this.right.rightHeight()){
                    this.right.rightRotate();
                }
                this.leftRotate();
            }
        }
        public void inorder(){
            if(this.left != null){
                this.left.inorder();
            }
            System.out.println(this);
            if(this.right != null){
                this.right.inorder();
            }
        }
        /**
         *  下面对二叉排序树进行删除操作
         *  1.第一个方法确定要删除的结点是否存在
         *  2.第二个方法确定要删除的结点的父节点
         *  因为方法中用到递归所以不能用if(){return;}这样的结构直接结束方法
         *   public Node search(int value){
         *             if(this.value == value){
         *                 return this;
         *             }
         *             if(this.left != null && this.value > value ){
         *                 this.left.search(value);
         *             }else if(this.right != null && value > this.value){
         *                 this.right.search(value);
         *             }
         *             return null;
         *         }
         *
         *
         */
        public Node search(int value){
            if(this.value == value){
                return this;
            }
            if(this.left != null && this.value > value ){
                return this.left.search(value);
            }else if(this.right != null && value > this.value){
                return this.right.search(value);
            }
            return null;
        }
        public Node searchParent(Node node){
            if(node == root){
                return null;
            }
            if(this.left != null && this.left.value == node.value ||
                    this.right != null && this.right.value == node.value){
                return this;
            }else if(this.left != null && node.value < this.value){
                return this.left.searchParent(node);
            }else if(this.right != null && node.value > this.value){
                return this.right.searchParent(node);
            }
            return null;
        }
        public int height(){
            return Math.max(this.left == null ? 0:this.left.height(),this.right == null ? 0:this.right.height()) + 1;
        }
        public int leftHeight(){
            return this.left == null ? 0 : this.left.height();
        }
        public int rightHeight(){
            return this.right == null ? 0 : this.right.height();
        }
        private void leftRotate(){
            Node pivot = new Node(this.value);
            pivot.left = this.left;
            pivot.right = this.right.left;
            this.value = this.right.value;
            this.right = this.right.right;
            this.left = pivot;
        }
        private void rightRotate(){
            Node pivot = new Node(this.value);
            pivot.right = this.right;
            pivot.left = this.left.right;
            this.value = this.left.value;
            this.left = this.left.left;
            this.right = pivot;
        }
    }
    public void addNode(Node newNode){
        if(root == null){
            root = newNode;
            return;
        }
        root.addNode(newNode);
    }
    public void inorder(){
        if(root == null){
            System.out.println("树为空");
            return;
        }
        root.inorder();
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值