首页 > 搜索 > 数据结构树的前序遍历算法,二叉树遍历算法集合(前中后序遍历的递归和非递归算法,层序遍历算法)

数据结构树的前序遍历算法,二叉树遍历算法集合(前中后序遍历的递归和非递归算法,层序遍历算法)

互联网 2020-10-24 20:07:58
在线算命,八字测算命理
评论# re: 二叉树遍历算法集合(前中后序遍历的递归和非递归算法,层序遍历算法)  回复  更多评论  good2007-06-16 18:04 | superdaxia# re: 二叉树遍历算法集合(前中后序遍历的递归和非递归算法,层序遍历算法)  回复  更多评论  你好,我是新手,在用VC调试时发现错误f:\hwh\二叉树\binarytree.h(64) : error C2027: use of undefined type 'BinaryTree'f:\hwh\二叉树\binarytree.h(8) : see declaration of 'BinaryTree'main.cppf:\hwh\二叉树\binarytree.h(64) : error C2027: use of undefined type 'BinaryTree'f:\hwh\二叉树\binarytree.h(8) : see declaration of 'BinaryTree'执行 cl.exe 时出错.

二叉树.exe - 1 error(s), 0 warning(s)为什么呢?

2007-12-25 11:37 | tomy# re: 二叉树遍历算法集合(前中后序遍历的递归和非递归算法,层序遍历算法)  回复  更多评论  @tomy你用的什么编译器?我用的VS2003,其次,你的工程里面有几个文件?这里一共有三个文件:binarytree.h,binarytree.cpp,main.cpp2007-12-26 11:18 | 创系# re: 二叉树遍历算法集合(前中后序遍历的递归和非递归算法,层序遍历算法)[未登录]  回复  更多评论  我用DEV C++编译后报以下信息,请指教:

C:\DOCUME~1\ADMINI~1\LOCALS~1\Temp/cceIaaaa.o(.text+0x52d):main.cpp: undefined reference to `BinaryTree::Traverse(BinaryTree::TraverseType, bool)'C:\DOCUME~1\ADMINI~1\LOCALS~1\Temp/cceIaaaa.o(.text+0x62f):main.cpp: more undefined references to `BinaryTree::Traverse(BinaryTree::TraverseType, bool)' followC:\DOCUME~1\ADMINI~1\LOCALS~1\Temp/cceIaaaa.o(.text+0x676):main.cpp: undefined reference to `BinaryTree::~BinaryTree()'collect2: ld returned 1 exit status

2008-01-06 01:10 | ben# re: 二叉树遍历算法集合(前中后序遍历的递归和非递归算法,层序遍历算法)  回复  更多评论  楼上的错误是DEV C++的工程文件/配置文件的问题吧?2008-01-06 01:23 | minidxer# re: 二叉树遍历算法集合(前中后序遍历的递归和非递归算法,层序遍历算法)[未登录]  回复  更多评论  已经解决了,谢谢指点!2008-01-06 15:19 | ben# re: 二叉树遍历算法集合(前中后序遍历的递归和非递归算法,层序遍历算法)  回复  更多评论  请问,为什么我的窗口上什么都米有?只有一个“请按任意键继续”,用的是VC++ 20082008-07-09 07:03 | liuyu# re: 二叉树遍历算法集合(前中后序遍历的递归和非递归算法,层序遍历算法)  回复  更多评论  其实如果不用 Stack 而改用 List 的话,三种非递归遍历将变得更加简单一致,一个 While 就够了

typedef std::list TreeNodeList;

typedef struct TreeNode{ItemNode;TreeNode* pRight;TreeNode* pLeft;boolbVisited; // 关键

TreeNode(Item node = 0, TreeNode* pright = NULL, TreeNode* pleft = NULL): Node(node), pRight(pright), pLeft(pleft), bVisited(false){}

}TreeNode, *PTreeNode;

// 非递归前序遍历树void BinaryTree::NoRecPreTraverseImpl(PTreeNode pTreenode){if (NULL == pTreenode)return;

TreeNodeList NodeList;PTreeNode pNode;NodeList.push_front(pTreenode);

while (!NodeList.empty()){pNode = NodeList.front();NodeList.pop_front();

std::cout pRight);if(pNode->pLeft != NULL) NodeList.push_front(pNode->pLeft);}}

// 非递归中序遍历树void BinaryTree::NoRecInTraverseImpl(PTreeNode pTreenode){if (NULL == pTreenode)return;

TreeNodeList NodeList;PTreeNode pNode;NodeList.push_front(pTreenode);

while (!NodeList.empty()){pNode = NodeList.front();

if(pNode->bVisited){NodeList.pop_front();pNode->bVisited = false;// 为下一次遍历做准备

std::cout pRight);}else{if(pNode->pLeft != NULL) NodeList.push_front(pNode->pLeft);pNode->bVisited = true;}

}}

// 非递归后序遍历树void BinaryTree::NoRecPostTraverseImpl(PTreeNode pTreenode){if (NULL == pTreenode)return;

TreeNodeList NodeList;PTreeNode pNode;NodeList.push_front(pTreenode);

while (!NodeList.empty()){pNode = NodeList.front();

if(pNode->bVisited){NodeList.pop_front();pNode->bVisited = false;// 为下一次遍历做准备

std::cout pRight);if(pNode->pLeft != NULL) NodeList.push_front(pNode->pLeft);pNode->bVisited = true;}

}}

2008-09-08 22:58 | Godspeed# re: 二叉树遍历算法集合(前中后序遍历的递归和非递归算法,层序遍历算法)[未登录]  回复  更多评论  非递归后续遍历修改为:public static void iterativePostorder(BinaryTree boot) {Stack stack = new Stack();BinaryTree current, pointer=boot;boolean bVisitRoot = false;//标志是否访问过根节点if (boot == null) {return;}stack.push(boot);current = boot.leftpoiter;while (!stack.empty()) {while (current != null) {//向左走到尽头stack.push(current);current = current.leftpoiter;}current = stack.peek();stack.pop();if ((current.rightpoiter == null)||(pointer == current.rightpoiter)) {visit(current);pointer = current;current = stack.peek();if (pointer == current.rightpoiter) {visit(current);stack.pop();pointer=current;current = null;} else {current = current.rightpoiter;}} else {if (current == boot && (bVisitRoot == true)) {visit(current);return;} else {if (current == boot) {bVisitRoot = true;}stack.push(current);current = current.rightpoiter;}}

}

}

2011-07-07 11:12 | hepeng
免责声明:非本网注明原创的信息,皆为程序自动获取互联网,目的在于传递更多信息,并不代表本网赞同其观点和对其真实性负责;如此页面有侵犯到您的权益,请给站长发送邮件,并提供相关证明(版权证明、身份证正反面、侵权链接),站长将在收到邮件12小时内删除。

相关阅读

一周热门

查看更多