【发布时间】:2011-05-02 02:03:33
【问题描述】:
我有一个有效的 sn-p 可用于包含节点的常规树。现在我只需要摆弄它来处理 2-3-4 棵树,这应该更容易,因为每条路径的距离相同,因为它是平衡的,对吧?
我可以使用的方法包括getNextChild()、split(),当然还有insert()。
public int height() {
return (height(root));
}
private int height(TNode localRoot) {
if(localRoot == null) {
return 0;
}
else {
//Find each sides depth
int lDepth = height(localRoot.leftChild);
int rDepth = height(localRoot.rightChild);
//Use the larger of the two
return (Math.max(lDepth, rDepth) + 1);
}
}
【问题讨论】:
-
可以摆脱左右深度并使用一行获取下一个孩子吗?
-
@John 我认为这打破了树的概念
-
是的,但是关于仅查找和返回高度,我走哪条路并不重要?如果他们都返回相同的?
-
@John 我相信这是正确的,当然除非你不正确地实现了 2-4 树:)
-
@Woot4Moo:B 树中的外部节点根据定义始终具有相同的高度。