English 中文(简体)
当用定购方式添加物品时, Bin树实际上作为相关清单。
原标题:Binary search tree effectively acts as linked list when items are added in an ordered way

I have a huge number of items to be stored in a collection. I need to locate an item by comparing it to a certain key and then tell if such an item exists. I use a binary search tree to do so.

class node
{
    public:
    node(const char *p_name) :
        greater(NULL),
        smaller(NULL),
        name(p_name)
    {}
    node *find(const char *p_name)
    {
        node *l_retval = this;

        for(; l_retval != NULL;)
        {
            int l = strcmp(p_name, l_retval->name.c_str());
            if (l == 0) break; // found it
            else
            if (l > 0) l_retval = greater; // the node searched for is in the  greater  branch
            else l_retval = smaller; // or in the  smaller  branch
        }
        return l_retval;
    }
    node *greater, *smaller;
    std::string name; // or any other type of data you would like to store
};

如果随意添加这些物品,所有物品都将被罚款。 如果有时以定购方式添加物品,则我的《商业仲裁示范法》作为相关清单(见下文)。

问题在于:鉴于《巴塞尔协议书》(平衡或联系式清单),我如何平衡《巴塞尔协议书》,因此重新编排,不能有效地作为相关名单?

例子见C++,但问题适用于比C++全数多语文。

如果你向我提供能够帮助我的联系,感谢!

最佳回答

If you don t want to use a self-balancing tree (red-black, AVL, etc.), then the "easy" solution is to just shuffle the collection before inserting it into the tree.

If you want a perfectly balanced tree, you could start with a sorted collection, then insert the middle element of that list into the tree and recursively do the same with the two remaining sorted subcollections.

I understand that you are investigating algorithms, but in real-life you d probably just want to use a std::map and not worry about the details of how it works.

问题回答

它是《巴塞尔协议书》的概念。

Now what you are looking for are derivates, not pure:

Self-balancing binary search tree

自我平衡双平树木在关键时期通过在树上进行转化(如树木轮换)来解决这一问题,以便保持与木).(n)的比例高度。 虽然涉及某种间接费用,但从长远来看,可以通过确保迅速执行后来的业务来证明。

保持高度始终保持其最低价值并不总是可行的;可以证明,任何插入算法若如此,就会有过多的间接费用。 [需要援引] 因此,大多数自平价的BST算法将高额保持在这一低约束等级的固定因素内。

自我平衡的树木最受欢迎的变量无疑是红黑树。

Links

Wikipedia在树算法上具有相当权威性:

第一个问题是,你为什么要执行? 使用<代码>std:setstd:map。 两部都布设了平衡树木。 你们需要提供命令功能,但除了这种职能(以及你不能改变规定命令的实地的明显限制)之外,这种职能应当受到罚款。

If you already have the elements stored in another container, you can use a map from the key that controls the order to a pointer/iterator into the original container (beware of any operations in the container that might invalidate pointers/iterators)

页: 1

A trick is to use an alternative compare function (eg some kind of hash, or just random) to move the linked list to a new linked list, and than consume the temporary list and re-insert into the original tree.

int node_cmp_random(struct node *one, struct node *two)
{       
return (rand() %2) ? -1 : 1;
}

最新资料:随机抽取(C代码)

void node_randomise(struct node **tpp)
{
struct node *new, *this, **hnd;

for (new=NULL; this = node_consume( tpp); ) {
    for (hnd= &new; *hnd; hnd = (rand()&1) ? &(*hnd)->prev : &(*hnd)->next) {;}
    *hnd = this;
    }
*tpp = new ;
}

struct node * node_consume(struct node **tpp)
{
struct node * ret;
if (!*tpp) return NULL;

while ((*tpp)->prev) tpp = &(*tpp)->prev;
ret = *tpp;
*tpp = ret->next;
ret->next = NULL;
return ret;
}

改变这一守则以使用功能性论点是显而易见的,并留待读者。





相关问题
How to add/merge several Big O s into one

If I have an algorithm which is comprised of (let s say) three sub-algorithms, all with different O() characteristics, e.g.: algorithm A: O(n) algorithm B: O(log(n)) algorithm C: O(n log(n)) How do ...

Grokking Timsort

There s a (relatively) new sort on the block called Timsort. It s been used as Python s list.sort, and is now going to be the new Array.sort in Java 7. There s some documentation and a tiny Wikipedia ...

Manually implementing high performance algorithms in .NET

As a learning experience I recently tried implementing Quicksort with 3 way partitioning in C#. Apart from needing to add an extra range check on the left/right variables before the recursive call, ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Enumerating All Minimal Directed Cycles Of A Directed Graph

I have a directed graph and my problem is to enumerate all the minimal (cycles that cannot be constructed as the union of other cycles) directed cycles of this graph. This is different from what the ...

Quick padding of a string in Delphi

I was trying to speed up a certain routine in an application, and my profiler, AQTime, identified one method in particular as a bottleneck. The method has been with us for years, and is part of a "...

热门标签