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

图的建立及输出其先序序列,中序序列,后续序列

时间:2016-05-19 23:13:20      阅读:146      评论:0      收藏:0      [点我收藏+]

标签:

 1 #include<stdio.h>
 2 #include<malloc.h>
 3 typedef struct node{
 4     char data;
 5     struct node *left;
 6     struct node *right;
 7 }Node;
 8 
 9 int i = 0;
10 char str[100];
11 
12 void create(Node** p){
13     if(str[i] == \0)
14         return ;
15     if(str[i++] != #){
16         *p = (Node*)malloc(sizeof(Node));
17         (*p)->left = (*p)->right = NULL;
18         (*p)->data = str[i-1];
19         create(&(*p)->left);
20         create(&(*p)->right);
21     }
22 }
23 
24 void print(Node *p){
25     if(p != NULL && p->data != #){
26         printf("%c ",p->data);
27         print(p->left);
28         print(p->right);
29     }
30 }
31 
32 void print_2(Node *p){
33     if(p != NULL && p->data != #){
34         print_2(p->left);
35         printf("%c ",p->data);
36         print_2(p->right);
37     }
38 }
39 
40 void print_3(Node *p){
41     if(p != NULL && p->data != #){
42         print_3(p->left);
43         print_3(p->right);
44         printf("%c ",p->data);
45     }
46 }
47 
48 int main(){
49     gets(str);
50     Node *head = NULL;
51     create(&head);
52     printf("先序序列:");
53     print(head);
54     printf("\n中序序列:");
55     print_2(head);
56 
57     printf("\n后序序列:");
58     print_3(head);
59 
60     return 0;
61 }

 

图的建立及输出其先序序列,中序序列,后续序列

标签:

原文地址:http://www.cnblogs.com/yfs123456/p/5510448.html

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