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

二叉树代码实现笔记

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

在节点中加入计数域

在节点中加入计数域后,算法的灵活度就得到许多改变。

加入计数域后的代码

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

class Node {
public:
    int key;
    Node *l;
    Node *r;
    int size; // = 树节点数
    Node(int _key): l(NULL), r(NULL), key(_key), size(1) {}
};

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

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 *joinR(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);
    // 从h中找到第k大的元素,返回元素的指针
    static Node *selectR(Node *h, int k);
    // 从h中找到第k大的元素,然后将其提升到树根部,返回更新后的树
    static Node *partR(Node *h, int k);
    // 将h树转换成平衡二叉树(不是完全平衡二叉树),返回平衡后的树
    static Node *balanceR(Node *h);
    // 组合任意的两棵树
    static Node *joinLR(Node *l, Node *r);
};

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);
}

int BTree::select(int k) {
    if (k < 0 || k >= root->size)
        return -1;
    Node *ret = selectR(this->root, k);
    return (ret == NULL) ? -1 : ret->key;
}

void BTree::kToRoot(int k) {
    if (k < 0 || k >= root->size) return;
    this->root = partR(root, k);
}

void BTree::balance() {
    this->root = balanceR(this->root);
}

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);
    }
    h->size++;
    return h;
}

Node *BTree::rotateR(Node *h) {
    if (h->l == NULL) return h;
    Node *x = h->l;
    h->l = x->r;
    x->r = h;
    // 重新计算旋转后h和x子树的大小
    h->size = 1; x->size = 1;
    if (h->l != NULL) h->size += h->l->size;
    if (h->r != NULL) h->size += h->r->size;
    if (x->l != NULL) x->size += x->l->size;
    if (x->r != NULL) x->size += x->r->size;
    return x;
}

Node *BTree::rotateL(Node *h) {
    if (h->r == NULL) return h;
    Node *x = h->r;
    h->r = x->l;
    x->l = h;
    // 重新计算旋转后h和x子树的大小
    h->size = 1; x->size = 1;
    if (h->l != NULL) h->size += h->l->size;
    if (h->r != NULL) h->size += h->r->size;
    if (x->l != NULL) x->size += x->l->size;
    if (x->r != NULL) x->size += x->r->size;
    return x;
}

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

Node *BTree::joinR(Node *l, Node *r) {
    if (r == NULL) return l;
    r = minToRoot(r); // 将r树的最小元素提升到根节点
    r->l = l;
    r->size += (r->l == NULL) ? 0 : r->l->size;
    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->size++;
        h = rotateR(h); // 右旋操作让左子树深度减一
    } else {
        h->r = insertT(h->r, key);
        h->size++;
        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 = joinR(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;
}

Node *BTree::selectR(Node *h, int k) {
    if (h == NULL) return NULL;
    int t = (h->l == NULL) ? 0 : h->l->size;
    if (t == k) return h;
    if (t > k) {
        return selectR(h->l, k);
    } else {
        return selectR(h->r, k-t-1);
    }
}

Node *BTree::partR(Node *h, int k) {
    int t = (h->l == NULL) ? 0 : h->l->size;
    if (t == k) return h;
    if (t > k) {
        h->l = partR(h->l, k);
        h = rotateR(h);
    } else {
        h->r = partR(h->r, k-t-1);
        h = rotateL(h);
    }
}

Node *BTree::balanceR(Node *h) {
    if (h == NULL || h->size < 2) return h; // 当元素个数小于2时候,不用平衡
    h = partR(h, h->size / 2); // 将中间元素提升到根部
    h->l = balanceR(h->l); // 平衡左子树
    h->r = balanceR(h->r); // 平衡右子树
    return h;
}

Node *BTree::joinLR(Node *l, Node *r) {
    if (l == NULL) return r;
    if (r == NULL) return l;
    r = insertT(r, l->key);
    r->l = joinLR(l->l, r->l);
    r->r = joinLR(l->r, r->r);
    delete l; // 冗余节点,所以需要删除
    return NULL;
}

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();

    cout << t.select(10) << endl;
    t.kToRoot(0);
    t.balance();
    return 0;
}

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

       

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