-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRandomChange.java
54 lines (47 loc) · 1.4 KB
/
RandomChange.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
package week_11.wangwei;
import util.wangwei.TreeNode;
import week_8.wangwei.BuildTree;
import week_8.wangwei.SearchTree;
/**
* 让命运决定谁是爸爸
* 产生一个0-2的随机数
* 0: 停止
* 1: 该节点的值跟左子节点交换
* 2: 该节点的值跟右子节点交换
* 直到0或叶子节点
* 这是一棵完全二叉树
* @author yohoyes
*/
public class RandomChange {
public static void main(String[] args) {
TreeNode<Integer> root = BuildTree.build(15);
//打印出来看看
SearchTree.bfs(root);
randomChange(root);
//打印出来看看
SearchTree.bfs(root);
}
/**
* 产生0-2的随机数
* @return
*/
private static int createRandom(){
return (int) (Math.random() * 3);
}
private static void randomChange(TreeNode<Integer> root){
int random = createRandom();
if(random==0||(root.getLeft()==null&&root.getRight()==null)){
return;
}else if(random==1){
int tmp = root.getLeft().getValue();
root.getLeft().setValue(root.getValue());
root.setValue(tmp);
randomChange(root.getLeft());
}else if(random==2){
int tmp = root.getRight().getValue();
root.getRight().setValue(root.getValue());
root.setValue(tmp);
randomChange(root.getRight());
}
}
}