Workstation topics · Data structures in plain words

Lesson 3 of 3 · about 20 minutes

Lookups and trees: find it fast, and things inside things

🏠The everyday picture

To call your aunt, you don't scroll through every number in your phone: you type her name and the number comes up. That's a lookup. And your family's history is a tree: grandparents at the top, their children below, and grandchildren below them. Programs use both shapes constantly.

📘What it really is

A lookup (called a dictionary in Python, an object or Map in JavaScript, a hash map in many other languages) stores key → value pairs:

A tree keeps things inside other things, with one root at the top:

📝A worked example

Sharma Textiles' stock as a lookup:

stock = { "Silk saree": 12, "Cotton saree": 40, "Dupatta": 25 }
stock["Dupatta"]          → 25      (found by its name, straight away)
stock["Silk saree"] = 10  → the old 12 is replaced by 10

And its shop categories as a tree:

Shop
├── Sarees
│   ├── Silk
│   └── Cotton
└── Dress material
    └── Dupattas

"Shop" is the root. "Silk", "Cotton" and "Dupattas" are leaves: they have nothing below them.

🖱️Try it

Draw your own family, or your school or office, as a tree. Who is the root? Which are the leaves? Then write five contacts from your phone as a lookup, name → number. What happens if two contacts have exactly the same name?

✅Quick check

In a lookup, you save "Tea" → 30, then later "Tea" → 18. How many entries for Tea are there?

Show the answer

One. A key appears only once, so the new value 18 replaces 30.

Your computer's folders: Documents holds Shop, and Shop holds Bills. Which is the leaf, if Bills has nothing inside?

Show the answer

Bills. A leaf is an item with nothing below it.

🧩Check yourself

Tap an answer to see why every option is right or wrong. There's no score and nothing is saved, so try as often as you like.

1. An app must find any customer's phone number from their customer ID, among a million customers, very quickly. Which shape fits best?

2. What happens if you store a second value under a key that already exists in a lookup?

3. Which of these is naturally a tree?

← PreviousQueues and stacks: who goes next?
ProLearners Assist