AVLTree Java 实现

写在前面: 这是一份java实现代码,便于供自己以后参阅,笔者java功底弱,实现的只是一个demo没有很好的封装。

code

/**
 * 
 */
package oj;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.*;

import com.sun.xml.internal.ws.policy.EffectiveAlternativeSelector;

import sun.reflect.generics.tree.Tree;




/**
 * @author zouzhitao
 *
 */
public class Main {
    public final static int INF = 0x3f3f3f3f;
    public final static int MAXN = 100;
    public static Scanner in = new Scanner(new BufferedInputStream(System.in));
    public static avlTree tree = new avlTree();
    public static void main(String[] args) {
        int n=in.nextInt();
        while (n-->0) {
            tree.insert(in.nextInt());
        }
        System.out.println(tree.root.key);
    }
}

class Node{
    private int height;
    public Node left,right;
    public int key;
    public Node(int key) {
        super();
        this.key = key;
        height =1;
    }
    public Node() {
        super();
        height =1;
    }
    public static int height(Node node) {
        return node==null? 0 : node.height;
    }
    public int updateHeight() {
        return height = Math.max(height(this.left), height(this.right))+1;
    }
    public static int bf(Node node) {
        return node==null? 0:height(node.right)-height(node.left);
    }
}

class avlTree{
    public Node root;
    public int size;
    public avlTree() {
        super();
        size=0;
        root = null;
    }
    public avlTree(int val) {
        root = new Node(val);
        size++;
    }
    private Node rotateL(Node ptr) {
        Node subL = ptr;
        ptr = subL.right;
        subL.right = ptr.left;
        ptr.left = subL;
        subL.updateHeight();
        ptr.updateHeight();
        return ptr;
    }
    private Node rotateR(Node ptr) {
        Node subR = ptr;
        ptr = subR.left;
        subR.left = ptr.right;
        ptr.right = subR;
        subR.updateHeight();
        ptr.updateHeight();
        return ptr;
    }
    private Node rotateLR(Node ptr) {
        Node subL = ptr.left;
        Node subR = ptr;
        ptr = subL.right;
        subL.right = ptr.left;
        subR.left = ptr.right;
        ptr.left = subL;
        ptr.right = subR;
        subR.updateHeight();
        subL.updateHeight();
        ptr.updateHeight();
        return ptr;
    }
    private Node rotateRL(Node ptr) {
        Node subL = ptr;
        Node subR = ptr.right;
        ptr = subR.left;
        subL.right = ptr.left;
        subR.left = ptr.right;
        ptr.left = subL;
        ptr.right = subR;
        subL.updateHeight();
        subR.updateHeight();
        ptr.updateHeight();
        return ptr;
    }
    private Node insert(Node now,int val) {
        if(now.key>val){
            if(now.left==null)now.left = new Node(val);
            else now.left = insert(now.left, val);
        }else{
            if(now.right==null) now.right = new Node(val);
            else now.right =insert(now.right, val);
        }
        now.updateHeight();
        int bf = Node.bf(now);
        if(bf==2){
            int tmpBf = Node.bf(now.right);
            now = tmpBf==1?rotateL(now):rotateRL(now);
        }else if(bf==-2){
            int tmpBf = Node.bf(now.left);
            now = tmpBf==-1?rotateR(now):rotateLR(now);
        }
        return now;
    }
    public Node insert(int val) {
        if(root==null)root = new Node(val);
        else root = insert(root, val);
        size++;
        return root;
    }
}

教训

找了几次bug 才了解了java的引用,java中貌似没有 node * & tmp,这样的东西????每次涉及到引用的函数入口都必须手动返回函数中更改过的引用,然后在调用处重新赋值,e.g.:

root = insert(root, val);
private Node rotateR(Node ptr) {
        Node subR = ptr;
        ptr = subR.left;
        subR.left = ptr.right;
        ptr.right = subR;
        subR.updateHeight();
        ptr.updateHeight();
        return ptr;
    }

你可能感兴趣的:(算法&数据结构)