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 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
| #include <bits/stdc++.h> using namespace std; const int MaxSize = 50;
typedef char ElementType; typedef struct bitnode { ElementType data; struct bitnode *left, *right; } bitnode, *bitree;
void CreateTree(bitree &b, char str[]){ char ch; bitree stack[MaxSize], p; int top = -1, k = 0, j = 0;
while ((ch = str[j++]) != '\0'){ switch (ch) { case '(': top++; stack[top] = p; k = 1; break; case ',': k = 2; break; case ')': top--; break; default: p = (bitree)malloc(sizeof(bitnode)); p->data = ch; p->left = p->right = NULL;
switch (k) { case 1: stack[top]->left = p; break; case 2: stack[top]->right = p; break; default: b = p; break; } } } }
void PrinTree(bitree b){ if (b){ cout << b->data; if (b->left != NULL || b->right != NULL){ cout << "("; PrinTree(b->left); if (b->right != NULL) cout << ","; PrinTree(b->right); cout << ")"; } } }
bool getValue(bitree b, ElementType value){ if (b == NULL) return false; if (b->data == value){ if (b->left) cout << "左孩子结点:" << b->left->data; else cout << " 无左孩子结点"; if (b->right) cout << " 右孩子结点:" << b->right->data; else cout << " 无右孩子结点"; cout << "\n"; return true; } if(getValue(b->left, value))return true; if(getValue(b->right, value))return true; }
int getDeep(bitree b){ if(b==NULL)return 0; return max(getDeep(b->left),getDeep(b->right))+1; }
int getNums(bitree b){ if(b==NULL)return 0; return getNums(b->left)+getNums(b->right)+1; }
int get2BranchNums(bitree b){ if (b == NULL||(b->left==NULL&&b->right==NULL)) return 0; if(b->left&&b->right) return 1+get2BranchNums(b->left)+get2BranchNums(b->right); return get2BranchNums(b->left)+get2BranchNums(b->right); }
int get1BranchNums(bitree b){ if (b == NULL||(b->left==NULL&&b->right==NULL)) return 0; if((b->left||b->right)&&!(b->left&&b->right)) return 1+get1BranchNums(b->left)+get1BranchNums(b->right); return get1BranchNums(b->left)+get1BranchNums(b->right); }
int getLeafNodes(bitree b){ if(b==NULL)return 0; if(b->right==NULL&&b->left==NULL)return 1; return getLeafNodes(b->left)+getLeafNodes(b->right); }
int getWidth(bitree b){ int tmpWidth=0,maxWidth=1; if(b==NULL)return 0; queue<bitnode*> q; q.push(b); while(q.size()){ tmpWidth = q.size(); maxWidth = max(tmpWidth,maxWidth); for (int i = 0; i < tmpWidth; i++) { bitnode *nodeTemp = q.front(); q.pop(); if (nodeTemp->left != NULL) { q.push(nodeTemp->left); } if(nodeTemp->right != NULL) { q.push(nodeTemp->right); } } } return maxWidth; }
bitnode* findLCA(bitree root, ElementType n1, ElementType n2){ if (root == NULL) return NULL; if (root->data == n1 || root->data == n2) return root; bitnode* left_lca = findLCA(root->left, n1, n2); bitnode* right_lca = findLCA(root->right, n1, n2); if (left_lca && right_lca) return root; return (left_lca != NULL)? left_lca: right_lca; }
bitree FreeTree(bitree b){ if (b != NULL){ FreeTree(b->left); FreeTree(b->right); free(b); b = NULL; } return b; }
int main(){ char str[] = "A(B(D(,G),),C(E,F))"; bitree root = NULL; ElementType tmp,tmp1,tmp2; int t1,t2; CreateTree(root, str);
cout << "字符串:" << str << "\n"; cout << "1,括号表示法输出该树:"; PrinTree(root); cout << "\n";
cout << "2,输入一个结点的值,输出该结点的左,右孩子的值\n"; cin >> tmp; if (!getValue(root, tmp)) cout << "查找失败!\n"; cout<<"3,输出该二叉树的高度: "; cout<<getDeep(root)<<"\n";
cout<<"4,输出该二叉树结点的个数: "; cout<<getNums(root)<<"\n";
cout<<"5,输出该二叉树双分支结点的个数: "; cout<<get2BranchNums(root)<<"\n";
cout<<"6,输出该二叉树单分支结点的个数: "; cout<<get1BranchNums(root)<<"\n";
cout<<"7,输出该二叉树叶子结点的个数: "; cout<<getLeafNodes(root)<<"\n";
cout<<"8,输出该二叉树的宽度: "; cout<<getWidth(root)<<"\n";
cout<<"9,给定该二叉树的两个结点,输出它们的最近的公共祖先: \n"; cout<<"请输入您所需要查找的两个结点:"; cin>>tmp1>>tmp2; bitnode* tmpnode = findLCA(root,tmp1,tmp2); if(tmpnode==NULL){ cout<<"结点: "<<tmp1<<" 和 "<<tmp2<<" 没有公共结点\n"; }else{ cout<<"结点: "<<tmp1<<" 和 "<<tmp2<<" 的最近公共结点为:"<<tmpnode->data<<"\n"; }
cout<<"10,销毁该二叉树\n"; root = FreeTree(root); if (root == NULL) cout << "二叉树销毁成功" << "\n";
return 0; }
|