数据结构——树的错题本

260 阅读2分钟

这是我参与更文挑战的第25天,活动详情查看:更文挑战

树的算法题主要是掌握先序后序中序层次的递归与非递归算法 编写后序遍历二叉树的非递归算法后序遍历非递归

void PostOrder(BiTree T){
    InitStack(S);
    p = T;
    r = NULL;
    while(p || !isEmpty(S)){
        if(p){
            push(S,p);
            p = p->lchild;
        }else{
            GetTop(S,p);
            if(p->rchild && p->rchild!=r){
                //若右子树存在且没被访问过
                p = p->rchild;
                push(S,p);
                p = p->lchild;
            }else{
                pop(S,p);
                visit(p->data);
                r = p;
                p = NULL;
            }
        }
    }
}

2.采用层次遍历的非递归方法求解二叉树的高度层次遍历非递归

int Btdepth(BiTree T){
    if(!T)return 0;
    int front =-1,rear=-1;
    int last =0,level=0;
    BiTree Q[MaxSize];
    Q[++rear] = T;
    BiTree p;
    while(front<rear){
        p=Q[++front];
        if(p->lchild)Q[++rear]=p->lchild;
        if(p->rchild)Q[++rear]=p->rchild;
        if(front == last){
            //该层的最右结点
            level++;
            last = rear;
        }
    }
    return level;
}

算法实现通过先序序列和中序序列确定唯一一颗二叉树《有点折半查找似的》

BiTree PreInCreat(ElemType A[],ElemType B[],int l1,int h1,int l2,int h2){
    //l1,h2为先序的第一和最后一个结点下标,l2,h2为中序的第一和最后一个结点下标
    // 初始调用时 l1 =l2=1,h1 = h2 =n;
    root=(BiTNode *)malloc(sizeof(BiTNode));
    root->data = A[l1];
    for(i=l2;B[i]!=root->data;i++);
    llen = i-l2;
    rlen = h2-i;
    if(llen) 
        root->lchild = PreInCreat(A,B,l1+1,l1+llen,l2,l2+llen-1);
    else
        root->lchild = NULL;
    if(rlen)
        root->rchild = PreInCreat(A,B,h1+1,h1+rlen,h2,h2+rlen-1);
     else
         root->rchild = NULL;
    return root;
}

二叉树采用二叉链存储结构存储,设计一个算法求先序遍历序列中第k‘个结点的值先序遍历递归

int i =1;
ElemType PreNode(BiTree b,int k){
    if(b==NULL)return '#';
    if(i==k)return b->data;
    i++;
    ch=PreNode(b->lchild,k);
    if(ch == '#'){
        ch = PreNode(b->rchild,k);
        return ch;
    }
    return ch;
}

找到二叉树p,q的最近公共结点r

typedef struct{
    BiTree t;
    int tag; // tag=0时代表左节点已被访问,tag=1时代表右节点已被访问
}stack;
stack s[],s1[];
BiTree Ancestor(BiTree ROOT,BiTNode *p,BiTNode *q){
    top = 0;bt=ROOT;
    while(bt!=NULL||top>0){
        while(bt!=NULL&&bt!=p&&bt!=q){
            while(bt!=NULL){
                s[++top].t =bt;
                s[top].tag =0 ;
                bt = bt->lchild;
            }
        }
        while(top!=0&&s[top].tag==1){
            if(s[top].t==p){
                for(i=1;i<=top;i++){
                    s1[i]=s[i];
                    top1=top;
                }
                if(s[top].t==q){
                    for(i=top;i>0;i--){
                        for(j=top1;j>0;j--){
                            if(s1[j].t==s[i].t)
                                return s[i].t;
                        }
                    }
                    //top--;?
                }top--;
            }//while
            
        }
    }
}

算中序线索二叉树里查找指定结点在后序的前驱结点的算法 BiThrTree

InPostPre(BiThrTree t,BiThrTree p){
    BiThrTree q;
    if(p->rtag==0)q = p->rchild;
    else if(p->ltag ==0 )q= p->lchild;
    else if (p->lchild == NULL) q=NULL // p是中序序列第一个结点
        else{
            //找p的祖先,再找祖先的做子女
            while(p->ltag==1&&p->lchild!=NULL)p=p->child;
            if(p->ltag==0){
                q=p->lchild;
            }else{
                q=NULL;
            }
        }
    return q;
    
}