void BinTree_LRD(LinkedBinTree *bt, void (*oper)(LinkedBinTree *p)) { if(bt) { BinTree_LRD(bt->left,oper); BinTree_LRD(bt->right,oper); oper(bt); } return; }
int main() { ChainBinTree *root = NULL; char select; void (*oper1)(); //指向函数的指针 oper1 = oper; do { printf("\n1.设置二叉树根元素 2.添加二叉树结点\n"); printf("3.前序遍历 4.中序遍历\n"); printf("5.后序遍历 6.二叉树深度\n"); printf("0.退出\n"); printf("你的选择:"); select = getchar(); switch (select) { case '1': root = InitRoot(); break; case '2': AddNode(root); break; case '3': printf("------------------------------\n"); printf("前序遍历的结果:\n"); BinTree_DLR(root,oper1); printf("------------------------------\n"); break; case '4': printf("------------------------------\n"); printf("中序遍历的结果:\n"); BinTree_LDR(root,oper1); printf("------------------------------\n"); break; case '5': printf("------------------------------\n"); printf("后序遍历的结果:\n"); BinTree_LRD(root,oper1); printf("------------------------------\n"); break; case '6': printf("------------------------------\n"); printf("二叉树的深度为:%d\n", Depth(root)); printf("------------------------------\n"); break; case '0': break; } getchar(); //抵消 \n }while(select!='0'); Clear(root); return 0; }
void BinTreeTest() { LinkedBinTree *root=NULL; int select; void (*oper1)(); oper1=oper; do { printf("\n1.设置二叉树根元素 2.添加二叉树结点\n"); printf("3.先序遍历 4.中序遍历\n"); printf("5.后序遍历 6.按层遍历\n"); printf("7.二叉树深度 0.退出\n"); scanf("%d", &select); switch(select){ case 1: //设置根元素 root=InitRoot(); break; case 2: //添加结点 AddNode(root); break; case 3://先序遍历 printf("\n先序遍历的结果:"); BinTree_DLR(root,oper1); printf("\n"); break; case 4://中序遍历 printf("\n中序遍历的结果:"); BinTree_LDR(root,oper1); printf("\n"); break; case 5://后序遍历 printf("\n后序遍历的结果:"); BinTree_LRD(root,oper1); printf("\n"); break; case 6://按层遍历 printf("\n按层遍历的结果:"); BinTree_Level(root,oper1); printf("\n"); break; case 7://二叉树的深度 printf("\n二叉树深度为:%d\n",BinTreeDepth(root)); break; case 0: break; } } while(select!=0); BinTreeClear(root); root=NULL; }