From 2573bd0c64665d4514ea828a73a06d697cdd94b2 Mon Sep 17 00:00:00 2001 From: bala <13452927+rfgsdhshgfh@user.noreply.gitee.com> Date: Sat, 13 Jan 2024 15:40:03 +0000 Subject: [PATCH] =?UTF-8?q?=E6=B1=82=E4=BA=8C=E5=8F=89=E6=A0=91=E4=B8=AD?= =?UTF-8?q?=E4=BB=8E=E6=A0=B9=E7=BB=93=E7=82=B9=E5=88=B0=E5=8F=B6=E5=AD=90?= =?UTF-8?q?=E7=BB=93=E7=82=B9=E7=9A=84=E6=A0=B9=E6=8D=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: bala <13452927+rfgsdhshgfh@user.noreply.gitee.com> --- ...1\347\232\204\346\240\271\346\215\256.cpp" | 138 ++++++++++++++++++ 1 file changed, 138 insertions(+) create mode 100644 "2224020152/\347\254\254\344\270\203\345\215\225\345\205\203/\346\261\202\344\272\214\345\217\211\346\240\221\344\270\255\344\273\216\346\240\271\347\273\223\347\202\271\345\210\260\345\217\266\345\255\220\347\273\223\347\202\271\347\232\204\346\240\271\346\215\256.cpp" diff --git "a/2224020152/\347\254\254\344\270\203\345\215\225\345\205\203/\346\261\202\344\272\214\345\217\211\346\240\221\344\270\255\344\273\216\346\240\271\347\273\223\347\202\271\345\210\260\345\217\266\345\255\220\347\273\223\347\202\271\347\232\204\346\240\271\346\215\256.cpp" "b/2224020152/\347\254\254\344\270\203\345\215\225\345\205\203/\346\261\202\344\272\214\345\217\211\346\240\221\344\270\255\344\273\216\346\240\271\347\273\223\347\202\271\345\210\260\345\217\266\345\255\220\347\273\223\347\202\271\347\232\204\346\240\271\346\215\256.cpp" new file mode 100644 index 00000000..0eb0b635 --- /dev/null +++ "b/2224020152/\347\254\254\344\270\203\345\215\225\345\205\203/\346\261\202\344\272\214\345\217\211\346\240\221\344\270\255\344\273\216\346\240\271\347\273\223\347\202\271\345\210\260\345\217\266\345\255\220\347\273\223\347\202\271\347\232\204\346\240\271\346\215\256.cpp" @@ -0,0 +1,138 @@ +#include "tree.cpp" +typedef char ElemType; + + +void AllPath1(BTNode *b,ElemType path[],int pathlen){ + int i; + if(b!=NULL){ + if(b->lchild==NULL && b->rchild==NULL){ + printf("%c到根结点的逆路径:%c->",b->data,b->data); + for(i=pathlen-1;i>0;i--) + printf("%c->",path[i]); + printf("%c\n",path[0]); + }else{ + path[pathlen] = b->data; + pathlen++; + AllPath1(b->lchild,path,pathlen); + AllPath1(b->rchild,path,pathlen); + } + } +} + + + +void LongPath1(BTNode *b,ElemType path[],int pathlen,ElemType longpath[],int &longpathlen ){ + int i; + if(b==NULL){ + if(pathlen>longpathlen){ + for(i=pathlen-1;i>=0;i--) + longpath[i]=path[i]; + longpathlen= pathlen; + } + } else{ + path[pathlen] = b->data; + pathlen++; + LongPath1(b->lchild,path,pathlen,longpath,longpathlen); + LongPath1(b->rchild,path,pathlen,longpath,longpathlen); + } +} + + +void AllPath2(BTNode *b){ + BTNode * st[MaxSize]; + int top=-1; + BTNode *p,*r; + bool flag; + p=b; + do{ + while(p!=NULL){ + top++; + st[top] = p; + p=p->lchild; + + } + r=NULL; + flag=true; + while(top>-1 && flag){ + p=st[top]; + if(p->rchild == r){ + if(p->lchild==NULL && p->rchild==NULL){ + printf("%c到根结点逆路径:",p->data); + for(int i=top;i>0;i--){ + printf("%c->",st[i]->data); + } + printf("%c\n",st[0]->data); + + } + top--; + }else{ + p=p->rchild; + flag=false; + } + } + } while(top>-1); +} + + +void AllPath3(BTNode *b){ + + struct snode{ + BTNode *node; + int parent; + }Qu[MaxSize]; + int front,rear,p; + front=rear=-1; + rear++; + Qu[rear].node=b; + Qu[rear].parent=-1; + while(frontlchild==NULL && b->rchild==NULL){ + printf("%c到根结点逆路径:",b->data); + p=front; + while(Qu[p].parent != -1){ + printf("%c->",Qu[p].node->data); + p=Qu[p].parent; + } + printf("%c\n",Qu[p].node->data); + } + if(b->lchild!=NULL){ + rear++; + Qu[rear].node=b->lchild; + Qu[rear].parent=front; + + } + if(b->rchild!=NULL){ + rear++; + Qu[rear].node=b->rchild; + Qu[rear].parent=front; + } + } + +} + +int main(){ + BTNode *b; + ElemType path[MaxSize],longpath[MaxSize]; + int i,longpathlen=0; + CreatBTree(b,"A(B(D,E(H(J,K(L,M(,N))))),C(F,G(,I)))"); + printf("二叉树b:"); + DispBTree(b);printf("\n"); + printf("先序遍历方法:\n"); + AllPath1(b,path,0); + LongPath1(b,path,0,longpath,longpathlen); + printf("第一最长逆路径长度:%d\n",longpathlen); + printf("第一最长逆路径:"); + for(i=longpathlen-1;i>=0;i--) + printf("%c",longpath[i]); + printf("\n"); + printf("后序非递归遍历方法:\n"); + AllPath2(b); + printf("层次遍历方法:\n"); + AllPath3(b); + DestroyBTree(b); + return 1; +} + + \ No newline at end of file -- Gitee