-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathI.cpp
More file actions
68 lines (67 loc) · 1.24 KB
/
I.cpp
File metadata and controls
68 lines (67 loc) · 1.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include<bits/stdc++.h>
using namespace std;
string s;
int i = -1;
struct ThrTree{
char val;
ThrTree* lchild;
ThrTree* rchild;
int ltag,rtag;
};
ThrTree* pre = NULL;
ThrTree* createBiTree(){
ThrTree* t = (ThrTree*)malloc(sizeof(ThrTree));
i++;
if(s[i] == '#' || i >= s.length()) return NULL;
else {
t->val = s[i];
t->ltag = 0;
t->rtag = 0;
t->lchild = createBiTree();
t->rchild = createBiTree();
return t;
}
}
void createThrTree(ThrTree* T){
if(T){
createThrTree(T->lchild);
if(T->lchild == NULL){
T->ltag = 1;
T->lchild = pre;
}
if(pre &&!(pre->rchild)){
pre->rchild = T;
pre->rtag = 1;
}
pre = T;
createThrTree(T->rchild);
}
}
ThrTree* nextNode(ThrTree* p){
if(p->rtag == 0){
p = p->rchild;
while(p->ltag == 0) p = p->lchild;
return p;
} else return p->rchild;
}
void searchThrTree(ThrTree* T){
ThrTree* t = T;
int n = 0;
for(t; t != NULL; t = nextNode(t)){
cout<<t->val;
if(t->ltag + t->rtag == 1) n++;
}
cout<<endl;
cout<<n+1<<endl;
}
int main(){
cin>>s;
ThrTree* T = createBiTree();
createThrTree(T);
pre->rchild = NULL;
pre->rtag = 1;
ThrTree* p = T;
while(p->ltag == 0) p = p->lchild;
searchThrTree(p);
return 0;
}