avp26/test/test2.py
2026-12-04 20:11:13 +00:00

52 lines
1.3 KiB
Python

def hash_insert(table, key):
n = len(table)
for i in range(n):
index = (key + i) % n
if table[index] == key:
return index
if table[index] is None:
table[index] = key
return index
return -1
def hash_find(table, key):
n = len(table)
for i in range(n):
index = (key + i) % n
if table[index] == key:
return index
if table[index] is None:
return -1
return -1
def bst_contains(tree, value):
if tree is None:
return False
if value == tree.value:
return True
if value < tree.value:
return bst_contains(tree.left, value)
return bst_contains(tree.right, value)
def bst_insert(tree, value):
if tree is None:
return BSTNode(value)
if value < tree.value:
tree.left = bst_insert(tree.left, value)
elif value > tree.value:
tree.right = bst_insert(tree.right, value)
return tree
def build_huffman(freq):
trees = []
for ch in freq:
trees.append((freq[ch], ch, HuffmanNode(ch, None, None)))
while len(trees) > 1:
trees.sort()
w1, c1, left = trees.pop(0)
w2, c2, right = trees.pop(0)
trees.append((w1 + w2, min(c1, c2), HuffmanNode(None, left, right)))
return trees[0][2]