diff --git a/src/BTree.py b/src/BTree.py index 4323f71c14beb6eba4c6910ea3da6a8df1fcc038..70dcca4b23dddbd6fcf2ae1b62b581de68e44b0a 100644 --- a/src/BTree.py +++ b/src/BTree.py @@ -1,12 +1,33 @@ +import Node as Bnode + + class Btree() : - def __init__(self, root) : + def __init__(self, k, root=None) : self.root = root + self.k = k - def recherche(self, valeur): - if (self.root != None) : - index = 0 - while index + def search(self, value, node=self.root): + (found, index) = node.search(value) + if (found): + return (node, index) + elif ( node.isLeaf() ): + return None + else : + self.search(value, node.childs[index]) + + def insertion(self, value): + if (self.root == None): + self.root = Bnode.Node(self.k) + self.root.keys[0] = value + else : + if ( len(self.root) == self.k): + new_root = Bnode.Node(self.k) + new_root.childs[0] = self.root + + + + #def search() : diff --git a/src/node.py b/src/node.py index 38f61342a10e66b954e018e3182eccb8539e8c03..70fe88405bcb49f68a730daf071e331739bbc9d3 100644 --- a/src/node.py +++ b/src/node.py @@ -1,41 +1,19 @@ -import util +from util import recherche_dichotomique -class node() : +class Node() : - def __init__(self, k, kmin, kmax) : + def __init__(self, nbKey) : self.keys = [] self.childs = [] - self.kmin = kmin - self.kmax = kmax - def search(self, Key): - return util.recherche_dichotomique(key, self.childs) - + def isLeaf(): + return (len(self.childs) == 0) + def getSize(self) : return len(Keys) - def addKey(self) : - - - def dicho(self, listKeys,key) : - t=len(listKeys)-1 - d, f = 0, t - while (f>= d) : - m=(d+f)//2 - - if (listKeys[m]==key) : - return m - if (listKeys[m]>key): - f=m - 1 - #if ( f< t and listKeys[f] == key) : - # return True,f - else : - d=m + 1 - #if (d < t and listKeys[d] == key) : - # return True, d - - return -1 - + def serach(self, k): + return recherche_dichotomique(k, self.keys) #def getSizeNode() : #def getPos() : diff --git a/src/util.py b/src/util.py index 1946bbee252e7a6f2727b085c2309221c66bac53..451bb9870e39c6149281c107503af3b9fab97f83 100644 --- a/src/util.py +++ b/src/util.py @@ -35,26 +35,46 @@ def recherche_dichotomique(x, l, cmp = compare): :Exemples: >>> recherche_dichotomique(1, []) - (False, -1) + (False, 0) >>> l = list(range(10)) >>> recherche_dichotomique(5, l) (True, 5) >>> recherche_dichotomique(5.5, l) - (False, -1) + (False, 6) """ - n = len(l) - debut, fin = 0, n + taille = len(l) + debut, fin = 0, taille 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 + milieu = (debut + fin)//2 + if cmp(x, l[milieu]) == 0: + return (True, milieu) + elif cmp(x, l[milieu]) > 0: + index = milieu +1 + debut = milieu + 1 else: - fin = m - index = 0 - return (False, -1) + fin = milieu + index = milieu + return (False, index) + +## LOIC + def dicho(self, listKeys,key) : + t=len(listKeys)-1 + d, f = 0, t + while (f>= d) : + m=(d+f)//2 + + if (listKeys[m]==key) : + return m + if (listKeys[m]>key): + f=m - 1 + #if ( f< t and listKeys[f] == key) : + # return True,f + else : + d=m + 1 + #if (d < t and listKeys[d] == key) : + # return True, d + + return -1 doctest.testmod() \ No newline at end of file