Tree

depth:n的depth为从root到n的路径的长度
height:n的height为从n到一个leaf的最长路径长度。树的height等于root的depth

preorder traversal: 遍历顺序,根,左子树,右子树
postorder traversal: 遍历顺序,左子树,右子树,根
inorder traversal: 遍历顺序, 左子树,根,右子树

  • Bineary search tree
    average depth = O(logN)
    worst case depth is O(N)
    implementation:
struct BinaryNode
{
    Object element; //The data in the node
    BinaryNode *left; //Left child
    BinaryNode *right; //Right child
};

contains:

/*Internam method to test if an item is in a subtree
  * x is itemto search for.
  * t is the node that roots the subtree
*/
bool contains(const Comparable & x, BinaryNode *t) const
{
    if (t == NULL) return false;
    else if (x < t->element) return contains(x, t->left);
    else if (x > t->element) return contains(x, t->right);
    else return true; //match
}

findMin and findMax (recursion and iteration implementation):

//recursion, return node containing the smallest item
BinaryNode * findMin(BinaryNode *t) const
{
    if(t == NULL) return NULL;
    if(t->left == NULL) return t;
    return findMin(t->left);
}
//iteration, return node containing the largest item
BinaryNode * findMax(BinaryNode *t) const
{
    if(t != NULL)
    {
        while (t->right != NULL)
        {
          t = t->right;
        }
    }
    return t;
}

insert:

/* x is the item to nsert
   t is the node that roots the subtree
   set the new root of the subtree
*/
void insert(const Comparable & x, BinaryNode * &t)
{
    if(t == NULL) t = new BinaryNode(x, NULL, NULL);
    else if (x < t->element) insert(x, t->left);
    else if (x > t->element) insert(x, t->right);
    else ; //duplicate, do nothing
}

remove:
需要考虑多种情况。
如果结点是树叶,可以直接删除。
如果结点有一个child,删除后将child与父结点连接,如下


Paste_Image.png

如果结点有两个child,一般的删除策略是用其右字数中的最小数据代替该节点并递归地删除那个节点。例子如下


Paste_Image.png

实现:
//效率不高,因为沿树两次搜索来查找和删除右子树中的最小节点
//如要提高效率,需要写一个特殊的removeMin  
void remove(const Comparable & x, BinaryNode * &t)
{
    if(t == NULL) return; //item not found, do nothing
    if(x < t->element) remove(x,t->left);
    else if(x > t->element) remove(x,t->right);
    else if(t->left != NULL && t->right != NULL) //Two children
    {
        t->element = findMin(t->right)->element;
        remove(t->element,t->right);
    } 
    else //node has only one child
    {
        BinaryNode *oldNode = t;
        t = (t->left != NULL) ? t->left : t->right;
        delete oldNode;
    }
}

makeEmpty and destructor:

~BinarySearchTree()
{
    makeEmpty();
}
void makeEmpty(BinaryNode * &t)
{
    if (t != NULL)
    {
        makeEmpty(t->left);
        makeEmpty(t->right);
        delete t;
    }
    t = NULL; //important
}

operator= and clone:

//deep copy
const BinarySearchTree & operator=(const BinarySearchTree &rhs)
{
    if(this != &rhs)
    {
        makeEmpty();
        root = clone(rhs.root);
    }
    return *this;
}
//method to clone subtree
BinaryNode *clone(BinaryNode *t) const
{
    if(t == NULL) return NULL;
    return new BinaryNode(t->element,clone(t->left),clone(t->right));
}
  • Complexity analysis:
    average case:
    O(logN) for all operation
    actually O(d) for all operations except makeEmpty and operator=
    d = depth including the node visited
    worst case: O(N)

  • Tree traversal:
    O(N) complexity
    print tree in sorted order:

//inorder
void printTree(BinaryNode *t) const
{
    if(t != NULL)
    {
        printTree(t->left);
        cout << t->element << endl;
        printTree(t->right);
    }
}

compute height:

int height(BinaryNode *t)
{
    if(t == NULL) return -1;
    else return 1 + max(height(t->left),height(t->right));
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 背景 一年多以前我在知乎上答了有关LeetCode的问题, 分享了一些自己做题目的经验。 张土汪:刷leetcod...
    土汪阅读 12,769评论 0 33
  • 1. AVL树 AVL树简单来说是带有平衡条件的二叉查找树.传统来说是其每个节点的左子树和右子树的高度最多差1(注...
    fredal阅读 1,844评论 0 4
  • 94. Binary Tree Inorder Traversal 中序 inorder:左节点->根节点->右节...
    Morphiaaa阅读 584评论 0 0
  • 每当清晨第一缕阳光打在露珠上的时候总会想起马尔江那句“妈妈,你看小草哭了”。 有人说要想追寻生命的意义,就是要任性...
    方辰阅读 910评论 2 4
  • 看到城楼的时候,惊觉它的矮小,只约三四米高,不似在影视中见到的高大威严。它真实的落在眼前,登楼不需几分,登高瞰半城...
    目觉阅读 510评论 2 3