diff --git a/src/BTree.py b/src/BTree.py index d87dfa65abe30364418dd181551b66fca6361182..4323f71c14beb6eba4c6910ea3da6a8df1fcc038 100644 --- a/src/BTree.py +++ b/src/BTree.py @@ -1,8 +1,13 @@ class Btree() : - def __init__(self,name,root) : - self.name=name - self.root=root + def __init__(self, root) : + self.root = root + + def recherche(self, valeur): + if (self.root != None) : + index = 0 + while index + #def search() : #def insertion(): diff --git a/src/node.py b/src/node.py index 6c5e258925e6c7a6eb9d181316b62d7ba0b15564..38f61342a10e66b954e018e3182eccb8539e8c03 100644 --- a/src/node.py +++ b/src/node.py @@ -1,16 +1,23 @@ +import util + class node() : - def __init__(self,keys,child,kmin,kmax) : - self.keys=keys - self.child=child - self.kmin=kmin - self.kmax=kmax - - #def search(key) : + def __init__(self, k, kmin, kmax) : + self.keys = [] + self.childs = [] + self.kmin = kmin + self.kmax = kmax + def search(self, Key): + return util.recherche_dichotomique(key, self.childs) + + def getSize(self) : + return len(Keys) + + def addKey(self) : - def dicho(listKeys,key) : + def dicho(self, listKeys,key) : t=len(listKeys)-1 d, f = 0, t while (f>= d) : @@ -33,10 +40,6 @@ class node() : #def getSizeNode() : #def getPos() : #def setNewChild() : - #def removeChild() : - - def getSize(listKey) : - return len(listKey) - + #def removeChild() : #def isLeaf() : \ No newline at end of file diff --git a/src/util.py b/src/util.py new file mode 100644 index 0000000000000000000000000000000000000000..1946bbee252e7a6f2727b085c2309221c66bac53 --- /dev/null +++ b/src/util.py @@ -0,0 +1,60 @@ +import doctest + +def compare(a, b): + """ + :param a: (any) un élément + :param b: (any) un élément + :return: (bool) * 1 si a est plus grand que b + * 0 si a est égal à b + * -1 si a est plus petit que b + :CU: a et b sont comparables + :Exemples: + + >>> compare(1, 2) + -1 + >>> compare('z', 'a') + 1 + >>> compare(0, 0) + 0 + """ + if a > b: + res = 1 + elif a < b: + res = -1 + else: + res = 0 + return res + +def recherche_dichotomique(x, l, cmp = compare): + """ + :param x: (any) un élément + :param l: (list) une liste + :return: (bool) True si x appartient à l, False sinon + :CU: x doit être comparable aux éléments de l, + l est triée + :Exemples: + + >>> recherche_dichotomique(1, []) + (False, -1) + >>> l = list(range(10)) + >>> recherche_dichotomique(5, l) + (True, 5) + >>> recherche_dichotomique(5.5, l) + (False, -1) + """ + n = len(l) + debut, fin = 0, n + index = 0 + while debut < fin: + m = (debut + fin)//2 + if cmp(x, l[m]) == 0: + return (True, m) + elif cmp(x, l[m]) > 0: + index += m + debut = m + 1 + else: + fin = m + index = 0 + return (False, -1) + +doctest.testmod() \ No newline at end of file