-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathThreadedBinaryTree.java
60 lines (52 loc) · 1.5 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
package week_11.dqy;
import util.dqy.QyQueue;
import util.dqy.QyTree;
import java.util.Scanner;
public class ThreadedBinaryTree<T> {
class Node<T> {
T val;
Node<T> left;
Node<T> right;
Node<T> fa;
Node(T val) {
this.val = val;
}
}
Node root = new Node(null);
public void buildTree() {
Scanner src = new Scanner(System.in);
this.root.val = src.next();
QyQueue<Node> q = new QyQueue<Node>();
q.push(this.root);
//当前队列不为空时
while (!q.isEmpty()) {
//获取结点
Node cur = q.getFront();
q.pop();
//创建两个结点分别为左节点和右节点
Node ln = new Node(src.next());
Node rn = new Node(src.next());
//“#”代表空
//判断当前点是否为空,为空则连入结点中,并继续处理后续的点
if (!ln.val.equals("#")) {
cur.left = ln;
q.push(ln);
}
if (!rn.val.equals("#")) {
cur.right = rn;
q.push(rn);
}
}
}
void dfs(Node root, Node fa) {
if(root == null) return;
root.fa = fa;
dfs(root.left, root);
dfs(root.right, root);
}
public static void main(String[] args) {
ThreadedBinaryTree newTree = new ThreadedBinaryTree();
newTree.buildTree();
newTree.dfs(newTree.root, null);
}
}