操作二叉树代码比较长 二叉树基本功能在了 注释打的比较少
1 #include "stdio.h" 2 #include "stdlib.h" 3 #define QUEUE_MAXSIZE 50 4 5 typedef char DATA; 6 typedef struct ChainTree 7 { 8 DATA data; 9 struct ChainTree *left; 10 struct ChainTree *right; 11 }ChainBinTree; 12 void oper(ChainBinTree *p) 13 { 14 printf("%c",p->data); 15 return; 16 } 17 ChainBinTree *BinTreeInit(ChainBinTree *node) //初始化二叉树 18 { 19 if(node!=NULL) 20 return node; 21 else 22 return NULL; 23 } 24 25 int BinTreeAddNode(ChainBinTree *bt,ChainBinTree *node,int n) 26 //bt为父节点,node为子节点,n=1添加左子树,n=2添加右子树 27 { 28 if(bt==NULL) 29 { 30 printf("父节点不存在,请先设置父节点!\n"); 31 return 0; 32 } 33 switch(n) 34 { 35 case 1: 36 if(bt->left) 37 { 38 printf("左子树不为空!\n"); 39 return 0; 40 } 41 else 42 bt->left=node; 43 break; 44 case 2: 45 if(bt->right) 46 { 47 printf("右子树不为空!\n"); 48 return 0; 49 } 50 else 51 bt->right=node; 52 break; 53 default: 54 printf("参数错误!\n"); 55 return 0; 56 } 57 return 1; 58 } 59 60 ChainBinTree *BinTreeLeft(ChainBinTree *bt) //返回左子树 61 { 62 if(bt) 63 return bt->left; 64 else 65 return NULL; 66 } 67 ChainBinTree *BinTreeRight(ChainBinTree *bt) //返回右子树 68 { 69 if(bt) 70 return bt->right; 71 else 72 return NULL; 73 } 74 75 int BinTreeIsEmpty(ChainBinTree *bt) 76 { 77 if(bt) 78 return 0; 79 else 80 return 1; 81 } 82 int BinTreeDepth(ChainBinTree *bt) //求二叉树深度 83 { 84 int dep1,dep2; 85 if(bt==NULL) 86 return 0; 87 else 88 { 89 dep1=BinTreeDepth(bt->left); 90 dep2=BinTreeDepth(bt->right); 91 if(dep1>dep2) 92 return dep1+1; 93 else 94 return dep2+1; 95 } 96 } 97 98 ChainBinTree *BinTreeFind(ChainBinTree *bt,DATA data) 99 { 100 ChainBinTree *p; 101 if(bt==NULL) 102 return NULL; 103 else 104 { 105 if(bt->data==data) 106 return bt; 107 else 108 { 109 if(p=BinTreeFind(bt->left,data)) 110 return p; 111 else if(p=BinTreeFind(bt->right,data)) 112 return p; 113 else 114 return NULL; 115 } 116 } 117 } 118 void BinTreeClear(ChainBinTree *bt) 119 { 120 if(bt) 121 { 122 BinTreeClear(bt->left); 123 BinTreeClear(bt->right); 124 free(bt); 125 bt=NULL; 126 } 127 return ; 128 } 129 void BinTree_DLR(ChainBinTree *bt,void (*oper)(ChainBinTree *p)) 130 { 131 if(bt) 132 { 133 oper(bt); 134 BinTree_DLR(bt->left,oper); 135 BinTree_DLR(bt->right,oper); 136 } 137 return; 138 } 139 void BinTree_LDR(ChainBinTree *bt,void (*oper)(ChainBinTree *p)) 140 { 141 if(bt) 142 { 143 144 BinTree_LDR(bt->left,oper); 145 oper(bt); 146 BinTree_LDR(bt->right,oper); 147 } 148 return; 149 } 150 void BinTree_LRD(ChainBinTree *bt,void (*oper)(ChainBinTree *p)) 151 { 152 if(bt) 153 { 154 155 BinTree_LRD(bt->left,oper); 156 BinTree_LRD(bt->right,oper); 157 oper(bt); 158 159 } 160 return; 161 } 162 163 void BinTree_Level(ChainBinTree *bt,void (*oper)(ChainBinTree *p)) 164 { 165 ChainBinTree *p; 166 ChainBinTree *q[QUEUE_MAXSIZE]; 167 int head=0,tail=0; 168 if(bt) 169 { 170 tail=(tail+1)%QUEUE_MAXSIZE; 171 q[tail]=bt; 172 } 173 while(head!=tail) 174 { 175 head=(head+1)%QUEUE_MAXSIZE; 176 p=q[head]; 177 oper(p); 178 if(p->left!=NULL) 179 { 180 tail=(tail+1)%QUEUE_MAXSIZE; 181 q[tail]=p->left; 182 } 183 if(p->right!=NULL) 184 { 185 tail=(tail+1)%QUEUE_MAXSIZE; 186 q[tail]=p->right; 187 } 188 } 189 return; 190 } 191 192 ChainBinTree *InitRoot() 193 { 194 ChainBinTree *node; 195 if(node=(ChainBinTree*)malloc(sizeof(ChainBinTree))) 196 { 197 printf("\n输入根节点数据:"); 198 scanf("%s",&node->data); 199 node->left=NULL; 200 node->right=NULL; 201 return node; 202 } 203 return NULL; 204 } 205 void AddNode(ChainBinTree *bt) 206 { 207 ChainBinTree *node,*parent; 208 DATA data; 209 char select; 210 if(node=(ChainBinTree*)malloc(sizeof(ChainBinTree))) 211 { 212 printf("\n输入二叉树节点数据:"); 213 fflush(stdin); 214 scanf("%s",&node->data); 215 node->left=NULL; 216 node->right=NULL; 217 printf("请输入父节点数据:"); 218 fflush(stdin); 219 scanf("%s",&data); 220 parent=BinTreeFind(bt,data); 221 if(!parent) 222 { 223 printf("未找到父节点!\n"); 224 free(node); 225 return ; 226 227 } 228 printf("1.添加到左子树\n2.添加到右子树\n"); 229 do{ 230 select=getchar(); 231 select-=‘0‘; 232 if(select==1||select==2) 233 BinTreeAddNode(parent,node,select); 234 }while(select!=1&&select!=2); 235 } 236 return ; 237 } 238 int main() 239 { 240 ChainBinTree *root=NULL; 241 char select; 242 void(*oper1)(ChainBinTree*); 243 oper1=oper; 244 do 245 { 246 printf("\n1.设置二叉树根元素 2.添加二叉树节点\n"); 247 printf("3.先序遍历 4.中序遍历\n"); 248 printf("5.后序遍历 6.按层遍历\n"); 249 printf("7.二叉树深度 0.退出\n"); 250 251 select=getchar(); 252 if(select==‘\n‘){ 253 select=getchar(); 254 } 255 switch(select) 256 { 257 case ‘1‘: 258 root=InitRoot(); 259 break; 260 case ‘2‘: 261 AddNode(root); 262 break; 263 case ‘3‘: 264 printf("\n先序遍历的结果:"); 265 BinTree_DLR(root,oper1); 266 printf("\n"); 267 break; 268 case ‘4‘: 269 printf("\n中序遍历的结果:"); 270 BinTree_LDR(root,oper1); 271 printf("\n"); 272 break; 273 case ‘5‘: 274 printf("\n后序遍历的结果:"); 275 BinTree_LRD(root,oper1); 276 printf("\n"); 277 break; 278 case ‘6‘: 279 printf("\n按层遍历的结果:"); 280 BinTree_Level(root,oper1); 281 printf("\n"); 282 break; 283 case ‘7‘: 284 printf("\n二叉树的深度为:%d\n",BinTreeDepth(root)); 285 break; 286 case ‘0‘: 287 break; 288 } 289 }while(select!=‘0‘); 290 BinTreeClear(root); 291 root=NULL; 292 return 0; 293 }
时间: 2024-10-09 20:16:59