等于比较两棵树的方法

时间:2011-03-21 20:46:49

标签: java recursion tree equals nodes

只是另一个小问题。我还必须为我的IntTree类创建一个equals方法,它循环遍历两个树并比较节点。如果树中的所有值都相等,则返回true,否则返回false。这是我到目前为止的代码:

private boolean equals(IntTreeNode node1, IntTreeNode node2){
    if ((node1 != null) || (node2 != null)){
        if (node1.equals(node2)){
            equals(node1.left, node2.left);
            equals(node1.right, node2.right);
            return node1.equals(node2);
        }
    }
    return false;
}

当我在我的Driver程序中调用此方法来比较两个完全相同的树(blah1.equals(blah2))时,我得到了错误。但是,当我打电话给blah1.equals(blah1)时,我得到了...... 我不确定我的退货声明是否正确

3 个答案:

答案 0 :(得分:4)

为什么在不处理结果的情况下做两个等于?

如果只是

,我会改变内在的
return equals(node1.left, node2.left) && equals(node1.right, node2.right); 

答案 1 :(得分:2)

我猜测你没有覆盖IntTreeNode的equals方法或者没有正确完成它。

我会这样做:

public boolean equals(Node other) {
    if (other == null ) return false;

    boolean valuesEqual = this.value == other.value;
    boolean leftEquals = this.left == null ? other.left == null : this.left.equals(other.left);
    boolean rightEquals = this.right == null ? other.right == null : this.right.equals(other.right);

    return valuesEqual && leftEquals && rightEquals;
}

然后比较两棵树,你可以拨打rootNode.equals(otherRootNode)

答案 2 :(得分:0)

0000003d webapp        E com.ibm.ws.webcontainer.webapp.WebApp logServletError SRVE0293E: [Servlet Error]-[dispatcher]: com.ibm.ws.webcontainer.webapp.WebAppErrorReport: 
           at com.ibm.ws.webcontainer.webapp.WebAppDispatcherContext.sendError(WebAppDispatcherContext.java:637)
           at com.ibm.ws.webcontainer.srt.SRTServletResponse.sendError(SRTServletResponse.java:1180)
           at javax.servlet.http.HttpServletResponseWrapper.sendError(HttpServletResponseWrapper.java:131)
           at gov.pa.dot.entss.web.controller.BaseController.handleUncaughtException(BaseController.java:85)
           at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
           at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:48)
           at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:37)
           at java.lang.reflect.Method.invoke(Method.java:600)
           at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerExceptionResolver.doInvokeMethod(AnnotationMethodHandlerExceptionResolver.java:340)
           at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerExceptionResolver.doResolveException(AnnotationMethodHandlerExceptionResolver.java:132)
           at org.springframework.web.servlet.handler.AbstractHandlerExceptionResolver.resolveException(AbstractHandlerExceptionResolver.java:136)
           at org.springframework.web.servlet.DispatcherServlet.processHandlerException(DispatcherServlet.java:987)
           at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:811)
           at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:719)
           at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:669)
           at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:574)
           at javax.servlet.http.HttpServlet.service(HttpServlet.java:718)
           at javax.servlet.http.HttpServlet.service(HttpServlet.java:831)
           at com.ibm.ws.webcontainer.servlet.ServletWrapper.service(ServletWrapper.java:1655)
           at com.ibm.ws.webcontainer.servlet.ServletWrapper.service(ServletWrapper.java:1595)
           at com.ibm.ws.webcontainer.filter.WebAppFilterChain.doFilter(WebAppFilterChain.java:131)
           at org.displaytag.filter.ResponseOverrideFilter.doFilter(ResponseOverrideFilter.java:125)
           at com.ibm.ws.webcontainer.filter.FilterInstanceWrapper.doFilter(FilterInstanceWrapper.java:188)
           at com.ibm.ws.webcontainer.filter.WebAppFilterChain.doFilter(WebAppFilterChain.java:116)
           at gov.pa.dot.entss.web.filter.ENTSSHttpFilter.doFilter(ENTSSHttpFilter.java:67)
           at com.ibm.ws.webcontainer.filter.FilterInstanceWrapper.doFilter(FilterInstanceWrapper.java:188)
           at com.ibm.ws.webcontainer.filter.WebAppFilterChain.doFilter(WebAppFilterChain.java:116)
           at com.ibm.ws.webcontainer.filter.WebAppFilterChain._doFilter(WebAppFilterChain.java:77)
           at com.ibm.ws.webcontainer.filter.WebAppFilterManager.doFilter(WebAppFilterManager.java:908)
           at com.ibm.ws.webcontainer.servlet.ServletWrapper.handleRequest(ServletWrapper.java:932)
           at com.ibm.ws.webcontainer.servlet.ServletWrapper.handleRequest(ServletWrapper.java:500)
           at com.ibm.ws.webcontainer.servlet.ServletWrapperImpl.handleRequest(ServletWrapperImpl.java:178)
           at com.ibm.ws.webcontainer.servlet.CacheServletWrapper.handleRequest(CacheServletWrapper.java:91)
           at com.ibm.ws.webcontainer.WebContainer.handleRequest(WebContainer.java:864)
           at com.ibm.ws.webcontainer.WSWebContainer.handleRequest(WSWebContainer.java:1583)
           at com.ibm.ws.webcontainer.channel.WCChannelLink.ready(WCChannelLink.java:186)
           at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.handleDiscrimination(HttpInboundLink.java:455)
           at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.handleNewInformation(HttpInboundLink.java:384)
           at com.ibm.ws.http.channel.inbound.impl.HttpICLReadCallback.complete(HttpICLReadCallback.java:83)
           at com.ibm.ws.tcp.channel.impl.AioReadCompletionListener.futureCompleted(AioReadCompletionListener.java:165)
           at com.ibm.io.async.AbstractAsyncFuture.invokeCallback(AbstractAsyncFuture.java:217)
           at com.ibm.io.async.AsyncChannelFuture.fireCompletionActions(AsyncChannelFuture.java:161)
           at com.ibm.io.async.AsyncFuture.completed(AsyncFuture.java:138)
           at com.ibm.io.async.ResultHandler.complete(ResultHandler.java:204)
           at com.ibm.io.async.ResultHandler.runEventProcessingLoop(ResultHandler.java:775)
           at com.ibm.io.async.ResultHandler$2.run(ResultHandler.java:905)
           at com.ibm.ws.util.ThreadPool$Worker.run(ThreadPool.java:1550)