How to implement a binary search tree in Python?

前端 未结 18 2201
眼角桃花
眼角桃花 2020-11-30 22:25

This is what I\'ve got so far but it is not working:

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

    def __init__(self,key):
        self.rChild = N         


        
18条回答
  •  不知归路
    2020-11-30 22:54

    The Op's Tree.insert method qualifies for the "Gross Misnomer of the Week" award -- it doesn't insert anything. It creates a node which is not attached to any other node (not that there are any nodes to attach it to) and then the created node is trashed when the method returns.

    For the edification of @Hugh Bothwell:

    >>> class Foo(object):
    ...    bar = None
    ...
    >>> a = Foo()
    >>> b = Foo()
    >>> a.bar
    >>> a.bar = 42
    >>> b.bar
    >>> b.bar = 666
    >>> a.bar
    42
    >>> b.bar
    666
    >>>
    

提交回复
热议问题