English 中文(简体)
A. 启动商行
原标题:Incrementing an iterator for a BST

对于我如何适当做到这一点,我感到迷惑不解,但我需要能够在使用模板的双平复式树级Im执行中增加一个炉子。

变压器的结构有目前的节点,还有确定其目前立场的愤怒。 唯一的问题Im是,当它使用++Iter时,我应如何确定它是否应该正确?

Here s the header file for the entire class:

template < typename TComparable, typename TValue > 
    class SearchTree
    {
    public:
        class Iterator;
    private:
        struct Node;
        typedef typename Node TNode;
    public:
        SearchTree( void );
        ~SearchTree( void );
    public:
        TValue find( const TComparable& k );
        TValue find( int32_t index );
        TValue find( const Iterator& pIter );

        Iterator begin( void ) const;
        Iterator end( void ) const;

        void insert( const TComparable& k, const TValue& v );
        void insert( const Iterator& pIter );

        friend class Iterator;
        friend class TNode;
    private:
        int32_t mNodeCount;
        TNode* mRoot;
    public:
        class Iterator 
        {
        public:
            Iterator( void );
            Iterator( int32_t position );
            ~Iterator( void );
            inline TNode* operator->( void ) const 
            { return mCurrentNode; }
            void operator++( void );
            bool operator==( const Iterator& pIter );
            bool operator!=( const Iterator& pIter );
        private:
            int32_t getNumStepsLeftToLeaf( void );
            int32_t getNumStepsRightToLeaf( void );
            bool isLeafNode( const Node*& n );
            bool isInternalNode( const Node*& n );
        private:
            TNode* mCurrentNode;
            int32_t mIterPosition;
            friend class TNode;
        };
    private:
        struct Node
        {
        public:
            Node( void ) : mParent( NULL ), mLeftChild( NULL ), mRightChild( NULL )
            {}
            ~Node( void )
            {
                if ( mParent ) delete mParent;
                if ( mLeftChild ) delete mLeftChild;
                if ( mRightChild ) delete mRightChild;
            }
            int32_t index;
            TComparable Key;
            TValue Value;
            TNode* mParent;
            TNode* mLeftChild;
            TNode* mRightChild;
        };
    };

请注意,由于我计划把这部法典的很多内容寄给安康,我无法利用STLport的例外情况(这是我所利用的——GnuSTL是GPL d,这意味着我可以做些什么(为了谋利)——如果任何人有任何矛盾的话,请让我知道的话)这样做。

而且,在有人提到推动之前,我已经尝试把这一努力赶到国库,但没有成功。 我愿利用这一点,因为这将使生活更加容易,但现在我愿意撰写自己的数据结构和算法。

就主持人而言,我猜测我在此设计中遗漏了一件事,如果有人知道这怎么办,请让我知道。 如果任何人要求,我也乐于为整个班子投诚。

问题回答

首先,预先固定的加油操作员有这一签名:

Iterator& operator++();

职位设置应以下列方式宣布和界定:

const Iterator& operator++(int){Iterator old = *this; ++(*this); return old;}

也许,你希望预先决定,要求下届(最左边)休息。

Iterator& Iterator::operator++(){
    myParent = mCurrentNode; 
    return  myParent.getRightOf(mCurrentNode); 
}

You see that I assumed you have a way to construct an iterator from a node you probably should have at least a private one.

I suppose you want to iterate through leaf nodes, (anyway the internal nodes implementation is not dissimilar). You need a getRightOf method more or less doing the following, let me write down some pseudocode for it:

Node* Node::getRightOf(Node* aNode){ 
    if aNode == mLeftChild{
        if mRightNode
            return mRightNode(this); 
        return mParent(this)
    }
    if aNode == mRightChild{
        return mParent(this)
    }
    if aNode == mParent{
        if mLeftNode
            return mLeftNode(this); 
        if mRightNode
            return mRightNode(this); 
        return this; 
    }
}

在集装箱末,你需要一些处理诸如拖拉之类的案件。

<><>

在Node s destructor I中:

if ( mParent ) delete mParent;

这看起来很危险:谁删除了孩子? 左胎或右子?





相关问题
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?

热门标签