- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在开发一个使用二叉搜索树的项目。该项目要求我创建一个名为 isBalanced 的方法,用于检查二叉搜索树是否在容差范围内平衡。我能够弄清楚如何做到这一点,但速度非常慢。我想知道是否有人有任何技巧可以提高效率。顺便说一句,如果子项为 null,.getLeftChild() 和 .getRightChild() 必须返回 IllegalStateExceptions。我个人更希望他们返回 null。
这是我的代码片段:
@Override
public <T> int getDepth(BinaryTreeNode<T> root) {
return recDepth(root,0);
}
public <T> int recDepth(BinaryTreeNode<T> root,int depth) {
int leftTree,rightTree;
if(!root.hasLeftChild()&&!root.hasRightChild()) return depth;
if(!root.hasLeftChild()) leftTree = 0;
else leftTree = recDepth(root.getLeftChild(),depth+1);
if(!root.hasRightChild()) rightTree = 0;
else rightTree = recDepth(root.getRightChild(),depth+1);
if(rightTree>leftTree) return rightTree;
else return leftTree;
}
@Override
public <T> boolean isBalanced(BinaryTreeNode<T> root, int tolerance) {
if(tolerance<0) throw new IllegalArgumentException("Can't have negative tolerance");
if(root==null) throw new NullPointerException();
return recBalanced(root,tolerance);
}
public <T> boolean recBalanced(BinaryTreeNode<T> root, int tolerance){
try{
if(Math.abs(getDepth(root.getLeftChild())-getDepth(root.getLeftChild()))<=tolerance){
return recBalanced(root.getLeftChild(),tolerance)&&recBalanced(root.getRightChild(),tolerance);
}else return false;
} catch (IllegalStateException e){
if(root.hasLeftChild()&&getDepth(root.getLeftChild())>tolerance-1) return false;
else if(root.hasRightChild()&&getDepth(root.getRightChild())>tolerance-1) return false;
else return true;
}
}
预先感谢您的帮助。
最佳答案
您的效率问题来自于这样的事实:您多次遍历相同的元素来计算子树的深度,然后计算其右子树和左子树的深度。
当你遇到这样的重叠问题,并且可以通过更小的问题来推断时,你的脑海中一定会响起一个铃声:动态规划。您可以计算 BinaryTreeNode<Integer>
,它将包含每个对应节点的深度(该树将具有与原始树相同的形状)。然后,您只需遍历该树一次即可执行计算,总时间复杂度为 O(n)
(但是使用了 O(n)
的内存)。
public BinaryTreeNode<Integer> computeDepthTree(BinaryTreeNode<T> bt) {
return computeDepth(bt,0);
}
public BinaryTreeNode<Integer> computeDepthTree(BinaryTreeNode<T> bt, int depth) {
BinaryTreeNode<Integer> result = new BinaryTreeNode<>(depth);
if (bt.getLeftNode() != null)
result.setLeftNode(computeDepthTree(bt.getLeftNode(),depth + 1));
if (bt.getRightNode() != null)
result.setRightNode(computeDepthTree(bt.getRightNode(),depth + 1));
return result;
}
计算出这棵树后,当遍历给定节点时,您将可以访问其 O(1)
中的深度。因此,同一父节点的子节点之间的深度比较将很便宜!
关于java - 低效二叉搜索树平衡算法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26834434/
我写了一个函数,应该用值替换两个定界符之间的代码,它返回(我将其应用到的字符串是 HTML 对象的 .outerHTML)。 这将类似于它在例如中的使用方式。 Vue.js 或 Angular。 看起
好的,我有一个 Django View ,如下所示: @render_to('home/main.html') def login(request): # also tried Client.
由于我创建的几乎所有项目都包含 ListView,因此我想到创建一个类,其中包含修改 ListView 的所有重要功能。它看起来像这样: 主窗体: ListViewFunctions LVF = ne
The default implementation on Stream creates a new single-byte array and then calls Read. While this
我当然不是 Drupal 专家,但我之前设计并构建了一些数据库,所以我对第 3 方团队正在处理的数据库结构感到困惑,我已经将 Sequel Pro 添加到其中虚拟内容。我认为如果使用 Drupal 的
我想生成一个随机的短十六进制字符串(比如 8 位或 16 位)。 有很多选择可以做到这一点,例如,从我的头顶开始: uuid.uuid4().hex[:8] md5().hexdigest()[:8]
我是一名优秀的程序员,十分优秀!