树结构的复制构造函数偶尔会删除叶子

时间:2013-01-11 01:57:00

标签: java tree copy copy-constructor

我有一个树状的结构,我的拷贝构造函数似乎偶尔会掉落一些“叶子”。

基本结构:

public class Arrow {
   ArrayList<Arrow> subArrows;
   Interval start;
   Interval end;
}

复制构造函数:

public Arrow(Arrow other) {
    this.start = new Interval(other.start);
    this.end = new Interval(other.end);
    if (other.subArrows != null) {
        this.subArrows = new ArrayList<Arrow>();
        for (Arrow sub : other.subArrows) {
            this.subArrows.add(new Arrow(sub));
        }
    } else {
        this.subArrows = new ArrayList<Arrow>();
    }
}

我预计这基本上会对我的树结构进行深层复制。相反,我偶尔会发现我的一个subArrows数组是空的。我没有注意到一种模式,除了它们往往是在我树的最低“水平”。

有什么想法吗?我有一段时间没用过java。

编辑:有几个人要求提供更多代码,所以这里有触及subArrows的所有地方。这是一个非常大的算法/数据结构,所以发布所有这些都是不合理的。

递归获取所有subArrows并返回一组它们。

Set<Arrow> allSubArrows(Arrow arrow) {
    Set<Arrow> arrowSet = new HashSet<Arrow>();
    if (arrow.subArrows != null && arrow.subArrows.size() > 0) {
        for (Arrow sub : arrow.subArrows) {
            arrowSet.addAll(allSubArrows(sub));
        }
        return arrowSet;
    } else {
        arrowSet.add(arrow);
        return arrowSet;
    }
}

Mathy背后的原因,但在底部修改了subArrows:

void enforceMonotonicity(Arrow arrow) {
    boolean changed = false;
    if (arrow.end != null && arrow.start != null) {
        if (arrow.start.isParallelTo(arrow.end)) {
            //either left to right or bottom to top
            if (arrow.start.isVertical()) {
                //left interval pointing to right interval
                if (arrow.start.startGraph.y > arrow.end.startGraph.y) {
                    arrow.end.startGraph.y = arrow.start.startGraph.y;
                    changed = true;
                }
                if (arrow.end.endGraph.y < arrow.start.endGraph.y) {
                    arrow.start.endGraph.y = arrow.end.endGraph.y;
                    changed = true;
                }
            } else {
                //bottom interval pointing to top interval
                if (arrow.start.startGraph.x > arrow.end.startGraph.x) {
                    arrow.end.startGraph.x = arrow.start.startGraph.x;
                    changed = true;
                }
                if (arrow.end.endGraph.x < arrow.start.endGraph.x) {
                    arrow.start.endGraph.x = arrow.end.endGraph.x;
                    changed = true;
                }
            }
        }
    }
    if (changed) {
        //check to make sure SOMETHING is still reachable, if not arrow = null
        if (arrow.start.isVertical()) {
            if (arrow.start.startGraph.y >= arrow.start.endGraph.y ||
                    arrow.end.startGraph.y >= arrow.end.endGraph.y) {
                arrow = null;
            }
        } else {
            if (arrow.start.startGraph.x >= arrow.start.endGraph.x ||
                    arrow.end.startGraph.x >= arrow.end.endGraph.x) {
                arrow = null;
            }
        }
        //if we changed the outer arrows, we need to recursively change the subarrows
        if (arrow != null && arrow.subArrows != null && arrow.subArrows.size() > 0) {
            for (Arrow sub : arrow.subArrows) {
                enforceMonotonicity(sub);
            }
        }
    }
}

合并算法的一部分:

HashSet<Arrow> mergeCells(Set<Arrow> first, Set<Arrow> second) {
    HashSet<Arrow> mergedCell = new HashSet<Arrow>();
    //loop through arrows in adjacent cells and find the ones that connect
    for (Arrow topArrow : first) {
        for (Arrow bottomArrow : second) {
            if(topArrow.start.intersects(bottomArrow.end)) {
                //merge arrows
                Interval middle = topArrow.start.intersection(bottomArrow.end);
                Arrow newArrow = new Arrow();
                if (middle != null) {
                    //if they connect, we copy the two arrows, modify their connection,
                    //create a new arrow with the constituents as subarrows, and add that to the mergedcell
                    //after the mergedcell is created, we can delete the base arrows
                    Arrow topCopy = new Arrow(topArrow);
                    topCopy.start = middle;
                    Arrow bottomCopy = new Arrow(bottomArrow);
                    bottomCopy.end = middle;

                    newArrow.subArrows.add(topCopy);
                    newArrow.subArrows.add(bottomCopy);
                    newArrow.start = bottomCopy.start;
                    newArrow.end = topCopy.end;

                    //if end and middle are parallel, we need to project monotonicity
                    //monotonicity is already enforced within a single cell
                    //and enforcemonotonicity knows whether or not start and end are parallel
                    enforceMonotonicity(newArrow);
                }
                //enforceMonotonicity could null out the new arrow
                if (newArrow != null && !newArrow.isNull()) {
                    mergedCell.add(newArrow);
                }
            }
        }
    }

    //keep the originals in case they can connect later on
    //(hashset doesn't allow duplicates, so no need to worry here)
    mergedCell.addAll(first);
    mergedCell.addAll(second);

    return mergedCell;
}

1 个答案:

答案 0 :(得分:0)

(other.subArrows == null)时,您的代码仍然会this.subArrows创建new ArrayList<Arrow>()。这不是一个真正的克隆(一个是null,另一个是NOT)。如果你想确保subArrow总是非空(这有助于在不检查null的情况下添加subArrow),你可以在字段定义中实例化它。

如果仍然无效,您可能需要显示更多代码,因为我们不知道如何在箭头中添加subArrows。此外,如果您的代码处于多线程环境下,并且该bug很难重现,则可能是同步问题。请注意,ArrayList不是线程安全的。请尝试VectorCollections.synchronizedList,或者正确同步关键块。同样,我们没有您的相关代码。

顺便说一句,声明List<Arrow> subArrows优于ArrayList<Arrow> subArrows

相关问题