From 76a3bfa7e7b55ab512570dc15e1ba427f7bb910d Mon Sep 17 00:00:00 2001 From: zzskin Date: Wed, 3 May 2017 11:59:22 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B1=82=E4=BA=8C=E5=8F=89=E6=A0=91=E6=B7=B1?= =?UTF-8?q?=E5=BA=A6=E3=80=81=E6=9C=80=E5=A4=A7=E5=AE=BD=E5=BA=A6=E3=80=81?= =?UTF-8?q?=E5=8F=B6=E5=AD=90=E7=BB=93=E7=82=B9=E5=92=8C=E9=9D=9E=E5=8F=B6?= =?UTF-8?q?=E5=AD=90=E7=BB=93=E7=82=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../test 6.c" | 168 ++++++++++++++++++ 1 file changed, 168 insertions(+) create mode 100644 "2017-1/zzskin/\346\261\202\344\272\214\345\217\211\346\240\221\346\267\261\345\272\246\343\200\201\346\234\200\345\244\247\345\256\275\345\272\246\343\200\201\345\217\266\345\255\220\347\273\223\347\202\271\345\222\214\351\235\236\345\217\266\345\255\220\347\273\223\347\202\271/test 6.c" diff --git "a/2017-1/zzskin/\346\261\202\344\272\214\345\217\211\346\240\221\346\267\261\345\272\246\343\200\201\346\234\200\345\244\247\345\256\275\345\272\246\343\200\201\345\217\266\345\255\220\347\273\223\347\202\271\345\222\214\351\235\236\345\217\266\345\255\220\347\273\223\347\202\271/test 6.c" "b/2017-1/zzskin/\346\261\202\344\272\214\345\217\211\346\240\221\346\267\261\345\272\246\343\200\201\346\234\200\345\244\247\345\256\275\345\272\246\343\200\201\345\217\266\345\255\220\347\273\223\347\202\271\345\222\214\351\235\236\345\217\266\345\255\220\347\273\223\347\202\271/test 6.c" new file mode 100644 index 00000000..6d388fef --- /dev/null +++ "b/2017-1/zzskin/\346\261\202\344\272\214\345\217\211\346\240\221\346\267\261\345\272\246\343\200\201\346\234\200\345\244\247\345\256\275\345\272\246\343\200\201\345\217\266\345\255\220\347\273\223\347\202\271\345\222\214\351\235\236\345\217\266\345\255\220\347\273\223\347\202\271/test 6.c" @@ -0,0 +1,168 @@ +#include +#include + +typedef int ElemType; +typedef char TElemType ; + +typedef struct BiTNode +{ + TElemType data; + struct BiTNode *lchild, *rchild; //左右孩子的指针 +}BiTNode, *BiTree; + +typedef enum +{ + OK, + ERROR, + OVERFLOW +}Status; //枚举定义(返回状态) + + +Status CreateBiTree(BiTree *T,TElemType *p); +void PostOrderTraverseTree(BiTree T); +int BiTreeDepth(BiTree T); +int BiTreeWidth(BiTree T); +int BiTreeCount(BiTree T); +int BiTreeCountLeaf(BiTree T); + + +int count = 0; //定义全局变量 用于对二叉树进行计数 + +Status CreateBiTree(BiTree *T,TElemType *p) // 创建二叉树 +{ + TElemType ch; + ch = p[count]; + count++; //定义数组存储字符串 + + if(ch == ' ') + { + return OK; + + } + else if(ch == '#') + { + *T = NULL; + } + else + { + if(!(*T = (BiTNode *)malloc(sizeof(BiTNode)))) + { + return OVERFLOW; + } + (*T)->data = ch; //根节点 + CreateBiTree(&(*T)->lchild,p); //构造左孩子 + CreateBiTree(&(*T)->rchild,p); //构造右孩子 + } + return OK; +} + +void PostOrderTraverseTree(BiTree T) +{ + if(T != NULL) + { + PostOrderTraverseTree(T->lchild); + PostOrderTraverseTree(T->rchild); + printf("%c",T->data); + } +} + +int BiTreeDepth(BiTree T) +{ + int depthleft,depthright; + if(T == NULL) + { + return 0; + } + else + { + depthleft = BiTreeDepth(T->lchild); + depthright = BiTreeDepth(T->rchild); + if(depthleft > depthright) + { + return (depthleft + 1); + } + else + { + return (depthright + 1); + } + } +} + +int pot[100]; //存放各层结点数 +int max = 0; //最大宽度 + +int BiTreeWidth(BiTree T) //递归求最大宽度 +{ + + if(T == NULL) + { + return 0; + } + count++; + pot[count]++; + if(max < pot[count]) + { + max = pot[count]; + } + BiTreeWidth(T->lchild); + BiTreeWidth(T->rchild); + count--; + return max; +} + +int BiTreeCount(BiTree T)//返回指针T所指二叉树中所有结点个数 +{ + if(T == NULL) + { + return 0; + } + else if((T->lchild == NULL) && (T->rchild == NULL)) + { + return 1; + } + else + { + return BiTreeCount(T->lchild) + BiTreeCount(T->rchild) + 1; + } +} + +int BiTreeCountLeaf(BiTree T)// 返回指针T所指二叉树中所有叶子结点个数 +{ + if(T == NULL) + { + return 0; + } + else if((T->lchild == NULL) && (T->rchild == NULL)) + { + return 1; + } + else + { + return BiTreeCountLeaf(T->lchild) + BiTreeCountLeaf(T->rchild); + } +} + + +int main() +{ + BiTree T = NULL; + + TElemType str1[40] = "ABDG###EH##I#K##C#F##"; + TElemType str2[40] = "ABD#F###CE###"; + + printf("处理第一个二叉树 : ABDG###EH##I#K##C#F## \n"); + CreateBiTree(&T,str1); + printf("此二叉树高度为%d\n最大宽度为%d\n",BiTreeDepth(T),BiTreeWidth(T)); + printf("此二叉树结点为%d\n叶子结点为%d",BiTreeCount(T),BiTreeCountLeaf(T)); + printf("\n"); + + count = 0; //处理第二个二叉树 + + printf("处理第二个二叉树 : ABD#F###CE### \n"); + CreateBiTree(&T,str2); + printf("此二叉树高度为%d\n最大宽度为%d\n",BiTreeDepth(T),BiTreeWidth(T)); + printf("此二叉树结点为%d\n叶子结点为%d",BiTreeCount(T),BiTreeCountLeaf(T)); + printf("\n"); + + return 0; +} \ No newline at end of file