繁体   English   中英

在AVL树中找到后继者

[英]find successor in AVL tree

有一个带有节点的avl树,每个节点只有3个字段:

1.正确的孩子

2.左孩子

3.值(键)

请注意,它没有这样的字段“父母”或“父亲”。 我的问题是:如果我们想知道树中每个节点的后继者,并且不使用任何LinkedList或任何集合,那么这样做的方法是什么?

你可以做这样的事情

// 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);
}

然后调用printSuccessors(root, null)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM