我有一個工作片斷,用於包含節點的常規樹。現在我只需要擺弄2-3-4的樹,這應該更容易,因爲每條路都是相同的距離,因爲它是平衡的,對嗎?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);
}
}
可以擺脫左右深度,並使用一條線獲得下一個孩子? – 2010-10-28 19:39:13
@John我認爲這打破了樹的概念 – Woot4Moo 2010-10-28 20:04:26
的確如此,但是關於找到並返回高度,我不知道採用哪條路徑?如果他們都返回相同的? – 2010-10-28 20:11:56