要检查它是完整的二叉树还是完全二叉树,或者两者都不是

 由来只有新人笑_谁能记得旧人哭 发布于 2023-02-12 17:45

如果是二叉树,我是新概念.我被困在一个问题很多天了.它是为了查找给定的树是二叉树还是完全二叉树,或者两者都不是.

我已经想到了很多算法,但它们都没有满足每一个案例.我试过谷歌,但没有适当的解决方案.

我想过使用Level Order Traversal Technique但是在将所有节点插入队列之后无法知道如何知道它们的级别.

对于完全二进制树,我尝试计算所有节点的度数是0还是2但是如果树有一些具有度的中间节点,则该逻辑也是错误的.

我使用链表制作了一棵树,基本 - 左子,右子关系方式.

对于完全二叉树,我做一个inorder traverl并检查度数是否为0或2,但这是错误的,如果某个节点处于某个较早级别的0度,那么输出也会成立.

对于完整的二叉树,我无法想出任何合适的东西.

谢谢.

我正在使用C++,所以如果逻辑使用指针那么它就没问题了.

1 个回答
  • 检查完全很容易:
    按此处的定义.http://courses.cs.vt.edu/cs3114/Summer11/Notes/T03a.BinaryTreeTheorems.pdf

    如果所有节点都有0个或2个子节点,则树已满.

    bool full(Node* tree)
    {
        // Empty tree is full so return true.
        // This also makes the recursive call easier.
        if (tree == NULL)
        {   return true;
        }
    
        // count the number of children
        int count = (tree->left == NULL?0:1) + (tree->right == NULL?0:1);
    
        // We are good if this node has 0 or 2 and full returns true for each child.
        // Don't need to check for left/right being NULL as the test on entry will catch
        // NULL and return true.
        return count != 1 && full(tree->left) && full(tree->right);
    }
    

    完成有点困难.
    但最简单的方法似乎是遍历树的广度(从左到右).在每个节点上,遍历列表的左侧和右侧(即使它们是NULL).在达到第一个NULL之后,应该只剩下要查找的NULL对象.如果在此之后找到非NULL对象,则它不是完整的树.

    bool complete(Node* tree)
    {
        // The breadth first list of nodes.
        std::list<Node*>  list;
        list.push_back(tree);   // add the root as a starting point.
    
        // Do a breadth first traversal.
        while(!list.empty())
        {
            Node* next = list.front();
            list.pop_front();
    
            if (next == NULL)
            {   break;
            }
    
            list.push_back(next->left);
            list.push_back(next->right);
        }
    
        // At this point there should only be NULL values left in the list.
        // If this is not true then we have failed.
    
        // Written in C++11 here.
        // But you should be able to traverse the list and look for any non NULL values.
        return std::find_if(list.begin(), list.end(), [](Node* e){return e != NULL;}) != list.end();
    }
    

    2023-02-12 17:47 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有