`
zjjzmw1
  • 浏览: 1353368 次
  • 性别: Icon_minigender_1
  • 来自: 开封
社区版块
存档分类
最新评论

二叉树的思路,查找1000个成语中的某一个成语。

阅读更多




#include<stdio.h>
#define MAX1 8800000 //准备查找成语文件的大小。
//把成语转换为二进制形式。
void toBinary(char *c,char *binaryArray) {
    char ch[9] = {'\0'};
    int k = 0;
    int i;
    int j = 0;
    while ('\0' != c[j])//一个字一个字的循环。
    {
        if (c[j] != ',') {//当没有遇见逗号的时候。
            for (i = 7;i >= 0;i--)
            {
                ch[i] = (c[j]&1)+'0';
                c[j]>>=1;
            }
            for (int kk = 0; kk<=7; ++kk) {
                binaryArray[k] = ch[kk];
                k++;
            }
        }else {
            binaryArray[k] = ',';
            k++;
        }
        j++;
    }
}
//定义自己的二叉树。
typedef struct Node
{
    char data;
    struct Node * left;
    struct Node * right;
}Node;
//把二进制生成树的方法。

Node *binaryToTree(char res[]){
    Node * p = NULL;
    p = (Node *)malloc(sizeof(Node));
    p->data = ' ';//二叉树的跟没有值。
    int flag = 0;
    Node * tempP = p;
    tempP->left = NULL;
    tempP->right = NULL;
    while (res[flag]!='\0') {
        if (res[flag]!=',') {
            if ((res[flag]=='0')&&(tempP->left==NULL)) {
                Node * p1 = (Node *)malloc(sizeof(Node));
                p1->left = NULL;
                p1->right = NULL;
                p1->data = res[flag];
                tempP->left=p1;
                tempP = p1;
            }else if ((res[flag]=='1')&&(tempP->right==NULL)){
                Node * p2 = (Node *)malloc(sizeof(Node));
                p2->left = NULL;
                p2->right = NULL;
                p2->data = res[flag];
                tempP->right=p2;
                tempP = p2;
            }else{
                tempP = res[flag]=='0'?tempP->left:tempP->right;
            }
        }else{
            tempP = p;
        }
        flag++;
    }
    return p;
}

int isInTree(Node *p,char c[]){
    Node * tempP = p;
    int result = 1;
    for (int i=0; i<96; ++i) {
        if ((tempP->left!=NULL)&&(c[i]==tempP->left->data)) {
            tempP = tempP->left;
        }else if((tempP->right!=NULL)&&(c[i]==tempP->right->data)){
            tempP = tempP->right;
        }else{
            result = 0;
        }
    }
    return result;
}





void outputBinaryTree_pre(Node * head)
{
    if(head)
    {
        printf("%c", head->data);
        outputBinaryTree_pre(head->left);
        outputBinaryTree_pre(head->right);
    }
}


int main()
{
    FILE *fp;
    char name[97] = "张明漂亮";//要查找的内容。
    //小的时候用栈完全可以,大的时候,必须用堆。
    char *filename = malloc(MAX1);//文件存放的内容。96*80000
    memset(filename,0,MAX1);//初始化为0,没有必要。
    char *bijiao = malloc(MAX1);
    memset(bijiao,0,MAX1);
    toBinary(name, bijiao);//把要找的内容转换为二进制。
    NSLog(@"bijiao===%s==",bijiao);
    if((fp=fopen("/Users/zhangmingwei/Desktop/33.txt","r"))==NULL) //以读写方式打开
    {
        printf("Can not open file\n");
        return 0;
    }
    fgets(filename, MAX1, fp);//读取文件内容。
    char *res = malloc(MAX1);
    //res 就是一会需要比较的那个包含10万个二进制的成语的字符数组了。
    toBinary(filename, res);//把文件内容转换为二进制。用第二种方法来判断。
    //现在只需要查找,看bijiao 是否在res 中就行了。res 中的数据是以逗号分割的。
//    printf("\nres===%s\n",res);
   
    Node *p = NULL;
    p = (Node *)malloc(sizeof(Node));
    p = binaryToTree(res);
    printf("\n============\n");
    outputBinaryTree_pre(p);
    printf("\n============\n");
    if (isInTree(p, bijiao)) {
        printf("\n成功了\n");
    }else{
        printf("\n失败了\n");
    }
   
   
    fclose(fp);
    free(filename);
    free(bijiao);
    free(res);
    return 0;
}


1
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics