30 lines
686 B
Python
30 lines
686 B
Python
def qsort(xs):
|
|
if xs == []: return []
|
|
|
|
pivot = xs[0]
|
|
left = [x for x in xs if x < pivot]
|
|
right = [x for x in xs[1:] if x >= pivot]
|
|
return [qsort(left)] + [pivot] + [qsort(right)]
|
|
|
|
def sorted(tree):
|
|
if tree == []: return []
|
|
return sorted(tree[0]) + [tree[1]] + sorted(tree[2])
|
|
|
|
def search(tree, x):
|
|
return _sorted(tree, x) != []
|
|
|
|
def insert(tree, x):
|
|
node = _sorted(tree, x)
|
|
if node == []:
|
|
node.append([])
|
|
node.append(x)
|
|
node.append([])
|
|
|
|
def _sorted(tree, i):
|
|
if tree == []: return tree
|
|
|
|
pivot = tree[1]
|
|
if pivot == i: return tree
|
|
elif i < pivot: return _sorted(tree[0], i)
|
|
else: return _sorted(tree[2], i)
|