相关疑难解决方法(0)

如何在Python中实现二进制搜索树?

这是我到目前为止所得到的但它不起作用:

class Node:
    rChild,lChild,data = None,None,None

    def __init__(self,key):
        self.rChild = None
        self.lChild = None
        self.data = key

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

    def insert(self,node,someNumber):
        if node is None:
            node = Node(someNumber)
        else:
            if node.data > someNumber:
                self.insert(node.rchild,someNumber)
            else:
                self.insert(node.rchild, someNumber)
        return

def main():
    t = Tree()
    t.root = Node(4)
    t.root.rchild = Node(5)
    print t.root.data #this works
    print t.root.rchild.data #this works too
    t = Tree()
    t.insert(t.root,4)
    t.insert(t.root,5)
    print t.root.data #this fails
    print t.root.rchild.data …
Run Code Online (Sandbox Code Playgroud)

python oop class binary-search-tree data-structures

35
推荐指数
6
解决办法
8万
查看次数

标签 统计

binary-search-tree ×1

class ×1

data-structures ×1

oop ×1

python ×1