当前位置:网站首页>Creating a binary tree (binary linked list) from a generalized table

Creating a binary tree (binary linked list) from a generalized table

2022-06-25 09:24:00 Building block mathematical modeling

Generalized tables create binary trees ( Binary list

This paper is a non recursive method , Slightly more complicated

1. Note that the node is created first and then assigned , Avoid null pointer exceptions ( t->lc=new BiTNode; t=t->lc;
2.* Note that when creating a node, first assign its left and right subtrees to NULL

// Generalized tables create binary trees 
void BTreeCreate(BT &t,char a[],int n)
{
    
    SS S;
    StackCreate(S);
    int i=1,j;
    t=new BiTNode;
    t->data=a[0];
    while(i<n)
    {
    
        if((a[i]=='(')&&Push(S,t))
        {
    
            j=1;
        }
        else if((a[i]==',')&&SVisit(S,t))
        {
    
            j=0;
        }
        else if((a[i]==')')&&Pop(S,t)) {
    }
        else if(a[i]=='#')
        {
    
            t=NULL;
        }
        else
        {
    
            if(j==1)
            {
    
                t->lc=new BiTNode;
                t=t->lc;
                t->lc=NULL;
                t->rc=NULL;
                t->data=a[i];
            }
            else if(j==0)
            {
    
                t->rc=new BiTNode;
                t=t->rc;
                t->lc=NULL;
                t->rc=NULL;
                t->data=a[i];
            }
        }
        i++;
    }
}
原网站

版权声明
本文为[Building block mathematical modeling]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202200554521446.html