树的基础

表:

表的数据结构一般有数组和链表,链表有单链表和双链表。数组优势在于查询数据,链表优势在于插入和删除数据。

栈(Stack):

表的一种,先进后出的数据结构,所有的操作都在栈顶完成,且只有栈顶元素是可见的。常用的方法是push(进栈)、pop(出栈)、top(检测是否处于栈顶)。对空栈进行pop和top一般认为是错误的,但在空间用尽时push是一个实现限制,不是错误。

栈的应用(平衡符号):编译器判断括号的输入语法错误。首先做一个空栈,依次读入字符串。若字符串是开放符号,则加入栈中。如果是闭符号,若当前栈为空,报错;否则,将栈顶元素弹出,若非对应,报错。文件末尾,栈非空,报错。

队列(queue):

队列也是表,先进先出。在表的末端插入元素,表的前端删除元素。

树的遍历:

先序遍历(节点 -> 左 -> 右):ABDHIECFJKG

中序遍历(左 -> 节点 -> 右): HDIBEAJFKCG

后序遍历(左 -> 右 -> 节点):HIDEBJKFGCA

二叉查找树:

对于树中的每一个节点X,左子树中的所有项的值小于X中的项,右子树中的项的值大于X中的项。

创建:

public class BinarySearchTree<AnyType> {
    AnyType element;
    BinarySearchTree left;
    BinarySearchTree right;
    
    public BinarySearchTree(AnyType element,BinarySearchTree<AnyType> left ,BinarySearchTree<AnyType> right){
        this.element = element;
        this.left = left;
        this.right = right;
    }
}

findMin方法:

private BinarySearchTree<AnyType> finMin(BinarySearchTree<AnyType> t) {
    if(t == null){
        reture null;
    }
    if(t.left == null){
        return t;
    }
    return finMin(t.left);
}

finMax方法:

private BinarySearchTree<AnyType> finMax(BinarySearchTree<AnyType> t) {
    if(t == null){
        reture null;
    }
    if(t.right == null){
        return t;
    }
    return finMax(t.right);
}

 

contains方法:

private boolean contains(AnyType x ,BinarySearchTree<AnyType> t) {
    if(t == null){
        return false;
    }
    //compareTo方法:int i = 2.compare(3),那么i<0;
    int compareResult = x.compareTo(t.element);
    if(i < 0){
        return contains(x,left);
    }else if(i > 0){
        return contains(x,right);
    }else{
        return true;
    }
}

insert方法:

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

remove方法:

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

 二叉树:

 二叉树是每个节点最多有两个子树的树结构。通常子树被称作“左子树”(left subtree)和“右子树”(right subtree)。二叉树常被用于实现二叉查找树和二叉堆。

 1> 二叉树中第i层最多有2^(i-1)个节点

 2> 深度为k的二叉树最多有2^k - 1个节点。

 3> 若页节点树为N0,度为2的节点树为N2,则有N0 = N2 + 1;(度:该性质只是针对节点而言,一个节点有n个子节点,则该节点的度为n。 树的度为树中最大的结点度)。

特别的二叉树:

满二叉树:除最后一层无任何子节点外,每一层上的所有结点都有两个子结点(最后一层上的无子结点的结点为叶子节点)。也可以这样理解,除叶子结点外的所有结点均有两个子结点。节点数达到最大值。所有叶子结点必须在同一层上.

完全二叉树:若设二叉树的深度为h,除第 h 层外,其它各层 (1~h-1) 的结点数都达到最大个数,第 h 层所有的结点都连续集中在最左边,这就是完全二叉树。完全二叉树是由满二叉树而引出来的。对于深度为K的,有N个结点的二叉树,当且仅当其每一个结点都与深度为K的满二叉树中编号从1至n的结点一一对应时称之为完全二叉树。    

 

 满二叉树一定是完全二叉树,完全二叉树不一定是满二叉树。

 平衡二叉树:带有平衡条件的二叉查找树。

它是一棵空树或它的左右两个子树的高度差的绝对值不超过1,并且左右两个子树都是一棵平衡二叉树。常用算法有红黑树、AVL、Treap、伸展树等。在平衡二叉搜索树中,我们可以看到,其高度一般都良好地维持在O(log(n)),大大降低了操作的时间复杂度。

 

posted @ 2017-04-14 00:52  小猪阿正  阅读(186)  评论(0编辑  收藏  举报