python二叉查找树

二叉查找树:
左节点<根节点<右节点

# 树结构
class Node(object):
    def __init__(self, value=None, left=None, right=None):
        self.value = value
        self.left = left
        self.right = right

1、新建树

# 根据数组,求二叉查找树
def create_find_tree(array):
    l = len(array)
    if l == 0:
        return None
    if l == 1:
        return Node(array[0])
    head = Node(array[0])
    for i in range(1,l):
        insert_tree(head, array[i])
    return head

2、插入

def insert_tree(root, v):
    if not root:
        return Node(v)
    if root.value > v:
        root.left = insert_tree(root.left, v)
    if root.value <= v:
        root.right = insert_tree(root.right, v)
    return root
点赞