[英]find successor in AVL tree
there is avl tree with nodes, and each node has only 3 fields: 有一个带有节点的avl树,每个节点只有3个字段:
1.right child 1.正确的孩子
2.left child 2.左孩子
3.value(key) 3.值(键)
notice that it doesnt have such field "parrent" or "father". 请注意,它没有这样的字段“父母”或“父亲”。 my question is: in case we want to know the successor of EVERY node in the tree, and without use any LinkedList or any collection, what is the method to do so? 我的问题是:如果我们想知道树中每个节点的后继者,并且不使用任何LinkedList或任何集合,那么这样做的方法是什么?
You could do something like this 你可以做这样的事情
// Returns the last node in the inorder traversal of tree,
// or prev if tree is null.
Node printSuccessors(Node tree, Node prev) {
if (tree == null) {
return prev;
}
Node lastLeft = printSuccessors(tree.leftChild(), prev);
if (lastLeft != null) {
System.out.println("The successor of " + lastLeft.key()
+ " is " + tree.key());
}
return printSuccessors(tree.rightChild(), tree);
}
and then call printSuccessors(root, null)
. 然后调用printSuccessors(root, null)
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.