设为首页 加入收藏

TOP

二叉树的建立及递归遍历
2014-11-23 19:33:45 】 浏览:5326
Tags:建立

1. #include
2. #include
3. #include
4.
5. //定义节点
6. typedef struct BiNode{
7. char data;
8. struct BiNode *lch;
9. struct BiNode *rch;
10. }BiNode,*BiTree;
11.
12. //先序拓展序列建立二叉树
13. void Create(BiTree &T)
14. {
15. T =(BiNode*) malloc (sizeof(BiNode));
16.
17. printf("Enter the data \n");
18. scanf(" %c",&T->data);
19. if(T->data=='#') T = NULL;
20. if(T){
21. printf("");
22. Create(T->lch);
23. Create(T->rch);
24. }
25. }
26.
27. //先序遍历 (递归)
28. void Preorder (BiTree T)
29. {
30. if (T) {
31. printf(" %c",T->data); // 访问根结点
32.
33. Preorder(T->lch); // 遍历左子树
34. Preorder(T->rch);// 遍历右子树
35. }
36. }
37.
38. //中序遍历 (递归)
39. void Inorder (BiTree T)
40. {
41. if(T) {
42. Inorder(T->lch);
43.
44. printf(" %c",T->data);
45.
46. Inorder(T->rch);
47. }
48. }
49.
50. //后序遍历 (递归)
51. void Postorder (BiTree T)
52. {
53. if(T) {
54. Postorder(T->lch);
55. Postorder(T->rch);
56.
57. printf(" %c",T->data);
58. }
59. }
60.
61. int main()
62. {
63. //建树
64. printf("The fuction Create() is called.\n");
65. BiTree T;
66. Create(T);
67.
68. //三种遍历递归算法
69. printf("\n");
70. printf("The fuction Preorder() is called.\n");
71. Preorder(T);
72.
73. printf("\n");
74. printf("The fuction Inorder() is called.\n");
75. Inorder(T);
76.
77. printf("\n");
78. printf("The fuction Postorder() is called.\n");
79. Postorder(T);
80.
81.
82. printf("\n");
83. system("pause");
84.
85. }

作者 w174504744

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇VC++锁屏程序遇到的问题 下一篇右键弹出菜单方法

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目