Trees Graphs and Hash Tables#
(a.k.a. When Data Gets Social and Starts Networking)
Congratulations, you’ve graduated from simple structures like stacks and lists. Now it’s time for the next evolutionary stage — where your data stops living in neat lines and starts forming relationships.
Welcome to Trees, Graphs, and Hash Tables — the power trio that turns your code into a data-driven soap opera. 🎭
🌲 Trees: The Family Drama of Data#
A tree is basically a hierarchy — like your company org chart, your family tree, or that multi-level marketing scheme your cousin keeps texting you about.
Each node holds a value, and points to child nodes — except the topmost one, which is called the root (because everything starts from there, including confusion).
class Node:
def __init__(self, data):
self.data = data
self.left = None
self.right = None
# Build a simple binary tree
root = Node("CEO")
root.left = Node("Manager")
root.right = Node("CFO")
root.left.left = Node("Intern")
Here’s the deal:
Root: The boss. Has no parent.
Leaves: The workers. No children, just coffee.
Branches: The middle managers — connected to everyone, but doing questionable amounts of work.
Common Tree Types:
Binary Tree: Each node has up to two kids. (Work-life balance.)
Binary Search Tree (BST): Left child is smaller, right child is bigger. (Basically, data with a sense of order.)
Heap: The one that always knows who’s the biggest or smallest. (Usually the drama queen.)
Trie: The data structure that helps autocomplete your terrible spelling.
Business analogy: Trees are your corporate hierarchy — efficient for decision-making, but if the CEO (root) falls, everyone panics.
🔗 Graphs: When Data Starts Networking#
Graphs are where things get… social. Forget hierarchy — in a graph, everyone knows everyone else.
Nodes (called vertices) are connected by edges, and those edges can be one-way or two-way.
# Graph represented using a dictionary
graph = {
"Alice": ["Bob", "Claire"],
"Bob": ["Alice", "Dan"],
"Claire": ["Alice", "Dan"],
"Dan": ["Bob", "Claire"]
}
That’s right — Alice and Dan are basically in a LinkedIn loop.
Graphs are the foundation of:
Social networks (Facebook, X, LinkedIn)
Route maps (Google Maps’ secret sauce)
Recommendation systems (“Because you liked cat videos… here’s more cat videos”)
Directed Graph: Arrows matter. Like one-way street friendships. Undirected Graph: Mutual connections. Or “We follow each other now.”
Weighted Graph: Every edge has a cost — like airline ticket prices or emotional baggage.
Business analogy: Graphs are like office politics — everyone’s connected somehow, and sometimes it’s best not to trace the path.
Fun Fact: Shortest-path algorithms like Dijkstra’s help find the quickest route between two nodes — or between your bed and the coffee machine. ☕
🧩 Hash Tables: Organized Chaos#
Now imagine you’re running a huge database of customers, and you want to find “John Smith.” Without a good system, you’ll be scrolling through more Johns than a contact list at a plumber’s convention.
Enter the Hash Table — part dictionary, part sorcery.
It uses a hash function to convert your key (like "John Smith") into a memory address.
In Python, this is literally what a dictionary does.
# Hash table using Python dict
phonebook = {
"Alice": "555-1234",
"Bob": "555-5678",
"Claire": "555-9999"
}
print(phonebook["Bob"]) # 555-5678
Behind the scenes:
A hash function turns "Bob" into a numeric hash, and boom — direct access, no searching.
But beware: Sometimes two keys hash to the same address. That’s called a collision, and it’s handled by fancy techniques like chaining or open addressing. Or, in programmer terms:
“When two people show up with the same name at Starbucks, just add a number to one of them.” ☕
Business analogy: Hash Tables are like super-efficient secretaries. They instantly find what you ask for — unless two people have the same name, then they get creative.
🧮 Comparing the Trio#
Feature |
Tree |
Graph |
Hash Table |
|---|---|---|---|
Structure |
Hierarchical |
Network |
Key–Value pairs |
Main Use |
Searching, sorting |
Relationships, paths |
Fast lookup |
Python Example |
Binary Tree |
Dictionary of lists |
Dictionary |
Analogy |
Family tree |
Social network |
Filing cabinet |
Complexity |
Moderate |
Brain-bending |
Magical |
🧘 Final Thoughts#
Trees teach order. Graphs teach connection. Hash Tables teach that life’s easier when you just hash things out. 😎
So next time you scroll through social media, navigate home, or type in your password — remember: you’re living in a world powered by these three structures. And they’re all silently judging your algorithmic efficiency.
# Your code here