您正在寻找的是后继算法。
这是如何定义的:
-
第一条规则:树中的第一个节点是树中最左边的节点。
-
下一条规则:一个节点的后继是:
-
Next-R 规则:如果有右子树,则在右子树中最左边的节点。
-
Next-U 规则:否则,向上遍历树
- 如果你右转(即这个节点是一个左孩子),那么那个父节点就是后继节点
- 如果您左转(即此节点是右子节点),请继续向上。
- 上不去,就没有继任者
如您所见,要使其工作,您需要一个父节点指针。
示例:
-
第一条规则:树中的第一个节点是树中最左边的节点:
(1)
-
Next-U 规则:由于
(1) 没有右子树,所以我们上到(3)。这是右转,接下来是(3)。
-
Next-R 规则:由于
(3) 有一个右子树,因此该子树中最左边的节点是下一个:(4)。
-
Next-U 规则:由于
(4) 没有右子树,所以我们上到(6)。这是右转,所以接下来是(6)。
-
Next-R 规则:由于
(6) 有一个右子树,该子树中最左边的节点就是下一个:(7)。
-
Next-U 规则:由于
(7) 没有右子树,所以我们上到(6)。这是左转,所以我们继续上(3)。这是左转,所以我们继续上(8)。这是右转,所以接下来是(8)。
-
Next-R 规则:由于
(8) 有一个右子树,该子树中最左边的节点就是下一个:(10)。
-
Next-R 规则:由于
(10) 有一个右子树,该子树中最左边的节点就是下一个:(13)。
-
Next-U 规则:由于
(13) 没有右子树,所以我们上到(14)。这是右转,所以接下来是(14)。
-
Next-U 规则:由于
(14) 没有右子树,所以我们上到(10)。这是左转,所以我们继续上(8)。这是一个左转,所以我们想继续往上走,但是由于(8) 没有父级,所以我们已经走到了尽头。 (14) 没有继任者。
伪代码
Node getLeftMost(Node n)
WHILE (n.leftChild != NULL)
n = n.leftChild
RETURN n
Node getFirst(Tree t)
IF (t.root == NULL) RETURN NULL
ELSE
RETURN getLeftMost(t.root);
Node getNext(Node n)
IF (n.rightChild != NULL)
RETURN getLeftMost(n.rightChild)
ELSE
WHILE (n.parent != NULL AND n == n.parent.rightChild)
n = n.parent;
RETURN n.parent;
PROCEDURE iterateOver(Tree t)
Node n = getFirst(t);
WHILE n != NULL
visit(n)
n = getNext(n)
Java 代码
这是上述算法的简单实现:
public class SuccessorIteration {
static class Node {
final Node left;
final Node right;
final int key;
Node parent;
Node(int key, Node left, Node right) {
this.key = key;
this.left = left;
this.right = right;
if (left != null) left.parent = this;
if (right != null) right.parent = this;
}
Node getLeftMost() {
Node n = this;
while (n.left != null) {
n = n.left;
}
return n;
}
Node getNext() {
if (right != null) {
return right.getLeftMost();
} else {
Node n = this;
while (n.parent != null && n == n.parent.right) {
n = n.parent;
}
return n.parent;
}
}
}
}
然后你可以有一个这样的测试工具:
static Node C(int key, Node left, Node right) {
return new Node(key, left, right);
}
static Node X(int key) { return C(key, null, null); }
static Node L(int key, Node left) { return C(key, left, null); }
static Node R(int key, Node right) { return C(key, null, right); }
public static void main(String[] args) {
Node n =
C(8,
C(3,
X(1),
C(6,
X(4),
X(7)
)
),
R(10,
L(14,
X(13)
)
)
);
Node current = n.getLeftMost();
while (current != null) {
System.out.print(current.key + " ");
current = current.getNext();
}
}
打印出来:
1 3 4 6 7 8 10 13 14
另见