数据结构 【实验7 二叉树基本操作】
實驗7???二叉樹基本操作
實驗目的
1.??熟悉二叉樹結點的結構和對二叉樹的基本操作。
2.??掌握對二叉樹每一種操作的具體實現。
3.??學會利用遞歸方法編寫對二叉樹這種遞歸數據結構進行處理的算法。
實驗內容
該程序的功能是實現二叉樹結點的類型定義和對二叉樹的基本操作。該程序包括二叉樹結構類型以及每一種操作的具體的函數定義和主函數。
/*?定義DataType為char類型?*/
typedef char DataType;
?
/*?二叉樹的結點類型?*/
typedef struct BitNode
{DataType data;
?????struct BitNode *lchild,*rchild;
}BitNode,*BitTree;
?
/*?初始化二叉樹,即把樹根指針置空?*/
void BinTreeInit(BitTree *BT)
?
/*?按先序次序建立一個二叉樹*/
void BinTreeCreat(BitTree *BT)
?
/*?檢查二叉樹是否為空?*/
int BinTreeEmpty(BitTree *BT)
?
/*?按任一種遍歷次序(包括按先序、中序、后序、按層次)輸出二叉樹中的所有結點?*/
void BinTraverse(BitTree *BT)
?
/*?求二叉樹的深度?*/
int BinTreeDepth(BitTree BT)
?
/*?求二叉樹中所有結點數?*/
int??BinTreeCount(BitTree BT)
?
/*?清除二叉樹,使之變為空樹?*/
void BinTreeClear(BitTree *BT)
?
1 #include <iostream> 2 #include <stdio.h> 3 #include <queue> 4 using namespace std; 5 6 /* 實驗內容 7 該程序的功能是實現二叉樹結點的類型定義和對二叉樹的基本操作。該程序包括二叉樹結構類型以及每一種操作的具體的函數定義和主函數。 8 */ 9 10 /* 定義DataType為char類型 */ 11 typedef char DataType; 12 13 /* 二叉樹的結點類型 */ 14 typedef struct BitNode{ 15 DataType data; 16 struct BitNode *lchild,*rchild; 17 }BitNode,*BitTree; 18 19 /* 初始化二叉樹,即把樹根指針置空 */ 20 void BinTreeInit(BitTree &BT) 21 { 22 BT = NULL; 23 } 24 25 /* 按先序次序建立一個二叉樹*/ 26 void BinTreeCreat(BitTree &BT) 27 { 28 //printf("請輸入該節點的元素值:"); 29 scanf("%c",&BT->data); 30 while(BT->data==' ' || BT->data=='\n'){ 31 scanf("%c",&BT->data); 32 } 33 if(BT->data=='#'){ //輸入為'#'說明該節點為空,返回上一步 34 BT=NULL; 35 return ; 36 } 37 BT->lchild = (BitNode*)malloc(sizeof(BitNode)); 38 BT->rchild = (BitNode*)malloc(sizeof(BitNode)); 39 printf("請輸入%c的左節點\n",BT->data); 40 BinTreeCreat(BT->lchild); 41 printf("請輸入%c的右節點\n",BT->data); 42 //printf("進入右子樹\n"); 43 BinTreeCreat(BT->rchild); 44 } 45 46 /* 檢查二叉樹是否為空 */ 47 int BinTreeEmpty(BitTree BT) 48 { 49 if(BT==NULL) 50 return 1; 51 else 52 return 0; 53 } 54 55 /* 按任一種遍歷次序(包括按先序、中序、后序、按層次)輸出二叉樹中的所有結點 */ 56 void BinTraverse1(BitTree BT) //按前序遍歷 57 { 58 if(BT==NULL) //遞歸出口 59 return ; 60 printf("%c",BT->data); 61 BinTraverse1(BT->lchild); 62 BinTraverse1(BT->rchild); 63 } 64 65 /* 按任一種遍歷次序(包括按先序、中序、后序、按層次)輸出二叉樹中的所有結點 */ 66 void BinTraverse2(BitTree BT) //按中序遍歷 67 { 68 if(BT==NULL) //遞歸出口 69 return ; 70 BinTraverse2(BT->lchild); 71 printf("%c",BT->data); 72 BinTraverse2(BT->rchild); 73 } 74 75 /* 按任一種遍歷次序(包括按先序、中序、后序、按層次)輸出二叉樹中的所有結點 */ 76 void BinTraverse3(BitTree BT) //按后序遍歷 77 { 78 if(BT==NULL) //遞歸出口 79 return ; 80 BinTraverse3(BT->lchild); 81 BinTraverse3(BT->rchild); 82 printf("%c",BT->data); 83 } 84 85 /* 按任一種遍歷次序(包括按先序、中序、后序、按層次)輸出二叉樹中的所有結點 */ 86 void BinTraverse4(BitTree BT) //按層次遍歷 87 { 88 queue <BitTree> q; 89 BitTree cur = BT; 90 q.push(cur); 91 while(!q.empty()){ //隊列空為止 92 cur = q.front(); 93 q.pop(); 94 if(cur==NULL) //遇到空節點退出本次循環 95 continue; 96 printf("%c",cur->data); //輸出當前節點元素 97 q.push(cur->lchild); //將該節點的兩個孩子節點入棧 98 q.push(cur->rchild); 99 } 100 } 101 102 /* 求二叉樹的深度 */ 103 int BinTreeDepth(BitTree BT) 104 { 105 if(BT==NULL) //遞歸出口 106 return 0; 107 //記錄左子樹深度和右子樹深度,返回較大的深度+1 108 int ldp = BinTreeDepth(BT->lchild); 109 int rdp = BinTreeDepth(BT->rchild); 110 return ldp>rdp?ldp+1:rdp+1; 111 } 112 113 /* 求二叉樹中所有結點數 */ 114 int BinTreeCount(BitTree BT) 115 { 116 if(BT==NULL) //遞歸出口 117 return 0; 118 //返回左子樹節點數和右子樹節點數再加上當前節點 119 return BinTreeCount(BT->lchild) + BinTreeCount(BT->rchild) + 1; 120 } 121 122 /* 清除二叉樹,使之變為空樹 */ 123 void BinTreeClear(BitTree &BT) 124 { 125 if(BT==NULL) //遞歸出口 126 return ; 127 //左右子樹找到底,然后返回的時候依次銷毀空間 128 BinTreeClear(BT->lchild); 129 BinTreeClear(BT->rchild); 130 free(BT); 131 BT=NULL; 132 } 133 134 int Menu() //菜單 135 { 136 int n; 137 printf("[1] 按先序次序建立一個二叉樹\n"); 138 printf("[2] 檢查二叉樹是否為空\n"); 139 printf("[3] 按先序輸出二叉樹中的所有結點\n"); 140 printf("[4] 按中序輸出二叉樹中的所有結點\n"); 141 printf("[5] 按后序輸出二叉樹中的所有結點\n"); 142 printf("[6] 按層次輸出二叉樹中的所有結點\n"); 143 printf("[7] 求二叉樹的深度\n"); 144 printf("[8] 求二叉樹中所有結點數\n"); 145 printf("[9] 清除二叉樹,使之變為空樹\n"); 146 printf("[0] 退出\n"); 147 scanf("%d",&n); 148 return n; 149 } 150 151 void Reply(BitTree &BT,int n) //對菜單的響應 152 { 153 switch(n){ 154 case 1: //創建二叉樹 155 if(BT!=NULL) 156 printf("已創建二叉樹! 請先清除二叉樹再創建!\n"); 157 else{ //二叉樹為空 158 printf("按先序依次增加節點,輸入'#'表示空節點!\n\n"); 159 BT = (BitNode*)malloc(sizeof(BitNode)); //創建根節點 160 BT->lchild = NULL; 161 BT->rchild = NULL; 162 printf("請輸入根節點的元素值:\n"); 163 BinTreeCreat(BT); 164 printf("\n二叉樹創建成功!\n\n"); 165 } 166 break; 167 case 2: //檢查二叉樹是否為空 168 if(BinTreeEmpty(BT)) 169 printf("二叉樹為空!\n\n"); 170 else 171 printf("二叉樹不為空!\n\n"); 172 break; 173 case 3: //按前序遍歷 174 if(BT==NULL){ 175 printf("二叉樹為空!無法遍歷!\n\n"); 176 break; 177 } 178 printf("前序遍歷順序為:\n"); 179 BinTraverse1(BT); 180 printf("\n\n"); 181 break; 182 case 4: //按中序遍歷 183 if(BT==NULL){ 184 printf("二叉樹為空!無法遍歷!\n\n"); 185 break; 186 } 187 printf("中序遍歷順序為:\n"); 188 BinTraverse2(BT); 189 printf("\n\n"); 190 break; 191 case 5: //按后序遍歷 192 if(BT==NULL){ 193 printf("二叉樹為空!無法遍歷!\n\n"); 194 break; 195 } 196 printf("后序遍歷順序為:\n"); 197 BinTraverse3(BT); 198 printf("\n\n"); 199 break; 200 case 6: //按層次遍歷 201 if(BT==NULL){ 202 printf("二叉樹為空!無法遍歷!\n\n"); 203 break; 204 } 205 printf("層次遍歷順序為:\n"); 206 BinTraverse4(BT); 207 printf("\n\n"); 208 break; 209 case 7: //求二叉樹的深度 210 printf("二叉樹的深度為:%d\n\n",BinTreeDepth(BT)); 211 break; 212 case 8: //求二叉樹的節點數 213 printf("二叉樹的總結點數為:%d\n\n",BinTreeCount(BT)); 214 break; 215 case 9: //清除二叉樹 216 if(BT==NULL){ 217 printf("二叉樹已經為空!無需清空!\n\n"); 218 break; 219 } 220 BinTreeClear(BT); 221 printf("清除成功!\n\n"); 222 break; 223 default: 224 exit(1); 225 } 226 system("pause"); 227 system("cls"); 228 } 229 230 int main() 231 { 232 BitTree BT = NULL; 233 BinTreeInit(BT); //初始化二叉樹 234 while(1){ 235 int n = Menu(); 236 Reply(BT,n); 237 } 238 return 0; 239 }?
Freecode : www.cnblogs.com/yym2013
轉載于:https://www.cnblogs.com/yym2013/p/3791088.html
總結
以上是生活随笔為你收集整理的数据结构 【实验7 二叉树基本操作】的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: POJ 3104 Drying 二分
- 下一篇: 迭代器(Iterator)模式