设为首页 加入收藏

TOP

二叉树代码(较全) (二)
2014-11-23 21:42:24 来源: 作者: 【 】 浏览:17
Tags:代码 较全
***********************************************************
// Name: _FirstVisite
// Desc: 先根遍历(非递归)
//******************************************************************************
void _FirstVisite(Node* tree)
{
ResetTree(tree);

typedef vector NodeStack;
NodeStack stack;
stack.push_back(tree); // 初始化栈

while (stack.size() > 0)
{
Node* topNode = stack.back();
if (!topNode->leftVisited && !topNode->rightVisited)
{
cout<data<<" ";
}

if (topNode->leftChild != NULL && !topNode->leftVisited)
{
stack.push_back(topNode->leftChild);
topNode->leftVisited = true;
}
else if (topNode->rightChild != NULL && !topNode->rightVisited)
{
stack.push_back(topNode->rightChild);
topNode->rightVisited = true;
}
else
{
stack.pop_back();
}
}
}

//******************************************************************************
// Name: __FirstVisite
// Desc: 非递归先根遍历思路二
//******************************************************************************
void __FirstVisite(Node* tree)
{
typedef vector NodeStack;
NodeStack stack;
Node *curNode = tree;
while(!stack.empty() || curNode != NULL)
{
while(curNode != NULL)
{
cout<data<<" ";
stack.push_back(curNode);
curNode = curNode->leftChild;
}

if(!stack.empty())
{
curNode = stack.back();
curNode = curNode->rightChild;
stack.pop_back();
}
}
}

//******************************************************************************
// Name: _CenterVisit
// Desc: 中根遍历(非递归)
//******************************************************************************
void _CenterVisite(Node* tree)
{
ResetTree(tree);

typedef vector NodeStack;
NodeStack stack;
stack.push_back(tree); // 初始化

while (stack.size() > 0)
{
Node* topNode = stack.back();
if (topNode->leftVisited && !topNode->rightVisited)
{
cout<data<<" ";
}


if (topNode->leftChild != NULL && !topNode->leftVisited)
{
stack.push_back(topNode->leftChild);
topNode->leftVisited = true;
}
else
{
if (topNode->rightChild != NULL && !topNode->rightVisited)
{
if (topNode->leftChild == NULL && topNode->rightChild != NULL)
{
cout<data<<" "; // 单边只有右子节点
}
stack.push_back(topNode->rightChild);
topNode->rightVisited = true;
}
else
{
if (topNode->leftChild == NULL && topNode->rightChild == NULL)
{
cout<data<<" ";
}
stack.pop_back();
}
}
}
}

//******************************************************************************
// Name: __CenterVisite
// Desc: 非递归中根遍历思路二
//******************************************************************************
void __CenterVisite(Node* tree)
{
typedef vector NodeStack;
NodeStack stack;

Node* curNode = tree;
while(!stack.empty() || curNode != NULL)
{
while(curNode != NULL)
{
stack.push_back(curNode);
curNode = curNode->leftChild;
}

if(!stack.empty())
{
curNode = stack.back();
cout<data<<" ";
curNode = curNode->rightChild;
stack.pop_back();
}
}
}

//******************************************************************************
// Name: _AfterVisite
// Desc: 后序遍历(非递归)
//******************************************************************************
void _AfterVisite(Node* tree)
{
ResetTree(tree);

typedef vector NodeStack;
NodeStack stack;
stack.push_back(tree); // 初始化

while (s

首页 上一页 1 2 3 4 下一页 尾页 2/4/4
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇C++一些注意点之操作符重载 下一篇poj2354

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·一篇说人话的文章, (2025-12-27 07:50:09)
·Python Web框架哪家 (2025-12-27 07:50:06)
·基于Python的数据分 (2025-12-27 07:50:03)
·深入理解 Java 集合 (2025-12-27 07:22:48)
·Java集合框架全面解 (2025-12-27 07:22:45)