From 2e6f9a359ae77b4ef7c963542dc2fa124560d210 Mon Sep 17 00:00:00 2001 From: Liu chenghuan <3571803003@qq.com> Date: Sat, 30 Dec 2023 12:53:58 +0000 Subject: [PATCH 1/3] =?UTF-8?q?=E8=B7=AF=E5=BE=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...1\346\240\221\350\267\257\345\276\204.cpp" | 246 ++++++++++++++++++ 1 file changed, 246 insertions(+) create mode 100644 "2224020085/chapter7/\344\272\214\345\217\211\346\240\221\350\267\257\345\276\204.cpp" diff --git "a/2224020085/chapter7/\344\272\214\345\217\211\346\240\221\350\267\257\345\276\204.cpp" "b/2224020085/chapter7/\344\272\214\345\217\211\346\240\221\350\267\257\345\276\204.cpp" new file mode 100644 index 00000000..6385a8be --- /dev/null +++ "b/2224020085/chapter7/\344\272\214\345\217\211\346\240\221\350\267\257\345\276\204.cpp" @@ -0,0 +1,246 @@ +#include +#include +#define MaxSize 100 +typedef char ElemType; //将ElemType类型设成char型 +//二叉树的声明 +typedef struct TuoNode{ + ElemType data; + struct node *lchild; //左孩子结点 + struct node *rchild; //右孩子结点 +}BTNode; +//创建二叉树 +void CreateBTree(BTNode *&b,char *str) +{ + BTNode *St[MaxSize],*p; + int top=-1,k,j=0; + char ch; + b=NULL; + ch=str[j]; + while(ch!='\0') + { + switch(ch) + { + case '(':top++;St[top]=p;k=1;break; //处理左子树 + case ')':top--;break; //子树处理完毕 + case ',':k=2;break; //处理右子树 + default:p=(BTNode *)malloc(sizeof(BTNode)); + p->data=ch;p->lchild=p->rchild=NULL; + if(b==NULL) + b=p; //将p设为二叉树的根节点 + else + { + switch(k) + { + case 1:St[top]->lchild=p;break; + case 2:St[top]->rchild=p;break; + } + } + } + j++;ch=str[j]; + } +} +//销毁二叉树 采用递归的思想 +void DestroyBTree(BTNode *&b) +{ + if(b!=NULL) + { + DestroyBTree(b->lchild); + DestroyBTree(b->rchild); + free(b); + } +} +//查找特定值的结点 +BTNode *FindNode(BTNode *b,ElemType x) +{ + BTNode *p; + if(b==NULL) //判断是否为空 + return NULL; + else if(b->data==x) + return b; + else + { + p=FindNode(b->lchild,x) //采用递归 + if(p!=NULL) + return p; + else + return FindNode(b->rchild,x); + } +} +//返回指定结点的左孩子指针 +BTNode* LchildNode(BTNode *p) +{ + return p->lchild; +} +//返回指定结点的右孩子指针 +BTNode* RchildNode(BTNode *p) +{ + return p->rchild; +} +//求二叉树高度 +int BTHeight(BTNode *b) +{ + int lchildh,rchildh; + if(b==NULL) + return(0); + else + { + lchildh=BTHeight(b->lchild); //采用递归 + rchildh=BTHeight(b->rchild); + return(lchildh>rchildh)?(lchildh+1):(rchildh+1); //加1=加一个根节点 + } +} +//用括号表示法输出二叉树 +void DispBTree(BTNode *b) +{ + if(b!=NULL) + { + printf("%c",b->data); + if(b->lchild!=NULL||b->rchild!=NULL) + { + printf("("); + DispBTree(b->lchild); + if(b->rchild!=NULL) + printf(","); + DispBTree(b->rchild); + printf(")"); + } + } +} +//先序遍历----叶子结点到根节点的逆路径 +void AllPath1(BTNode *b,ElemType path[],int pathlen) +{ + if(b!=NULL) + { + if(b->lchild==NULL && b->rchild==NULL) //b为叶子结点 + { + printf("%c到根节点逆路径:%c->",b->data,b->data); + for(int 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) +{ + if(b==NULL) + { + if(pathlen>longpathlen) + { + for(int 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--; + r=p; + } + 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->rchild!=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; + CreateBTree(b,"A(B(D,E(H(J,K(L,M(,N))))),C(F,G(,I)))"); + printf("二叉树b:");DispBTree(b); + printf("先序遍历方法");AllPath(b,path,0); + LongPath1(b,path,0,longpath,longpathlen); + printf("第一条最长逆路径长度:%d",longpathlen); + printf("第一条最长逆路径:"); + for(i=longpathlen-1;i>=0;i--) + printf("%c",longpath[i]); + printf(*"后序非递归遍历方法");AllPath2(b); + printf("层次遍历方法");AllPath3(b); + DestroyBTree(b); + return 1; +} \ No newline at end of file -- Gitee From 2b89af48e52726ebf524c483d4e149eb64712db4 Mon Sep 17 00:00:00 2001 From: Liu chenghuan <3571803003@qq.com> Date: Sat, 30 Dec 2023 12:56:48 +0000 Subject: [PATCH 2/3] =?UTF-8?q?=E5=93=88=E5=A4=AB=E6=9B=BC=E6=A0=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...0\345\244\253\346\233\274\346\240\221.cpp" | 101 ++++++++++++++++++ 1 file changed, 101 insertions(+) create mode 100644 "2224020085/chapter7/\345\223\210\345\244\253\346\233\274\346\240\221.cpp" diff --git "a/2224020085/chapter7/\345\223\210\345\244\253\346\233\274\346\240\221.cpp" "b/2224020085/chapter7/\345\223\210\345\244\253\346\233\274\346\240\221.cpp" new file mode 100644 index 00000000..bc3075af --- /dev/null +++ "b/2224020085/chapter7/\345\223\210\345\244\253\346\233\274\346\240\221.cpp" @@ -0,0 +1,101 @@ +#include +#include +#define N 50 //叶子结点 +#define M 2*N-1 //结点总数 +typedef struct +{ + char data[5];//结点值 + int weight;//权重 + int parent;//双亲结点 + int lchild;//左孩子结点 + int rchild;//右孩子结点 +}HTNode; +typedef struct +{ + char cd[N];//存放哈夫曼编码 + int start; +}HCopde; +void CreateHT(HTNode ht[],int n) +{ + int i,k,lnode,rnode; + int min1,min2; + for(i=0;i<2*n-1;i++) + ht[i].parent=ht[i].lchild=ht[i].rchild=-1; + for(i=n;i<2*n-1;i++) + { + min1=min2=32767; + lnode=rnode=-1; + for(k=0;k<=i-1;k++) + if(ht[k].parent==-1) + { + if(ht[k].weight Date: Fri, 12 Jan 2024 14:17:18 +0000 Subject: [PATCH 3/3] floyd --- ...2\345\276\267\347\256\227\346\263\225.cpp" | 163 ++++++++++++++++++ ...3\346\216\245\347\237\251\351\230\265.cpp" | 115 ++++++++++++ 2 files changed, 278 insertions(+) create mode 100644 "2224020085/chapter8/\345\274\227\346\264\233\344\274\212\345\276\267\347\256\227\346\263\225.cpp" create mode 100644 "2224020085/chapter8/\351\202\273\346\216\245\347\237\251\351\230\265.cpp" diff --git "a/2224020085/chapter8/\345\274\227\346\264\233\344\274\212\345\276\267\347\256\227\346\263\225.cpp" "b/2224020085/chapter8/\345\274\227\346\264\233\344\274\212\345\276\267\347\256\227\346\263\225.cpp" new file mode 100644 index 00000000..f7004f50 --- /dev/null +++ "b/2224020085/chapter8/\345\274\227\346\264\233\344\274\212\345\276\267\347\256\227\346\263\225.cpp" @@ -0,0 +1,163 @@ +#include +#include +#define INF 32767 +#define MAXV 100 +typedef char InfoType; +typedef struct +{ + int no; + InfoType info; +}VertexType; +typedef struct +{ + int edges[MAXV][MAXV]; + int n,e; + VertexType vexs[MAXV]; +}MatGraph; +typedef struct ANode +{ + int adjvex; + struct ANode *nextarc; + int weight; +}ArcNode; +typedef struct Vnode +{ + InfoType info; + int count; + ArcNode *firstarc; +}VNode; +typedef struct +{ + VNode adjlist[MAXV]; + int n,e; +}AdjGraph; +void CreateMat(MatGraph &g,int A[MAXV][MAXV],int n,int e) +{ + int i,j; + g.n=n;g.e=e; + for(i=0;iadjlist[i].firstarc=NULL; + for(i=0;i=0;j--) + if(A[i][j]!=0&&A[i][j]!=INF) + { + p=(ArcNode *)malloc(sizeof(ArcNode)); + p->adjvex=j; + p->weight=A[i][j]; + p->nextarc=G->adjlist[i].firstarc; + G->adjlist[i].firstarc=p; + } + G->n=n;G->e=n; +} +void DispAdj(AdjGraph *G) +{ + ArcNode *p; + for(int i=0;in;i++) + { + p=G->adjlist[i].firstarc; + printf("%3d",i); + while(p!=NULL) + { + printf("%3d[%d]->",p->adjvex,p->weight); + p=p->nextarc; + } + } +} +void DestroyAdj(AdjGraph *&G) +{ + ArcNode *pre,*p; + for(int i=0;in;i++) + { + pre=G->adjlist[i].firstarc; + if(pre!=NULL) + { + p=pre->nextarc; + while(p!=NULL) + { + free(pre); + pre=p;p=p->nextarc; + } + free(pre); + } + } + free(G); +} +void Dispath(MatGraph g,int A[][MAXV],int path[][MAXV]) +{ + int i,j,k,s; + int apath[MAXV],d; + for(i=0;i=0;s--) + printf("%d",apath[s]); + printf("\t路径长度为:%d\n",A[i][j]); + } + } +} +void Floyd(MatGraph g) +{ + int A[MAXV][MAXV],path[MAXV][MAXV]; + int i,j,k; + for(i=0;iA[i][k]+A[k][j]) + { + A[i][j]=A[i][k]+A[k][j]; + path[i][j]=path[k][j]; + } + } + Dispath(g,A,path); +} +int main() +{ + MatGraph g; + int A[MAXV][MAXV]={{0,5,INF,7,INF,INF},{INF,0,4,INF,INF,INF},{8,INF,0,INF,INF,9},{INF,INF,5,0,INF,6},{INF,INF,INF,5,0,INF},{3,INF,INF,INF,1,0}}; + int n=6;e=10; + CreateMat(g,A,n,e); + DispMat(g); + Floyd(g); + return 1; +} \ No newline at end of file diff --git "a/2224020085/chapter8/\351\202\273\346\216\245\347\237\251\351\230\265.cpp" "b/2224020085/chapter8/\351\202\273\346\216\245\347\237\251\351\230\265.cpp" new file mode 100644 index 00000000..7e192856 --- /dev/null +++ "b/2224020085/chapter8/\351\202\273\346\216\245\347\237\251\351\230\265.cpp" @@ -0,0 +1,115 @@ +#include +#include +#define INF 32767 +#define MAXV 100 +typedef char InfoType; +typedef struct +{ + int no; + InfoType info; +}VertexType; +typedef struct +{ + int edges[MAXV][MAXV]; + int n,e; + VertexType vexs[MAXV]; +}MatGraph; +typedef struct ANode +{ + int adjvex; + struct ANode *nextarc; + int weight; +}ArcNode; +typedef struct Vnode +{ + InfoType info; + int count; + ArcNode *firstarc; +}VNode; +typedef struct +{ + VNode adjlist[MAXV]; + int n,e; +}AdjGraph; +void CreateMat(MatGraph &g,int A[MAXV][MAXV],int n,int e) +{ + int i,j; + g.n=n;g.e=e; + for(i=0;iadjlist[i].firstarc=NULL; + for(i=0;i=0;j--) + if(A[i][j]!=0&&A[i][j]!=INF) + { + p=(ArcNode *)malloc(sizeof(ArcNode)); + p->adjvex=j; + p->weight=A[i][j]; + p->nextarc=G->adjlist[i].firstarc; + G->adjlist[i].firstarc=p; + } + G->n=n;G->e=n; +} +void DispAdj(AdjGraph *G) +{ + ArcNode *p; + for(int i=0;in;i++) + { + p=G->adjlist[i].firstarc; + printf("%3d",i); + while(p!=NULL) + { + printf("%3d[%d]->",p->adjvex,p->weight); + p=p->nextarc; + } + } +} +void DestroyAdj(AdjGraph *&G) +{ + ArcNode *pre,*p; + for(int i=0;in;i++) + { + pre=G->adjlist[i].firstarc; + if(pre!=NULL) + { + p=pre->nextarc; + while(p!=NULL) + { + free(pre); + pre=p;p=p->nextarc; + } + free(pre); + } + } + free(G); +} +int main() +{ + MatGraph g; + AdjGraph *G; + int A[MAXV][MAXV]={{0,5,INF,7,INF,INF},{INF,0,4,INF,INF,INF},{8,INF,0,INF,INF,9},{INF,INF,5,0,INF,6},{INF,INF,INF,5,0,INF},{3,INF,INF,INF,1,0}}; + int n=6,e=10; + CreateMat(g,A,n,e); + DispMat(g); + CreateAdj(G,A,n,e); + DispAdj(G); + DestroyAdj(G); +} \ No newline at end of file -- Gitee