您的位置:首页 > 大数据 > 人工智能

03-树3 Tree Traversals Again

2016-04-21 16:44 501 查看
03-树3 Tree Traversals Again (25分)

An inorder binary tree traversal can be implemented in a non-recursive way with a stack. For example, suppose that when a 6-node binary tree (with the keys numbered from 1 to 6) is traversed, the stack operations are:
push(1); push(2); push(3); pop(); pop(); push(4); pop(); pop(); push(5); push(6); pop(); pop(). Then a unique binary tree (shown in Figure 1) can be generated from this sequence of operations. Your task is to give the postorder traversal sequence of this tree.



Figure 1


Input Specification:

Each input file contains one test case. For each case, the first line contains a positive integer NN (\le
30≤30)
which is the total number of nodes in a tree (and hence the nodes are numbered from 1 to NN).
Then 2N2N lines
follow, each describes a stack operation in the format: "Push X" where X is the index of the node being pushed onto the stack; or "Pop" meaning to pop one node from the stack.


Output Specification:

For each test case, print the postorder traversal sequence of the corresponding tree in one line. A solution is guaranteed to exist. All the numbers must be separated by exactly one space, and there must be no extra
space at the end of the line.


Sample Input:

6
Push 1
Push 2
Push 3
Pop
Pop
Push 4
Pop
Pop
Push 5
Push 6
Pop
Pop


Sample Output:

3 4 2 6 5 1


题目思路:以先序遍历压栈,中序遍历pop,后序遍历输出

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

#define MaxTree 30
#define ElementType int
#define ERROR -1
#define BinTree int

/*以先序遍历压栈,中序遍历pop,后序遍历输出*/

//struct TreeNode
//{
//	ElementType Element;
//	BinTree Left;
//	BinTree Right;
//}T1[MaxTree];

//***************堆栈相关 ***************************************
typedef int Position;
struct SNode {
ElementType *Data; /* 存储元素的数组 */
Position Top;      /* 栈顶指针 */
int MaxSize;       /* 堆栈最大容量 */
};
typedef struct SNode *Stack;

Stack CreateStack( int MaxSize )
{
Stack S = (Stack)malloc(sizeof(struct SNode));
S->Data = (ElementType *)malloc(MaxSize * sizeof(ElementType));
S->Top = -1;
S->MaxSize = MaxSize;
return S;
}

bool IsFull( Stack S )
{
return (S->Top == S->MaxSize-1);
}

bool Push( Stack S, ElementType X )
{
if ( IsFull(S) ) {
printf("堆栈满");
return false;
}
else {
S->Data[++(S->Top)] = X;
return true;
}
}

bool IsEmpty( Stack S )
{
return (S->Top == -1);
}

ElementType Pop( Stack S )
{
if ( IsEmpty(S) ) {
printf("堆栈空");
return ERROR; /* ERROR是ElementType的特殊值,标志错误 */
}
else
return ( S->Data[(S->Top)--] );
}

ElementType GetTopElement(Stack S){
return(S->Data[S->Top]);
}

//***************全局变量************************************************
int preorder[MaxTree]={0}, inorder[MaxTree]={0}, postorder[MaxTree]={0};
int pre_i = 0 , in_i = 0, post_i = 0;

//***********************************************************************
//通过中序遍历和前序遍历来得到后序遍历
void GetPostOrder(int pre_i,int in_i,int post_i,int N)
{
if(N==0)//没有输入节点
{
return;
}
else if(N == 1){//只有根节点
postorder[post_i+N-1] = preorder[pre_i];
return ;
}//后序遍历的最后一个节点和前序遍历的第一个节点为根节点

int left,right,root;
root = preorder[pre_i];
postorder[post_i+N-1] = root;
for(int i = 0; i < N; i++)//通过中序遍历来找到左子树和右子树
{
if(inorder[in_i+i] == root)
{
left = i;//left tree numnode
right = N - i - 1;//right tree num node
break;
}
}
//遍历左子树和右子树
GetPostOrder(pre_i+1, in_i,post_i,left);
GetPostOrder(pre_i+left+1, in_i+left+1, post_i+left,right);

}

//***********************************************************

int main()
{
int data,N;
char str[30];

//freopen("test.txt", "r", stdin);
scanf("%d\n",&N);

Stack S1 = CreateStack(MaxTree);

for(int i = 0; i < (2*N); i++ ){
scanf("%s",str);
if(!strcmp(str,"Push"))//strcmp比较两个字符串,相等输出0
{
scanf("%d",&data);
preorder[pre_i++] = data;
Push(S1,data);
}
else if(!strcmp(str,"Pop"))
{
inorder[in_i++]	= Pop(S1);
}
}

GetPostOrder(0,0,0,N);

int flag = 1;

for(int j = 0; j < N; j++ ){
if(flag){
flag = 0;
printf("%d",postorder[j]);
}
else{
printf(" %d",postorder[j]);
}

}

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: