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

 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 }

 

posted @ 2016-05-19 22:28  赤云封天  阅读(384)  评论(0编辑  收藏  举报