Project 2: Contact Book Application
Implementing the Basic Structure
Welcome to Project 2, where we will apply our knowledge from the previous chapters to create something practical and engaging: a Contact Book Application. This project is a fantastic way to see how data structures like binary search trees (BSTs) can be used in real-world applications. Our contact book will allow users to add, delete, search, and list contacts. The searching functionality will be powered by binary search, making the process efficient even with a large number of contacts.
This project will not only reinforce your understanding of BSTs and binary search but also give you hands-on experience in building a functional application.
The first step in our project is to define the basic structure for storing contact information. We'll use a binary search tree, where each node will represent a contact with details like name, phone number, and email.
1. Defining a Contact Node
Each contact will be a node in our BST. Let's start by defining the structure of a node.
class ContactNode:
def __init__(self, name, phone, email):
self.name = name
self.phone = phone
self.email = email
self.left = None
self.right = None
Here, name
, phone
, and email
are the contact details, while left
and right
will point to the left and right children of the node in the tree, respectively.
2. Building the Binary Search Tree
Now, let's create the BST structure to hold these contacts. We'll implement the insertion method, which will allow us to add new contacts.
class ContactBookBST:
def __init__(self):
self.root = None
def insert(self, root, node):
if root is None:
return node
if node.name < root.name:
root.left = self.insert(root.left, node)
else:
root.right = self.insert(root.right, node)
return root
def add_contact(self, name, phone, email):
new_contact = ContactNode(name, phone, email)
if self.root is None:
self.root = new_contact
else:
self.insert(self.root, new_contact)
In this ContactBookBST
class, we define an add_contact
method to add new contacts. The contacts are inserted into the BST based on the alphabetical order of their names.
3. Testing Basic Insertion:
Let's test the basic structure by adding a few contacts.
# Example Usage
contact_book = ContactBookBST()
contact_book.add_contact("Alice", "123-456-7890", "alice@email.com")
contact_book.add_contact("Bob", "987-654-3210", "bob@email.com")
# At this point, we have added two contacts to our contact book.
This sets the stage for our contact book application. We've established a fundamental structure to store and organize contacts efficiently using a BST. As we progress, we'll add more features like searching for a contact using binary search, deleting a contact, and listing all contacts.
Implementing the Basic Structure
Welcome to Project 2, where we will apply our knowledge from the previous chapters to create something practical and engaging: a Contact Book Application. This project is a fantastic way to see how data structures like binary search trees (BSTs) can be used in real-world applications. Our contact book will allow users to add, delete, search, and list contacts. The searching functionality will be powered by binary search, making the process efficient even with a large number of contacts.
This project will not only reinforce your understanding of BSTs and binary search but also give you hands-on experience in building a functional application.
The first step in our project is to define the basic structure for storing contact information. We'll use a binary search tree, where each node will represent a contact with details like name, phone number, and email.
1. Defining a Contact Node
Each contact will be a node in our BST. Let's start by defining the structure of a node.
class ContactNode:
def __init__(self, name, phone, email):
self.name = name
self.phone = phone
self.email = email
self.left = None
self.right = None
Here, name
, phone
, and email
are the contact details, while left
and right
will point to the left and right children of the node in the tree, respectively.
2. Building the Binary Search Tree
Now, let's create the BST structure to hold these contacts. We'll implement the insertion method, which will allow us to add new contacts.
class ContactBookBST:
def __init__(self):
self.root = None
def insert(self, root, node):
if root is None:
return node
if node.name < root.name:
root.left = self.insert(root.left, node)
else:
root.right = self.insert(root.right, node)
return root
def add_contact(self, name, phone, email):
new_contact = ContactNode(name, phone, email)
if self.root is None:
self.root = new_contact
else:
self.insert(self.root, new_contact)
In this ContactBookBST
class, we define an add_contact
method to add new contacts. The contacts are inserted into the BST based on the alphabetical order of their names.
3. Testing Basic Insertion:
Let's test the basic structure by adding a few contacts.
# Example Usage
contact_book = ContactBookBST()
contact_book.add_contact("Alice", "123-456-7890", "alice@email.com")
contact_book.add_contact("Bob", "987-654-3210", "bob@email.com")
# At this point, we have added two contacts to our contact book.
This sets the stage for our contact book application. We've established a fundamental structure to store and organize contacts efficiently using a BST. As we progress, we'll add more features like searching for a contact using binary search, deleting a contact, and listing all contacts.
Implementing the Basic Structure
Welcome to Project 2, where we will apply our knowledge from the previous chapters to create something practical and engaging: a Contact Book Application. This project is a fantastic way to see how data structures like binary search trees (BSTs) can be used in real-world applications. Our contact book will allow users to add, delete, search, and list contacts. The searching functionality will be powered by binary search, making the process efficient even with a large number of contacts.
This project will not only reinforce your understanding of BSTs and binary search but also give you hands-on experience in building a functional application.
The first step in our project is to define the basic structure for storing contact information. We'll use a binary search tree, where each node will represent a contact with details like name, phone number, and email.
1. Defining a Contact Node
Each contact will be a node in our BST. Let's start by defining the structure of a node.
class ContactNode:
def __init__(self, name, phone, email):
self.name = name
self.phone = phone
self.email = email
self.left = None
self.right = None
Here, name
, phone
, and email
are the contact details, while left
and right
will point to the left and right children of the node in the tree, respectively.
2. Building the Binary Search Tree
Now, let's create the BST structure to hold these contacts. We'll implement the insertion method, which will allow us to add new contacts.
class ContactBookBST:
def __init__(self):
self.root = None
def insert(self, root, node):
if root is None:
return node
if node.name < root.name:
root.left = self.insert(root.left, node)
else:
root.right = self.insert(root.right, node)
return root
def add_contact(self, name, phone, email):
new_contact = ContactNode(name, phone, email)
if self.root is None:
self.root = new_contact
else:
self.insert(self.root, new_contact)
In this ContactBookBST
class, we define an add_contact
method to add new contacts. The contacts are inserted into the BST based on the alphabetical order of their names.
3. Testing Basic Insertion:
Let's test the basic structure by adding a few contacts.
# Example Usage
contact_book = ContactBookBST()
contact_book.add_contact("Alice", "123-456-7890", "alice@email.com")
contact_book.add_contact("Bob", "987-654-3210", "bob@email.com")
# At this point, we have added two contacts to our contact book.
This sets the stage for our contact book application. We've established a fundamental structure to store and organize contacts efficiently using a BST. As we progress, we'll add more features like searching for a contact using binary search, deleting a contact, and listing all contacts.
Implementing the Basic Structure
Welcome to Project 2, where we will apply our knowledge from the previous chapters to create something practical and engaging: a Contact Book Application. This project is a fantastic way to see how data structures like binary search trees (BSTs) can be used in real-world applications. Our contact book will allow users to add, delete, search, and list contacts. The searching functionality will be powered by binary search, making the process efficient even with a large number of contacts.
This project will not only reinforce your understanding of BSTs and binary search but also give you hands-on experience in building a functional application.
The first step in our project is to define the basic structure for storing contact information. We'll use a binary search tree, where each node will represent a contact with details like name, phone number, and email.
1. Defining a Contact Node
Each contact will be a node in our BST. Let's start by defining the structure of a node.
class ContactNode:
def __init__(self, name, phone, email):
self.name = name
self.phone = phone
self.email = email
self.left = None
self.right = None
Here, name
, phone
, and email
are the contact details, while left
and right
will point to the left and right children of the node in the tree, respectively.
2. Building the Binary Search Tree
Now, let's create the BST structure to hold these contacts. We'll implement the insertion method, which will allow us to add new contacts.
class ContactBookBST:
def __init__(self):
self.root = None
def insert(self, root, node):
if root is None:
return node
if node.name < root.name:
root.left = self.insert(root.left, node)
else:
root.right = self.insert(root.right, node)
return root
def add_contact(self, name, phone, email):
new_contact = ContactNode(name, phone, email)
if self.root is None:
self.root = new_contact
else:
self.insert(self.root, new_contact)
In this ContactBookBST
class, we define an add_contact
method to add new contacts. The contacts are inserted into the BST based on the alphabetical order of their names.
3. Testing Basic Insertion:
Let's test the basic structure by adding a few contacts.
# Example Usage
contact_book = ContactBookBST()
contact_book.add_contact("Alice", "123-456-7890", "alice@email.com")
contact_book.add_contact("Bob", "987-654-3210", "bob@email.com")
# At this point, we have added two contacts to our contact book.
This sets the stage for our contact book application. We've established a fundamental structure to store and organize contacts efficiently using a BST. As we progress, we'll add more features like searching for a contact using binary search, deleting a contact, and listing all contacts.