1
using System;
2
3
namespace BiThrTree
4
{
5
/// <summary>
6
/// 定义结点类:
7
/// </summary>
8
class BTNode
9
{
10
//先序:先处理父结点,然后递归处理孩子
11
//中序:先处理左孩子,再处理根节结,然后递归处理右孩子
12
//后序:先递归处理所有孩子,再递归处理节点本点
13
public char data;
14
public int ltag, rtag;//0表示线索,1表示结点
15
public BTNode lchild, rchild;
16
}
17
class BiThrTree
18
{
19
/// <summary>
20
/// 建立一棵新二叉树:
21
/// </summary>
22
/// <param name="T"></param>
23
static public void CreateBiThrTree(ref BTNode T)
24
{
25
char ch;
26
ch = (char)Console.Read();
27
if (ch == '#')
28
{
29
T = null;
30
}
31
else
32
{
33
T = new BTNode();
34
T.data = ch;
35
CreateBiThrTree(ref T.lchild);
36
CreateBiThrTree(ref T.rchild);
37
}
38
}
39
/// <summary>
40
/// 线索化二叉树:
41
/// </summary>
42
/// <param name="T"></param>
43
static BTNode pre, H;
44
static public void Threading(ref BTNode T)
45
{
46
H = pre = new BTNode();
47
pre.rchild = pre.lchild = null;
48
pre.rtag = pre.ltag = 0;
49
Thread(ref T);
50
}
51
static public void Thread(ref BTNode T)
52
{
53
if (T != null)
54
{
55
if (T.lchild == null) { T.lchild = pre; T.ltag = 0; }
56
else { T.ltag = 1; }
57
if (T.rchild == null) { T.rtag = 0; }
58
else { T.rtag = 1; }
59
if (pre.rchild == null && pre.rtag == 0) pre.rchild = T;
60
pre = T;
61
if (T.ltag == 1) Thread(ref T.lchild);
62
if (T.rtag == 1) Thread(ref T.rchild);
63
}
64
}
65
/// <summary>
66
/// 先序输出:
67
/// </summary>
68
static public void PrePrint(BTNode T)
69
{
70
if (T != null)
71
{
72
Console.Write(T.data + "\t");
73
if (T.ltag == 1) PrePrint(T.lchild);
74
if (T.rtag == 1) PrePrint(T.rchild);
75
}
76
}
77
/// <summary>
78
/// 先序线索遍历输出:
79
/// </summary>
80
static public void PreThrPrint(BTNode T)
81
{
82
T = H.rchild;
83
//Console.WriteLine("H.rchild.date::"+H.rchild.data);
84
while (T != null)
85
{
86
Console.Write(T.data + "\t");
87
if (T.rtag == 0) T = T.rchild;
88
else
89
{
90
if (T.ltag == 1) T = T.lchild;
91
else
92
{
93
T = T.rchild;
94
}
95
}
96
}
97
}
98
/// <summary>
99
/// Deepth of a BiThrTree:
100
/// </summary>
101
static public int Deepth(BTNode T)
102
{
103
int a, b;
104
if (T != null)
105
{
106
if (T.ltag == 1) a = Deepth(T.lchild); else a = 0;
107
if (T.rtag == 1) b = Deepth(T.rchild); else b = 0;
108
return (1 + max(a, b));
109
}
110
else
111
{
112
return 0;
113
}
114
}
115
static public int max(params int[] w)
116
{
117
int max;
118
max = w[0];
119
for (int i = 0; i < w.Length; i++)
120
if (max < w[i]) max = w[i];
121
return max;
122
}
123
/// <summary>
124
/// 复制线索二叉树:
125
/// </summary>
126
static public void DulplicateBiThrTree(BTNode T1, ref BTNode T2)
127
{
128
if (T1 != null)
129
{
130
T2 = new BTNode();
131
T2.data = T1.data;
132
T2.ltag = T1.ltag; T2.rtag = T1.rtag;
133
if (T2.ltag == 1) DulplicateBiThrTree(T1.lchild, ref T2.lchild); else T2.lchild = T1.lchild;
134
if (T2.rtag == 1) DulplicateBiThrTree(T1.rchild, ref T2.rchild); else T2.rchild = T1.rchild;
135
}
136
}
137
138
static void Main()
139
{
140
BTNode mytree = null;
141
Console.WriteLine("Please input a tree(for example:abc##d##ed###):");
142
CreateBiThrTree(ref mytree);
143
Threading(ref mytree);
144
PrePrint(mytree);
145
Console.WriteLine("\n按先序输出:\n");
146
PreThrPrint(mytree);
147
Console.WriteLine("\n该树的深度为:{0}", Deepth(mytree));
148
BTNode mytree2 = null;
149
Console.WriteLine("调用复制函数得到的新树为:");
150
DulplicateBiThrTree(mytree, ref mytree2);
151
PrePrint(mytree2);
152
Console.ReadLine();
153
Console.ReadLine();
154
}
155
156
}
157
158
}

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

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158
