哈尔滨工业大学计算机科学与技术学院
实验报告
课程名称:数据结构与算法
课程类型:必修
实验项目:树型结构的建立与遍历
实验题目:二叉树存储结构的建立与遍历
实验日期:2021.10.27
班级:2003001
学号:120L021011
姓名:石卓凡
一、 实验目的
\1. 掌握树的链式存储方式及其操作实现(创建、遍历、查找等)。
\2. 掌握二叉树用不同方法表示所对应的不同输入形式。
\3. 掌握二叉树中各种重要性质在解决实际问题中的应用。
\4. 掌握哈夫曼树的构造方法及其编码方法。
\5. 掌握二叉排序树的特性及其构造方法。
二、实验要求及实验环境
1.编写建立二叉树的二叉链表存储结构(左右链表示)的程序,并以适当的形式显示和保存二叉树;
2.采用二叉树的二叉链表存储结构,编写程序实现二叉树的先序、中序和后序遍历的递归和非递归算法以及层序遍历算法,并以适当的形式显示和保存二叉树及其相应的遍历序列;
3****.给定一个二叉树, 编写算法完成下列应用:(二选一)
(1)判断其是否为完全二叉树;
(2)求二叉树中任意两个结点的公共祖先。
实验环境:
操作系统:Win7/Win10
集成开发环境:devcpp++
外部库:暂无
二、 设计思想(本程序中的用到的所有数据类型的定义,主程序的流程图及各程序模块之间的调用关系)
1. 函数功能和外部接口设计
本系统总计设计了13个函数,每个函数的功能和接口设计如下表所示:
序号 |
函数名 |
函数功能 |
函数参数 |
函数返回值 |
1 |
Main |
根据order执行各函数 |
无 |
无 |
2 |
Creat |
读入TNode.txt的信息,建立根节点为Node的二叉树 |
BTreeNode* &Node |
NULL |
3 |
VLR |
前序遍历 |
BTreeNode *T |
NULL |
4 |
LVR |
中序遍历 |
BTreeNode *T |
NULL |
5 |
LBR |
后序遍历 |
BTreeNode *T |
NULL |
6 |
LevelOrder |
层序遍历 |
BTreeNode* T |
NULL |
7 |
IsWanQuanErChaShu |
判断是否为完全二叉树 |
BTreeNode* T |
Bool(True or false) |
8 |
ShuZuHua |
将链式存储转为顺序数组存储 |
BTreeNode* NODE,TreeList TList[],int i |
NULL |
9 |
GongGongZuXian |
求两个节点对应的最近公共组先 |
TreeList TList[],char A,char B |
NULL |
10 |
PreOrder_2 |
先序遍历非递归 |
BTreeNode* T |
NULL |
11 |
InOrder_2 |
中序遍历非递归 |
BTreeNode* T |
NULL |
12 |
PostOrder_2 |
后序遍历非递归 |
BTreeNode* T |
NULL |
13 |
Show |
图形展示二叉树 |
BTreeNode* T |
NULL |
14 |
Depth |
求出树的深度 |
BTreeNode* T |
Int(depth) |
数据定义
typedef struct BTreeNode
{
char Value;
struct BTreeNode *Left;
struct BTreeNode *Right;
}BTreeNode;
typedef struct TreeList
{
char Value;
}TreeList;
1.逻辑设计
(1)先序递归遍历
1.如果树T不为空
1.1输出T的数据域
1.2递归的遍历左子树
1.3递归的遍历右子树
(2)中序递归遍历
1.如果树T不为空
1.1递归的遍历左子树
1.2输出T的数据域
1.3递归的遍历右子树
(3)后序递归遍历
1.如果树T不为空
1.1递归的遍历左子树
1.2递归的遍历右子树
1.3输出T的数据域
(4)先序遍历非递归
\1. 如果根节点不为空则入栈
\2. While循环,当T不空或者栈不空
2.1 借助while循环沿着T左子树,并且输出当前T,一直走到最左下角的空结点
2.2 如果栈非空,则T=栈顶元素,并且弹出栈顶,进入T的右子树
2.3 在新的右子树为根节点重复上述进行遍历
(5)中序遍历非递归
\1. 如果根节点不为空则入栈
2 While循环,当T不空或者栈不空
2.1借助while循环沿着T左子树一直走到最左下角的空结点
2.2如果栈非空,则T=栈顶元素,并且弹出栈顶,并且输出当前T,进入T的右子树
2.3在新的右子树为根节点重复上述进行遍历
(6)后序遍历非递归
\1. 如果根节点不为空则入栈
\2. While循环,当T不空或者栈不空
2.1如果T不空则一直T=T的左子树,并且进栈
2.2如果T为空,T等于栈顶元素,并且弹出栈顶元素,考虑T的右子树,如果右子树存在且从未访问过,则最右子树进行后序遍历。否则,输出当前的T,并且用r记录当前的T,令T=NULL进行下一次循环
(7)层序遍历
\1. 如果根节点不为空则入队
\2. 循环直到队列为空
2.1 q=队头元素且出队,cout<<q;
2.2 如果q有左儿子则入队
2.3 如果q有右儿子则入队
(8)判断是否为完全二叉树
\1. 如果根节点不为空则入队
\2. 循环直到队列为空
2.1 如果左右孩子都有,直接左右孩子入队
2.2 如果左孩子为空,右孩子不为空,则不是完全二叉树
2.3 如果左孩子不为空,有孩子为空 ,或者如果左右孩子都为空,则从此以后访问到的结点都必须是叶子结点。如果此后出现非叶子结点,则不是完全二叉树
(9)求两个节点对应的最近公共组先
\1. 利用递归将链式存储转为数组存储
1.1 如果传入的NODE不为空,则将Value和传入的下标i,对应存入数组,并且再次调用函数,将NODE的左右儿子及其对应下标传入函数
1.2如果传入的NODE为空,则Value为#
\2. While循环直到找到公共祖先
2.1 将两个结点下标较大的除以2,将其下标指向其父节点下标
2.2 当两个节点下标相同时候,结束循环
(10)图形展示二叉树
\1. 如果根节点不为空则入队,同时利用Depth求出树的深度
\2. While直到所有都已经成功输出
2.1 len记录当前队里size代表这一层有多少个结点
2.2 While将当前这一层的所有节点输出,将左右子树入队,并且左右子树为空时候NEW一个节点将Value设为#,利用深度控制输出次数
(11)创建二叉树
\1. 文件读入根结点,入队
\2. 如果根节点不空
2.1 弹出队列首元素,读入左儿子,入队
2.2 读入右儿子,入队
2.3 借队列依次重复读入
(12)求出树的深度
\1. 如果结点存在,则递归调用返回左右子树最大值+1
\2. 如果节点不存在,直接return 0
2.物理设计
1.BTreeNode用到左右儿子链式存储,链表结点中储存了Value值, BTreeNode *Left指向该节点的左儿子,BTreeNode *Right指向该节点的右儿子
2.TreeList用到左右儿子顺序存储,存储了Char型的Value,代表这个树节点的value值,下标关系代表和其他节点相应的父子关系
3.stack栈用链式存储,栈有指向头节点的指针Node *head指向最初进入的元素(栈底),指向尾结点的Node *p,p指向栈顶最新进入的元素,记录栈元素数量的int length,而每个Node节点有对应数据类型的data,指向下一个结点的Node *next,
4.queue队列用链式存储,有指向队首的指针Node *head,指向队尾的指针Node *rear,记录队列元素多少的int length,而每一个Node结点内部都有,对应数据类型的data,指向下一个结点的Node *next
\3. 主程序流程图及各程序模块之间的调用关系
\4.
流程图
调用关系

