📜  alberi binari di ricerca python 代码示例

📅  最后修改于: 2022-03-11 14:45:25.266000             🧑  作者: Mango

代码示例1
class Node:
    def __init__(self, value=None, left=None, right=None):
        self.left = None
        self.right = None
        self.value = value

    def insertValue(self, value):
        if not self.value:
            self.value = value
            return
        if self.value == value:
            return
        if value < self.value:
            if self.left:
                self.left.insertValue(value)
                return
            self.left = Node(value)
            return
        if self.right:
            self.right.insertValue(value)
            return
        self.right = Node(value)

    def getMin(self):
        while self.left is not None:
            self = self.left
        return self.value

    def getMax(self):
        while self.right is not None:
            self = self.right
        return self.value

def buildABR(lista):
    bst = Node()
    for x in lista:
        bst.insertValue(x)
    return bst

if __name__ == "__main__":
    lista = [99, 6, 18, -1, 21, 11, 3, 5, 4, 24, 18]
    abr = buildABR(lista)
    print(abr.getMax())
    print(abr.getMin())