#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stack>
#include <iostream>
typedef struct tag_Tree
{
char data;
struct tag_Tree* lchild;
struct tag_Tree* rchild;
}Tree;
void Visit(Tree* t)
{
printf("元素为:%c\n", t->data);
}
void PreOrder(Tree* root)
{
if (root == NULL)
{
return;
}
Visit(root);
PreOrder(root->lchild);
PreOrder(root->rchild);
}
void InOrder(Tree* root)
{
if (root == NULL)
{
return;
}
InOrder(root->lchild);
Visit(root);
InOrder(root->rchild);
}
void PostOrder(Tree* root)
{
if (root == NULL)
{
return;
}
PostOrder(root->lchild);
PostOrder(root->rchild);
Visit(root);
}
void CountOfLeaf(Tree* root ,int * count)
{
if (root == NULL)
{
return;
}
if ((root->lchild == NULL) && (root->rchild == NULL))
{
++*count;
Visit(root);
}
CountOfLeaf(root->lchild,count);
CountOfLeaf(root->rchild,count);
}
int DepthOfTree(Tree* root)
{
int leftDepth = 0;
int rightDepth = 0;
if (root == NULL)
{
return 0;
}
leftDepth = DepthOfTree(root->lchild);
rightDepth = DepthOfTree(root->rchild);
return (leftDepth > rightDepth ? leftDepth : rightDepth) + 1;
}
int CountOfTree(Tree* root)
{
int lchild = 0;
int rchild = 0;
if (root == NULL)
{
return 0;
}
if ((root->lchild == NULL) && (root->rchild == NULL))
{
return 1;
}
lchild = CountOfTree(root->lchild);
rchild = CountOfTree(root->rchild);
return lchild + rchild;
}
Tree* CopyTree(Tree* root)
{
Tree* newT = NULL;
if (root == NULL) {
return NULL;
}
newT = (Tree*)malloc(sizeof(Tree));
if (newT == NULL) {
return NULL;
}
newT->data = root->data;
newT->lchild = CopyTree(root->lchild);
newT->rchild = CopyTree(root->rchild);
return newT;
}
using namespace std;
Tree* GoLeft(Tree* lchild, stack<Tree*>& s)
{
while (lchild->lchild != NULL)
{
s.push(lchild);
lchild = lchild->lchild;
}
return lchild;
}
void PreOrder2(Tree* root)
{
stack<Tree*> s;
Tree* p = root;
while (!s.empty() || p)
{
if (p)
{
Visit(p);
s.push(p);
p = p->lchild;
}
else
{
p = s.top();
s.pop();
p = p->rchild;
}
}
}
void InOrder2(Tree* root)
{
Tree* t = NULL;
stack<Tree*> s;
if (root == NULL)
{
return;
}
t = GoLeft(root, s);
while (t != NULL)
{
Visit(t);
if (t->rchild != NULL)
{
t = GoLeft(t->rchild, s);
}
else if (!s.empty())
{
t = s.top();
s.pop();
}
else
{
t = NULL;
}
}
}
void InOrder3(Tree* root)
{
stack<Tree*> s;
Tree* p = root;
while (p != NULL || !s.empty())
{
while (p != NULL)
{
s.push(p);
p = p->lchild;
}
if (!s.empty())
{
p = s.top();
Visit(p);
s.pop();
p = p->rchild;
}
}
}
void PostOrder2(Tree* root)
{
if (root == NULL)
{
return;
}
stack<Tree*> s;
Tree* cur;
Tree* pre = NULL;
s.push(root);
while (!s.empty())
{
cur = s.top();
if ((cur->lchild == NULL && cur->rchild == NULL)
|| (pre != NULL && (pre == cur->lchild || pre == cur->rchild)))
{
Visit(cur);
s.pop();
pre = cur;
}
else
{
if (cur->rchild != NULL)
{
s.push(cur->rchild);
}
if (cur->lchild != NULL)
{
s.push(cur->lchild);
}
}
}
}
struct BTNode
{
Tree* btnode;
bool isFirst;
};
void PostOrder3(Tree* root)
{
stack<BTNode*> s;
Tree* p = root;
BTNode* temp;
while (p != NULL || !s.empty())
{
while (p != NULL)
{
BTNode* btn = new BTNode;
btn->btnode = p;
btn->isFirst = true;
s.push(btn);
p = p->lchild;
}
if (!s.empty())
{
temp = s.top();
s.pop();
if (temp->isFirst == true)
{
temp->isFirst = false;
s.push(temp);
p = temp->btnode->rchild;
}
else
{
Visit(temp->btnode);
delete temp;
temp = NULL;
p = NULL;
}
}
}
}
Tree* CreateTree()
{
Tree* t = NULL;
char ch;
scanf("%c", &ch);
if (ch == '#'){
return NULL;
}
else{
t = (Tree*)malloc(sizeof(Tree));
memset(t, 0, sizeof(Tree));
if (t == NULL){
return NULL;
}
t->data = ch;
t->lchild = CreateTree();
t->rchild = CreateTree();
}
return t;
}
int main()
{
Tree* myTree = NULL;
myTree = CreateTree();
if (myTree != NULL)
{
PostOrder(myTree);
}
return 0;
}