创建二叉树

我搜索过的关于二叉树的大多数问题都显示了二叉搜索树的实现,而不是二叉树。完全二叉树的项是:

  • 要么是一棵空树,要么它有 1 个节点和 2 个孩子,其中每个孩子都是另一个二叉树。

  • 所有级别都已满(可能最后一个级别除外)

  • 最底层的所有叶子都尽可能靠左。

我想出了一个概念,但它似乎没有正确运行递归——有人知道我做错了什么吗?

class Node():

    def __init__(self, key):

        self.key = key

        self.left = None

        self.right = None


    def add(self, key): 

        if self.key: 

            if self.left is None: 

                self.left = Node(key) 

            else: 

                self.left.add(key)


                if self.right is None: 

                    self.right = Node(key) 

                else: 

                    self.right.add(key) 

        else: 

            self.key = key

        return (self.key)


青春有我
浏览 120回答 3
3回答

慕雪6442864

您的代码中的问题是您多次添加相同的值。您添加节点,然后仍然更深入地递归,在那里您执行相同的操作。更深层次的问题是,在到达树的底层并检测到该层的不完整位置之前,您并不知道将节点插入何处。找到正确的插入点可能需要遍历整棵树……这打败了您最初期望使用二叉树获得的速度增益。我在这里提供三种解决方案,从最有效的开始:1.使用列表作为树实现对于完整的树,需要特别考虑:如果按级别对节点进行编号,从 0 开始作为根节点,并且在每个级别内从左到右,您会注意到节点的父节点的编号为(k-1) /2当它自己的编号是k时。在另一个方向:如果编号为k的节点有子节点,则其左子节点的编号为k*2+1,右子节点的编号大一。因为树是完整的,所以这个编号永远不会有间隙,所以你可以将节点存储在一个列表中,并使用该列表的索引为节点编号。现在将节点添加到树中仅意味着将其附加到该列表。您没有Node对象,只有树列表,该列表中的索引是您的节点引用。这是一个实现:class CompleteTree(list):&nbsp; &nbsp; def add(self, key):&nbsp; &nbsp; &nbsp; &nbsp; self.append(key)&nbsp; &nbsp; &nbsp; &nbsp; return len(self) - 1&nbsp; &nbsp; def left(self, i):&nbsp; &nbsp; &nbsp; &nbsp; return i * 2 + 1 if i * 2 + 1 < len(self) else -1&nbsp; &nbsp; def right(self, i):&nbsp; &nbsp; &nbsp; &nbsp; return i * 2 + 2 if i * 2 + 2 < len(self) else -1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; @staticmethod&nbsp; &nbsp; def parent(i):&nbsp; &nbsp; &nbsp; &nbsp; return (i - 1) // 2&nbsp; &nbsp; def swapwithparent(self, i):&nbsp; &nbsp; &nbsp; &nbsp; if i > 0:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; p = self.parent(i)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self[p], self[i] = self[i], self[p]&nbsp; &nbsp; def inorder(self, i=0):&nbsp; &nbsp; &nbsp; &nbsp; left = self.left(i)&nbsp; &nbsp; &nbsp; &nbsp; right = self.right(i)&nbsp; &nbsp; &nbsp; &nbsp; if left >= 0:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; yield from self.inorder(left)&nbsp; &nbsp; &nbsp; &nbsp; yield i&nbsp; &nbsp; &nbsp; &nbsp; if right >= 0:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; yield from self.inorder(right)&nbsp; &nbsp; @staticmethod&nbsp; &nbsp; def depth(i):&nbsp; &nbsp; &nbsp; &nbsp; return (i + 1).bit_length() - 1这是一个创建示例树的演示,然后打印按顺序遍历访问的键,按它们在树中的深度缩进:tree = CompleteTree()tree.add(1)tree.add(2)tree.add(3)tree.add(4)tree.add(5)for node in tree.inorder():&nbsp; &nbsp; print("&nbsp; " * tree.depth(node), tree[node])当然,这意味着您必须引用与使用真实Node类时略有不同的节点,但效率的提高是有回报的。2.使用额外的属性如果您知道一棵(子)树中有多少个节点,那么从该数字的位表示,您就可以知道应该在何处添加下一个节点。例如,在您的示例树中,您有 5 个节点。想象一下,你想给那棵树加一个 6。根节点会告诉您当前有 5,因此您需要将其更新为 6。在二进制中为 110。忽略最左边的 1 位,其余位告诉您是向左还是向右。在这种情况下,您应该向右走 (1),然后最后向左走 (0),在那个方向上创建节点。您可以迭代或递归地执行此操作。这是一个递归的实现:class Node():&nbsp; &nbsp; def __init__(self, key):&nbsp; &nbsp; &nbsp; &nbsp; self.key = key&nbsp; &nbsp; &nbsp; &nbsp; self.left = None&nbsp; &nbsp; &nbsp; &nbsp; self.right = None&nbsp; &nbsp; &nbsp; &nbsp; self.count = 1&nbsp; &nbsp; def add(self, key):&nbsp; &nbsp; &nbsp; &nbsp; self.count += 1&nbsp; &nbsp; &nbsp; &nbsp; if self.left is None:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.left = Node(key)&nbsp; &nbsp; &nbsp; &nbsp; elif self.right is None:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.right = Node(key)&nbsp; &nbsp; &nbsp; &nbsp; # extract from the count the second-most significant bit:&nbsp; &nbsp; &nbsp; &nbsp; elif self.count & (1 << (self.count.bit_length() - 2)):&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.right.add(key)&nbsp; &nbsp; &nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.left.add(key)&nbsp; &nbsp; def inorder(self):&nbsp; &nbsp; &nbsp; &nbsp; if self.left:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; yield from self.left.inorder()&nbsp; &nbsp; &nbsp; &nbsp; yield self&nbsp; &nbsp; &nbsp; &nbsp; if self.right:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; yield from self.right.inorder()tree = Node(1)tree.add(2)tree.add(3)tree.add(4)tree.add(5)for node in tree.inorder():&nbsp; &nbsp; print(node.key)3.无额外财产如果没有属性可以添加到Node对象,则需要进行更广泛的搜索才能找到正确的插入点:class Node():&nbsp; &nbsp; def __init__(self, key):&nbsp; &nbsp; &nbsp; &nbsp; self.key = key&nbsp; &nbsp; &nbsp; &nbsp; self.left = None&nbsp; &nbsp; &nbsp; &nbsp; self.right = None&nbsp; &nbsp; def newparent(self):&nbsp; &nbsp; &nbsp; &nbsp; # Finds the node that should serve as parent for a new node&nbsp; &nbsp; &nbsp; &nbsp; # It returns a tuple:&nbsp; &nbsp; &nbsp; &nbsp; #&nbsp; &nbsp;if parent found: [-1, parent for new node]&nbsp; &nbsp; &nbsp; &nbsp; #&nbsp; &nbsp;if not found: [height, left-most leaf]&nbsp; &nbsp; &nbsp; &nbsp; # In the latter case, the subtree is perfect, and its left-most&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; # leaf is the node to be used, unless self is a right child&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; # and its sibling has the insertion point.&nbsp; &nbsp; &nbsp; &nbsp; if self.right:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; right = self.right.newparent()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if right[0] == -1: # found inbalance&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return right&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; left = self.left.newparent()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if left[0] == -1: # found inbalance&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return left&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if left[0] != right[0]:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return [-1, right[1]] # found inbalance&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # temporary result in perfect subtree&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return [left[0]+1, left[1]]&nbsp; &nbsp; &nbsp; &nbsp; elif self.left:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return [-1, self] # found inbalance&nbsp; &nbsp; &nbsp; &nbsp; # temporary result for leaf&nbsp; &nbsp; &nbsp; &nbsp; return [0, self]&nbsp; &nbsp; def add(self, key):&nbsp; &nbsp; &nbsp; &nbsp; _, parent = self.newparent()&nbsp; &nbsp; &nbsp; &nbsp; if not parent.left:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; parent.left = Node(key)&nbsp; &nbsp; &nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; parent.right = Node(key)&nbsp; &nbsp; def __repr__(self):&nbsp; &nbsp; &nbsp; &nbsp; s = ""&nbsp; &nbsp; &nbsp; &nbsp; if self.left:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; s += str(self.left).replace("\n", "\n&nbsp; ")&nbsp; &nbsp; &nbsp; &nbsp; s += "\n" + str(self.key)&nbsp; &nbsp; &nbsp; &nbsp; if self.right:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; s += str(self.right).replace("\n", "\n&nbsp; ")&nbsp; &nbsp; &nbsp; &nbsp; return stree = Node(1)tree.add(2)tree.add(3)tree.add(4)tree.add(5)print(tree)这从右到左递归搜索树,以找到要添加的节点的候选父节点。对于大树,可以通过根据这些路径的长度在从根到叶的路径之间进行二进制搜索来稍微改进一下。但它仍然不会像前两种解决方案那样高效。

