-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNode.java
More file actions
64 lines (54 loc) · 1.6 KB
/
Copy pathNode.java
File metadata and controls
64 lines (54 loc) · 1.6 KB
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
package cx.Tree.BinSort;
/**
* 二叉搜索树结点类
*/
public class Node {
int value;
Node left;
Node right;
public Node(int value) {
this.value = value;
}
//向子树添加结点
public void add(Node node) {
if (node == null) {
return;
}
if (node.value < this.value) {
if (this.left == null) this.left = node;
else this.left.add(node);
} else {
if (this.right == null) this.right = node;
else this.right.add(node);
}
}
//二叉排序树的中序遍历就是从小到大排序的结果
public void midShow(Node node) {
if (node == null) return;
midShow(node.left);
System.out.print(node.value + " ");
midShow(node.right);
}
//结点的查找
public Node search(int value) {
if (this.value == value) {
return this;
} else if (value < this.value) {
if (left == null) return null;
return left.search(value);
} else {
if (right == null) return null;
return right.search(value);
}
}
//搜索父结点
public Node searchParent(int value) {
if ((this.left != null && this.left.value == value) || (this.right != null && this.right.value == value))
return this;
else {
if (this.value > value && this.left != null) return this.left.searchParent(value);
else if (this.value < value && this.right != null) return this.right.searchParent(value);
}
return null;
}
}