二叉树程序有个bug求解决
程序代码:
#include <stdio.h> #include <stdlib.h> //申请内存头文件 #include <conio.h> //无回显头文件 #define MAXLEN 20 typedef char DATA; //用typedef定义DATA为字符型 typedef struct CBT{ DATA data; struct CBT *left; //定义左子树结点指针 (递归 struct CBT *right; //定义右子树结点指针 (递归 }CBTType; void TreeNodeData(CBTType *treeNode){ printf("%c", treeNode->data); } CBTType *InitTree(){ CBTType *node; //定义数据结构为CBTType型的结点指针 if (node = (CBTType *)malloc(sizeof(CBTType))){ //分配内存 printf("请输入数据:"); scanf("%s", &node->data); //保存数据 node->left = NULL; //左子树为空 node->right = NULL; //右子树为空 if (node != NULL)return node; //进行判断,如果“初始化”成功则返回结点 else return NULL; } return NULL; } void AddTreeNode(CBTType *treeNode){ CBTType *pnode; CBTType *parent; DATA data; char menusel; if (pnode = (CBTType *)malloc(sizeof(CBTType))){ //为结点申请内存 printf("输入二叉树结点数据:"); fflush(stdin); //清空输入缓冲区 scanf("%s", &pnode->data); pnode->left = NULL; //左子树为空 pnode->right = NULL; //右子树为空 printf("请输入父结点数据:"); fflush(stdin); scanf("%s", &data); parent = TreeFindNode(treeNode, data); //根据数据找结点 if (!parent){ //如果不存在则释放内存,返回 printf("未找到该父结点!"); free(pnode); return; } printf("1.添加该结点到左子树\n2.添加该结点到右子树\n"); //以下是为结点选择父结点的左右子树 do{ //循环开始 menusel = getch(); //输入1或2给menusel menusel -= '0'; //有歧义!!!! if (menusel == 1 || menusel == 2){ //在输入1或2给menusel的情况下 if (parent == NULL)printf("不存在父结点,请先设置父结点"); //先检查,如果存在父结点则进入switch选择 else { switch (menusel){ case 1: if (parent->left)printf("左子树有结点,不为空"); else parent->left = pnode; break; case 2: if (parent->right)printf("右子树有结点,不为空"); else parent->right = pnode; break; default: ; printf("无效参数!"); } } } } while (menusel != 1 || menusel != 2); //注意执行do……while的循环条件,是menusel的值为1或2 } } CBTType *TreeFindNode(CBTType *treeNode, DATA data){ CBTType *ptr; //左右子树递归时用到 if (treeNode == NULL){ //如果根结点为空,直接return NULL return NULL; } else{ if (treeNode->data == data)return treeNode; else{ if (ptr = TreeFindNode(treeNode->left, data))return ptr; //左子树递归查找(本函数赋给新建结点指针) else if (ptr = TreeFindNode(treeNode->right, data))return ptr; //右子树递归查找 else return NULL; } } } void DLRTree(CBTType *treeNode, void(*TreeNodeData)(CBTType *p)){ //指针函数作形参 if (treeNode){ TreeNodeData(treeNode); //显示结点数据 DLRTree(treeNode->left, TreeNodeData); //递归遍历左子树 DLRTree(treeNode->right, TreeNodeData); //递归遍历右子树 } } void LDRTree(CBTType *treeNode, void(*TreeNodeData)(CBTType *p)){ if (treeNode){ LDRTree(treeNode->left, TreeNodeData); TreeNodeData(treeNode); LDRTree(treeNode->right, TreeNodeData); } } void LRDTree(CBTType *treeNode, void(*TreeNodeData)(CBTType *p)){ //指针函数作形参 if (treeNode){ DLRTree(treeNode->left, TreeNodeData); //递归遍历左子树 DLRTree(treeNode->right, TreeNodeData); //递归遍历右子树 TreeNodeData(treeNode); //显示结点数据 } } int TreeDepth(CBTType *treeNode){ int depleft; //定义左子树深度 int depright; //定义右子树深度 if (treeNode == NULL)return 0; //如果是空树return 0 else { depleft = TreeDepth(treeNode->left); //左子树递归 depright = TreeDepth(treeNode->right); //右子树递归 if (depleft > depright)return depleft + 1; //左右子树深度值比较,输出大的加1 else return depright + 1; } } void ClearTree(CBTType *treeNode){ if (treeNode){ ClearTree(treeNode->left); //递归清空左子树 ClearTree(treeNode->right); //递归清空右子树 free(treeNode); //释放当前结点所占内存 treeNode = NULL; } } void main(){ CBTType *root = NULL; //定义root为指向二叉树根节点指针(root汉译为“跟”) char menusel; //汉译为“菜单” 用于switch多分支选择 void(*TreeNodeData1)(CBTType *treeNode); //指向函数的指针 (无输入参数,返回值为void) TreeNodeData1 = TreeNodeData; //指针指向具体操作的函数 root = InitTree(); //设置根元素 //添加结点 do{ printf("请选择菜单添加二叉树结点"); printf("0.退出"); printf("1.添加二叉树结点"); menusel = getch(); switch (menusel){ case '1':AddTreeNode(root); break; case '0':break; default: ; } } while (menusel != '0'); //遍历----------- do{ printf("请选择菜单遍历二叉树(输入0表示退出):\n"); printf("1.先序遍历\n"); printf("2.中序遍历\n"); printf("3.后序遍历\n"); menusel = getch(); switch (menusel){ case '0': break; case '1': printf("先序遍历结果:"); DLRTree(root, TreeNodeData1); printf("\n"); break; case '2': printf("中序遍历结果:"); LDRTree(root, TreeNodeData1); printf("\n"); break; case '3': printf("后序遍历结果:"); LRDTree(root, TreeNodeData1); printf("\n"); break; default: ; } } while (menusel != '0'); //深度--------------------------- printf("二叉树深度为:%d", TreeDepth(root)); ClearTree(root); root = NULL; }