-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththreadTree.c
79 lines (62 loc) · 1.29 KB
/
threadTree.c
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
69
70
71
72
73
74
75
76
77
78
79
//
// main.c
// AM
//
// Created by 余彬 on 15/7/10.
// Copyright (c) 2015年 余彬. All rights reserved.
//
#include <stdio.h>
#include <stdlib.h>
typedef enum Type{
Thread,
Link,
}Type;
typedef struct Node{
char data;
struct Node *lChild,*rChild;
Type rTag;
Type lTag;
}Node,*pNode;
pNode Pre;
void BuildTree(pNode *pN){
char c;
pNode pNew;
scanf("%c",&c);
if(c==' '){
pN=NULL;
}else if(c!='\n'){
pNew=(pNode)malloc(sizeof(Node));
pNew->rChild=pNew->lChild=NULL;
pNew->lTag=pNew->rTag=Link;
pNew->data=c;
(*pN)=pNew;
BuildTree(&pNew->lChild);
BuildTree(&pNew->rChild);
}
}
void ThrTree(pNode *pN){
if((*pN)->lChild!=NULL && (*pN)->lTag==Link){
ThrTree(&((*pN)->lChild));
}
if(Pre->rChild==NULL){
Pre->rChild=*pN;
Pre->rTag=Thread;
}
if((*pN)->lChild==NULL){
(*pN)->lChild=Pre;
(*pN)->lTag=Thread;
}
Pre=(*pN);
if((*pN)->rChild!=NULL && (*pN)->rTag==Link){
ThrTree(&((*pN)->rChild));
}
}
int main(){
pNode pHead;
printf("Please enter the expression:\n");
BuildTree(&pHead);
Pre=pHead;
ThrTree(&pHead);
Pre->rChild=pHead;
return 1;
}