1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
| #include<stdio.h> #include<stdlib.h> #include<malloc.h> #include<iostream> #define ERROR -1 typedef char DataType; typedef struct TreeNode { DataType elem; struct TreeNode* rchild; struct TreeNode* lchild; }TreeNode, *PTREE;
void CreatTree(PTREE *Root) { char val = 0; val = getchar(); if (val == '*' || val == '#' || val == '.') (*Root) = NULL; else { (*Root) = (PTREE)malloc(sizeof(TreeNode)); if ((*Root) == NULL) { printf("创建结点失败,无法分配可用内存..."); exit(-1); } else { (*Root)->elem = val; CreatTree(&(*Root)->lchild); CreatTree(&(*Root)->rchild); } } }
void PreOrderTraverse (PTREE Root) { if (Root == NULL) return; else { putchar(Root->elem); PreOrderTraverse(Root->lchild); PreOrderTraverse(Root->rchild); } }
void InOrderTraverse(PTREE Root) { if (Root != NULL) { InOrderTraverse(Root->lchild); putchar(Root->elem); InOrderTraverse(Root->rchild); } }
void PostOrderTraverse(PTREE Root) { if (Root != NULL) { PostOrderTraverse(Root->lchild); PostOrderTraverse(Root->rchild); putchar(Root->elem); } }
int Depth(PTREE Root) { if (Root == NULL) { return 0; } else { return (Depth(Root->lchild)>Depth(Root->rchild) ? Depth(Root->lchild) : Depth(Root->rchild )) + 1; } }
int GetNodeNum(PTREE Tree) { if (NULL == Tree) { return 0; } else { return GetNodeNum(Tree->lchild) + GetNodeNum(Tree->rchild) + 1; } }
int GetLeafNum(PTREE Tree) { if (Tree == NULL) return 0; if (Tree->lchild == NULL&&Tree->rchild == NULL) return 1; return GetLeafNum(Tree->lchild) + GetLeafNum(Tree->rchild); }
int GetNodde1Num(PTREE Tree) { if (Tree == NULL)
return 0;
if (Tree->lchild == NULL&&Tree->rchild != NULL || Tree->rchild == NULL&&Tree->lchild != NULL)
return 1 + GetNodde1Num(Tree->lchild) + GetNodde1Num(Tree->rchild); return GetNodde1Num(Tree->lchild) + GetNodde1Num(Tree->rchild); }
int GetNodde2Num(PTREE Tree) { if (Tree == NULL) return 0; if (Tree->lchild != NULL&&Tree->rchild != NULL) return 1 + GetNodde2Num(Tree->lchild) + GetNodde2Num(Tree->rchild); return GetNodde2Num(Tree->lchild) + GetNodde2Num(Tree->rchild); } void myprintf(char* c,int n) { std::cout <<c<< "求得结果为\t" << n << std::endl; } int main(void) { PTREE root=NULL; char ch = NULL; printf("菜单:\n" "0 先序建表\n" "1 前序遍历\n" "2 中序遍历\n" "3 后序遍历\n" "4 树的深度\n" "5 递归所有结点\n" "6 等式求所有结点\n" "7 递归求所有叶子结点\n" "8 性质求所有叶子结点\n" "9 递归求所有度为1的结点\n" "a 等式求所有度为1的结点\n" "b 递归求所有度为2的结点\n" "c 性质求所有度为2的结点\n"); while (true) { std::cin >> ch; switch (ch) { case '0':getchar(); CreatTree(&root); break; case '1':printf("1 前序遍历结果: "); PreOrderTraverse(root); printf("\n"); break; case '2':printf("2 中序遍历结果: "); InOrderTraverse(root); printf("\n"); break; case '3':printf("3 后序遍历结果: "); PostOrderTraverse(root); printf("\n"); break; case '4':myprintf("4 树的深度 ",Depth(root)); break; case '5':myprintf("5 递归所有结点 ",GetNodeNum(root)); break; case '6':myprintf("6 等式求所有结点 ",GetLeafNum(root) + GetNodde2Num(root) + GetNodde1Num(root)); break; case '7':myprintf("7 递归求所有叶子结点 ",GetLeafNum(root)); break; case '8':myprintf("8 性质求所有叶子结点 ",GetNodde2Num(root) + 1); break; case '9':myprintf("9 递归求所有度为1的结点 ",GetNodde1Num(root)); break; case 'a':myprintf("a 等式求所有度为1的结点 ",GetNodeNum(root) - GetLeafNum(root) - GetNodde2Num(root)); break; case 'b':myprintf("b 递归求所有度为2的结点 ",GetNodde2Num(root)); break; case 'c':myprintf("c 性质求所有度为2的结点 ",GetLeafNum(root) - 1); break; case '\n':case '\t':break; default:system("pause");return 0;break; } } }
|