-
Notifications
You must be signed in to change notification settings - Fork 0
/
ThreadedBinaryTree.java
97 lines (85 loc) · 2.13 KB
/
ThreadedBinaryTree.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
package week_11.wangwei;
/**
* 线索二叉树
* @author yohoyes
*/
public class ThreadedBinaryTree {
public static void main(String[] args) {
String str = "#123#567";
char[] chars = str.toCharArray();
//建立根节点
Node<Character> root = new Node<Character>(chars[1]);
//建树
buildTree(root,chars,1);
search(null,root);
}
private static Node search(Node root, Node node){
if(node==null){
return root;
}
node.setPre(root);
root = search(node, node.getLeft());
return search(root, node.getRight());
}
/**
* 建立树
* arr[0]空着
* 根目录在arr[1]
* @param arr
*/
public static void buildTree(Node root, char[] arr, int i){
//只需要遍历到树的一半的位置就可以了
if(i>arr.length/2){return;}
//判断有无左子节点
if(2*i<arr.length){
//判断左子节点是不是空
if(arr[i*2]!='#') {
Node<Character> left = new Node<Character>(arr[i * 2]);
root.setLeft(left);
//建立左子树
buildTree(left, arr, i * 2);
}
}
//判断有无右子节点
if(2*i+1<arr.length){
//判断右子节点是不是空
if(arr[i*2+1]!='#') {
Node<Character> right = new Node<Character>(arr[i * 2 + 1]);
root.setRight(right);
//建立右子树
buildTree(right, arr, i * 2 + 1);
}
}
}
}
/**
* 节点类
* @param <T>
*/
class Node<T>{
Node<T> pre;
Node<T> left;
Node<T> right;
T value;
public Node(T value) {
this.value = value;
}
public Node getPre() {
return pre;
}
public void setPre(Node pre) {
this.pre = pre;
}
public void setLeft(Node left) {
this.left = left;
}
public void setRight(Node right) {
this.right = right;
}
public Node getLeft() {
return left;
}
public Node getRight() {
return right;
}
}