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);
}
}
I have a working snippet to use for a regular tree comprising nodes. Now I just need to fiddle with it to work for 2-3-4 trees, which should be easier, since each path is the same distance, since it's balanced, right?
Methods I have at my disposal include getNextChild()
, split()
, and of course 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);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果它是平衡的,您应该能够向下递归树的一侧来确定高度。
If it's balanced, you should just be able to recurse down one side of the tree to determine the height.