C++中的一个迭代二叉树遍历
An iterative binary tree traversal in C++
我正在为二进制搜索树创建一个非递归遍历。然而,我遇到了几个非常奇怪的错误。
这是我的横向函数的代码:
void BinarySearchTree<ItemType>::nrInOrderTraversal(void visit(ItemType&)) const
{
stack <int> nodeStack;
int *curPtr;
bool done = false;
while (!done)
{
if (rootPtr() != 0)
{
//Place pointer to node on stack before traversing the node's left subtree
nodeStack.push(rootPtr());
//Traverse the left subtree
rootPtr() = rootPtr()->getLeftChildPtr();
}
else //Backtrack from the empty subtree and visit the node at the top of the stack;
//however if the stack is empty, you are done.
{
if(!nodeStack.empty())
{
nodeStack.top(rootPtr());
visit(rootPtr()->getItem());
nodeStack.pop();
//Traverse the right subtree of the node just visited
rootPtr() = rootPtr()->getRightChildPtr();
}
else
done = true;
}
}
}
和我主要的遍历代码部分:
BinarySearchTree<string>* tree4Ptr = new BinarySearchTree<string>();
tree4Ptr->add("10");
tree4Ptr->add("20");
tree4Ptr->add("30");
tree4Ptr->add("40");
tree4Ptr->add("50");
tree4Ptr->add("60");
tree4Ptr->add("70");
tree4Ptr->add("80");
tree4Ptr->add("90");
tree4Ptr->add("100");
tree4Ptr->add("110");
tree4Ptr->add("120");
tree4Ptr->add("130");
tree4Ptr->add("140");
tree4Ptr->add("150");
tree4Ptr->add("160");
cout<<"Tree 4 nrInOrderTraversal: "<<endl;
tree4Ptr-> nrInOrderTraversal(display);
我得到的特别错误是:
C:UsersMauraDocumentsCSC2014ATJprog6BinarySearchTree.cpp|328|error: '((const BinarySearchTree<std::basic_string<char> >*)this)->BinarySearchTree<std::basic_string<char> >::rootPtr' cannot be used as a function|
C:UsersMauraDocumentsCSC2014ATJprog6BinarySearchTree.cpp|331|error: '((const BinarySearchTree<std::basic_string<char> >*)this)->BinarySearchTree<std::basic_string<char> >::rootPtr' cannot be used as a function|
C:UsersMauraDocumentsCSC2014ATJprog6BinarySearchTree.cpp|334|error: '((const BinarySearchTree<std::basic_string<char> >*)this)->BinarySearchTree<std::basic_string<char> >::rootPtr' cannot be used as a function|
C:UsersMauraDocumentsCSC2014ATJprog6BinarySearchTree.cpp|334|error: '((const BinarySearchTree<std::basic_string<char> >*)this)->BinarySearchTree<std::basic_string<char> >::rootPtr' cannot be used as a function|
C:UsersMauraDocumentsCSC2014ATJprog6BinarySearchTree.cpp|341|error: '((const BinarySearchTree<std::basic_string<char> >*)this)->BinarySearchTree<std::basic_string<char> >::rootPtr' cannot be used as a function|
C:UsersMauraDocumentsCSC2014ATJprog6BinarySearchTree.cpp|342|error: '((const BinarySearchTree<std::basic_string<char> >*)this)->BinarySearchTree<std::basic_string<char> >::rootPtr' cannot be used as a function|
C:UsersMauraDocumentsCSC2014ATJprog6BinarySearchTree.cpp|346|error: '((const BinarySearchTree<std::basic_string<char> >*)this)->BinarySearchTree<std::basic_string<char> >::rootPtr' cannot be used as a function|
C:UsersMauraDocumentsCSC2014ATJprog6BinarySearchTree.cpp|346|error: '((const BinarySearchTree<std::basic_string<char> >*)this)->BinarySearchTree<std::basic_string<char> >::rootPtr' cannot be used as a function|
void display(string& anItem)
{
cout << "Displaying item - " << anItem << endl;
} // end display
有些人要求提供有关rootPtr的头文件,所以这里是
/** Link-based implementation of the ADT binary search tree.
@file BinarySearchTree.h */
#ifndef _BINARY_SEARCH_TREE
#define _BINARY_SEARCH_TREE
#include "BinaryTreeInterface.h"
#include "BinaryNode.h"
#include "BinaryNodeTree.h"
#include "NotFoundException.h"
#include "PrecondViolatedExcep.h"
template<class ItemType>
class BinarySearchTree : public BinaryNodeTree<ItemType>
{
private:
BinaryNode<ItemType>* rootPtr;
protected:
//------------------------------------------------------------
// Protected Utility Methods Section:
// Recursive helper methods for the public methods.
//------------------------------------------------------------
// Recursively finds where the given node should be placed and
// inserts it in a leaf at that point.
BinaryNode<ItemType>* insertInorder(BinaryNode<ItemType>* subTreePtr,
BinaryNode<ItemType>* newNode);
// Removes the given target value from the tree while maintaining a
// binary search tree.
BinaryNode<ItemType>* removeValue(BinaryNode<ItemType>* subTreePtr,
const ItemType target,
bool& success);
// Removes a given node from a tree while maintaining a
// binary search tree.
BinaryNode<ItemType>* removeNode(BinaryNode<ItemType>* nodePtr);
// Removes the leftmost node in the left subtree of the node
// pointed to by nodePtr.
// Sets inorderSuccessor to the value in this node.
// Returns a pointer to the revised subtree.
BinaryNode<ItemType>* removeLeftmostNode(BinaryNode<ItemType>* subTreePtr,
ItemType& inorderSuccessor);
// Returns a pointer to the node containing the given value,
// or 0 if not found.
BinaryNode<ItemType>* findNode(BinaryNode<ItemType>* treePtr,
const ItemType& target) const;
public:
//------------------------------------------------------------
// Constructor and Destructor Section.
//------------------------------------------------------------
BinarySearchTree();
BinarySearchTree(const ItemType& rootItem);
BinarySearchTree(const BinarySearchTree<ItemType>& tree);
virtual ~BinarySearchTree();
//------------------------------------------------------------
// Public Methods Section.
//------------------------------------------------------------
bool isEmpty() const;
int getHeight() const;
int getNumberOfNodes() const;
ItemType getRootData() const throw(PrecondViolatedExcep);
void setRootData(const ItemType& newData) const throw(PrecondViolatedExcep);
bool add(const ItemType& newEntry);
bool remove(const ItemType& anEntry);
void clear();
ItemType getEntry(const ItemType& anEntry) const throw(NotFoundException);
bool contains(const ItemType& anEntry) const;
//------------------------------------------------------------
// Public Traversals Section.
//------------------------------------------------------------
void preorderTraverse(void visit(ItemType&)) const;
void inorderTraverse(void visit(ItemType&)) const;
void postorderTraverse(void visit(ItemType&)) const;
void nrInOrederTraversal(void visit (ItemType&)) const;
//------------------------------------------------------------
// Overloaded Operator Section.
//------------------------------------------------------------
BinarySearchTree<ItemType>& operator=(const BinarySearchTree<ItemType>& rightHandSide);
}; // end BinarySearchTree
#include "BinarySearchTree.cpp"
#endif
rootPtr是一个数据成员,而不是一个方法,因此不应将其作为函数调用。也就是说,在代码中每次提到rootPtr后都要删除()
。
[edit]标题显示:rootPtr
是一个对象,而不是一个函数/方法。删除对rootPtr
的所有引用之后的()。
例如,而不是rootPtr() != 0
写入CCD_ 5。
Btw。如果可以使用C++11,那么指针应该使用nullptr
而不是0
。即写入rootPtr != nullptr
这一行的拼写错误。
tree4Ptr-> nrInOrederTraversal(display);
相关文章:
- 为什么 C++ std::unordered_map 从 emplace/ 找到返回一个迭代器?
- 为什么我的模板化函数需要从一个迭代器转换到另一个迭代器?
- 转到基于范围的 for 循环中的下一个迭代器
- 有没有一个迭代器的例子,它不会使用 ptrdiff_t 作为其difference_type?
- 每个花哨的指针都应该是一个迭代器吗?
- 如何使用find_if获取最后一个迭代器
- 为什么我无法在C++中将一个迭代器分配给另一个迭代器?
- 递增迭代器以指向另一个迭代器的下一个元素
- 使用一个迭代器迭代向量的向量
- 我如何归因于迭代器的另一个迭代
- 传入一个迭代器,该迭代器只迭代满足特定条件的元素
- 另一个迭代器引用的对象被另一个迭代器删除
- 使用给定的第一个和最后一个迭代器迭代范围
- 如何从给定另一个迭代器向量的向量中删除元素
- 如何寻址循环中的下一个迭代器并在同一地址插入元素
- 如何编写一个迭代器包装器来组合底层迭代器中的顺序值组
- 我正在尝试为我的 DynamicArray 类创建一个迭代器。为什么 STL 排序不适用于我的迭代器?
- 找到一个位置并返回一个迭代器不起作用
- 任务在删除其中一个迭代对象时会导致段错误
- 存储当前和下一个迭代器