手机版
你好,游客 登录 注册
背景:
阅读新闻

二叉树代码实现笔记

[日期:2014-02-05] 来源:Linux社区  作者:minghe_uestc [字体:大 中 小]

特殊的插入(根插入)

Node *BTree::insertT(Node *h, int key) {
    if (h == NULL) return new Node(key);
    if (h->key > key) {
        h->l = insertT(h->l, key);
        h = rotateR(h); // 右旋操作让左子树深度减一
    } else {
        h->r = insertT(h->r, key);
        h = rotateL(h);
    }
    return h;
}

这个代码可以帮助更好的理解左旋与右旋操作,向左子树插入,则通过右旋可以让左子树深度减一,反之亦然。最后插入的元素会不断的上浮返回到根部,所以称作为根插入。

删除节点

Node *BTree::deleteR(Node *h, int key) {
    if (h == NULL) return h;
    if (h->key == key) { // 关键字相同
        Node *x = h;
        h = join(h->l, h->r); // 合并左右两个子树
        delete x;
    } else if (h->key < key) {
        h->r = deleteR(h->r, key); // 在右子树中删除key
    } else {
        h->l = deleteR(h->l, key); // 在左子树中删除key
    }
    return h;
}

删除操作比较复杂,但是如果可以理解之前的join操作,那就很容易理解这个代码:

利用join操作将删除节点后空余出来的两个子树合并成一棵树,这样就解决了删除时候复杂的操作了。

小结

二叉树是所有树中最简单的,其实现也很容易理解,只要能够体会这些操作中的递归操作的语义即可。

普通二叉树代码

#include <iostream>
#include <algorithm>
using namespace std;

class Node {
public:
    int key;
    struct Node *l;
    struct Node *r;
    Node(int _key): l(NULL), r(NULL), key(_key) {}
};

class BTree {
public:
    BTree() : root(NULL) {}
    void insert(int key);
    void insertRoot(int key);
    void deleteNode(int key);
    void print();

private:
    Node *root;

private:
    // 向h树指向的树中添加元素key,返回更新后的树
    static Node *insertR(Node *h, int key);
    // 右旋操作,可以令左子树深度减一,返回旋转后的树
    static Node *rotateR(Node *h);
    // 左旋操作,可以令右子树深度减一,返回旋转后的树
    static Node *rotateL(Node *h);
    // 将h树最小的元素上浮到树根部,返回最后上浮后的树
    static Node *minToRoot(Node *h);
    // 将两棵树合并成一棵树,返回合并后的树
    static Node *join(Node *l, Node *r);
    // 向h树中插入元素,并且插入的元素会存在树根部,返回插入后的树
    static Node *insertT(Node *h, int key);
    // 中序遍历h树
    static void inOrder(Node *h);
    // 从h树中删除key,返回删除后的树
    static Node *deleteR(Node *h, int key);
};

void BTree::insert(int key) {
    this->root = insertR(this->root, key);
}

void BTree::insertRoot(int key) {
    this->root = insertT(this->root, key);
}

void BTree::print() {
    inOrder(this->root);
}

void BTree::deleteNode(int key) {
    this->root = deleteR(this->root, key);
}

Node *BTree::insertR(Node *h, int key) {
    if (h == NULL) return new Node(key);
    if (h->key < key) {
        h->r = insertR(h->r, key);
    } else {
        h->l = insertR(h->l, key);
    }
    return h;
}

Node *BTree::rotateR(Node *h) {
    if (h->l == NULL) return h;
    Node *x = h->l;
    h->l = x->r;
    x->r = h;
    return x;
}

Node *BTree::rotateL(Node *h) {
    if (h->r == NULL) return h;
    Node *x = h->r;
    h->r = x->l;
    x->l = h;
    return x;
}

Node *BTree::minToRoot(Node *h) {
    if (h->l != NULL)  {
        h->l = minToRoot(h->l); // 将最小元素提升到左子树的根部
        h = rotateR(h); // 将左子树中最小元素提升到根部
    }
    return h;
}

Node *BTree::join(Node *l, Node *r) {
    if (r == NULL) return l;
    r = minToRoot(r); // 将r树的最小元素提升到根节点
    r->l = l;
    return r;
}

Node *BTree::insertT(Node *h, int key) {
    if (h == NULL) return new Node(key);
    if (h->key > key) {
        h->l = insertT(h->l, key);
        h = rotateR(h); // 右旋操作让左子树深度减一
    } else {
        h->r = insertT(h->r, key);
        h = rotateL(h);
    }
    return h;
}

void BTree::inOrder(Node *h) {
    if (h == NULL) return;
    inOrder(h->l);
    cout << h->key << " ";
    inOrder(h->r);
}

Node *BTree::deleteR(Node *h, int key) {
    if (h == NULL) return h;
    if (h->key == key) { // 关键字相同
        Node *x = h;
        h = join(h->l, h->r); // 合并左右两个子树
        delete x;
    } else if (h->key < key) {
        h->r = deleteR(h->r, key); // 在右子树中删除key
    } else {
        h->l = deleteR(h->l, key); // 在左子树中删除key
    }
    return h;
}

int main() {
    int a[] = {3, 2, 1, 4, 9, 8, 6, 7, 5};
    BTree t;
    for (int i = 0; i < 9; i++)
        t.insert(a[i]);

    t.insertRoot(4);
    t.print();
    cout << endl;
    t.deleteNode(4);
    t.print();
    return 0;
}

linux
相关资讯       二叉树 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款