poj-1577 Falling Leaves (BST建树+遍历)

 考试时,一个不被仔细读的题 QAQ 记录下来,吸取教训

K - Falling Leaves(25)

 POJ - 1577 

 
Figure 1
Figure 1 shows a graphical representation of a binary tree of letters. People familiar with binary trees can skip over the definitions of a binary tree of letters, leaves of a binary tree, and a binary search tree of letters, and go right to The problem. 

A binary tree of letters may be one of two things: 

  1. It may be empty. 
  2. It may have a root node. A node has a letter as data and refers to a left and a right subtree. The left and right subtrees are also binary trees of letters.

In the graphical representation of a binary tree of letters: 

  1. Empty trees are omitted completely. 
  2. Each node is indicated by 
    • Its letter data, 
    • A line segment down to the left to the left subtree, if the left subtree is nonempty, 
    • A line segment down to the right to the right subtree, if the right subtree is nonempty.

A leaf in a binary tree is a node whose subtrees are both empty. In the example in Figure 1, this would be the five nodes with data B, D, H, P, and Y. 

The preorder traversal of a tree of letters satisfies the defining properties: 

  1. If the tree is empty, then the preorder traversal is empty. 
  2. If the tree is not empty, then the preorder traversal consists of the following, in order 
    • The data from the root node, 
    • The preorder traversal of the root's left subtree, 
    • The preorder traversal of the root's right subtree.

The preorder traversal of the tree in Figure 1 is KGCBDHQMPY. 

A tree like the one in Figure 1 is also a binary search tree of letters. A binary search tree of letters is a binary tree of letters in which each node satisfies: 

The root's data comes later in the alphabet than all the data in the nodes in the left subtree. 

The root's data comes earlier in the alphabet than all the data in the nodes in the right subtree. 

The problem: 

Consider the following sequence of operations on a binary search tree of letters 

Remove the leaves and list the data removed 
Repeat this procedure until the tree is empty 
Starting from the tree below on the left, we produce the sequence of trees shown, and then the empty tree 


by removing the leaves with data 

BDHPY 
CM 
GQ 


Your problem is to start with such a sequence of lines of leaves from a binary search tree of letters and output the preorder traversal of the tree.

Input

The input will contain one or more data sets. Each data set is a sequence of one or more lines of capital letters. 

The lines contain the leaves removed from a binary search tree in the stages described above. The letters on a line will be listed in increasing alphabetical order. Data sets are separated by a line containing only an asterisk ('*'). 

The last data set is followed by a line containing only a dollar sign ('$'). There are no blanks or empty lines in the input.

Output

For each input data set, there is a unique binary search tree that would produce the sequence of leaves. The output is a line containing only the preorder traversal of that tree, with no blanks.

Sample Input

BDHPY
CM
GQ
K
*
AC
B
$

Sample Output

KGCBDHQMPY
BAC

一道思路比较简单的BST题,比赛的时候debug了无数遍,结果还是错了QAQ 后来仔细读了一下题。。发觉当时居然连题目要求都没看清 =_=

没看到<搜索树>这个关键词的时候,我以为是以先序的方式建树(愚蠢),这种做法需要注意左右儿子null的情况下,只要插上了一个叶子就得返回,此法理论上是可行的,其实它适用于所有扒树叶的题,只是过于繁琐了。

其实这个题只需要逆序建一个排序树就好了,因为建树时,后放上去的一定都是叶子节点,一个自上到下,一个自下而上,关系就很明了了。

这是正确思路AC的

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct no
{
    char data;
    struct no *lc,*rc;
} node;
void p(node *root)//输出
{
    if(root)
    {
        printf("%c",root->data);
        p(root->lc);
        p(root->rc);
    }
}
node *creat(node *root,char x)
{
    if(root==NULL)
    {
        root=(node *)malloc(sizeof(node));
        root->data=x;
        root->lc=root->rc=NULL;
    }
    else if(x>root->data)
        root->rc=creat(root->rc,x);
    else if(x<root->data)
        root->lc=creat(root->lc,x);
    return root;
}
int main()
{
    char a,que[503];
    int top=0,i;
    node *root;
    memset(que,0,sizeof(que));
    root=NULL;
    while(scanf("%c",&a)!=EOF)
    {
        if(a>='A'&&a<='Z')
            que[top++]=a;
        else if(a=='*'||a=='$')
        {
            for(i=top-1; i>=0; i--)
            {
                root=creat(root,que[i]);
            }
            p(root);
            printf("\n");
            memset(que,0,sizeof(que));
            top=0;
            root=NULL;
        }
        if(a=='$')break;
    }
    return 0;
}

这是错误思路的代码,运行不出来,我认为理论上是可以的。先贴上抽空再改改

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct no
{
    char data;
    struct no *lc,*rc;
} node;
int f;
void p(node *root)//输出
{
    if(root)
    {
        printf("%c",root->data);
        p(root->lc);
        p(root->rc);
    }
}
node *vis(node *root,int x)//安上
{
    node *p;
    if(root->lc&&root->rc)return root;

    p=(node *)malloc(sizeof(node));
    p->lc=p->rc=NULL;
    p->data=x;

    if(!root->lc)
        root->lc=p,f=1;
    else if(!root->rc)
        root->rc=p,f=1;
    return root;
}
node *pre(node *root,int x)//找位置
{
    f=0;
    if(f==0)
    {  //printf("111111111111");
        root=vis(root,x);
        if(f==1)
            return root;
        pre(root->lc,x);
        pre(root->rc,x);
    }
    return root;
}
void creat(char que[],int len[])
{
    node *root;
    int i,j;
    root=(node *)malloc(sizeof(node));
    root->lc=root->rc=NULL;
    for(j=0; len[j]!=0; j++)
    {
        for(i=0; i<=len[j]-1; i++)
        {
            root=pre(root,que[i]);
        }
    }
     p(root);
}
int main()
{
    char a[503],que[503];
    int len[503],top,i,v;
    v=0,top=0;
    memset(len,0,sizeof(len));
    while(scanf("%s",a)!=EOF&&a[0]!='$')
    {
        if(a[0]=='*')
        {
            creat(que,len);
            top=0;
            v=0;
            memset(len,0,sizeof(len));
        }
        len[v]=strlen(a);
        //printf("%d,%d\n",len[v],v);
        for(i=0; i<=len[v]-1; i++)
            que[top++]=a[i];
        v++;
    }
    return 0;
}

 

猜你喜欢

转载自blog.csdn.net/flyf000/article/details/81738551
今日推荐