C语言演示二叉树算法

 时间:2024-10-11 19:20:59

1、首先打开VC++6.0

C语言演示二叉树算法

3、选择C++ source file 新建一个空白文档

C语言演示二叉树算法

5、定义树的结点结构typedef struct TreeNode{ char data;/*树中结点的数据是一个字符*/ struct TreeNode *lchild; struct TreeNode *rchild;}TREENODE;

C语言演示二叉树算法

7、用函数建立一个二叉树int CreateBiTree(TREENODE **T)/涯箨唁峦*按先序次序输入二叉树中结点的值,以空字符表示空树*/{char i;if(ch[inc++]==' ') *T = NULL;else{printf("%c\n",ch[inc-1]);if(!(*T = (TREENODE *)malloc(sizeof(TREENODE)))) return -1;(*T)->data = ch[inc-1];printf("%c\n",(*T)->data);CreateBiTree(&((*T)->lchild));CreateBiTree(&((*T)->rchild));}return 1;}

C语言演示二叉树算法

9、中序遍历二叉树int InOderTraverse(TREENODE *T){if(T){InOderTraverse(T->lchild);printf("%c ",T->data);InOderTraverse(T->rchild);}return 1;}

C语言演示二叉树算法

11、利用先序遍历来计算树中的结点数void CountNodeNum(TREENODE *T){if(T){NodeNum ++;CountNodeNum(T->lchild);CountNodeNum(T->rchild);}}

C语言演示二叉树算法

13、主函数int main(拘七呷憎){TREENODE *T;int i;CreateBiTree(&T);do{ puts("************************忮氽阝另**************************"); puts("* you can choose: *"); puts("* 1: Traverse the Binary tree by pre order *"); puts("* 2: Traverse the Binary tree by mid order *"); puts("* 3: Traverse the Binary tree by back order *"); puts("* 4: Count the node num of the Binary tree *"); puts("* 5: Count the leaf node num of the Binary tree*"); puts("**************************************************"); puts("please input your choice:"); scanf("%d",&i); switch(i) { case 1:printf("The preoder is:\n");PreOderTraverse(T);printf("\n");break;case 2:printf("The midoder is:\n");InOderTraverse(T);printf("\n");break;case 3:printf("The backoder is:\n");BackOderTraverse(T);printf("\n");break;case 4:CountNodeNum(T);printf("The nodenum of the tree is:%d\n",NodeNum);break;case 5:CountLeafNum(T);printf("The leafnum of the tree is:%d\n",LeafNum);break; } printf("please input any char to go on\n"); getch();}while((i>=1)&&(i<6));getch();return 1;}

C语言演示二叉树算法
  • ADT eclipse导出lib(jar包)
  • python3 tkinter教程
  • php如何获得查询mysql结果集的记录数
  • Navicat for mysql怎么设置使用自动保存
  • 如何在ES6中给Map添加元素并总结有几种方法
  • 热门搜索
    欧洲旅游天气 佛冈旅游 长沙有什么旅游景点 澳洲旅游网 杭州市旅游 迪拜旅游公司 哈尔滨旅游路线 天津旅游团 北京旅游局网站 海南旅游卫视