至尊宝的传说

您可以使用 sklearn 决策树,因为它们也可以设置为二元决策树。链接到此处的文档。

茅侃侃

你真的需要以某种方式扩充你的树。因为这不是二叉搜索树,所以关于每个节点的唯一真实信息是它是否有左孩子和右孩子。不幸的是,这对导航完整的二叉树没有帮助。想象一个有 10 个级别的完整二叉树。直到第 9 级,每个节点都有一个左孩子和一个右孩子,所以你无法知道从哪条路径向下到达叶子。那么问题来了,你给每个节点添加什么信息呢?我会添加该树中的节点数。维护计数很容易,因为每次下降到子树时,您都知道要在该节点的计数上加一。你要识别的是最左边的不完美子树。每个完美二叉树都有 n = 2^k - 1,其中 k 是层数,n 是节点数。有一些快速简便的方法可以检查一个数是否小于 2 的幂(参见这个问题的第一个答案),事实上,在一棵完整的二叉树中,每个节点最多有一个不是根的子节点的完美二叉树。遵循一个简单的规则来添加节点:如果左孩子为 None,则设置root.left = Node(key)并返回否则,如果右孩子为 None,则设置root.right = Node(key)并返回如果当前节点的子节点之一是不完美子树的根,则使该节点成为当前节点(沿该子树下降)否则,如果大小不相等,则将具有较小子树的节点作为当前节点。否则,使左子节点成为当前节点。通过用以该节点为根的子树的大小扩充每个节点,您可以在每个节点上获得构建递归解决方案所需的所有信息。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python