PTA.3——tree(1)

 第一题:Binary Search Tree 输出某一层

Give a sequence of distinct integer to build the binary search tree, and output the nodes on particular level.

输入格式:

3 lines.

The first line means the number of sequence. This number is more than 1 and less than 20.

The second line gives the sequence of distinct integer. The integers are divided by comma. You must build the tree according to the input order.

The third line means the number of level(>=1). The root node is located on level 1. And the children of the root is located on level 2. And so on.

输出格式:

1 line.

A sequence of numbers divided by comma. The numbers represent the nodes on the given level from left to right.

If there is no node on the level, then output -1.

输入样例:

在这里给出一组输入。例如:

10
32,3,56,4,75,53,33,76,90,2,
3

输出样例:

在这里给出相应的输出。例如:

2,4,53,75,

分析: 

1.需要分层——设置level变量关注层级

2.步骤:构建——插入——输出

代码:

#include<stdio.h>
#include<stdlib.h>
typedef struct Tnode{
    int data;
    int level;
    struct Tnode* lchild;
    struct Tnode* rchild;
}* PtrtoT;
PtrtoT Creat(){//创建头节点
    PtrtoT Head=malloc(sizeof(struct Tnode));
    scanf("%d",&Head->data);
    getchar();
    Head->level=1;
    Head->lchild=NULL;
    Head->rchild=NULL;
    return Head;
}
PtrtoT Insert(PtrtoT T,int s,int count){//插入新节点
    if(T==NULL){
        T=malloc(sizeof(struct Tnode));
        if(T==NULL)//不要忘记!!!!
            printf("Error!");
        else{
            T->data=s;
            T->level=count;
            T->lchild=NULL;
            T->rchild=NULL;
        }
    }
    else{
        if(T->data>s){//比root小,去左边
            T->lchild=Insert(T->lchild,s,++count);
        }
        else if(T->data<s)
            T->rchild=Insert(T->rchild,s,++count);
    }
    return T;
}
void treeprintf(int m,PtrtoT S,int *pa){//遍历
    if(S==NULL)
        return;
    else{
        treeprintf(m,S->lchild,pa);
        if(S->level==m){
            printf("%d,",S->data);
            *pa=1;
        }
        treeprintf(m,S->rchild,pa);
}
    }
    
int main(){
    int number;
    scanf("%d",&number);//先输入再建树
    PtrtoT Tree=Creat();//root
    int i;
    for(i=2;i<=number;i++){//注意这里从2开始
        int new;
        scanf("%d",&new);//要插入的数字
        getchar();
        int m=1;
        Insert(Tree,new,m);//将new插进tree里面
    }
    int print;
    scanf("%d",&print);
    int flag=0;
    treeprintf(print,Tree,&flag);
    if(flag==0)
    printf("-1");
}

注意:

如何判断遍历的层级==需要的层级?

设置flag,用指针形式传进函数,值得注意的是指针赋值时要加*,如  *pa=1

第二题:Traverse Binary Search Tree 先序遍历

Given a sequence of distinct integers to build the binary search tree, and traverse the tree by Preorder.

输入格式:

1line.
The line gives a sequence of distinct integers. These integers are separated by commas. The binary search tree must be built according to the input order of these integers.

输出格式:

1 line.
One sequence of integers separated by commas. The sequence is the traversal result of the Preorder.

输入样例:

在这里给出一组输入。例如:

4,2,6,1,3,5,7,

输出样例:

在这里给出相应的输出。例如:

4,2,1,3,6,5,7,

代码:

#include<stdio.h>
#include<stdlib.h>
typedef struct Tnode{
    int data;
    struct Tnode* lchild;
    struct Tnode* rchild;
}* PtrtoT;
PtrtoT Creat(){
    PtrtoT Head=malloc(sizeof(struct Tnode));
    scanf("%d",&Head->data);
    getchar();
    Head->lchild=NULL;
    Head->rchild=NULL;
    return Head;
}
PtrtoT Insert(PtrtoT T,int s){
    if(T==NULL){
        T=malloc(sizeof(struct Tnode));
        if(T==NULL)
            printf("Error!");
        else{
            T->data=s;
            T->lchild=NULL;
            T->rchild=NULL;
        }
    }
    else{
        if(T->data>s){//比root小,去左边
            T->lchild=Insert(T->lchild,s);
        }
        else if(T->data<s)
            T->rchild=Insert(T->rchild,s);
    }
    return T;
}
void treeprintf(PtrtoT S){
    if(S){//前序输出
	    printf("%d,",S->data);
		treeprintf(S->lchild);//然后左
		treeprintf(S->rchild);//然后右
	}
}
int main(){
    PtrtoT Tree=Creat();//root
    int new;
    //这里的设计很重要
    while((scanf("%d",&new))==1&&new){
        getchar();//不要忘记
        Insert(Tree,new);//将new插进tree里面
    }
    treeprintf(Tree);
}

 注意:

如何判断输入已结束?

scanf仍能运行,且插入变量不为空(这里还没搞懂🥺)

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值