-樹

正文

  • 二叉樹
    定義 : 其中每個幾點都不能有多餘兩個的兒子
public class BinaryNode<AnyType> {

    public BinaryNode(AnyType element) {
        this(element, null, null);
    }

    public BinaryNode(AnyType element, BinaryNode left, BinaryNode right) {
        this.element = element;
        this.left = left;
        this.right = right;
    }

    AnyType element;
    BinaryNode left;
    BinaryNode right;
}
  • 二叉查找樹
    定義 : 一種特殊的二叉樹 , 其中每個幾點都不能有多餘兩個的兒子.
public class BinarySearchTree<AnyType extends Comparable<? super AnyType>> {

    public static class BinaryNode<AnyType> {

        public BinaryNode(AnyType element) {
            this(element, null, null);
        }

        public BinaryNode(AnyType element, BinaryNode left, BinaryNode right) {
            this.element = element;
            this.left = left;
            this.right = right;
        }

        AnyType element;
        BinaryNode left;
        BinaryNode right;
    }

    private BinaryNode<AnyType> root;

    public BinarySearchTree() {
        root = null;
    }

    public void makeEmpty() {
        root = null;
    }

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

    public boolean contains(AnyType x) {
        return contains(x, root);
    }

    public AnyType findMin() {
        if (isEmpty()) {
            throw new BufferUnderflowException();
        }
        return findMin(root).element;
    }

    public AnyType findMax() {
        if (isEmpty()) {
            throw new BufferUnderflowException();
        }
        return findMax(root).element;
    }

    public void insert(AnyType x) {
        root = insert(x , root);
    }

    public void remove(AnyType x) {
        root = remove(x , root);
    }

    private boolean contains(AnyType x , BinaryNode<AnyType> t ) {
        if (t == null) {
            return false;
        }

        int compareResult = x.compareTo(t.element);

        if (compareResult < 0) {
            return contains(x, t.left);
        } else {
            return compareResult <= 0 || contains(x, t.right);
        }
    }

    private BinaryNode<AnyType> findMin(BinaryNode<AnyType> t) {
        if (t == null) {
            return null;
        } else if (t.left == null) {
            return t;
        }
        return findMin(t.left);
    }

    private BinaryNode<AnyType> findMax(BinaryNode<AnyType> t) {
        if (t != null) {
            while(t.right != null) {
                t = t.right;
            }
        }
        return t;
    }

    private BinaryNode<AnyType> insert(AnyType x , BinaryNode<AnyType> t) {
        if (t == null) {
            return new BinaryNode<AnyType>(x , null , null);
        }
        int compareResult = x.compareTo(t.element);
        if (compareResult < 0) {
            t.left = insert(x , t.left);
        } else if (compareResult > 0) {
            t.right = insert(x , t.right);
        }
        return t;
    }

    private BinaryNode<AnyType> remove(AnyType x , BinaryNode<AnyType> t) {
        if (t == null) {
            return t;
        }
        int compareResult = x.compareTo(t.element);
        if (compareResult < 0) {
            t.left = remove(x , t.left);
        } else if (compareResult > 0) {
            t.right = remove(x , t.right);
        } else if (t.left != null && t.right != null) {
            t.element = (AnyType) findMin(t.right).element;
            t.right = remove(t.element , t.right);
        } else {
            t = (t.left != null) ? t.left : t.right;
        }
        return t;
    }

    public void printTree() {
        if (isEmpty()) {
            System.out.print("Empty Tree");
        } else {
            printTree(root);
        }
    }

    private void printTree(BinaryNode<AnyType> t) {
        if (t != null) {
            printTree(t.left);
            System.out.print(t.element);
            printTree(t.right);
        }
    }

    private int height(BinaryNode<AnyType> t) {
        if (t == null) {
            return -1;
        } else {
            return 1 + Math.max(height(t.left) , height(t.right));
        }
    }
}
  • AVL樹
    定義 : 帶有平衡條件的二叉查找樹
public class AvlNode<AnyType> {

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

    public AvlNode(AnyType element, AvlNode<AnyType> left, AvlNode<AnyType> right) {
        this.element = element;
        this.left = left;
        this.right = right;
        this.height = 0;
    }

    AnyType element;
    AvlNode<AnyType> left;
    AvlNode<AnyType> right;
    int height;

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

    private AvlNode<AnyType> insert(AnyType x, AvlNode<AnyType> t) {
        if (t == null) {
            return new AvlNode<AnyType>(x, null, null);
        }
        int compareResult = compare(x, t.element);

        if (compareResult < 0) {
            t.left = insert(x, t.left);
            if (height(t.left) - height(t.right) == 2) {
                if (compare(x, t.left.element) < 0) {
                    t = rotateWithLeftChild(t);
                } else {
                    t = doubleWithLeftChild(t);
                }
            }
        } else if (compareResult > 0) {
            t.right = insert(x, t.right);
            if (height(t.right) - height(t.left) == 2) {
                if (compare(x, t.right.element) < 0) {
                    t = rotateWithRightChild(t);
                } else {
                    t = doubleWithRightChild(t);
                }
            }
        } else {
            t.height = Math.max(height(t.left), height(t.right)) + 1;
        }
        return t;
    }

    private AvlNode<AnyType> rotateWithLeftChild(AvlNode<AnyType> k2) {
        AvlNode<AnyType> k1 = k2.left;
        k2.left = k1.right;
        k1.right = k2;
        k2.height =  Math.max(height(k2.left), height(k2.right)) + 1;
        k1.height =  Math.max(height(k1.left), k2.height) + 1;
        return k1;
    }

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

標準庫的集合與映射

  • ArrayList和LinkedList查找效率很低.
  • ArrayList和LinkedList的區別 :
    1. ArrayList提供了List ADT可增長數組的實現,使用ArrayList的優點在於,對get和set的調用花費常數時間.其缺點是新項的插入和現有項的刪除代價昂貴.
    2. LinkedList提供了List ADT雙向鏈表的實現.新項的插入和現有項的刪除均開銷很小,缺點是不易做索引,對get的調用是昂貴的.
  • Set接口代表不重複元素的Collection. 有序狀態的Set實現是TreeSet.
  • Map是一個接口,代表由關鍵字以及它們的值組成的一些項的集合.有序狀態map的實現是TreeMap.
發佈了82 篇原創文章 · 獲贊 3 · 訪問量 7萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章