我需要編寫一個函數來查找符合特殊條件的前一個/下一個葉節點,這個節點位於單根樹中的任何節點。 (在父一階)java alogrithm:從樹中的任意節點查找符合特殊條件的前/後葉節點
該API將是這樣的:
Node findNextLeafNode(Node currentNode, Condition condition);
Node findPretLeafNode(Node currentNode, Condition condition);
其中currentNode
處於樹中的任何節點,並且Node
被定義爲:
interface Node{
/** @return the Node's parent or null if Node is root */
Node getParent();
/** @return true if node is root */
boolean isRoot();
/** @return non-null array of child nodes (zero length for leaf nodes) */
Node[] getChildren();
/** @return the number of child nodes. If node is leaf, value is 0 */
int getChildCount();
}
而Condition
接口定義了針對給定的Node
檢查約束的語義。
interface Condition{
/** @return true if provided node meets the condition */
boolean check(Node node);
}
我的問題:
是否有這樣一個共同的情景現有庫或算法?我對基於堆棧或遞歸算法開放。僞代碼,鏈接到開源庫,或者如果你想分享你自己的代碼,將不勝感激。
(如果沒有,我需要花時間去重新發明了相同的輪和在這裏後來粘貼共享。)
感謝。
-----------------------------寫一個方法getNext()........
// currentNode must be descendant of root
public static Node getNextNode(Node currentNode, Node root)
{
// 1. if is has child, next is its first child
if (currentNode.getChildSize() > 0) {
return currentNode.getChildren()[0];
}
// 2. if it has no child, check if its is the last child of his parent
else {
// if it is root and has no children, return null
if (currentNode == root) {
return null;
}
// else should have parent which is or under root;
Node parent = currentNode.getParent();
int index = getIndex(currentNode);
if (!isLastofParent(currentNode)) {
// ----a. if not last, next is his parent's next
return currentNode.getParent().getChildren()[index + 1];
}
else {
// ----b. if it is the last one, return its parent's next right if there is. while until root
Node tmp = parent;
while (tmp != root) {
int parentIndex = getIndex(tmp);
if (!isLastofParent(tmp)) {
return tmp.getParent().getChildren()[parentIndex + 1];
}
tmp = tmp.getParent();
}
}
}
return null;
}
private static boolean isLastofParent(Node node)
{
if (getIndex(node) == node.getParent().getChildSize() - 1) {
return true;
}
return false;
}
private static int getIndex(Node currentNode)
{
Node parent = currentNode.getParent();
for (int i = 0; i < parent.getChildSize(); i++) {
if (parent.getChildren()[i] == currentNode) {
return i;
}
}
//TODO: error condition handling, will not happen if tree not change
return -1;
}
------------------------全搜索更容易............
public static Node getNextFailNode(Node currentNode, Node root, Condition condition)
{
boolean foundCurrentNode = false;
Stack<Node> stack = new Stack<Node>();
stack.push(root);
while (!stack.isEmpty()) {
Node tmp = stack.pop();
System.out.println("-popup---------" +tmp+ " ");
if (foundCurrentNode && checkCondition(tmp, condition)) {
return tmp;
}
if (tmp == currentNode) {
foundCurrentNode = true;
}
if (tmp.getChildSize() > 0) {
for (int i = tmp.getChildSize() - 1; i >= 0; i--) {
stack.push(tmp.getChildren()[i]);
}
}
}
return null;
}
謝謝,對我來說太重了。 –