经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » C++ » 查看文章
2018.12.4数据结构上机考试
来源:cnblogs  作者:MaxZheng2018  时间:2018/12/5 9:54:40  对本文有异议
  1.  

 

【实验内容及要求】

  对于左儿子—右兄弟链接存储的树,任给树中两个节点,判断二者 之间的关系属于祖孙、父子、兄弟、其他关系中的哪种关系。注意,一 个节点是另外一个节点的儿子节点的儿子节点则称这两个节点之间为 祖孙关系。设计的程序应完成如下的功能:

  1. 通过用户输入建立一棵树,程序应能正确输出该树的节点数,以 验证输入的树是否正确;

  2. 基于输入的树,程序应能正确输出给定节点对之间的关系。 

  1. 1 #include<iostream>
  2. 2 #include<string>
  3. 3 #include<stack>
  4. 4 #include<queue>
  5. 5 using namespace std;
  6. 6
  7. 7 class FTree {
  8. 8 public:
  9. 9 FTree();
  10. 10 void getNodeNum();
  11. 11 //递归先序构造二叉树形式的树/森林
  12. 12 FTree(string str, int& index);
  13. 13 //2)树和森林的先根遍历的递归和迭代算法;
  14. 14 void preOrderbyRecursive();
  15. 15 void preOrderbyIteration();
  16. 16 //3)树和森林的后根遍历的递归和迭代算法;
  17. 17 void afterOrderbyRecursive();
  18. 18 void afterOrderbyIteration();
  19. 19 //4)树和森林的层次遍历算法。
  20. 20 void levelOrder();
  21. 21 void getReltion(char node1,char node2);
  22. 22 void getReltion();
  23. 23 FTree* getNode(char node);
  24. 24 ~FTree();
  25. 25 bool isFatherOf(char node);
  26. 26 bool isBrotherOf(char node);
  27. 27 bool isGrandFatherOf(char node);
  28. 28 bool isOtherTree(char node1,char node2);
  29. 29 bool isSameTree(char node);
  30. 30 bool haveLittleSon(char node);
  31. 31 protected:
  32. 32 char data;
  33. 33 FTree* left_son;
  34. 34 FTree* right_brother;
  35. 35 int node_num;
  36. 36 };
  37. 37
  38. 38 int main() {
  39. 39
  40. 40 //string ss = "124##57###3#6##"; int index = 0;
  41. 41 //string ss = "RAB#C#D##EF##GH#IJ#####";
  42. 42 string ss;
  43. 43 cin >> ss;
  44. 44 int index = 0;
  45. 45 FTree t(ss, index);
  46. 46 t.getNodeNum();
  47. 47 cout << "从字符串 " << ss << " 构造树t " << endl;
  48. 48 //t.getReltion('R','C');
  49. 49 while (1) {
  50. 50 t.getReltion();
  51. 51 }
  52. 52
  53. 53 }
  54. 54
  55. 55
  56. 56
  57. 57 FTree::FTree()
  58. 58 {
  59. 59 data = NULL;
  60. 60 left_son = 0;
  61. 61 right_brother = 0;
  62. 62 }
  63. 63 void FTree::getNodeNum()
  64. 64 {
  65. 65 cout << "树的节点个数为" << node_num << endl;
  66. 66 }
  67. 67 FTree::FTree(string str, int& index)
  68. 68 {
  69. 69 if (str[index] == NULL) {
  70. 70 cout << "输入的字符串错误!";
  71. 71 }
  72. 72 node_num = 0;
  73. 73 if (index <= str.size() - 1 && str[index] != '#') {
  74. 74 data = str[index];
  75. 75 index++;
  76. 76 node_num++;
  77. 77 if (index <= str.size() - 1) {
  78. 78 if (str[index] == '#') {
  79. 79 left_son = 0;
  80. 80 index++;
  81. 81 }
  82. 82 else {
  83. 83 left_son = new FTree(str, index);
  84. 84 node_num += left_son->node_num;
  85. 85 }
  86. 86 }
  87. 87 if (index <= str.size() - 1) {
  88. 88 if (str[index] == '#') {
  89. 89 right_brother = 0;
  90. 90 index++;
  91. 91 }
  92. 92 else {
  93. 93 right_brother = new FTree(str, index);
  94. 94 node_num += right_brother->node_num;
  95. 95 }
  96. 96 }
  97. 97 }
  98. 98 else {
  99. 99 data = NULL;
  100. 100 index++;
  101. 101 left_son = 0;
  102. 102 right_brother = 0;
  103. 103 }
  104. 104 }
  105. 105
  106. 106 void FTree::preOrderbyRecursive()
  107. 107 {
  108. 108 if (data == NULL) {
  109. 109 return;
  110. 110 }
  111. 111 cout << data;
  112. 112 if (left_son != 0) {
  113. 113 left_son->preOrderbyRecursive();
  114. 114 }
  115. 115 if (right_brother != 0) {
  116. 116 right_brother->preOrderbyRecursive();
  117. 117 }
  118. 118 }
  119. 119
  120. 120 void FTree::preOrderbyIteration()
  121. 121 {
  122. 122 stack<FTree*> sta;
  123. 123 if (data == NULL) { return; }
  124. 124 cout << data;
  125. 125 if (left_son != 0) { sta.push(left_son); }
  126. 126 if (right_brother != 0) { sta.push(right_brother); }
  127. 127 while (!sta.empty()) {
  128. 128 FTree* f = sta.top(); sta.pop();
  129. 129 cout << f->data;
  130. 130 if (right_brother != 0) { sta.push(right_brother); }
  131. 131 if (left_son != 0) { sta.push(left_son); }
  132. 132 }
  133. 133 }
  134. 134
  135. 135 void FTree::afterOrderbyRecursive()
  136. 136 {
  137. 137 if (data == NULL) {
  138. 138 return;
  139. 139 }
  140. 140
  141. 141 if (left_son != 0) {
  142. 142 left_son->afterOrderbyRecursive();
  143. 143 }
  144. 144 cout << data;
  145. 145 if (right_brother != 0) {
  146. 146 right_brother->afterOrderbyRecursive();
  147. 147 }
  148. 148
  149. 149 }
  150. 150
  151. 151 void FTree::afterOrderbyIteration()
  152. 152 {
  153. 153 stack<FTree*> sta;
  154. 154 if (data == NULL) { return; }
  155. 155 if (right_brother != 0) { sta.push(right_brother); }
  156. 156 sta.push(this);
  157. 157 if (left_son != 0) { sta.push(left_son); }
  158. 158 while (!sta.empty()) {
  159. 159 FTree* f = sta.top(); sta.pop();
  160. 160 if (right_brother != 0) { sta.push(right_brother); }
  161. 161 cout << f->data;
  162. 162 if (left_son != 0) { sta.push(left_son); }
  163. 163 }
  164. 164 }
  165. 165
  166. 166 void FTree::levelOrder()
  167. 167 {
  168. 168 queue<FTree*> que;
  169. 169 que.push(this);
  170. 170 while (!que.empty()) {
  171. 171 FTree* f = que.front(); que.pop();
  172. 172 while (f != 0) {
  173. 173 cout << f->data;
  174. 174 if (f->left_son != 0) {
  175. 175 que.push(f->left_son);
  176. 176 }
  177. 177 f = f->right_brother;
  178. 178 }
  179. 179
  180. 180 }
  181. 181 }
  182. 182
  183. 183 void FTree::getReltion(char node1, char node2)
  184. 184 {
  185. 185 FTree* no1 = getNode(node1);
  186. 186 FTree* no2 = getNode(node2);
  187. 187 if (no1->isFatherOf(node2) == 1) {
  188. 188 return;
  189. 189 }
  190. 190 if (no2->isFatherOf(node1) == 1) {
  191. 191 return;
  192. 192 }
  193. 193 if (no1->isGrandFatherOf(node2) == 1) {
  194. 194 return;
  195. 195 }
  196. 196 if (no2->isGrandFatherOf(node1) == 1) {
  197. 197 return;
  198. 198 }
  199. 199
  200. 200 if (no1->isBrotherOf(node2) == 1) {
  201. 201 return;
  202. 202 }
  203. 203 if (no2->isBrotherOf(node1) == 1) {
  204. 204 return;
  205. 205 }
  206. 206 if (this->isOtherTree(node1,node2) == 1) {
  207. 207 return;
  208. 208 }
  209. 209 if (no1->isSameTree(node2) == 1) {
  210. 210 return;
  211. 211 }
  212. 212 if (no2->isSameTree(node1) == 1) {
  213. 213 return;
  214. 214 }
  215. 215 cout << "其他关系" << endl;
  216. 216 }
  217. 217
  218. 218 void FTree::getReltion()
  219. 219 {
  220. 220 char node1, node2;
  221. 221 cout << "\nplease input node1,node2\n";
  222. 222 cin >> node1;
  223. 223 cin>>node2;
  224. 224 getReltion(node1, node2);
  225. 225 }
  226. 226
  227. 227 FTree * FTree::getNode(char node)
  228. 228 {
  229. 229 if (data == node) {
  230. 230 return this;
  231. 231 }
  232. 232 FTree* find = 0;
  233. 233 FTree* current = left_son;
  234. 234 while (current != 0) {
  235. 235 find = current->getNode(node);
  236. 236 if (find != nullptr) {
  237. 237 return find;
  238. 238 }
  239. 239 current = current->right_brother;
  240. 240 }
  241. 241 return 0;
  242. 242 return find;
  243. 243 }
  244. 244
  245. 245 FTree::~FTree()
  246. 246 {
  247. 247 if (left_son != 0) {
  248. 248 //cout << left_son->data;
  249. 249 delete left_son;
  250. 250 }
  251. 251 if (right_brother != 0) {
  252. 252 //cout << right_brother->data;
  253. 253 delete right_brother;
  254. 254 }
  255. 255
  256. 256 }
  257. 257
  258. 258 bool FTree::isFatherOf(char node)
  259. 259 {
  260. 260
  261. 261 FTree* current = left_son;
  262. 262 while (current != 0) {
  263. 263 if (current->data == node) {
  264. 264 cout << data << "" << node << "是父子关系,";
  265. 265 cout << data << "" << node << "的父亲"<<endl;
  266. 266 return 1;
  267. 267 }
  268. 268 else {
  269. 269 current = current->right_brother;
  270. 270 }
  271. 271 }
  272. 272 return false;
  273. 273 }
  274. 274
  275. 275 bool FTree::isBrotherOf(char node)
  276. 276 {
  277. 277 FTree* current = right_brother;
  278. 278 while (current != 0) {
  279. 279 if (current->data == node) {
  280. 280 cout << data << "" << node << "是兄弟关系\n";
  281. 281 cout << data << "" << node << "的兄长";
  282. 282 return 1;
  283. 283 }
  284. 284 else {
  285. 285 current = current->right_brother;
  286. 286 }
  287. 287 }
  288. 288 return false;
  289. 289 }
  290. 290
  291. 291 bool FTree::isGrandFatherOf(char node)
  292. 292 {
  293. 293 FTree* current = left_son;
  294. 294 while (current != 0) {
  295. 295 if (current->left_son != 0) {
  296. 296 FTree* currentson = current->left_son;
  297. 297 while (currentson != 0) {
  298. 298 if (currentson->data == node) {
  299. 299 cout << data << "" << node << "是祖孙关系\n";
  300. 300 cout << data << "" << node << "的祖父";
  301. 301 return 1;
  302. 302 }
  303. 303 currentson = currentson->right_brother;
  304. 304 }
  305. 305 }
  306. 306 current = current->right_brother;
  307. 307 }
  308. 308 return false;
  309. 309 }
  310. 310
  311. 311 bool FTree::isOtherTree(char node1, char node2)
  312. 312 {
  313. 313 int flag = 0;
  314. 314 FTree* current = left_son;
  315. 315 while (current != 0) {
  316. 316 if (current->haveLittleSon(node1) ){
  317. 317 flag++;
  318. 318 }
  319. 319 else {
  320. 320 if (current->haveLittleSon(node2)) {
  321. 321 flag++;
  322. 322 }
  323. 323 }
  324. 324 current = current->right_brother;
  325. 325 }
  326. 326 if (flag == 2) {
  327. 327 cout << "其他关系,"<< node1 << "" << node2<<"不在同一分支上," <<endl;
  328. 328 return 1;
  329. 329 }
  330. 330 return false;
  331. 331 }
  332. 332
  333. 333 bool FTree::isSameTree(char node)
  334. 334 {
  335. 335
  336. 336 FTree* current = left_son;
  337. 337 while (current != 0) {
  338. 338 if (current->left_son != 0) {
  339. 339 if (current->haveLittleSon(node)) {
  340. 340 cout << "其他关系,在同一分支上," << data << "" << node << "祖父以上长辈"<<endl;
  341. 341 return 1;
  342. 342 }
  343. 343 }
  344. 344 current = current->right_brother;
  345. 345 }
  346. 346 return false;
  347. 347 }
  348. 348
  349. 349 bool FTree::haveLittleSon(char node)
  350. 350 {
  351. 351 if (data == node) {
  352. 352 return 1;
  353. 353 }
  354. 354 bool flag=0;
  355. 355 FTree* current = left_son;
  356. 356 while (current != 0) {
  357. 357 if (current->data == node) {
  358. 358 return 1;
  359. 359 }
  360. 360 flag = current->haveLittleSon(node);
  361. 361 current = current->right_brother;
  362. 362 }
  363. 363 return flag;
  364. 364 }

 

测试用的树

 

有哪些收获:

  1.注意审题,只有四种类型

  2.函数模块化,使函数尽量简短易读

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号