zoukankan      html  css  js  c++  java
  • 练习:输入二叉树结构,输出中序遍历--C语言

    #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!
    ",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,笔记

  • 相关阅读:
    Ubuntu在用root账户使用xftp连接时提示拒绝连接
    Ubuntu设置root账户密码
    Ubuntu安装Nginx
    Ubuntu不能上网解决办法
    Ubuntu16.04修改静态ip地址
    Ubuntu下vi编辑器不听话
    thinkpad t420安装debian需要注意的细节
    debian7配置iptables
    debian的甘特图工具
    debian修改ssh端口
  • 原文地址:https://www.cnblogs.com/yuntimer/p/14224315.html
Copyright © 2011-2022 走看看