二叉树的遍历
struct Node
{
int value;
Node* left;
Node* right;
Node(int val):value(val),left(NULL),right(NULL)
{
}
};
#include <windows.h>
#include <stack>
#include <list>
using namespace std;
void preOrder(Node* root)
{
if(root != NULL)
{
printf("%d-",root->value);
preOrder(root->left);
preOrder(root->right);
}
}
void inOrder(Node* root)
{
if(root != NULL)
{
inOrder(root->left);
printf("%d-",root->value);
inOrder(root->right);
}
}
void postOrder(Node* root)
{
if(root != NULL)
{
postOrder(root->left);
postOrder(root->right);
printf("%d-",root->value);
}
}
void preOrder_2(Node* root)
{
stack<Node*> stk;
Node* cur = root;
while(stk.size() >0 || cur != NULL)
{
while(cur != NULL)
{
printf("%d-",cur->value);
stk.push(cur);
cur = cur->left;
}
if(stk.size() >0)
{
cur = stk.top();
stk.pop();
cur = cur->right;
}
}
}
void inOrder_2(Node* root)
{
stack<Node*> stk;
Node* cur = root;
while(stk.size() >0 || cur != NULL)
{
while(cur != NULL)
{
stk.push(cur);
cur = cur->left;
}
if(stk.size() >0)
{
cur = stk.top();
printf("%d-",cur->value);
stk.pop();
cur = cur->right;
}
}
}
void postOrder_2(Node* root,stack<Node*>& postStk)
{
stack<Node*> stk;
Node* cur = root;
while(stk.size() >0 || cur != NULL)
{
while(cur != NULL)
{
stk.push(cur);
postStk.push(cur);
cur = cur->right;
}
if(stk.size() >0)
{
cur = stk.top();
stk.pop();
cur = cur->left;
}
}
}
void levelOrder(Node* root)
{
Node* cur = root;
list<Node*> nList;
nList.push_back(cur);
while(nList.size() >0)
{
cur = nList.front();
nList.pop_front();
printf("%d-",cur->value);
if(cur->left != NULL)
{
nList.push_back(cur->left);
}
if(cur->right != NULL)
{
nList.push_back(cur->right);
}
}
}
void createTree(Node*& root)
{
root = new Node(0);
Node* n1 = new Node(1);
Node* n2 = new Node(2);
Node* n3 = new Node(3);
Node* n4 = new Node(4);
Node* n5 = new Node(5);
Node* n6 = new Node(6);
Node* n7 = new Node(7);
Node* n8 = new Node(8);
root->left = n1;
root->right = n2;
n1->left = n3;
n1->right = n4;
n2->left = n5;
n2->right = n6;
n3->left = n7;
n3->right = n8;
}
int main(int argc, char* argv[])
{
Node* root;
createTree(root);
preOrder(root);
printf("\n");
preOrder_2(root);
printf("\n");
inOrder(root);
printf("\n");
inOrder_2(root);
printf("\n");
postOrder(root);
printf("\n");
stack<Node*> postStk;
postOrder_2(root,postStk);
while(postStk.size() >0)
{
printf("%d-",postStk.top()->value);
postStk.pop();
}
printf("\n");
levelOrder(root);
system("pause");
return 0;
}
{
int value;
Node* left;
Node* right;
Node(int val):value(val),left(NULL),right(NULL)
{
}
};
#include <windows.h>
#include <stack>
#include <list>
using namespace std;
void preOrder(Node* root)
{
if(root != NULL)
{
printf("%d-",root->value);
preOrder(root->left);
preOrder(root->right);
}
}
void inOrder(Node* root)
{
if(root != NULL)
{
inOrder(root->left);
printf("%d-",root->value);
inOrder(root->right);
}
}
void postOrder(Node* root)
{
if(root != NULL)
{
postOrder(root->left);
postOrder(root->right);
printf("%d-",root->value);
}
}
void preOrder_2(Node* root)
{
stack<Node*> stk;
Node* cur = root;
while(stk.size() >0 || cur != NULL)
{
while(cur != NULL)
{
printf("%d-",cur->value);
stk.push(cur);
cur = cur->left;
}
if(stk.size() >0)
{
cur = stk.top();
stk.pop();
cur = cur->right;
}
}
}
void inOrder_2(Node* root)
{
stack<Node*> stk;
Node* cur = root;
while(stk.size() >0 || cur != NULL)
{
while(cur != NULL)
{
stk.push(cur);
cur = cur->left;
}
if(stk.size() >0)
{
cur = stk.top();
printf("%d-",cur->value);
stk.pop();
cur = cur->right;
}
}
}
void postOrder_2(Node* root,stack<Node*>& postStk)
{
stack<Node*> stk;
Node* cur = root;
while(stk.size() >0 || cur != NULL)
{
while(cur != NULL)
{
stk.push(cur);
postStk.push(cur);
cur = cur->right;
}
if(stk.size() >0)
{
cur = stk.top();
stk.pop();
cur = cur->left;
}
}
}
void levelOrder(Node* root)
{
Node* cur = root;
list<Node*> nList;
nList.push_back(cur);
while(nList.size() >0)
{
cur = nList.front();
nList.pop_front();
printf("%d-",cur->value);
if(cur->left != NULL)
{
nList.push_back(cur->left);
}
if(cur->right != NULL)
{
nList.push_back(cur->right);
}
}
}
void createTree(Node*& root)
{
root = new Node(0);
Node* n1 = new Node(1);
Node* n2 = new Node(2);
Node* n3 = new Node(3);
Node* n4 = new Node(4);
Node* n5 = new Node(5);
Node* n6 = new Node(6);
Node* n7 = new Node(7);
Node* n8 = new Node(8);
root->left = n1;
root->right = n2;
n1->left = n3;
n1->right = n4;
n2->left = n5;
n2->right = n6;
n3->left = n7;
n3->right = n8;
}
int main(int argc, char* argv[])
{
Node* root;
createTree(root);
preOrder(root);
printf("\n");
preOrder_2(root);
printf("\n");
inOrder(root);
printf("\n");
inOrder_2(root);
printf("\n");
postOrder(root);
printf("\n");
stack<Node*> postStk;
postOrder_2(root,postStk);
while(postStk.size() >0)
{
printf("%d-",postStk.top()->value);
postStk.pop();
}
printf("\n");
levelOrder(root);
system("pause");
return 0;
}