#include<stdio.h>
#include<stdio.h>
#define MaxNameLen 100
typedef struct TreeNode{
    char* name;
    Struct TreeNode* left_child;
    Struct TreeNode* right_child;
}TreeNode;

TreeNode* NewTreeNode(const char* name){
    TreeNode* node = NULL;
    node = (TreeNode*)malloc(size of(TreeNode));
    memset(node,0,sizeof(TreeNode));
    node->name = strdup(name);
    return node
}

void FreeTreenode(TreeNode* node){
    if(node->name) free(node->name);
    if(node->left_child) FreeTreeNode(node->left_child);
    if(node->right_child) FreeTreeNode(node->right_child);
    memset(node,0,sizeof(*node));
}

void delete_space(char* str){
    int i=0,j=0;
    while(str[j]==0x20) j++;
    while(str[j]) str[i++]=str[j++];
    str[i]=0;
    while(i>0&&str[i]==0x20) str[i--]=0;
}

int GetNodeName(char* name,const char* str){
    int len=0;
    const char* bkstr=str;
    name[0]=0;
    while(*str){
        if(*str==',') break;
        if(*str=='{') break;
        if(*str=='}') break;
        name[len]=*str;
        name[len+1]=0;
        str++;
        len++;
        if(len>=MaxNameLen){
            printf("[error],too long name %s!\r\n",name);
            exit(0);
        }
    }
    delete_space(name);
    return str-bkstr;
}

TreeNode* GetTreeNode(char* str){
    char name[MaxNameLen+1];
    TreeNode* node;
    int len,n,found_b=0,found_c=0;
    char* bkstr;
    char bkch;

    len=GetTreeNode(name);
    node=NewTreeNode(name);
    if(len==0) return NULL;
    if(str[len]!='{'){
        return node;
    }
    str+=len+1;
    n=1;
    bkstr=str;
    while(*str){
        if(*str=='{') n++;
        if(*str=='}'){
            n--;
            if(n==0){
                bkch = *str;
                *str=0;
                node->left_child=GetTreeNode(bkstr);
                *str=bkch;
                found_b=1;
                found_c=1;
                str++;
                break;
                }
        }
        if(*str==','){
            if(n==1){
                bkch=*str;
                *str=0;
                node->left_chlid=GetTreeNode(bkstr);
                *str=bkch;
                found_b=1;
                str++;
                break;
            }
        }
        *str++;
    }
    if(found_b==0){
        return node;
    }
    if(found_c) return node;
    bktsr=str;

    while(*str){
        if(*str=='{') n++;
        if(*str=='}') n--;
        if(n==0){
            bkch=*str;
            *str=0;
            node->right_child=GetTreeNode(bkstr);
            *str=bkch;
            found_c=1;
            str++;
            break;
        }
        *str++;
    }
}

void PrintTree(TreeNode* root){
    if(root==NULL) return;
    if(root->left_child) PrintTree(root->left_child);
    printf("%s",root->name);
    if(root->right_child) PrintTree(root->right_child);
}

void FreeTree(TreeNode* root){
    FreeTreeNode(root);
}

in mian(int argc,char* argv[]){
    TreeNode* root=NULL;
    char teststr[1024];
    if(argc<2){
        scanf("%s",teststr);
    }
    else{
        strcpy(teststr,argv[1];)
    }
    root=GetTreeNode(teststr);
    if(root){
        PrintTree(root);
    }
    if(root) FreeTree(root);
    return 0;
}

  

别人帮写的,能用python实现就好。

2021-1-2,笔记

posted on 2021-01-02 22:49  三天乐趣  阅读(189)  评论(0编辑  收藏  举报