求众数的算法研究

求众数是一个古老的问题。众数:是一组数据中出现次数最多的数值。求众数的主要算法有:
1,hash表 时间复杂度为O(n),但空间极大,通常让人难以承受
2,排序 对元素表进行排序,然后统计元素出现的个数,得出众数。时间复杂度为O(nlgn),空间复杂度为O(n)
3,二叉搜索树 用rbtree之类的树来实现。如果实现的好,复杂度和排序接近。

这三种方法各有所长,但是都有一些问题。所以最近我脑洞大开,想扩张二叉搜索树以实现更简单、更高效的众数算法。这个算法的复杂度约为O(nlgn),但是实际来看,效率比普通的二叉树实现效率高得多。看一下简单的性能测试(随机数据):

  • Core(TM) i3-3240T 2.90GHz
  • 4.00GB
  • Windows 7 32位
数据量/n MyTreeTimes/ms std::mapTimes/ms pbds::rbtreeTimes/ms std::sortTimes/ms
100000 15 78 93 32
1000000 143 811 936 343
10000000 1435 8253 9397 4040

可见,这种方法比现有的树算法优势明显,对于排序方法也有一定优势(当然这也有手写快于封装的因素)。但这还只是随机数据的测试,如果数据是特殊的(众数出现次数很多),效率会更高。

好了关子卖完了,这种算法的思路很简(ju)单(ruo):对于一个bst,每一个node记录两个值:key和times(数字和出现的次数)。bst基于key构建,而每次插入时,一旦当前节点子树的times大于当前节点的times,就把子树上旋。经过多次插入后,根的key即是一个众数,times即是它出现的次数。

对于这种树(下面称ModeTree)的定义是:

  1. 空树是ModeTree
  2. 一个对{key, times, leftchild, rightchild}称为一个节点
  3. 如果对于任意一个节点N,N的左孩子是空树或者左孩子的key小于N的key,N的右节点是空树或者右孩子的key大于N的key,且N的times大于或等于它的孩子的times,则N是ModeTree

如果还是不太懂,我们就把需要的知识复习一下。

0、数据结构
普通二叉树多一个times值、、这个没什么问题,直接上代码

typedef struct node;
typedef node *tree;

struct node {
       int key;
       /*这个是数字本身*/
       int times;
       /*出现的次数*/
       tree lc, rc;
       /*左右孩子*/
};

tree root;

1、treap的左右旋转
这里的思想和treap有点接近,而且旋转操作和treap是一样一样的。可以参考我的其他文章http://blog.csdn.net/oiljt12138/article/details/50411996
在这里用一幅图解释左右旋转(注意具体方法):
这里写图片描述

代码给出一个实例:

void liftLeftChild(tree &node)
{
       tree tmp = node->lc;
       node->lc = tmp->rc;
       tmp->rc = node;
       node = tmp;
       tmp = NULL;
} /*左节点上旋*/

void liftRightChild(tree &node)
{
    tree tmp = node->rc;
    node->rc = tmp->lc;
    tmp->lc = node;
    node =tmp;
    tmp = NULL;
} /*右节点上旋*/

2、左右旋转在什么时候做
左右旋转是为了维护性质三中 孩子的times比父母小(即越靠近根出现次数越多)这一性质。那么什么时候旋转最合适呢?显然如果专门来维护性质很不划算。
我们考虑什么能破坏性质:显然,只有插入数据才能导致一个节点的times发生改变,从而使它的times大于其父亲,破坏性质3。那么,不妨在插入后检查是否违反性质,如果违反就进行旋转。
插入pushKey代码如下:

