definsert(word): current_word = word current_node = root insert_operation_1(current_word, current_node)
definsert_operation_1(current_word, current_node): if current_word not empty: X = current_word[0]
if X in current_node.child: current_word = current_word[1:] current_node = child_node insert_operation_1(current_word, current_node) else: insert_operation_2(current_word, current_node)
else: mark current_node insert_node
definsert_operation_2(current_word, current_node): X = current_word[0] M.value = x M.father = current_node current_node.child = M
current_word = current_word[1:] if current_word not empty: current_node = M insert_operation_2(current_word, current_node)
else: mark M insert_node
查找操作:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
deffind(word): current_word = word current_node = root return find_opration(current_word, current_node)
deffind_opration(current_word, current_node): if current_word not empty: X = current_word[0] if X in current_node.child_node: current_word = current_word[1:] current_node = child_node return find_opration(current_word, current_node) else: return false else: return current_node.isword
defdelete(word): current_word = word current_node = root return delete_operation(current_word, current_node)
defdelete_operation(current_word, current_node): if current_word not empty: X = current_word[0] if X in current_node.child_node: current_word = current_word[1:] current_node = child_node is_deleted = delete_operation(current_word, current_node) else: return false else: if current_node have child_node: mark current_node no_word_node else: delete current_node return true