***********************************************************
// 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