English 中文(简体)
履历
原标题:Stackoverflow exception when traversing BST

我在C++实施了一个基于链接的BST(双向搜索树),以完成我的一项任务。 我写了我的整个班子和一切都好事,但我的任务要求我为以下活动安排时间:

a.  A sorted list of 50000, 75000, and 100000 items
b.  A random list of 50000, 75000, and 100000 items

这一精彩之处,我可以插入数字,但也请我打上<条码>,<>FindHala(和CountLeaves()。 我的问题是,我先使用<条码>recursion履行两项职能。 自2006年以来 我有这样一大名单,编号为Im 正在读到stackovercc/code> 例外。

我的阶级定义如下:

template <class TItem>
class BinarySearchTree
{
public:
    struct BinarySearchTreeNode
    {
    public:
        TItem Data;
        BinarySearchTreeNode* LeftChild;
        BinarySearchTreeNode* RightChild;
    };

    BinarySearchTreeNode* RootNode;

    BinarySearchTree();
    ~BinarySearchTree();

    void InsertItem(TItem);

    void PrintTree();
    void PrintTree(BinarySearchTreeNode*);

    void DeleteTree();
    void DeleteTree(BinarySearchTreeNode*&);

    int CountLeaves();
    int CountLeaves(BinarySearchTreeNode*);

    int FindHeight();
    int FindHeight(BinarySearchTreeNode*);

    int SingleParents();
    int SingleParents(BinarySearchTreeNode*);

    TItem FindMin();
    TItem FindMin(BinarySearchTreeNode*);

    TItem FindMax();
    TItem FindMax(BinarySearchTreeNode*);
};

<>执行>

template <class TItem>
int BinarySearchTree<TItem>::FindHeight()
{
    return FindHeight(RootNode);
}

template <class TItem>
int BinarySearchTree<TItem>::FindHeight(BinarySearchTreeNode* Node)
{
    if(Node == NULL)
        return 0;

    return 1 + max(FindHeight(Node->LeftChild), FindHeight(Node->RightChild));
}

<>执行

template <class TItem>
int BinarySearchTree<TItem>::CountLeaves()
{
    return CountLeaves(RootNode);
}

template <class TItem>
int BinarySearchTree<TItem>::CountLeaves(BinarySearchTreeNode* Node)
{
    if(Node == NULL)
        return 0;
    else if(Node->LeftChild == NULL && Node->RightChild == NULL)
        return 1;
    else
        return CountLeaves(Node->LeftChild) + CountLeaves(Node->RightChild);
}

我试图想一下,我如何能够在不再次接触的情况下执行这两种方法,但我完全 st。 没有人有什么想法?

问题回答

如果保持平衡,就不应对100 000个des子的树木进行回收。 深度只能是17,在显示的执行中不会使用太多的分量。 因此,似乎建造树木的法典并没有正确平衡。

我发现,该网页非常开明,因为它谈论的是将使用回收功能转换为使用回收功能的机械。

它还有显示守则的例子。

不妨在作此改动时予以计算。 储存des的高度,即,在诺德物体中添加像高高的惯性。 树也有高和树叶。 当你插入一分点时,如果其父母是(was)一页,则页数会发生重大变化,但如果没有,则将页数增加1。 另外,新 no子的高度是母子的高度+1,因此如果高于目前树木的高度,则更新。 其家庭工作,因此获得实际法典的帮助。

Balance your tree occasionally. If your tree is getting stackoverflow on FindHeight(), that means your tree is way unbalanced. If the tree is balanced it should only have a depth of about 20 nodes for 100000 elements.

The easiest (but fairly slow) way of re-balancing unbalanced binary tree is to allocate an array of TItem big enough to hold all of the data in the tree, insert all of your data into it in sorted order, and delete all of the nodes. Then rebuild the tree from the array recursively. The root is the node in the middle. root->left is the middle of the left half, root->right is the middle of the right half. Repeat recursively. This is the easiest way to rebalance, but it is slowish and takes lots of memory temporarily. On the other hand, you only have to do this when you detect that the tree is very unbalanced, (depth on insert is more than 100).

另一种(更优惠的)选择是平衡各字。 这样做的最直观办法是,追踪目前 no下多少个 no子。 如果右孩子的“子女” no子与左孩子一样多了两倍以上,就离开了“生育”。 反之亦然。 在如何在互联网上进行树木轮换方面存在着一些障碍。 这使得插入稍微缓慢,但你并没有出现第一种选择所制造的零星大规模 st。 另一方面,你们必须不断更新所有“子女”的计数,就像你们的轮值一样。

为了计算这些假期,不必再作改动,使用STL用作RB-特里底<代码>的代号:std:map ......为您的树木创建<代码>begin(>和end(>)功能,以证明所订购的第一和最后一条(在此情况下是左端,然后是右端)。 然后产生称为职能的职能。

缩略语 缩略语

对于一个特定的<代码>目前_node,将把一个点子回到下一个树冠。 牢记这项工作需要额外的<条码>:点码>,以在您的<条码>中标明。

您的计算法increment(>)将研究如下内容:

  1. Check to see if there is a right-child to the current node.
  2. If there is a right-child, use a while-loop to find the left-most node of that right subtree. This will be the "next" node. Otherwise go to step #3.
  3. If there is no right-child on the current node, then check to see if the current node is the left-child of its parent node.
  4. If step #3 is true, then the "next" node is the parent node, so you can stop at this point, otherwise go the next step.
  5. If the step #3 was false, then the current node is the right-child of the parent. Thus you will need to keep moving up to the next parent node using a while loop until you come across a node that is a left-child of its parent node. The parent of this left-child node will then be the "next" node, and you can stop.
  6. Finally, if step #5 returns you to the root, then the current node is the last node in the tree, and the iterator has reached the end of the tree.

Finally you ll need a bool leaf(const BinarySearchTreeNode* current_node) function that will test whether a given node is a leaf node. Thus you counter function can simply iterate though the tree and find all the leaf nodes, returning a final count once it s done.

如果你想要测量一个不平衡的树木的最大深度,而不重新吸收,那么,在你的树冠图(<>insert()功能中,你需要跟踪点子的深度。 这只是您的<代码>node中的变量。 在树中添加 no时确定的类型。 那么,你就可以通过这三条,找到一页的最长深度。

BTW, 不幸的是,这种方法的复杂性将变成O(N)......在靠近O(log N)冰的地方。





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

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->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签