简体   繁体   English

使用 jGrasp 可视化递归

[英]Visualize recursion using jGrasp

the one that I like to see how it works graphically is this one我喜欢以图形方式查看它是如何工作的就是这个

 public boolean splitArray(int[] nums) {
    int index = 0;
    int sum1 = 0;
    int sum2 = 0;

    return recallArray(nums, index, sum1, sum2);

}

public boolean recallArray(int[] nums, int index, int sum1, int sum2){
    if(index >= nums.length){
        return sum1 == sum2;
    }
        int value = nums[index];
        return recallArray(nums,index + 1, sum1 + value,sum2) 
        || recallArray(nums,index + 1, sum1 ,sum2 + value);
}

I try to use Jgrasp , that have a good visual debugger , but it only show me how the variables changes through the recursion calls, it is possible to Jgrasp to do the tree of recursion or something similar?, how?我尝试使用 Jgrasp ,它有一个很好的可视化调试器,但它只向我展示了变量如何通过递归调用而变化,Jgrasp 可以做递归树或类似的事情吗?,如何?

You could do it yourself with a class (RACheck here) exploiting try-with-resources.您可以使用利用 try-with-resources 的类(此处为 RACheck)自行完成。

public boolean recallArray(int[] nums, int index, int sum1, int sum2) {
    try (new RACheck(nums, index, sum1, sum2)) {
        if (index >= nums.length) {
            return sum1 == sum2;
        }
        int value = nums[index];
        return recallArray(nums,index + 1, sum1 + value,sum2) 
            || recallArray(nums,index + 1, sum1 ,sum2 + value);
    }
}

class RACheck implements Autocloseable {
    static int recursionDepth;
    int myRecursionDepth;

    final int[] aliasToNums;
    final int[] copyOfNums;

    RAChec(int[] nums, int index, int sum1, int sum2) {
        myRecursionDepth = recursionDepth++;
        // copy initial values.
        this.aliasOfNums = nums;
        this.copyOfNums = Arrays.copyOf(nums, nums.length);
        // dump enter method
    }

    @Override
    public void close() {
        // dump leave method
        // highlight changes to nums
    }
}

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

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