简体   繁体   English

如何在Python中建立二叉树

[英]How to build a binary tree in Python

I am trying to build a binary tree, but stuck at adding two nodes to root. 我正在尝试建立一个二叉树,但是在向根节点添加两个节点方面陷入了困境。

What function should I write to enable adding node to these two nodes? 我应该编写什么函数来将节点添加到这两个节点?

I have the following code: 我有以下代码:

class Btree:
        def __init__(self, root):
                self.key = root
                self.lc = None
                self.rc = None

        def insert_lc(self, newNode):
                if self.lc == None:
                        self.lc = Btree(newNode)
                else:
                        t = Btree(newNode)
                        t.lc = self.lc
                        self.lc = t
        def insert_rc(self, newNode):
                if self.rc == None:
                        self.rc = Btree(newNode)
                else:
                        t = Btree(newNode)
                        t.rc = self.rc
                        self.rc = t

        def get_rc(self):
                return self.rc
        def get_lc(self):
                return self.lc

        def set_Root(self, val):
                self.key = val
        def get_Root(self):
                return self.key

r = Btree(1)
r.insert_lc(2)
r.insert_rc(4)

I think I need a function so I can add left child and right child to nodes which have value 2 and 4 我想我需要一个函数,以便可以将左子元素和右子元素添加到值为2和4的节点上

First of all, I do not quite understand why do you assign your new node's left (right) child to that new node itself. 首先,我不太了解为什么将新节点的左(右)子节点分配给该新节点本身。 This will get you an infinite loop if you try to search the tree later. 如果稍后尝试搜索树,则将导致无限循环。

t = Btree(newNode)
#The line below seems unnecessary
t.lc = self.lc
self.lc = t

Second of all, in the Btree constructor you have a parameter called root which seems like it's intended to be a reference to the root of the current node. 其次,在Btree构造函数中,您有一个名为root的参数,看来它打算作为对当前节点根的引用。 However, you pass the node's key as that parameter, so there is no way to access node's root. 但是,您将节点的密钥作为该参数传递,因此无法访问节点的根。 I understand that is might be not even necessary in your case, still that parameter name is quite confusing. 我知道在您的情况下可能甚至没有必要,但参数名称仍然很混乱。 If you want to have a reference to the parent node you should add another parameter to your constructor: 如果要引用父节点,则应在构造函数中添加另一个参数:

def __init__(self, root, key):
    self.root = root
    self.key = key
    self.lc = None
    self.rc = None

This way you have both key and a parent node reference. 这样,您既具有键又具有父节点引用。 You also will need to modify insert_rc and insert_lc methods: 您还需要修改insert_rcinsert_lc方法:

def insert_rc(self, newNode):
    if self.rc == None:
        #Parent node is the one the insert_rc method is called on,
        #so we pass self as the root parameter
        self.rc = Btree(self, newNode)
    else:
        t = Btree(self, newNode)
        self.rc = t
#Same with insert_lc

Finally,to answer your initial question: to add children to the added nodes you simply call get_rc or get_lc both of which will return you instances of Btree so you can add child nodes to them: 最后,要回答你最初的问题:给孩子添加到加节点上,您只需拨打get_rcget_lc两者将返回的情况下, Btree ,以便您可以添加子节点对他们说:

r = Btree(1)
r.insert_lc(2)
r.insert_rc(4)
#Insert left child to the node with key=2
r.get_lc().insert_lc(3)
#Insert right child to the node with key=4
r.get_rc().insert_rc(5)

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

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