How to implement a binary search tree in Python?

前端 未结 18 2112
眼角桃花
眼角桃花 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条回答
  •  -上瘾入骨i
    2020-11-30 22:59

    I find the solutions a bit clumsy on the insert part. You could return the root reference and simplify it a bit:

    def binary_insert(root, node):
        if root is None:
            return node
        if root.data > node.data:
            root.l_child = binary_insert(root.l_child, node)
        else:
            root.r_child = binary_insert(root.r_child, node)
        return root
    

提交回复
热议问题