小编Sho*_*ama的帖子

C标准二叉树

就C编程而言,我几乎都是一个菜鸟.

尝试了几天从表单的表达式创建二叉树:

A(B,C(D,$))
Run Code Online (Sandbox Code Playgroud)

每个字母都是节点.

'(' 在我的树下(向右)下降.

',' 去我树的左侧分支

'$' 插入一个NULL节点.

')' 意味着上升到一个水平.

这是我在编码2-3天后想出来的:

#define SUCCESS 0

typedef struct BinaryTree
{
char info;
BinaryTree *left,*right,*father;
}BinaryTree;



int create(BinaryTree*nodeBT, const char *expression)
{   
    nodeBT *aux;
    nodeBT *root;
    nodeBT *parent;
    nodeBT=(BinaryTree*) malloc (sizeof(BinaryTree));         
        nodeBT->info=*expression;
    nodeBT->right=nodeBT->left=NULL;
    nodeBT->father = NULL;

    ++expression;   
    parent=nodeBT;                                                 
    root=nodeBT;

    while (*expression)
        {if (isalpha (*expression))
            {aux=(BinaryTree*) malloc (sizeof(BinaryTree));
             aux->info=*expression;
             aux->dr=nodeBT->st=NULL;
             aux->father= parent;
             nodeBT=aux;}

        if (*expression== '(')
            {parent=nodeBT;
            nodeBT=nodeBT->dr;}

        if (*expression== ',')
            {nodeBT=nodeBT->father;
            nodeBT=nodeBT->dr;}

        if (*expression== ')')
            {nodeBT=nodeBT->father;
            parent= nodeBT->nodeBT;}

        if (*expression== '$') …
Run Code Online (Sandbox Code Playgroud)

c tree binary-tree data-structures

4
推荐指数
1
解决办法
854
查看次数

标签 统计

binary-tree ×1

c ×1

data-structures ×1

tree ×1