当前位置: 首页 > 知识库问答 >
问题:

搜索一棵二叉树是否是另一棵二叉树的子树

蒙才
2023-03-14

我有一个很严重的问题,就是在一棵树中重复搜索子树。

我试过了,但是。。。

public static boolean containsTree (Node t1, Node t2)
{
    if (t2 == null)
        return true;
    // Empty tree is always a subtree.
    else
        return subTree(t1, t2);
}

public static boolean subTree (Node t1, Node t2)
{
    if (t1 == null)
        return false; // Big tree is over.
    if (t1.getName().equalsIgnoreCase(t2.getName()))
        return matchTree(t1, t2);
    return (subTree(t1.getChild(), t2) || subTree(t1.getBrother(), t2));
}

private static boolean matchTree (Node t1, Node t2)
{
    if (t1 == null && t2 == null)
        return true; // Both trees are empty.
    if (t1 == null || t2 == null)
        return false; // Big tree empty and subtree still not found.
    if (!t1.getName().equalsIgnoreCase(t2.getName()))
        return false;
    return (matchTree(t1.getChild(), t2.getChild()) && matchTree(
            t1.getBrother(), t2.getBrother()));
}

似乎没有正确的形式。containsTree函数在找到与另一个节点不同的节点时停止搜索。

下面是两棵树的例子。

          1                 
         / \                                    3
        /   \                                   /\
       /     \                                 /  \
      2      3                                7    8
      /\     /\
     /  \   /  \
     3   4 5    6
                 \
                  3
                  /\
                 /  \
                 7   8

在这种情况下,当函数比较右边的子树和左边的子树时,当find等于父节点但它有不同的子节点时,它会停止搜索。我需要函数不要停止搜索,而是抛出这一点,搜索所有其他子节点及其子树。

共有1个答案

汤玉宸
2023-03-14

假设我们班是这样的:

class Node{
    node left, right;
    int value;    

    boolean equals(Node n){
        // if one child is null we can skip
        if(this.left == null && this.left != null ||
           this.left != null && this.left == null ){
            return false;
        }
        if(this.right == null && this.right != null ||
           this.right != null && this.right == null ){
            return false;
        }
        // 
        return (this.value == n.value) && 
               ( this.left == null || (this.left.equals(n.left)) ) && 
// if this.left == null, n.left is null too and the || will skip the equals()
               ( this.right == null || (this.right.equals(n.right)) );
// same for right
    }
}

方法将递归检查一棵树是否与另一棵树相同,因此,让我们检查它是否是子树:

boolean isSubTree(Node other){
    return this.equals(other) || 
         (other.left != null && this.isSubTree(other.left)) || 
// if left is null, this cant be a subtree 
         (other.right != null && this.isSubTree(other.right));
// same for right
}

没有测试,但我认为它可以这样工作。我跳过了构造函数和其他函数。

(位重载返回语句: P)

你应该记住一些事情:树通常用于搜索值,并且大部分被实现为二叉树:led-child

显然,你应该确保你的树不绕圈子

 类似资料:
  • 本文向大家介绍C语言判定一棵二叉树是否为二叉搜索树的方法分析,包括了C语言判定一棵二叉树是否为二叉搜索树的方法分析的使用技巧和注意事项,需要的朋友参考一下 本文实例讲述了C语言判定一棵二叉树是否为二叉搜索树的方法。分享给大家供大家参考,具体如下: 问题 给定一棵二叉树,判定该二叉树是否是二叉搜索树(Binary Search Tree)? 解法1:暴力搜索 首先说明一下二叉树和二叉搜索树的区别。二

  • 我写了一个函数,如果给定的二叉树是二叉搜索树,则返回true,否则返回false。 我的功能对吗?

  • 我正试图解决这个问题,但我遇到了一些麻烦: 在二进制搜索树(BST)中: 节点左子树中每个节点的数据值小于该节点的数据值。 节点右侧子树中每个节点的数据值大于该节点的数据值。 如您所见,节点(4)位于节点(3)的左侧子树中,尽管4大于3,因此方法应该返回。但是,我的代码返回。 我怎么能控制这个案子?如何检查左/右子树中的所有值都低于/大于根(不仅是直接子树)?

  • 编写一个Lisp程序来检查一个二叉树是否是二叉搜索树。 我正在尝试编写一个二进制递归方法,但我是一个初学者,我不知道从这里去哪里。

  • 树的特征和定义 树(Tree)是元素的集合。我们先以比较直观的方式介绍树。下面的数据结构是一个树: 树有多个节点(node),用以储存元素。某些节点之间存在一定的关系,用连线表示,连线称为边(edge)。边的上端节点称为父节点,下端称为子节点。树像是一个不断分叉的树根。 每个节点可以有多个子节点(children),而该节点是相应子节点的父节点(parent)。比如说,3,5是6的子节点,6是3,

  • 堆属性说: 如果A是B的父节点,则节点A的键相对于节点B的键进行排序,并在堆中应用相同的排序。要么父节点的键总是大于或等于子节点的键,最高键在根节点(这种堆称为最大堆),要么父节点的键小于或等于子节点的键,最低键在根节点(最小堆)。 但是为什么在这个wiki中,二进制堆必须是一个完整的二叉树呢?在我的印象中,堆属性并不意味着这一点。