zoukankan      html  css  js  c++  java
  • 王道树算法练习

    //先序非递归
    void PreOrder2(BiTree T) {
        InitStack(S);
        BiTree p=T;
        while(p||!IsEmpty(S)) {
            if(p) {
                visit(p);
                push(S,p);
                p=p->lchild;
            } else {
                pop(S,p);
                p=p->rchild;
            }
        }
    }
    //中序非递归
    void InOrder2(BiTree T) {
        InitStack(S);
        BiTree p=T;
        while(p||!IsEmpty(S)) {
            if(p) {
                push(S,p);
                p=p->lchild;
            } else {
                pop(s,p);
                visit(p);
                p=p->rchild;
    
            }
        }
    }
    
    
    //后序非递归
    void PostOrder(BiTree T) {
        InitStack(S);
        p=T;
        r=NULL;
        while(p||!IsEmpty(S)) {
            if(p) {
                Push(S,p);
                p=p->lchild;
            } else {
                p=Gettop(S);
                if(p->rchild!=NULL&&p->rchild!=r) {
                    p=p->rchild
                      push(s,p);
                    p=p->lchild;
                } else {
                    pop(s,p);
                    visit(p->data);
                    r=p;
                    p=NULL;
                }
            }
        }
    }
    //层次遍历
    void LevelOrder(BiTree T) {
        InitQueue(Q);
        BiTree p;
        EnQueue(Q,T);
    while(!IsEmpty(Q))
    {
    DeQueue(Q,p);
         visit(p);
        if(p->lchild!=NULL)
            EnQueue(Q,p->lchild);
        if(p->rchild!=NULL)
            EnQueue(Q,p->rchild);

    }
    } //已知一棵二叉树按顺序存储结构进行存储,设计一个算法,求编号分别为i和j的两个结点的最近的公共祖先结点的值 ElemType Comm_Ancestor(SqTree T,int i,int j) { if(T[i]!='#'||T[j]!='#') { while(i!=j) { if(i<j) j/=2; if(i>j) i/=2; } return T[i]; } } //二叉树的自下而上,从右向左的层次遍历算法 void InvertLevel(BiTree bt) { Stack s; Queue Q; if(bt!=NULL) { InitStack(s); InitQueue(Q); EnQueue(Q,bt); while(IsEmpty(Q)==false) { DeQueue(Q,p); Push(s,p); if(p->lchild)EnQueue(Q,p->lchild); if(p->rchild)EnQueue(Q,P->rchild); } while(IsEmpty(s)==false) { Pop(s,p); visit(p->data); } } } //假设二叉树采用二叉链表形式存储,设计一个非递归算法求二叉树的高度 int Btdepth(BiTree T) { if(!T)return 0; int front=-1,rear=-1; int level=0,last=0; BiTree Q[maxsize]; Bitree p; Q[++rear]=T; while(front<rear) { p=Q[++front]; level++; if(p->lchild)Q[++rear]=p->lchild; if(p->rchild)Q[++rear]=p->rchild; if(last==front) { level++; last=rear; } return level; } } int Btdepth2(BiTree T) { if(T==NULL)return 0; int ldepth=Btdepth2(T->lchild); int rdepth=Btdepth2(T->rchild); return ldepth>rdepth?ldepth+1:rdepth+1; } //假设二叉树中各结点的值互不相同,其 //先序遍历序列和中序遍历序列分别存于两个一维数组A[1...n]、B[1...n]中编写算法建立该二叉链表 BiTree PreInCreat(ElemType A[],ElemType B[],int l1,int h1,int l2,int h2) { root=(BiTNode*)malloc(sizeof(BiTNode)); root->data=A[l1]; for(i=l2; B[i]!=root->data; i++); int llen=i-l2; int rlen=h2-i; if(llen) { root->lchild=precreate(A,B,l1+1,l1+llen,l2,l2+llen-1); } else root->lchild=NULL; if(rlen) { T->rchild=precreate(A,B,h1-rlen+1,h1,h2-rlen+1,h2); } else root->rchild=NULL; return root; } //二叉树按二叉链表形式存储,写一个判别给定二叉树是否是完全二叉树的算法 bool IsComplete(BiTree T) { InitQueue(Q); if(!T) return 1; EnQueue(Q,T); while(!IsEmpty(Q)) { DeQueue(Q,p); if(p) { EnQueue(Q,p->lchild); EnQueue(Q,p->rchild); } else { while(!IsEmpty(Q)) { DeQueue(Q,p); if(p)return 0; } } } return 1; } //假设二叉树采用二叉链表形式存储,计算双分支结点个数 int DsonNodes(Bitree b) { if(b==NULL) return 0; else if(b->lchild!=NULL&&b->rchild!=NULL) return DsonNodes(b->lchild)+DsonNodes(b->rchild)+1; else return DsonNodes(b->lchild)+DsonNodes(b->rchild); } //交换左右子树 void swap(Bitree T) { if(T) { swap(T->lchild); swap(T->rchild); temp=T->lchild; T-lchild=T-rchild; T->rchild=temp; } } //求先序第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!='#') return ch; ch=PreNode(b->rchild,k); return ch; } //查找所有值x的结点并删去以他为根的子树 void DeleteXTree(BiTree bt) { if(bt) { DeleteXTree(bt->lchild); DeleteXTree(bt->rchild); free(bt); } } void Search(BiTree bt,ElemType x) { BiTree Q[]; if(bt) { if(bt->data==x) { DeleteXTree(bt); exit(0); } } InitQueue(Q); EnQueue(Q,bt); while(!IsEmpty(Q)) { DeQueue(Q,p); if(p->lchild) if(p->lchild->data==x) { DeleteXTree(p->lchild); p->lchild==NULL; } else EnQueue(Q,p->lchild); if(p->rchild) if(p->rchild->data==x) { DeleteXTree(p->rchild); p->rchild==NULL; } else EnQueue(Q,p->rchild); } } //在二叉树中查找值为x的结点,打印值为x的结点的所有祖先,假设值为x的结点不多于一个 typedef struct { Bitree t; int tag;//tag=0表示左子女被访问tag=1表示右子女被访问 } stack; void Search(BiTree bt,ElemType x) { stack s[]; top=0; while(bt!=NULL||top>0) { while(bt!=NULL&&bt->data==x) { s[++top].t=bt; s[top]=0; bt=bt->lchild; } if(bt->data==x) { printf("所查到的所有祖先结点的值为: "); for(int i=1; i<=top; i++) printf("%d",s[i].t->data); exit(1); } while(top!=0&&s[top].tag==1) top--; if(top!=0) { s[top].tag=1; bt=s[top].t->rchild; } } } //设一棵二叉树的结点结构为(LLINK,INFO,RLINK),ROOT为指向该二叉树根结点的指针,p和 //q分别为指向二叉树中任意两个结点的指针,求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) { 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--; } if(top!=0) { s[top].tag=1; bt=s[top].t->rchild; } } return NULL; } //假设二叉树采用二叉链表存储,求二叉树b的宽度(即具有节点数最多的那一层的结点个数) //设有一棵满二叉树(所有结点值均不相同)已知其先序序列为pre,设计一个算法求其后序序列post //设计一个算法将二叉树的叶子结点按照从左到右的顺序连成一个单链表,表头指针为head,二叉树 //按二叉链表方式存储,链接时用叶节点的右指针域来存放单链表指针 //设计判断两棵二叉树是否相似的额算法,所谓二叉树T1和T2相似。指的是T1和T2都是空的二叉树或都只有一个根节点; //或者T1的左子树和T2的左子树是相似的且T1的右子树和T2的右子树是相似的 //写出在中序线索二叉树里查找后序前驱结点的算法 //求T的wpl的算法 //
  • 相关阅读:
    关于document.onkeydown
    kubernetes PV存储卷类型 Marathon
    0015leetcode算法实现之三数之和3sumpython&golang实现 Marathon
    0018leetcode算法实现之四数之和4sumpython&golang实现 Marathon
    ubuntu20.04调整时区时间同步 Marathon
    0054leetcode算法实现之螺旋矩阵spiralMatrixpython&golang实现 Marathon
    0005剑指offer替换空格python&golang实现 Marathon
    使用 kubectl 管理 Secret Marathon
    0058leetcode算法实现之左翻转字符串reverseLeftStringpython%golang实现 Marathon
    0059leetcode算法实现之螺旋矩阵IIspiralMatrixIIpython&golang实现 Marathon
  • 原文地址:https://www.cnblogs.com/tianyudizhua/p/13429979.html
Copyright © 2011-2022 走看看