선착순

2408 단어 cnull
#include<stdio.h>
#include<iostream>
#include<stack>
using namespace std;

class Node
{
public:
	Node(char data,Node *left,Node *right):data(data),left(left),right(right),flag(false){}
	char data;
	Node *left,*right;
	bool flag;//rightBrach_Dealed
};
void PrintPre(Node *head)
{
	stack<Node*> s;
	Node *pNode=head;

	while(pNode || !s.empty())
	{
		while(pNode)
		{
			cout<<pNode->data;
			s.push(pNode);
			pNode=pNode->left;
		}
		if(!s.empty())
		{
			pNode=s.top();
			pNode=pNode->right;
			s.pop();
		}
	}
	cout<<endl;
}
void PrintIn(Node *head)
{
	if(head==NULL)
		return;
	stack<Node*> s;
	Node *pNode=head;

	while(pNode || !s.empty())
	{
		while(pNode)
		{
			s.push(pNode);
			pNode=pNode->left;
		}
		if(!s.empty())
		{
			pNode=s.top();
			cout<<pNode->data;
			pNode=pNode->right;
			s.pop();
		}
	}
	cout<<endl;
}
void PrintPost(Node *head)
{
	stack<Node*> s;
	Node *pNode=head; 

	while(pNode || !s.empty())
	{
		while(pNode)
		{
			s.push(pNode);
			pNode=pNode->left;
		}
		if(!s.empty())
		{
			pNode=s.top();
			if(pNode->right==NULL || pNode->flag==true)
			{
				cout<<pNode->data;
				s.pop();
				pNode=NULL;
			}
			else
			{
				pNode->flag=true;
				pNode=pNode->right;
			}
		}
	}
	cout<<endl;
}
int main()
{
	Node *Y=new Node('Y',new Node('Z',NULL,NULL),NULL);
	Node *B=new Node('B',new Node('X',Y,NULL),new Node('D',NULL,NULL));
	Node *C=new Node('C',new Node('E',NULL,NULL),new Node('F',new Node('G',NULL,NULL),NULL));
	Node *A=new Node('A',B,C);
	/*
	                  A
			B					C
		 X     D             E     F
	   Y                          G
	  Z

	*/
	PrintPre(A);
	PrintIn(A);
	PrintPost(A);
	return 0;
}

좋은 웹페이지 즐겨찾기