码迷,mamicode.com
首页 > 其他好文 > 详细

二叉树[C实现]

时间:2016-04-30 06:32:58      阅读:98      评论:0      收藏:0      [点我收藏+]

标签:

 1 #include<stdio.h>
 2 #include<malloc.h>
 3 #include<iostream>
 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 }

 

二叉树[C实现]

标签:

原文地址:http://www.cnblogs.com/xuanll/p/5448011.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!