四、测试结果
样例1:





样例2****:




五、****经验体会与不足
经验体会:
(1)对于遍历的非递归算法就是将递归算法利用while循环与if条件判断以及队列和栈的辅助,同一个思路写出来,递归算法简单易懂,非递归能够更进一步深入理解这种方式
(2)在求判断是否为完全二叉树等很多关于二叉树的问题都是基于遍历二叉树的算法上来做的
(3)在求公共祖先时候发现二叉树的存储方式,链式存储和数组存储各有利弊,在求公共祖先时候需要将链式转为数组,利用数组中父子结点对应下标数学关系很容易求出公共祖先
(4)在后面我们所学习到的图的深度遍历和树的先序遍历十分相似,在非递归上也都应用到栈。
过程暴露的不足:
① 对于书写非递归遍历时候,没有一个总体的把握,第一次写while循环内部判断条件总是没有考虑完全
② 解决方法:对while循环判断条件在写出程序大体框架之后再补充写出判断条件
③ 对于首次从链式存储转为数组存储树的时候非递归代码占了100多行,过于冗长
④ 解决方法:换成递归算法,直观而且简洁
六、****附录:源代码(带注释)
**
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 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219
|
\#include<iostream>
\#include<stdlib.h>
\#include<stdio.h>
using namespace std;
\#define MAXSIZE 100
typedef struct BTreeNode
{
char Value;
struct BTreeNode *Left;
struct BTreeNode *Right;
}BTreeNode;
typedef struct TreeList
{
char Value;
}TreeList;
template<class T>class stack
{
private:
struct Node
{
T data;
Node *next;
};
Node *head;
Node *p;
int length;
public:
stack()
{
head = NULL;
length = 0;
}
void push(T n)
{
Node *q = new Node;
q->data = n;
if (head == NULL)
{
q->next = head;
head = q;
p = q;
}
else
{
q->next = p;
p = q;
}
length++;
}
void pop()
{
if (length <= 0)
{
abort();
}
Node *q;
T data;
q = p;
data = p->data;
p = p->next;
delete(q);
length--;
}
int size()
{
return length;
}
T top()
{
return p->data;
}
bool empty()
{
if (length == 0)
{
return true;
}
else
{
return false;
}
}
};
template<class T>class queue
{
private:
struct Node
{
T data;
Node *next;
};
Node *head;
Node *rear;
int length;
public:
queue()
{
head = NULL;
rear = NULL;
length = 0;
}
void push(T n)
{
Node *node = new Node;
node->data = n;
node->next = NULL;
if (head == NULL)
{
head = node;
rear = node;
}
else
{
rear->next = node;
rear = node;
}
length++;
}
void pop()
{
if (length <= 0)
{
abort();
}
Node *temp = head;
head = head->next;
delete (temp);
length--;
}
int size()
{
return length;
}
T front()
{
return head->data;
}
bool empty()
{
if (length == 0)
{
return true;
}
else
{
return false;
}
}
};
void Creat(BTreeNode* &Node);
void VLR(BTreeNode *T);
void LVR(BTreeNode *T);
void LBR(BTreeNode* T);
void LevelOrder(BTreeNode* T);
bool IsWanQuanErChaShu(BTreeNode* T);
void ShuZuHua(BTreeNode* NODE,TreeList TList[],int i)
{
if(NODE)
{
TList[i].Value=NODE->Value;
ShuZuHua(NODE->Left,TList,2*i);
ShuZuHua(NODE->Right,TList,2*i+1);
}
else
{
TList[i].Value='#';
}
}
void GongGongZuXian(TreeList TList[],char A,char B)
{
int Father=-1;
int Anum=-1;
int Bnum=-1;
for(int i = 1;i<MAXSIZE;i++)
{
if(A==TList[i].Value)
Anum=i;
if(B==TList[i].Value)
Bnum=i;
}
while(1)
{
if(Anum==Bnum)
{
cout<<"祖先:"<<TList[Anum].Value;
return;
}
else if(Anum>Bnum)
{
Anum=Anum/2;
}
else if(Bnum>Anum)
Bnum=Bnum/2;
}
}
void Creat(BTreeNode* &Node)
{
FILE *p;
FILE *pout;
if((pout=fopen("TNodeOut.txt","a"))==NULL)
{
cout<<"文件打开失败";
exit(0);
}
if((p=fopen("TNode.txt","r"))==NULL)
{
cout<<"文件打开失败";
exit(0);
}
queue<BTreeNode*> que;
char Value;
fscanf(p,"%c ",&Value);
if(Value=='#')
{
Node=NULL;
return;
}
else
{
Node = new BTreeNode;
Node->Value = Value;
que.push(Node);
}
while(!que.empty())
{
BTreeNode *node = que.front();
que.pop();
printf("%c ",node->Value);
fprintf(pout,"%c ",node->Value);
fscanf(p,"%c ",&Value);
if(Value!='#')
{
node->Left = new BTreeNode;
node->Left->Value =Value;
printf("Left -> %c ",Value);
fprintf(pout,"Left -> %c ",Value);
que.push(node->Left);
}
else
{
node->Left=NULL;
printf("Left -> %c ",Value);
fprintf(pout,"Left -> %c ",Value);
}
fscanf(p,"%c ",&Value);
if(Value!='#')
{
node->Right = new BTreeNode;
node->Right->Value =Value;
printf("Right -> %c \n",Value);
fprintf(pout,"Right -> %c \n",Value);
que.push(node->Right);
}
else
{
node->Right=NULL;
printf("Right -> %c \n",Value);
fprintf(pout,"Right -> %c \n",Value);
}
}
fclose(p);
fclose(pout);
}
void VLR(BTreeNode *T)
{
if(T==NULL)
return ;
else
{
cout<<T->Value;
VLR(T->Left);
VLR(T->Right);
}
}
void LVR(BTreeNode *T)
{
if(T==NULL)
{
return;
}
else
{
LVR(T->Left);
cout<<T->Value;
LVR(T->Right);
}
}
void LBR(BTreeNode* T)
{
if(T==NULL)
{
return;
}
else
{
LVR(T->Left);
LVR(T->Right);
cout<<T->Value;
}
}
void LevelOrder(BTreeNode* T)
{
FILE *pout;
if((pout=fopen("TNodeOut.txt","a"))==NULL)
{
cout<<"文件打开失败";
exit(0);
}
fprintf(pout,"层序遍历:");
if(T ==NULL)
return ;
queue<BTreeNode*> que;
que.push(T);
while(!que.empty())
{
BTreeNode* t;
t=que.front();
que.pop();
fprintf(pout,"%c",t->Value);
cout<<t->Value;
if(t->Left!=NULL)
que.push(t->Left);
if(t->Right!=NULL)
que.push(t->Right);
}
fprintf(pout,"\n");
fclose(pout);
}
void PreOrder_2(BTreeNode* T)
{
FILE *pout;
if((pout=fopen("TNodeOut.txt","a"))==NULL)
{
cout<<"文件打开失败";
exit(0);
}
fprintf(pout,"前序遍历:");
if(T==NULL)
return;
stack<BTreeNode*> s;
while(T||!s.empty())
{
while(T)
{
fprintf(pout,"%c",T->Value);
cout<<T->Value;
s.push(T);
T=T->Left;
}
if(!s.empty())
{
T=s.top();
s.pop();
T=T->Right;
}
}
fprintf(pout,"\n");
fclose(pout);
}
void InOrder_2(BTreeNode* T)
{
FILE *pout;
if((pout=fopen("TNodeOut.txt","a"))==NULL)
{
cout<<"文件打开失败";
exit(0);
}
fprintf(pout,"中序遍历:");
if(T==NULL)
return;
stack<BTreeNode*> s;
while(T||!s.empty())
{
while(T)
{
s.push(T);
T=T->Left;
}
if(!s.empty())
{
T=s.top();
s.pop();
fprintf(pout,"%c",T->Value);
cout<<T->Value;
T=T->Right;
}
}
fprintf(pout,"\n");
fclose(pout);
}
void PostOrder_2(BTreeNode* T)
{
if(T==NULL)
return;
BTreeNode* r=NULL;
stack<BTreeNode*> s;
while(T||!s.empty())
{
if(T)
{
s.push(T);
T=T->Left;
}
else
{
T=s.top();
if(T->Right&&r!=T->Right)
{
T=T->Right;
}
else
{
s.pop();
cout<<T->Value;
r=T;
T=NULL;
}
}
}
}
bool IsWanQuanErChaShu(BTreeNode* T)
{
if(T==NULL)
return false;
queue<BTreeNode*>q;
q.push(T);
while(!q.empty())
{
BTreeNode* Top = q.front();
if(Top->Left&&Top->Right)
{
q.pop();
q.push(Top->Left);
q.push(Top->Right);
}
if(Top->Left==NULL&Top->Right!=NULL)
{
return false;
}
if((Top->Left&&Top->Right==NULL)||(Top->Left==NULL&&Top->Right==NULL))
{
q.pop();
if(Top->Left)
q.push(Top->Left);
while(!q.empty())
{
Top=q.front();
if(Top->Left==NULL&Top->Right==NULL)
{
q.pop();
}
else
{
return false;
}
}
return true;
}
}
return true;
}
int Depth(BTreeNode* T)
{
if(T)
return max(Depth(T->Left),Depth(T->Right))+1;
else
return 0;
}
void Show(BTreeNode* T)
{
int TDepth = Depth(T);
if(T==NULL)
return;
queue<BTreeNode*> q;
q.push(T);
while(1)
{
int len=q.size();
if(TDepth==0)
break;
while(len>0)
{
T =q.front();
q.pop();
cout<<T->Value;
if(T->Left)
q.push(T->Left);
else
{
BTreeNode* temp;
temp=new BTreeNode;
temp->Value='#';
temp->Left=NULL;
temp->Right=NULL;
q.push(temp);
}
if(T->Right)
q.push(T->Right);
else
{
BTreeNode* temp;
temp=new BTreeNode;
temp->Value='#';
temp->Left=NULL;
temp->Right=NULL;
q.push(temp);
}
len--;
}
cout<<endl;
TDepth--;
}
}
int main()
{
BTreeNode* T;
TreeList TList[MAXSIZE];
int n;
cout<<" -------------------------------------------------------------------\n";
cout<<" |================== 二叉树的相关操作 =================|\n";
cout<<" -------------------------------------------------------------------\n\n";
cout<<" |================== 1.前序 =================|\n";
cout<<" |================== 2.中序 =================|\n";
cout<<" |================== 3.后序 =================|\n";
cout<<" |================== 4.层序 =================|\n";
cout<<" |================== 5.前序(非递归) =================|\n";
cout<<" |================== 6.中序(非递归) =================|\n";
cout<<" |================== 7.后序(非递归) =================|\n";
cout<<" |================== 8.图形显示 =================|\n";
cout<<" |================== 9.判断是否为完全二叉树 =================|\n";
cout<<" |================== 10.公共祖先 =================|\n";
Creat(T);
while(1)
{
cout<<"请输入";
cin>>n;
switch(n)
{
case 1:
cout<<"前序";
VLR(T);
cout<<endl;
break;
case 2:
cout<<"中序";
LVR(T);
cout<<endl;
break;
case 3:
cout<<"后序";
LBR(T);
cout<<endl;
break;
case 4:
cout<<"层序";
LevelOrder(T);
cout<<endl;
break;
case 5:
cout<<"前序(非递归)";
PreOrder_2(T);
cout<<endl;
break;
case 6:
cout<<"中序(非递归)";
InOrder_2(T);
cout<<endl;
break;
case 7:
cout<<"后序(非递归)";
PostOrder_2(T);
cout<<endl;
break;
case 8:
cout<<"图形显示\n";
Show(T);
cout<<endl;
break;
case 10:
cout<<"公共祖先"<<endl;
ShuZuHua(T,TList,1);
char C1,C2;
cin>>C1>>C2;
GongGongZuXian(TList,C1,C2);
cout<<"\n";
break;
case 9:
if(IsWanQuanErChaShu(T)==true)
cout<<"是完全二叉树\n";
else
cout<<"不是完全二叉树\n";
break;
}
}
}
|