-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathBinaryNode.java
More file actions
60 lines (50 loc) · 1.36 KB
/
BinaryNode.java
File metadata and controls
60 lines (50 loc) · 1.36 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
// Brett Fazio, node in a binary tree. Two children.
class BinaryNode {
BinaryNode left, right;
int value;
public BinaryNode(int v) {
value = v;
left = null;
right = null;
}
public void addChild(BinaryNode cocuk) {
if (cocuk.value > value) {
if (right == null) {
right = cocuk;
}else {
right.addChild(cocuk);
}
}else {
if (left == null) {
left = cocuk;
}else {
left.addChild(cocuk);
}
}
}
public boolean transverse(BinaryNode cocuk) {
if (cocuk.left == null && this.left != null) {
return false;
}
if (cocuk.left != null && this.left == null) {
return false;
}
if (cocuk.right == null && this.right != null) {
return false;
}
if (cocuk.right != null && this.right == null) {
return false;
}
if (cocuk.left != null) {
if (!this.left.transverse(cocuk.left)) {
return false;
}
}
if (cocuk.right != null) {
if (!this.right.transverse(cocuk.right)) {
return false;
}
}
return true;
}
}