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

search et insert

parent 072d024b
Branches
No related tags found
No related merge requests found
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() :
......
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() :
......
......@@ -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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment