<数据结构>-树

正文

  • 二叉树
    定义 : 其中每个几点都不能有多余两个的儿子
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.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值