北航 机试 文章识别 二插搜索树

题目

描述:输入一篇可能未经排版的文章,挑选出其中的单词【单词中不包含“(”等特殊符号】,然后按字典序输出。
输入:从文件中读取文章
输出:按字典序输出单词
样例:
输入
When most kids go to school and study the knowledge, few special kids have made their history. A small boy from America wins the international contest by defeating adult competitors. He becomes the youngest winner. He has the gift and so many parents want to have such a kid. Though we are not that smart, we still can study hard to realize our dreams.
输出
a
adult
america
and
are
becomes
boy
by
can
competitors
contest
defeating
dreams
few
rom
gift
go
hard
has
have
he
history
international
kid
kids
knowledge
made
many
most
not
our
p
rents
realize
school
small
smart
so
special
still
study
such
that
the
their
though
to
want
we
when
winner
wins
youngest

代码
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>

typedef struct Node{
    char w[50];
    struct Node* parent;
    struct Node* lchild;
    struct Node* rchild;
}BTree;

void binary_insert(BTree* T,BTree* w){
    BTree* y =NULL;
    BTree* x = T->lchild;

    int res = 0;
    while(x!=NULL){
        y = x;
        res = strcmp(w->w,x->w);
        if(res>0){
            x = x->rchild;
        }
        else if(res<0){
            x = x->lchild;
        }
        else{
            return;
        }
    }
    w->parent=y;
    if(y == NULL){
        T->lchild=w;
    }
    else if(res>0){
        y->rchild=w;
    }
    else if(res<0){
        y->lchild=w;
    }
}

//the order of dictionary

void inOrder(BTree* root){
    if(root == NULL){
        return;
    }
    else{
        inOrder(root->lchild);
        printf("%s\n",root->w);
        inOrder(root->rchild);
    }
}

int main(){
    FILE* fp;
    int c;
    char word[50];
    BTree T;
    BTree* mid;
    T.lchild=NULL;
    int count = 0;
    if( (fp = fopen("./article.txt","r"))==NULL){
        puts("error");
        exit(0);
    }
    int i=0;
    while(!feof(fp)){
        c = getc(fp);
        if((c>= 65 && c<=90) || (c>=97 && c<=122)) {//过滤非字母符号
            word[i++] = (char)tolower(c);
        }
        else if(i>0){ //碰见一个非字母符号产生单词
            word[i] = '\0';
            count++;
            i=0;
            mid =(BTree*)malloc(sizeof(BTree));
            mid->lchild=mid->rchild=mid->parent = NULL;
            strcpy(mid->w,word);
            //插入字母到平衡二叉树
            //printf("%s ***",mid->w);
            binary_insert(&T,mid);
        }
    }
    if(T.lchild == NULL){
        printf("*****");
    }
    inOrder(T.lchild);
    return 0;
}

/*
 * dosen'twork
 else if(){
    BTree mid;
    mid.lchild=mid.rchild=mid.parent=null;
    strcpy(mid.w,word);
 }

 the problem of pointer

 x is same woth w  in the function of  binaer_insert()
 * */
遇到的问题
  1. idea打开文件问题 需要在Edit Configration 里面修改当前工作路劲,不然找不到文件
  2. 局部标量使用,代码需要不断产生新的节点,如果是局部变量的话,节点会在在出了{}之后被清空,不能正常访问
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值