USTC机试—根据文件中的一串数字建立二叉排序树并且

//算法思想:根据输入序列建立二叉排序树,最后用后序遍历输出该二叉树
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define N 100
 struct  Node{
    struct Node *lchild;
 struct Node *rchild;
 int c;
}Tree[N];//静态结点N个
int loc=0;
Node *create(){//静态分配一个结点
    Tree[loc].lchild=Tree[loc].rchild=NULL;
 return &Tree[loc++];
}
int count=0;//输入整数数组的下标
Node * bstInsert(Node *T,int key){//二叉排序树的创建,其一定是插在根节点上的
    if(T==NULL){T=create();T->c=key;return T;}
    if(key<T->c){T->lchild=bstInsert(T->lchild,key);}
 if(key>T->c){T->rchild=bstInsert(T->rchild,key);}
 return T;//返回根节点指针
}
void postOrder(Node *T){//后序遍历输出字符串
 if(T->lchild!=NULL)postOrder(T->lchild);
 if(T->rchild!=NULL)postOrder(T->rchild);
      printf("%d ",T->c);
}
int main(){
 int temp[N];
 int i=0;
 scanf("%d",&temp[i]);
    while(temp[i]!=0){
   scanf("%d",&temp[++i]);
 }//输入数据
 Node *T=NULL;
 int count=0;
 while(temp[count]!=0){
 T=bstInsert(T,temp[count++]);
 }
 postOrder(T);
 printf("\n");
return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值