Beispiel #1
0
class PhoneBook:

    def __init__(self):
        self.__phonebook = BST()

    def insert(self, number, name):
        entry = self.__phonebook.lookup(name)
        if entry is None:
            self.__phonebook.insert(name, [number])
        else:
            value = entry.get_value()
            value.append(number)

    def lookup(self, name):
        entry = self.__phonebook.lookup(name)
        if entry is not None:
            print('{}: [{}]'.format(entry.get_key(), entry.get_value()))
        else:
            print("NOT FOUND!")

    def list(self):
        all_entries = self.__phonebook.traverse(1)
        for e in all_entries:
            print('{}: [{}]'.format(e[0], ', '.join(e[1])))

    def remove(self, name):
        self.__phonebook.remove(name)
Beispiel #2
0
class PhoneBook():
    def __init__(self):
        self.tree = BST(None, compare)

#inserts a new contact
    def insert(self, number, name):
        found = self.tree.lookup(lambda x: x["value"]["name"] is name)
        if found is not None:
            found["value"]["number"] = number
        else:
            self.tree.insert({
                "name": name,
                "number": number
            })

#lookup a name and print its phone number
    def lookup(self, name):
        return self.tree.lookup(lambda x: x["value"]["name"] is name)

#list all records in an alphabetical order
    def list(self):
        result = []
        self.tree.traverse(lambda x: result.append(x["value"]))
        return result

#remove a record for a given name
    def remove(name):
        pass