#include <iostream>
#include <windows.h>
#include <stack>
#include <set>
using namespace std;
struct TreeNode {
int value;
TreeNode* lChild;
TreeNode* rChild;
TreeNode(int v) :value(v), lChild(NULL), rChild(NULL)
{
}
};
void PrintValue(int value)
{
cout << value << " ";
}
void InOrder(TreeNode* root)
{
cout << "*****************中序遍历*********************" << endl;
if (NULL == root)
return;
stack<TreeNode*> treeStack;
treeStack.push(root);
TreeNode* curLeftChild = root->lChild;
while (curLeftChild)
{
treeStack.push(curLeftChild);
curLeftChild = curLeftChild->lChild;
}
while (!treeStack.empty())
{
TreeNode* curNode = treeStack.top();
treeStack.pop();
PrintValue(curNode->value);
if (curNode->rChild)
{
treeStack.push(curNode->rChild);
curNode = curNode->rChild;
while (curNode->lChild)
{
treeStack.push(curNode->lChild);
curNode = curNode->lChild;
}
}
}
cout << endl;
cout << "**********************************************" << endl;
}
void InOrderR(TreeNode* root)
{
if (NULL == root)
return;
InOrderR(root->lChild);
PrintValue(root->value);
InOrderR(root->rChild);
}
void PreOrder(TreeNode* root)
{
cout << "*****************前序遍历*********************" << endl;
if (NULL == root)
return;
TreeNode* curNode = root;
stack<TreeNode*> treeS;
while (curNode)
{
PrintValue(curNode->value);
treeS.push(curNode);
curNode = curNode->lChild;
}
while (!treeS.empty())
{
curNode = treeS.top();
treeS.pop();
if (curNode->rChild)
{
curNode = curNode->rChild;
PrintValue(curNode->value);
treeS.push(curNode);
while (curNode->lChild)
{
curNode = curNode->lChild;
PrintValue(curNode->value);
treeS.push(curNode);
}
}
}
cout << endl;
cout << "**********************************************" << endl;
}
void PreOrderR(TreeNode* root)
{
if (NULL == root)
return;
PrintValue(root->value);
PreOrderR(root->lChild);
PreOrderR(root->rChild);
}
void PostOrder(TreeNode* root)
{
cout << "*****************后序遍历*********************" << endl;
if (NULL == root)
return;
stack<TreeNode*> treeStack;
set<TreeNode*> reachSet;
treeStack.push(root);
TreeNode* curLeftChild = root->lChild;
while (curLeftChild)
{
treeStack.push(curLeftChild);
curLeftChild = curLeftChild->lChild;
}
while (!treeStack.empty())
{
TreeNode* curNode = treeStack.top();
if (curNode->rChild&&(reachSet.find(curNode)== reachSet.end()))
{
reachSet.insert(curNode);
curNode = curNode->rChild;
treeStack.push(curNode);
while (curNode->lChild)
{
treeStack.push(curNode->lChild);
curNode = curNode->lChild;
}
}
else
{
PrintValue(curNode->value);
treeStack.pop();
}
}
cout << endl;
cout << "**********************************************" << endl;
}
void PostOrderR(TreeNode* root)
{
if (NULL == root)
return;
PostOrderR(root->lChild);
PostOrderR(root->rChild);
PrintValue(root->value);
}
int main(int argc, char* argv[])
{
TreeNode* root = new TreeNode(1);
TreeNode* l1 = new TreeNode(2);
TreeNode* r1 = new TreeNode(3);
root->lChild = l1;
l1->rChild = r1;
TreeNode* l2 = new TreeNode(4);
TreeNode* r2 = new TreeNode(5);
r1->lChild = l2;
l2->rChild = r2;
TreeNode* l3 = new TreeNode(6);
TreeNode* r3 = new TreeNode(7);
r2->lChild = l3;
l3->rChild = r3;
InOrder(root);
cout << "*****************中序遍历递归*********************" << endl;
InOrderR(root);
cout << endl;
cout << "**********************************************" << endl;
PreOrder(root);
cout << "*****************前序遍历递归*********************" << endl;
PreOrderR(root);
cout << endl;
cout << "**********************************************" << endl;
PostOrder(root);
cout << "*****************后序遍历递归*********************" << endl;
PostOrderR(root);
cout << endl;
cout << "**********************************************" << endl;
system("pause");
return 0;
}
二叉树的遍历
最新推荐文章于 2023-07-12 17:34:57 发布