-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWeek11.java
254 lines (239 loc) · 8.18 KB
/
Week11.java
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
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
package week_11.wuyanzhen;
/**
* @author Florence
*/
import util.wuyanzhen.TreeNode;
import java.util.HashMap;
import java.util.Map;
/**
* @author Florence
*/
public class Week11 {
public static void main(String[] args) {
// ThreadTreeNode<Integer> a=new ThreadTreeNode<>();
// ThreadTreeNode<Integer> b=new ThreadTreeNode<>();
// ThreadTreeNode<Integer> c=new ThreadTreeNode<>();
// ThreadTreeNode<Integer> d=new ThreadTreeNode<>();
// ThreadTreeNode<Integer> e=new ThreadTreeNode<>();
// a.setLeft(b);
// a.setRight(c);
// b.setLeft(d);
// c.setRight(e);
// initThreadTree(a);
TreeNode<Integer> a = new TreeNode<>(1);
System.out.println(a.equals(a));
TreeNode<Integer> b = new TreeNode<>(2);
TreeNode<Integer> c = new TreeNode<>(3);
TreeNode<Integer> d = new TreeNode<>(4);
TreeNode<Integer> e = new TreeNode<>(5);
a.setLeft(b);
a.setRight(c);
b.setLeft(d);
c.setRight(e);
randomZeroOneTwoExchange(a);
System.out.println("*************************************");
}
/**
* 线索二叉树节点
* @param <T>
*/
static class ThreadTreeNode<T> {
private ThreadTreeNode<T> left=null;
private ThreadTreeNode<T> right=null;
private ThreadTreeNode<T> Thread=null;
public ThreadTreeNode<T> getLeft() {
return left;
}
public void setLeft(ThreadTreeNode<T> left) {
this.left = left;
}
public ThreadTreeNode<T> getRight() {
return right;
}
public void setRight(ThreadTreeNode<T> right) {
this.right = right;
}
public ThreadTreeNode<T> getThread() {
return Thread;
}
public void setThread(ThreadTreeNode<T> thread) {
Thread = thread;
}
}
public static<T> void initThreadTree(ThreadTreeNode<T> root){
if (root==null){
return;
}
ThreadTreeNode<T> left = root.getLeft();
//如果左节点不为空,则将左节点的线索指向当前节点
if (left !=null){
//设置线索
left.setThread(root);
//递归左子树
initThreadTree(root.getLeft());
}
ThreadTreeNode<T> right = root.getRight();
//同上
if (right !=null){
right.setThread(root);
initThreadTree(root.getRight());
}
}
/**
* 0:停止 1:跟左节点交换 2:跟右节点交换
* @param root 根节点
* @param <T>
*/
public static <T> void randomZeroOneTwoExchange(TreeNode<T> root){
if (root==null){
return;
}
int randomNum=getRandomNum(0,2);
//相当于前序遍历
int isContinue = visitNode(randomNum, root);
if (isContinue==0){
return;
}
randomZeroOneTwoExchange(root.getLeft());
randomZeroOneTwoExchange(root.getRight());
}
static int count=0;
private static <T> int visitNode(int randomNum, TreeNode<T> root) {
count++;
T data = root.getData();
if (randomNum==0){
System.out.println("step "+count+":stop");
return randomNum;
}
//跟左节点交换
else if (randomNum==1){
TreeNode<T> left = root.getLeft();
//如果左节点不为空,则继续进行,否则就结束
if (left!=null) {
root.setData(left.getData());
left.setData(data);
System.out.println("step " + count + ":exchange leftNodeAndRoot");
}
else {
System.out.println("step "+count+":stop");
return 0;
}
}
//跟右节点交换
else if (randomNum==2){
TreeNode<T> right = root.getRight();
if (right!=null) {
root.setData(right.getData());
right.setData(data);
System.out.println("step " + count + ":exchange rightNodeAndRoot");
}else {
System.out.println("step "+count+":stop");
return 0;
}
}
return 1;
}
public static void exchangeNearestLeafNode(TreeNode<Integer> root){
Map<TreeNode<Integer>,Boolean> isUse= new HashMap<>(20);
Map<TreeNode<Integer>,String> nodeReflectEncodeString = new HashMap<>(20);
resGetTheEncodingStrMap(nodeReflectEncodeString,root,"");
//先拿出一个叶子节点,然后另外的不是这个叶子节点本身的进行对比长度
for (Map.Entry<TreeNode<Integer>,String> mainTreeNodeStringEntry:nodeReflectEncodeString.entrySet()){
int minGap=Integer.MAX_VALUE;
TreeNode<Integer> minGapNode=null;
TreeNode<Integer> curNode = mainTreeNodeStringEntry.getKey();
for (Map.Entry<TreeNode<Integer>,String> otherTreeNodeStringEntry:nodeReflectEncodeString.entrySet()){
//如果跟当前访问的不是同个节点
if (!curNode.equals(otherTreeNodeStringEntry.getKey())){
//获取两个叶子节点间的距离
int gap=getTwoNodeGap(mainTreeNodeStringEntry.getValue(),otherTreeNodeStringEntry.getValue());
//如果比当前距离小(转换)
if (gap<minGap){
minGap=gap;
minGapNode=otherTreeNodeStringEntry.getKey();
}
}
}
//如果当前节点没被交换过
if (!isUse.get(curNode)){
exchangeTwoTreeNode(curNode,minGapNode);
isUse.put(curNode,true);
}
}
}
/**
* 递归获取编码字符串map,一个叶子节点对应的编码串
* @param root
* @param nowStr
*/
public static void resGetTheEncodingStrMap(Map<TreeNode<Integer>, String> map, TreeNode<Integer> root, String nowStr) {
if (root != null) {
nowStr += root.getData();
if (root.getRight() == null && root.getLeft() == null) {
map.put(root,nowStr);
}
resGetTheEncodingStrMap(map, root.getLeft(), nowStr);
resGetTheEncodingStrMap(map, root.getRight(), nowStr);
}
}
/**
* 计算两点的距离
* @param s1 编码完的字符串1
* @param s2 编码完的字符串2
* @return
*/
public static int getTwoNodeGap(String s1, String s2) {
int s1Length = s1.length();
int s2Length = s2.length();
if (s1Length>s2Length){
return getTwoNodeGap(s2,s1);
}
for (int i = 0; i< s1Length; i++){
if (s1.charAt(i)!=s2.charAt(i)){
return s1Length+s2Length-2*i+1;
}
}
if (s1Length == s2Length){
return 0;
}
else {
return s2Length-s1Length+1;
}
}
/**
* 交换两个叶子节点
* @param curNode
* @param minGapNode
*/
private static void exchangeTwoTreeNode(TreeNode<Integer> curNode, TreeNode<Integer> minGapNode) {
TreeNode<Integer> curNodeParent = curNode.getParent();
TreeNode<Integer> minGapNodeParent = minGapNode.getParent();
//设置当前主动交换节点的相应的叶子节点
if (curNodeParent.getLeft().equals(curNode)){
curNodeParent.setLeft(minGapNode);
}
else {
curNodeParent.setRight(minGapNode);
}
//设置当前主动交换节点的相应的叶子节点
if (minGapNodeParent.getLeft().equals(minGapNode)){
minGapNodeParent.setLeft(curNode);
}
else {
minGapNodeParent.setRight(curNode);
}
}
/**
* 获取随机数 [bottomBound,topBound]
*
* @param bottomBound 下界
* @param topBound 上界
* @return 获取到的随机数
*/
public static int getRandomNum(int bottomBound, int topBound) {
int c = topBound - bottomBound + 1;
double x = Math.random() * c;
int y = (int) x;
return y + bottomBound;
}
}