如何检查二叉树是否已满?

时间:2018-11-26 02:36:10

标签: python binary-tree

这里的学生。我已经编码了一种添加到BinaryTree类中的方法,该方法检查二进制树是否已满(其余类代码来自Python教科书)。我相信它可以正常工作,但是对编码来说是新手,而且我不知道如何在没有树的外观的情况下检查所有叶子是否在同一水平上。谁能看看isFullBinaryTree(self)方法是否正确?这节课快结束了。该测试程序似乎正在运行,但我并不肯定。

我的想法是,如果我为树的每一侧编码一个计数变量,并且如果两边的计数都相同,则叶子必须全部处于同一水平,这样一棵完整的树就可以了。

这是完整的代码:

class TreeNode:
    def __init__(self, e):
        self.element = e
        self.left = None # Point to the left node, default None
        self.right = None # Point to the right node, default None

class BinaryTree:
    def __init__(self):
        self.root = None
        self.size = 0

    # Insert element e into the binary search tree
    # Return True if the element is inserted successfully
    def insert(self, e):
        if self.root == None:
            self.root = self.createNewNode(e)  # Create a new root/Create the node for e as the root
        else:
            # Locate the parent node
            parent = None
            current = self.root
            while current != None:
                if e < current.element:
                    parent = current  # Keep the parent
                    current = current.left  # Go left
                elif e > current.element:
                    parent = current  # Keep the parent
                    current = current.right  # Go right
                else:
                    return False  # Duplicate node not inserted

            # Create a new node for e and attach it to parent
            if e < parent.element:
                parent.left = self.createNewNode(e)
            else:
                parent.right = self.createNewNode(e)
                self.size += 1  # Increase tree size

            return True  # Element inserted

    # Create a new TreeNode for element e
    def createNewNode(self, e):
        return TreeNode(e)

    # Returns true if the tree is a full binary tree
    def isFullBinaryTree(self):
        current = self.root  # Start from the root

        while current != None:
            leftNode = current.left
            rightNode = current.right
            leftCount = 0
            rightCount = 0
            while leftNode != None:
                current = leftNode
                leftNode = current.left
                leftCount += 1  # add 1 because we are moving from current one time
            while rightNode != None:
                current = rightNode
                rightNode = current.right
                rightCount += 1  # add 1 because we are moving from current one time

            if leftCount == rightCount:
                return True
            else:
                return False

        return False



def main():


    numbers = [2, 4, 3, 1, 8, 5, 6, 7, 0]
    intTree1 = BinaryTree()
    for e in numbers:
        intTree1.insert(e)

    print("\nIs intTree1 full? ", end = "")
    print(intTree1.isFullBinaryTree())

    numbers2 = [2, 4, 3, 1, 8, 5, 6, 7]
    intTree2 = BinaryTree()
    for e in numbers2:
        intTree2.insert(e)


    print("\nIs intTree2 full? ", end = "")
    print(intTree2.isFullBinaryTree())


main()

1 个答案:

答案 0 :(得分:1)

如果您的树看起来像/\,那么我认为它会返回True,因为您只在迭代树的外部,而不检查内部分支的充满度

我建议不要使用循环,甚至不要计数,

尽管如此,您仍需要在TreeNode类上实现isFullBinaryTree方法。

class TreeNode:
    def __init__(self, e):
        self.element = e
        self.left = None # Point to the left node, default None
        self.right = None # Point to the right node, default None

    def isFullBinaryTree(self):
        # check if we are a leaf 
        if self.left is None and self.right is None:
            return True 
        # recursively check the left fullness 
        full_left = self.left.isFullBinaryTree() if self.left else False 
        # recursively check the right fullness 
        full_right = self.right.isFullBinaryTree() if self.right else False
        # return True if checked that both left and right are full 
        return full_left and full_right

执行完此操作后,BinaryTree类就可以简单地检查根是否存在以及根TreeNode是否被视为已满。

例如

def isFullBinaryTree(self):
    return self.root.isFullBinaryTree() if self.root else False