alberi binari di ricerca python

Solutions on MaxInterview for alberi binari di ricerca python by the best coders in the world

showing results for - "alberi binari di ricerca python"
Regina
26 Jan 2019
1class Node:
2    def __init__(self, value=None, left=None, right=None):
3        self.left = None
4        self.right = None
5        self.value = value
6
7    def insertValue(self, value):
8        if not self.value:
9            self.value = value
10            return
11        if self.value == value:
12            return
13        if value < self.value:
14            if self.left:
15                self.left.insertValue(value)
16                return
17            self.left = Node(value)
18            return
19        if self.right:
20            self.right.insertValue(value)
21            return
22        self.right = Node(value)
23
24    def getMin(self):
25        while self.left is not None:
26            self = self.left
27        return self.value
28
29    def getMax(self):
30        while self.right is not None:
31            self = self.right
32        return self.value
33
34def buildABR(lista):
35    bst = Node()
36    for x in lista:
37        bst.insertValue(x)
38    return bst
39
40if __name__ == "__main__":
41    lista = [99, 6, 18, -1, 21, 11, 3, 5, 4, 24, 18]
42    abr = buildABR(lista)
43    print(abr.getMax())
44    print(abr.getMin())