-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNode.java
More file actions
106 lines (86 loc) · 2.66 KB
/
Copy pathNode.java
File metadata and controls
106 lines (86 loc) · 2.66 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
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
98
99
100
101
102
103
104
105
106
import java.util.*;
import java.util.concurrent.atomic.*;
public class Node {
// values for dummy nodes
public static final int DUMMY1 = Integer.MAX_VALUE;
public static final int DUMMY2 = Integer.MAX_VALUE - 1;
// mark/flag values
public static final int CLEAN = 0;
public static final int IFLAG = 1;
public static final int DFLAG = 2;
public static final int MARK = 3;
private int key;
public AtomicStampedReference<Info> state;
public AtomicReference<Node> left, right;
private boolean isLeaf;
public Node(int key, Node left, Node right) {
// constructor for an internal node
this.key = key;
this.left = new AtomicReference<Node>(left);
this.right = new AtomicReference<Node>(right);
state = new AtomicStampedReference<Info>(null,CLEAN);
isLeaf = false;
}
public Node(int key) {
// constructor for a leaf
this.key = key;
this.left = null;
this.right = null;
state = null;
isLeaf = true;
}
public boolean isLeaf() {
return isLeaf;
}
public int getKey() {
return key;
}
public boolean verify(int lower, int upper) {
/* this method recursively verifies upper/lower bounds on the keys */
if (key != Node.DUMMY1 && key != Node.DUMMY2) {
if (key < lower)
return false;
if (key >= upper)
return false;
}
if (!isLeaf) {
Node l = left.get();
Node r = right.get();
return l.verify(lower,key) && r.verify(key,upper);
}
return true;
}
public String prettyPrint() {
/* this method returns a string representation of the current subtree
internal nodes are enclosed in round brackets, leaves are enclosed in square brackets
e.g. ( 5 [ 3 ] ( 7 [ 5 ] [ 7 ] ) )
*/
if (isLeaf)
return "[ " + key + " ]";
else
return "( " + key + " " + left.get().prettyPrint() + " " + right.get().prettyPrint() + " )";
}
public TreeSet<Integer> getKeys() {
/* this method returns the set of keys of the current subtree */
TreeSet<Integer> result = new TreeSet<Integer>();
if (isLeaf) {
if (key != Node.DUMMY1 && key != Node.DUMMY2)
result.add(key);
}
else {
result.addAll(left.get().getKeys());
result.addAll(right.get().getKeys());
}
return result;
}
public String DOTFormat() {
/* this method returns a string representing the current subtree in .DOT format
(without the graph name and curly brackets)
*/
String kStr = (key < 0) ? ("m" + (-1 * key)) : ""+key;
if (isLeaf)
return "l" + kStr + "\nl" + kStr + " [label="+ key + "]\n";
else
return "i" + kStr + " -> " + left.get().DOTFormat() + "i" + kStr + " -> " + right.get().DOTFormat() + "i" + kStr + " [label="+ key + "]\n";
}
}