void pushKey(tree &n, int key)
{
    if (!n) {
        /*当前节点为空
        * 那么这个数据还没有插入过。
        * 新开一个空间并将times设为1即可
        */
        n = new node;
        n->lc = n->rc = NULL;
        n->key = key;
        n->times = 1;
    } else if (n->key == key) {
        /*已经找到
        * times加一就可以了
        */
        n->times++;
    } else if (n->key > key) {
        /*以下两种情况都和bst一样
        * 如果查询数据比当前节点小
        * 就向左查询
        */
        pushKey(n->lc, key);
        /*左孩子的times大于当前节点
        * 将左孩子上旋
        */
        if (n->lc->times > n->times)
            liftLeftChild(n);
    } else if (n->key < key) {
        /*如果查询数据比当前节点大
        * 就向右查询
        */
        pushKey(n->rc, key);
        /*右孩子的times大于当前节点
        * 将右孩子上旋
        */
        if (n->rc->times > n->times)
            liftRightChild(n);
    }
}

3、剩下的问题

// 获取众数出现次数
int maxTimes() {
    return root->times;
}
/*获取一个众数(根节点)
* 如果要获取所有众数也很方便
* 直接递归向下找所有times = maxTimes()的节点即可
*/
int mode() {
    return root->key;
}

/*如果认(jiao)真(qing)还可以写个删除,和bst完全一样*/

inline void del(tree &nd) {
    if (nd) {
        del(nd->lc);
        del(nd->rc);
        delete nd;
    }
}

4、完整代码

#include <iostream>
#include <stdio.h>
#include <cstring>
using namespace std;

typedef struct node;
typedef node *tree;

struct node {
       int key;
       int times;
       tree lc, rc;
};

tree root;

inline void liftLeftChild(tree &node)
{
       tree tmp = node->lc;
       node->lc = tmp->rc;
       tmp->rc = node;
       node = tmp;
       tmp = NULL;
}

inline void liftRightChild(tree &node)
{
    tree tmp = node->rc;
    node->rc = tmp->lc;
    tmp->lc = node;
    node =tmp;
    tmp = NULL;
} 

inline void pushKey(tree &n, int key)
{
    if (!n) {
        n = new node;
        n->lc = n->rc = NULL;
        n->key = key;
        n->times = 1;
    } else if (n->key == key) {
        n->times++;
    } else if (n->key > key) {
        pushKey(n->lc, key);
        if (n->lc->times > n->times)
            liftLeftChild(n);
    } else if (n->key < key) {
        pushKey(n->rc, key);
        if (n->rc->times > n->times)
            liftRightChild(n);
    }
}

inline int maxTimes() {
    return root->times;
}

inline int mode() {
    return root->key;
}

inline int times(tree nd, int key) {
    if (!nd) return 0;
    if (nd->key == key)
        return nd->times;
    if (nd->key < key)
        return times(nd->rc, key);
    else
        return times(nd->lc, key);
}

int main()
{
    int n, t;
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) {
        scanf("%d", &t);
        pushKey(root, t);
    }
    printf("%d\n", mode());
    return 0;
}

5、算法正确性分析
算法的正确性是显然的。如果pushKey导致一个节点的孩子的times更大,必定会触发上旋,一路上旋到需要的地方(即性质3一定不会被破坏)。特别的,这种算法可以很方便的求出第二众数、第三众数等等。

6、算法复杂度分析
由测试来看,这种方法的效率是很高的。因为出现频率更高的数更有可能是众数,上旋可以使下次操作更快。如果众数出现次数特别多,许多操作都只需要常数时间。

显然在最坏情况下,复杂度为O(n^2),即数字都不重复而且是单调的。对于这种情况,可以使用随机化上旋出现次数相同(times相同)的节点的方法来解决,实现非常简单。(这样实现的二叉树有高概率是良好的平衡树?请大神解释)
在最好情况下,即众数出现次数充分多时,复杂度为O(n),几乎和hash媲美。
在通常情况下,由于这样的方法类似于treap,复杂度应该为O(nlgn)。

从总体上看,这种方法是很优秀的(特别是便于实现)。

7、总结
这是对二叉排序树(更准确地是对treap)的一种扩张,事实证明这种方法很成功。看来,扩张已有的数据结构是一种很好的学习方法。

posted @ 2016-02-20 13:45  ljt12138  阅读(2446)  评论(0编辑  收藏  举报