C ++ AVL树迭代器不会正确递增

时间:2012-03-07 08:08:42

标签: c++ iterator infinite-loop avl-tree

我在AvlTree类中实现了一个类迭代器。我的AvlTree节点如下:

    struct AvlNode
{
    Comparable element;
    list<int> lines; //line occurrences
    bool flag; //checks validity
    AvlNode   *left;
    AvlNode   *right;
    AvlNode   *parent; //parent pointer 
    int       height;

    AvlNode( const Comparable & theElement, AvlNode *lt, AvlNode *rt, AvlNode *pt,
                                                     int h = 0, bool b = true )
      : element( theElement ), left( lt ), right( rt ), parent( pt ), height( h ), flag( b ) { }
};

我的迭代器如下:

     class iterator
 {
    protected:

        friend class AvlTree<Comparable>;
        AvlNode * node;

        AvlNode * findInOrderSuccessor(AvlNode * & t)
        {
            AvlNode * temp;
            //node has a right child
            // so successor is leftmost node of right subtree
            if(t->right != NULL)
            {
                temp = t->right; //go right
                //go all the way left
                while(temp->left != NULL)
                {
                    temp = temp->left; 
                }
                return temp;
            }

            //node has no right child
            //if we are someone's left child, go up one
            if(t->parent->left == t)
            {
                //return your parent
                temp = t->parent;
                return temp;
            }
            //if we are someone's right child, go up until the current node
            //is someone's left child, then go up one more
            temp = t->parent;
            while(temp->parent->left != temp)
            {
                temp = temp->parent; //go up 
            }
            //return your parent
            temp = t->parent;
            return temp;

        }

    public:
        iterator(AvlNode * p) : node(p)
            { }

        //overload * to make *iterator return the element of its node
        Comparable & operator*() 
            { return node->element; }

        iterator operator++ (int) //postfix operator
        {
            node = findInOrderSuccessor(node);
            return iterator(node);
        }

        // == comparison overload
        bool operator==(iterator  rhs)
            { return node == rhs.node; }
        // != comparison overload
        bool operator!=(iterator  rhs)
            { return !(*this == rhs); }
};

我的AvlTree还有一个开始和结束迭代器作为公共成员:

    //begin iterator points to leftmost node
iterator begin()
{ //return pointer to leftmost node
    AvlNode *temp = root;
    while(temp->left != NULL)
        temp = temp->left;
    return iterator(temp);
}

//end iterator points to one after rightmost node
iterator end()
{ //return NULL right pointer of rightmost node
    AvlNode * temp = root;
    while(temp->right != NULL)
        temp = temp->right;
    return iterator( temp->right );
}

我的问题是当我尝试在main中运行以下内容时:

    for(AvlTree<string>::iterator itr = tree.begin(); itr != (tree.end()); itr++)
        cout << *itr << endl;

不是按顺序输出字符串树中的所有单词,而是获得树中第一个顺序项的无限循环。我似乎无法弄清楚为什么它不会超过第一个项目。

1 个答案:

答案 0 :(得分:1)

以下迭代代码有效(来自my AVL tree;将leftright替换为link[0]link[1]):

BAVLNode * BAVL_GetFirst (const BAVL *o)
{
    if (!o->root) {
        return NULL;
    }

    BAVLNode *n = o->root;
    while (n->link[0]) {
        n = n->link[0];
    }

    return n;
}

BAVLNode * BAVL_GetNext (const BAVL *o, BAVLNode *n)
{
    if (n->link[1]) {
        n = n->link[1];
        while (n->link[0]) {
            n = n->link[0];
        }
    } else {
        while (n->parent && n == n->parent->link[1]) {
            n = n->parent;
        }
        n = n->parent;
    }

    return n;
}

就您的代码而言,首先,end()不需要找到最右边的节点才能返回iterator(NULL);它可以在不看树的情况下返回。

然而,算法中的实际错误似乎在这里:

        temp = t->parent;
WRONG:  while(temp->parent->left != temp)
        {
            temp = temp->parent; //go up 
        }
        //return your parent
WRONG:  temp = t->parent;
        return temp;

    }

我标记的第一行可能会尝试NULL指针取消引用,应该更改为:

while(temp->parent && temp->parent->left != temp)

第二个:

temp = temp->parent;

另外,您可能已经从我的代码中注意到以下内容现在非常丰富;它可以被移除,并且(固定的)剩余代码将以完全相同的方式处理它。它也遭受了我在上面指出的相同的NULL指针解除引用。

//if we are someone's left child, go up one
if(t->parent->left == t)
{
    //return your parent
    temp = t->parent;
    return temp;
}