简体   繁体   English

需要帮助编写一个 function 来检查两个 BST 是否具有相同的结构

[英]Need help writing a function to check if two BSTs have the same structure

I'm trying write a function to check if two trees have the same structure, regardless of values, and the code that I wrote so far doesn't work.我正在尝试编写 function 来检查两棵树是否具有相同的结构,无论值如何,并且我到目前为止编写的代码不起作用。 Any help or pointers (pun intended) would be greatly appreciated.任何帮助或指示(双关语)将不胜感激。

template <class T>
bool BST<T>::similarStructure(BST Tree2) {
    if (isEmpty()) {
        cout << "tree is empty\n";
        return;
    }

    StackArray<node<T>> s1, s2; // s1 for traversal,, s2 for printing

    s1.Push(root);
    s2.Push(tree2.root);
    while (!s1.IsEmpty() &&!s2.isempty()) {
        node<T> n = s1.Pop();
        node<T> n1=s2.Pop();
        if (n->right != NULL && n1->right!=NULL){
            s1.Push(n->right);
            s2.Push(n1->right); }
         else{
           return false;}

        if (n->left != NULL && n->left!=NULL){
            s1.Push(n->left); 
            s2.Push(n1->left);}
        else{
            return false;}

    }
    return true;

I must be in a generous mood.我一定心情愉快。

A simple in-order traversal will solve the problem.一个简单的中序遍历将解决这个问题。 Just check at each node that the left field is null or not null in both trees, same test for the right field, then recurse.只需在两个树中检查每个节点的左侧字段是 null 还是不是 null,对右侧字段进行相同的测试,然后递归。

This template function does the work, you need to call it with the root node pointer of your two trees.这个模板 function 可以完成工作,您需要使用两棵树的根节点指针调用它。

template <class T>
bool similarStructure(node<T>* x, node<T>* y) const
{
    if (x == y)
        return true;
    if (x == nullptr || y == nullptr)
        return false;
    return similarStructure(x->left, y->left) && similarStructure(x->right, y->right);
}

Untested code.未经测试的代码。

Here's a sketch of what you can try out:这是您可以尝试的草图:

bool similarStructure(BST t1, BST t2)
{
  return !(t1.root ^ t2.root) 
         && t1.root 
         && similarStructure(t1.root->left, t2.root->left)
         && similarStructure(t1.root->right, t2.root->right)
}

Your function is invalid because at least in this part of the function你的 function 是无效的,因为至少在 function 这部分

template <class T>
bool BST<T>::similarStructure( BST Tree2 ) 
{
    if (isEmpty()) {
        cout << "tree is empty\n";
        return;
    }
    //...

it returns nothing though the function has the return type bool.尽管 function 的返回类型为 bool,但它什么也不返回。

Using your approach the function can look the following way使用您的方法 function 可以如下所示

template <class T>
bool BST<T>::similarStructure( const BST &Tree2 ) const 
{
    if ( isEmpty() && Tree2.isEmpty() ) return true;

    StackArray<node<T>> s1, s2;

    s1.Push( root );
    s2.Push( tree2.root );

    bool theSame = true;

    while ( theSame && !s1.isEmpty() ) 
    {
        node<T> n  = s1.Pop();
        node<T> n1 = s2.Pop();

        theSame = ( n->right != NULL ) == ( n1->right != NULL );

        if ( theSame && n->right != NULL )
        { 
            s1.Push( n->right );
            s2.Push( n1->right );
        }

        if ( theSame )
        {
            theSame == ( n->left != NULL ) == ( n->left != NULL );
            if ( theSame && n->left != NULL )
            {
                s1.Push( n->left ); 
                s2.Push( n1->left );
            }
        }
    }

    return theSame;
}

Pay attention to that the function is declared with the qualifier const .注意 function 是用限定符const声明的。 It means that the member function isEmoty of the class BST<T> also shall be declared with the qualifier const .这意味着 class BST<T>的成员 function isEmoty也应使用限定符const声明。

bool isEmpty() const;

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

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