树的基本操作
1 #include <stdio.h>
2 #include"queue.h"
3 #include<stddef.h>
4 #include<stdlib.h>
5 //typedef char TreeType;
6 //typedef struct TreeNode
7 //{
8 // TreeType data;
9 // struct TreeNode *lchild;
10 // struct TreeNode *rchild;
11 //}TreeNode;
12
13 void Treeinit(TreeNode **root)
14 {
15 if(root==NULL)
16 return;
17 *root=NULL;
18 }
19 void TreePreOrder(TreeNode *root)
20 {
21 if(root==NULL)
22 return;
23 printf("%c ",root->data);
24 TreePreOrder(root->lchild);
25 TreePreOrder(root->rchild);
26 }
27 void TreeLnOrder(TreeNode *root)
28 {
29 if(root==NULL)
30 return;
31 TreeLnOrder(root->lchild);
32 printf("%c ",root->data);
33 TreeLnOrder(root->rchild);
34 }
35 void TreePostOrder(TreeNode *root)
36 {
37 if(root==NULL)
38 return;
39 TreePostOrder(root->lchild);
40 TreePostOrder(root->rchild);
41 printf("%c ",root->data);
42 }
43 TreeNode* CreateNode(TreeType vlaue)
44 {
45 TreeNode* root=(TreeNode*)malloc(sizeof(TreeNode));
46 root->data=vlaue;
47 root->lchild=NULL;
48 root->rchild=NULL;
49 return root;
50 }
51 TreeNode* _TreeCreate(TreeType array[],size_t size,size_t *index,TreeType null_node)
52 {
53 if(index==NULL)
54 return NULL;
55 if(*index >= size)
56 return NULL;
57 if(array[*index]==null_node)
58 return NULL;
59 TreeNode* new_node=CreateNode(array[*index]);
60 ++(*index);
61 new_node->lchild=_TreeCreate(array,size,index,null_node);
62 ++(*index);
63 new_node->rchild=_TreeCreate(array,size,index,null_node);
64 return new_node;
65 }
66 TreeNode* TreeCreate(TreeType array[],size_t size,TreeType null_node)
67 {
68 size_t index=0;
69 return _TreeCreate(array,size,&index,null_node);
70 }
71 void TreeLeveOrder(TreeNode *root)
72 {
73 if(root==NULL)
74 return;
75 QueueNode queue;
76 SeqQueueinit(&queue);
77 SeqQueuePush(&queue,root);
78 while(1)
79 {
80 TreeNode front;
81 SeqQueuefront(&queue,&front);
82 if((&queue)->size==0)
83 break;
84 printf("%c ",(&front)->data);
85 SeqQueuePop(&queue);
86 if((&front)->lchild!=NULL)
87 SeqQueuePush(&queue,(&front)->lchild);
88 if((&front)->rchild!=NULL)
89 SeqQueuePush(&queue,(&front)->rchild);
90 }
91 }
92 void TreeDestroy(TreeNode *root)
93 {
94 if(root=NULL)
95 return;
96 TreeDestroy(root->lchild);
97 TreeDestroy(root->rchild);
98 free(root);
99 }
100 void TreeDestroy2(TreeNode *root)
101 {
102 if(root==NULL)
103 return;
104 TreeNode *lchild=root->lchild;
105 TreeNode *rchild=root->rchild;
106 free(root);
107 TreeDestroy2(lchild);
108 TreeDestroy2(rchild);
109 }
110 void TreeSize(TreeNode* root,size_t *size)
111 {
112 if(root==NULL)
113 return;
114 ++(*size);
115 TreeSize(root->lchild,size);
116 TreeSize(root->rchild,size);
117 }
118 int TreeSize2(TreeNode *root)
119 {
120 if(root==NULL)
121 return;
122 return TreeSize2(root->lchild)+TreeSize2(root->rchild)+1;
123 }
124 int TreeLeafSize(TreeNode *root)
125 {
126 if(root==NULL)
127 return;
128 if(root->lchild==NULL&&root->rchild==NULL)
129 return 1;
130 return TreeLeafSize(root->lchild)+TreeLeafSize(root->rchild);
131 }
测试代码
132 int main()
133 {
134 size_t a=0;
135 TreeNode *root;
136 Treeinit(&root);
137 TreeType array[]="abd#g##e##cf#h###";
138 root=TreeCreate(array,sizeof(array)/sizeof(TreeType)-1,'#');
139 // TreePreOrder(root);
140 // TreeDestroy2(root);
141 // a=TreeSize2(root);
142 a=TreeLeafSize(root);
143 printf("%d",a);
144 TreeLeveOrder(root);
145 return 0;
146 }
用到的queue.h
4 #include<stddef.h>
5 #define MAX_SIZE 100
6
7 typedef char TreeType;
8 typedef struct TreeNode
9 {
10 TreeType data;
11 struct TreeNode *lchild;
12 struct TreeNode *rchild;
13 }TreeNode;
14
15 typedef TreeNode QueueType;
16 typedef struct QueueNode
17 {
18 QueueType *data;
19 size_t size;
20 size_t head;
21 size_t tail;
22 }QueueNode;
23
24 void SeqQueueinit(QueueNode *queue);
25 void SeqQueuePush(QueueNode *queue,QueueType *value);
26 void SeqQueuePop(QueueNode *queue);
27 void SeqQueuefront(QueueNode *queue,QueueType *value);
28
queue源文件
1 #include"queue.h"
2 void SeqQueueinit(QueueNode *queue)
3 {
4 if(queue==NULL)
5 return ;
6 queue->data=(QueueType*)malloc(MAX_SIZE*sizeof(QueueType));
7 queue->size=0;
8 queue->head=0;
9 queue->tail=0;
10 }
11 void SeqQueuePush(QueueNode *queue,QueueType *value)
12 {
13 if(queue==NULL)
14 return;
15 if(queue->tail>=MAX_SIZE)
16 {
17 queue->tail=0;
18 }
19 queue->data[queue->tail++]=*value;
20 queue->size++;
21 }
22 void SeqQueuePop(QueueNode *queue)
23 {
24 if(queue==NULL)
25 return;
26 if(queue->size==0)
27 return ;
28 queue->head++;
29 queue->size--;
30 if(queue->head>=MAX_SIZE)
31 {
32 queue->head=0;
33 }
34 }
35 void SeqQueuefront(QueueNode *queue,QueueType *value)
36 {
37 if(queue==NULL)
38 return;
39 if(queue->size==0)
40 return ;
41 *value=queue->data[queue->head];
42
43 }