-------------------------------------
典型例题16:数据结构问题---树的存储结构和运算
-------------------------------------
1 #include <iostream>
2 #include <cstdlib>
3
4 using namespace std;
5
6 const int k = 3;
7
8 typedef char DataType;
9
10 struct GTreeNode{
11 DataType data;
12 GTreeNode* t[k];
13 };
14
15 typedef struct GTreeNode* ElemType;
16
17 struct Queue{
18 ElemType *queue;
19 int front,rear;
20 int maxsize;
21 };
22
23 void InitQueue(Queue& Q)
24 {
25 Q.maxsize = 30;
26 Q.queue = new ElemType[Q.maxsize];
27 Q.front = Q.rear =0;
28 }
29
30 void EnQueue(Queue& Q,ElemType item)
31 {
32 if((Q.rear+1)%Q.maxsize == Q.front){
33 int k = sizeof(ElemType);
34 Q.queue = (ElemType*)realloc(Q.queue,2*Q.maxsize*k);
35 if(Q.rear!=Q.maxsize-1){
36 for(int i =0;i<Q.rear;++i)
37 Q.queue[i+Q.maxsize]=Q.queue[i];
38 Q.rear+=Q.maxsize;
39 }
40 Q.maxsize = 2*Q.maxsize;
41 }
42 Q.rear = (Q.rear+1)%Q.maxsize;
43 Q.queue[Q.rear] = item;
44 }
45
46 ElemType OutQueue(Queue& Q)
47 {
48 if(Q.front == Q.rear){
49 cerr<<"queue empty,can't delete item!"<<endl;
50 exit(1);
51 }
52 Q.front = (Q.front+1)%Q.maxsize;
53 return Q.queue[Q.front];
54 }
55
56 bool EmptyQueue(Queue& Q)
57 {
58 return Q.front == Q.rear;
59 }
60
61 void InitGTree(GTreeNode *>)
62 {
63 GT = NULL;
64 }
65
66 void CreateGTree(GTreeNode *>,char* a)
67 {
68 const int ms = 20;
69 GTreeNode * s[ms];//stack
70 int d[ms]; //children to parents
71
72 int top = -1;
73 GT = NULL;
74 GTreeNode *p;
75 int i = 0;
76 while(a[i])
77 {
78 switch (a[i])
79 {
80 case ' '://nop for spaces;
81 break;
82 case '(':
83 top++;s[top]=p;d[top]=0;
84 break;
85 case ')':
86 top--;
87 break;
88 case ',':
89 d[top]++;
90 break;
91 default:
92 p = new GTreeNode;
93 p->data = a[i];
94 for(int i = 0;i<k;i++)p->t[i] = NULL;
95 if(GT == NULL)
96 GT = p;
97 else{
98 s[top]->t[d[top]]=p;
99 }
100 }
101 i++;
102 }
103 }
104
105 bool EmptyGTree(GTreeNode* GT)
106 {
107 return GT == NULL;
108 }
109
110 int depthgtree(GTreeNode* GT)
111 {
112 if(EmptyGTree(GT))
113 return 0;
114 else{
115 int max = 0;
116 for(int i = 0;i<=k;i++)
117 {
118 int d = depthgtree(GT->t[i]);
119 if(d>max) max = d;
120 }
121 return max+1;
122 }
123 }
124
125 bool FindGTree(GTreeNode *GT,DataType& x)
126 {
127 if(EmptyGTree(GT))
128 return 0;
129 else{
130 if (GT->data == x)
131 {
132 x = GT->data;
133 return true;
134 }
135 for(int i = 0;i<k;i++)
136 if(FindGTree(GT->t[i],x))return true;
137 return false;
138 }
139 }
140
141 //print the tree
142
143 void printgtree(GTreeNode * GT)
144 {
145
146 if(!EmptyGTree(GT))
147 {
148 cout<<GT->data<<' ';
149 int i;
150 for(i = 0;i<k;i++)
151 if(GT->t[i] != NULL) break;
152 if (i<k)
153 {
154 cout<<'(';
155 printgtree(GT->t[0]);
156 for(i = 1;i<k;i++){
157 cout << ',';
158 printgtree(GT->t[i]);
159 }
160 cout<<')';
161 }
162 }
163 }
164
165 // clear the tree
166
167 void cleargtree(GTreeNode *>)
168 {
169 if (!EmptyGTree(GT))
170 {
171 for(int i = 0;i<k;i++)
172 cleargtree(GT->t[i]);
173 delete GT;
174 GT = NULL;
175 }
176 }
177
178 void PreRoot(GTreeNode *GT)
179 {
180 if (!EmptyGTree(GT)){
181 cout<<GT->data<<' ';
182 for(int i =0;i<k;i++)
183 PreRoot(GT->t[i]);
184 }
185 }
186
187 void PostRoot(GTreeNode *GT)
188 {
189 if(!EmptyGTree(GT)){
190 for(int i=0;i<k;i++)
191 PostRoot(GT->t[i]);
192 cout<<GT->data<<' ';
193 }
194 }
195
196 void LayerOrder(GTreeNode* GT)
197 {
198 Queue q;
199 InitQueue(q);
200 GTreeNode* p;
201 if(!EmptyGTree(GT)){
202 EnQueue(q,GT);
203 }
204 while(!EmptyQueue(q)){
205 p = OutQueue(q);
206 cout<<p->data<<' ';
207 for(int i=0;i<k;++i)
208 if(p->t[i]!=NULL)
209 EnQueue(q,p->t[i]);
210
211 }//end of while
212 }
213
214 int main()
215 {
216 GTreeNode * gt;
217 InitGTree(gt);
218 char b[50];
219 cout << "请输入用广义表表示的字符串("<<k<<"叉树)"<<":" << endl;
220 cin.getline(b,sizeof(b));
221 CreateGTree(gt,b);
222 printgtree(gt);
223 cout<<endl;
224 cout << "先根:";PreRoot(gt);cout<<endl;
225 cout << "后根:";PostRoot(gt);cout<< endl;
226 cout << "按层:";LayerOrder(gt);cout<< endl;
227
228 char x;
229
230 cout<<"输入一个待查字符:";
231 cin>>x;
232 if(FindGTree(gt,x))
233 cout<<"Find "<<x<< " Success"<<endl;
234 else
235 cout<<"Find "<<x<< " Failure"<<endl;
236 cout<<"深度:";cout<<depthgtree(gt)<<endl;
237 cleargtree(gt);
238 return 0;
239 }
----------------------------
$ ./a.out
请输入用广义表表示的字符串(3叉树):
a(b(,e,f(,j)),c,d(g(k,,l),h,i))
a (b (,e ,f (,j ,)),c ,d (g (k ,,l ),h ,i ))
先根:a b e f j c d g k l h i
后根:e j f b c k l g h i d a
按层:a b c d e f g h i j k l
输入一个待查字符:e
Find e Success
深度:4