Skip to content
Snippets Groups Projects
Commit 072d024b authored by Mamadu-lamarana Bah's avatar Mamadu-lamarana Bah :speech_balloon:
Browse files

ajout de util.recherchedicho

parent c25f14b5
Branches
No related tags found
No related merge requests found
class Btree() : class Btree() :
def __init__(self,name,root) : def __init__(self, root) :
self.name=name self.root = root
self.root=root
def recherche(self, valeur):
if (self.root != None) :
index = 0
while index
#def search() : #def search() :
#def insertion(): #def insertion():
......
import util
class node() : class node() :
def __init__(self,keys,child,kmin,kmax) : def __init__(self, k, kmin, kmax) :
self.keys=keys self.keys = []
self.child=child self.childs = []
self.kmin=kmin self.kmin = kmin
self.kmax=kmax self.kmax = kmax
#def search(key) :
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 t=len(listKeys)-1
d, f = 0, t d, f = 0, t
while (f>= d) : while (f>= d) :
...@@ -33,10 +40,6 @@ class node() : ...@@ -33,10 +40,6 @@ class node() :
#def getSizeNode() : #def getSizeNode() :
#def getPos() : #def getPos() :
#def setNewChild() : #def setNewChild() :
#def removeChild() : #def removeChild() :
def getSize(listKey) :
return len(listKey)
#def isLeaf() : #def isLeaf() :
\ No newline at end of file
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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment