Hiring Partners
Segilola Resources Operating LimitedTaviatech TechnologiesFCMBMTN NigeriaAdvans La Fayette Microfinance BankAboki AfricaFirst Bank of NigeriaOPay
โ† Certified Racket AI Expert ยท Lesson 5 of 8

Module Four

๐Ÿ“– Every lesson in this course is free to read right here, no account needed. Create a free account to track your progress, take the exam, and earn your certificate.
5
Module 4 ยท Racket AI Specialist โ€“ Data Structures

๐Ÿ—‚๏ธ Module Four ยท Data Structures โ€“ Organising Information

Certified Racket AI Specialist ยท Building Smart Containers

๐Ÿ“– Module Introduction

Hello, young organiser! ๐Ÿ‘‹ In Module Three, we learned to repeat tasks and process lists. But real-world data is often more complex than a simple list. We need better ways to organise information.

Think about a school. You don't just have a list of students โ€” you also have their names, ages, classes, and scores. You need to keep all that related information together. That's where data structures come in.

In this module, we will learn about structures (custom containers for related data), association lists (key-value pairs), and trees (hierarchical data). These tools will help you build smarter, more realistic AI systems.

๐Ÿ“ฆ Fun fact: Data structures are like different types of boxes โ€” some are good for storing books, others for storing toys. Choosing the right box makes everything easier!

๐ŸŽฏ Learning Objectives

After this module, you will be able to:

  • โœ”๏ธ Understand why we need different data structures.
  • โœ”๏ธ Create your own structures using struct.
  • โœ”๏ธ Use association lists to store key-value pairs.
  • โœ”๏ธ Build simple trees to represent hierarchical data.
  • โœ”๏ธ Access and update data inside structures and lists.
  • โœ”๏ธ Choose the right data structure for the job.
  • โœ”๏ธ Build a small AI system that uses multiple structures.

๐Ÿ“š Warm-up Story ยท The Great Library

Imagine you are in charge of a huge library. You have thousands of books. If you just pile them up, nobody can find anything. But if you organise them by category, author, and title, everything becomes easy to find.

In programming, data structures are like your library organisation system. They help you store, find, and use information quickly and correctly.

Today, we will build our own "organisers" โ€” structures to hold related information, dictionaries to look things up, and trees to show relationships. Let's get organised!

   Without structure:
   [book1, book2, book3, ...]  (confusing)

   With structure:
   [ (title "Racket" author "Smith" year 2020),
     (title "AI" author "Jones" year 2021) ]
   (clear and organised)
  

๐Ÿงฉ Main Lessons

Lesson 1 ยท Why Data Structures Matter

Definition: A data structure is a way to store and organise data so that we can use it easily.

Why important: AI systems handle lots of data. Good structures make AI faster and easier to build.

Simple explanation: Think of a drawer. If you throw everything in, it's messy. If you use dividers, you can find things quickly. Data structures are like dividers.

Real-life: A phone book organises names and numbers.

School: A timetable organises subjects by day and time.

Home: A recipe book organises dishes by type (soup, rice, etc.).

Nigerian: A market seller organises goods by type: yams here, tomatoes there.

Illustration:

   Without structure:   [2, "Ade", #t, 5.6, "hello"]
   With structure:      (person name "Ade" age 12)
  
๐Ÿ“Œ Mini summary: Data structures organise information so it's easy to understand and use.

Lesson 2 ยท Structures โ€“ Custom Containers

Definition: A structure (struct) is a custom data type that groups related pieces of information together.

Why important: Structures let you create your own data types, like "Student" or "Book", with named fields.

Simple explanation: It's like a form you fill out: name, age, class. Each person gets their own form.

Real-life: A passport contains name, photo, nationality.

School: A report card contains subject, score, grade.

Home: A gift card contains sender, receiver, amount.

Nigerian: A voter's card contains name, age, polling unit.

Illustration:

   (struct student (name age class))
   (define s1 (student "Ade" 12 "JSS2"))
   (student-name s1) โ†’ "Ade"
   (student-age s1)  โ†’ 12
  
๐Ÿ“Œ Mini summary: struct creates a new type with named fields. Access fields with functions.

Lesson 3 ยท Creating and Using Structures

Let's create a structure for a book: title, author, pages.

   (struct book (title author pages))
   (define b1 (book "Racket for AI" "Dr. Ada" 120))
   (book-title b1)  โ†’ "Racket for AI"
   (book-author b1) โ†’ "Dr. Ada"
   (book-pages b1)  โ†’ 120
  

You can also make lists of structures.

๐Ÿ“Œ Mini summary: Define a structure, then create instances using the constructor (book ...).

Lesson 4 ยท Association Lists โ€“ Lookup Tables

Definition: An association list (alist) is a list of pairs: (key . value). It acts like a dictionary or lookup table.

Why important: AI often needs to look up values by key โ€” like finding a word in a dictionary.

Simple explanation: It's like a label on a box: the label (key) tells you what's inside (value).

Real-life: A dictionary: word โ†’ meaning.

School: A class list: name โ†’ score.

Home: A chore chart: chore โ†’ who does it.

Nigerian: A menu: food โ†’ price.

Illustration:

   (define phone-list '((Ade . 080123) (Bola . 080456)))
   (assoc 'Ade phone-list) โ†’ '(Ade . 080123)
   (cdr (assoc 'Ade phone-list)) โ†’ 080123
  
๐Ÿ“Œ Mini summary: Association lists map keys to values. Use assoc to find a key.

Lesson 5 ยท Working with Association Lists

We can add, find, and update values in alists. Racket provides functions like assoc and alist-update.

๐Ÿ“Œ Mini summary: Association lists are simple and fast for small lookups.

Lesson 6 ยท Trees โ€“ Hierarchical Data

Definition: A tree is a data structure that has a root and branches. Each branch can have more branches. It's used for hierarchical data.

Why important: Many things are hierarchical: family trees, file systems, decision trees in AI.

Simple explanation: It's like a family tree: grandparents at the top, parents below, children below them.

Real-life: A company organisation chart: CEO โ†’ managers โ†’ employees.

School: A school system: Principal โ†’ Head teachers โ†’ Class teachers โ†’ Students.

Home: A wardrobe: clothes โ†’ tops โ†’ shirts.

Nigerian: Traditional title system: Oba โ†’ chiefs โ†’ villagers.

Illustration:

         Root
        /    \
     NodeA   NodeB
     /  \      |
   Leaf1 Leaf2 Leaf3
  
๐Ÿ“Œ Mini summary: Trees represent parent-child relationships.

Lesson 7 ยท Building a Simple Tree in Racket

We can represent a tree using lists: (value children...). For example: (1 (2 (4) (5)) (3 (6)))

   (define tree '(1 (2 (4) (5)) (3 (6))))
   ; tree with root 1, children 2 and 3, etc.
  

We can write functions to traverse trees.

๐Ÿ“Œ Mini summary: Lists can represent trees. We can process them recursively.

Lesson 8 ยท Using Structures for Tree Nodes

Better: use structures to make trees clearer.

   (struct node (value left right))
   (define tree (node 5 (node 3 #f #f) (node 7 #f #f)))
  

This is a binary tree: each node has a value, left child, and right child.

๐Ÿ“Œ Mini summary: Structures make tree nodes easy to understand.

Lesson 9 ยท Traversing a Tree

We can visit every node in a tree (called traversal). There are different orders: pre-order, in-order, post-order.

   (define (in-order tree)
     (if (empty? tree)
         '()
         (append (in-order (node-left tree))
                 (list (node-value tree))
                 (in-order (node-right tree)))))
  
๐Ÿ“Œ Mini summary: Traversal means visiting each node in a specific order.

Lesson 10 ยท When to Use Each Structure

StructureUse whenExample
ListSimple ordered datascores of a game
StructRelated fieldsstudent record
Association listLookup by keydictionary
TreeHierarchical datafamily tree
๐Ÿ“Œ Mini summary: Choose the structure that fits your data and what you want to do.

Lesson 11 ยท Building a Small AI System โ€“ Library Manager

Let's build a system that manages books using structures and association lists.

   (struct book (title author year))
   (define library '())
   (define (add-book library b) (cons b library))
   (define (find-by-author library author)
     (filter (lambda (b) (equal? (book-author b) author)) library))
  

This is a simple AI for managing a library!

๐Ÿ“Œ Mini summary: We combined structures and lists to build a useful system.

Lesson 12 ยท Putting It All Together โ€“ A Student Database

Create a student structure with name, age, and scores. Store them in a list. Write functions to find students by name and to calculate average score.

๐Ÿ“Œ Mini summary: We built a complete mini database โ€” a foundation for AI systems.

๐Ÿ“– Key Vocabulary

  • Data structure: A way to organise data.
  • Struct: A custom container for related fields.
  • Association list (alist): A list of (key . value) pairs.
  • Tree: A hierarchical structure with root and branches.
  • Node: An element in a tree.
  • Traversal: Visiting every node in a tree.
  • Key-value pair: A key that maps to a value.

๐Ÿง  Important Concepts

  • Organisation: Good structures make programs easier to write and understand.
  • Access: Each structure provides ways to get and set data.
  • Hierarchy: Trees show parent-child relationships.
  • Composition: Structures can contain other structures.

๐Ÿ“˜ Step-by-Step Explanations

How to create a structure and use it:

  1. Define the structure: (struct person (name age))
  2. Create an instance: (define p (person "Ada" 12))
  3. Access fields: (person-name p) โ†’ "Ada"
  4. Use in lists: (list (person "Bob" 10) (person "Eve" 11))

๐ŸŒ Real-life Examples

  • Online store: structures for products (name, price, stock).
  • Social media: structures for users (name, posts, friends).
  • Bank: structures for accounts (owner, balance, transactions).

๐Ÿ‡ณ๐Ÿ‡ฌ Nigerian Examples

  • Market inventory: alist for item โ†’ price.
  • School system: structures for students and teachers.
  • Village hierarchy: tree for traditional rulers.

๐ŸŽˆ Fun Examples

  • Struct for a video game character (name, health, power).
  • Alist for pet names โ†’ animal type.
  • Tree for a story: beginning โ†’ middle โ†’ end.

๐Ÿ  Everyday Examples

  • Contacts: alist with name โ†’ phone number.
  • Recipe: struct for ingredients and steps.
  • To-do list: list of tasks with priorities.

๐Ÿง‘โ€๐Ÿซ Teacher Notes

Use physical objects: folders for structs, sticky notes for alists, family trees for trees. Encourage students to think about organising their own belongings. Relate every structure to something tangible.

๐Ÿ‘ช Parent Tips

Ask your child to describe how they organise their room. Relate that to data structures. Encourage them to think about how a library or shop organises items. Practice creating structures for things at home.

๐ŸŒŸ Interesting Facts

  • The first data structures were developed in the 1950s.
  • AI uses trees for decision-making (decision trees).
  • Google uses huge trees to organise web pages.

๐Ÿค” Did You Know?

Racket's struct can automatically generate functions for you: a constructor, field accessors, and even a predicate (like student?).

๐Ÿงพ Remember This

  • Structures group related data.
  • Association lists map keys to values.
  • Trees show hierarchical relationships.
  • Choose the right structure for your data.

โš ๏ธ Common Mistakes

  • Forgetting to use struct before creating an instance.
  • Using assoc on an alist with improper formatting.
  • Confusing tree nodes with lists.
  • Not handling empty trees in recursive functions.

โœ… Best Practices

  • Name structures clearly: student, book, etc.
  • Use empty? to check for empty lists and trees.
  • Write helper functions to access nested data.
  • Document what each field means.

๐Ÿ“Š ASCII Illustrations

   Structure example:
   +------------------------+
   | Student                |
   +------------------------+
   | name: "Ade"            |
   | age: 12                |
   | class: "JSS2"          |
   +------------------------+
  
   Tree example:
         +-----+
         | 10  |  (root)
         +-----+
        /       \
     +---+     +---+
     | 5 |     | 15 |
     +---+     +---+
    /     \         \
  +---+   +---+     +---+
  | 2 |   | 7 |     | 20 |
  +---+   +---+     +---+
  

๐Ÿ“‹ Comparison Table

StructureUseAccess
Listordered itemsfirst, rest
Structnamed fieldsfield functions
Alistlookupassoc
Treehierarchyrecursive traversal

๐Ÿ“Œ End-of-Module Summary

You've learned how to organise data like a pro! You can now create structures, use association lists, and build trees. These tools are essential for any AI system โ€” they help you store, find, and manage information efficiently. You're building the foundation for intelligent data handling.

โ“ Frequently Asked Questions (10)

  1. What is a data structure? โ€” A way to organise data.
  2. Why use struct? โ€” To group related fields.
  3. What is an alist? โ€” A list of key-value pairs.
  4. What is a tree? โ€” A hierarchical structure.
  5. How do I access a struct field? โ€” Use the field accessor.
  6. How do I find a key in an alist? โ€” Use assoc.
  7. How do I traverse a tree? โ€” Recursively visit left and right.
  8. Can I put a struct inside another struct? โ€” Yes, you can nest them.
  9. Are alists fast? โ€” They are fast for small data.
  10. Why are trees important for AI? โ€” AI uses decision trees and hierarchical data.

๐Ÿ“ Review Questions (15)

  1. What is a data structure?
  2. How do you define a struct in Racket?
  3. How do you create an instance of a struct?
  4. What is an association list?
  5. How do you look up a key in an alist?
  6. What is a tree?
  7. How do you represent a binary tree?
  8. What is tree traversal?
  9. Write a struct for a "car" with make, model, year.
  10. Write an alist for a menu (food โ†’ price).
  11. How do you check if an alist has a key?
  12. What is a node in a tree?
  13. Why is hierarchy useful?
  14. Can a tree have more than two children?
  15. Give an example of a tree in real life.

โœ๏ธ Fill-in-the-Blank

  1. A ______ groups related fields together.
  2. An association list stores ______ pairs.
  3. A ______ has a root and branches.
  4. We use ______ to find a key in an alist.
  5. ______ traversal visits every node in a tree.

โœ… True or False

  1. Structures can have any number of fields. (True)
  2. Association lists can only store numbers. (False)
  3. A tree can have only two children. (False โ€” it can have many.)
  4. You can nest a struct inside another struct. (True)
  5. Alists are used for hierarchical data. (False โ€” trees are.)

๐Ÿ”˜ Multiple Choice (15)

  1. Which groups related fields? A) struct B) alist C) tree D) list โ†’ A
  2. Which is a key-value pair list? A) alist B) struct C) tree D) list โ†’ A
  3. Which has a root and branches? A) tree B) struct C) alist D) list โ†’ A
  4. How do you access a struct field? A) field function B) assoc C) first D) rest โ†’ A
  5. How do you find a key in an alist? A) assoc B) struct C) tree D) list โ†’ A
  6. What is a node? A) element in a tree B) field in struct C) key in alist D) item in list โ†’ A
  7. Which is best for a dictionary? A) alist B) struct C) tree D) list โ†’ A
  8. Which is best for a student record? A) struct B) alist C) tree D) list โ†’ A
  9. Which is best for a family tree? A) tree B) struct C) alist D) list โ†’ A
  10. Can a tree have no children? A) yes (leaf) B) no C) only one D) always two โ†’ A
  11. What does (assoc 'a '((a . 1) (b . 2))) return? A) '(a . 1) B) 1 C) #f D) error โ†’ A
  12. What does (struct person (name age)) create? A) a struct type B) a list C) a tree D) an alist โ†’ A
  13. How do you create a person? A) (person "Ade" 12) B) (make-person) C) (struct person) D) (list "Ade" 12) โ†’ A
  14. What is a leaf in a tree? A) a node with no children B) a node with many children C) the root D) a branch โ†’ A
  15. Why use data structures? A) to organise data B) to slow down programs C) to delete data D) to confuse programmers โ†’ A

๐Ÿ”— Matching

TermMatch
1. structA. key-value pairs
2. alistB. hierarchical data
3. treeC. custom fields

Answers: 1-C, 2-A, 3-B

๐Ÿ“ Short Answer

  1. Explain the difference between a struct and an alist.
  2. Why are trees useful for AI?
  3. Write a struct for a "movie" with title, director, year.

๐Ÿ“– Scenario-based Exercises

Scenario: You are building a school management system. You need to store student details (name, age, class) and also a list of their subjects and scores. How would you organise this using data structures?

Answer: Use a struct for student, and inside it, use an alist or another struct for subjects and scores.

๐Ÿ‘ฅ Group Activity

Each group designs a data structure for a "shop inventory" โ€” products with name, price, and quantity. They also create an alist for discounts. Present the design.

๐Ÿง‘โ€๐Ÿ’ป Individual Activity

Write a struct for a "phone" with brand, model, and price. Create a list of three phones and write a function to find the most expensive phone.

๐Ÿ’ฌ Classroom Discussion Questions

  • What data structure would you use for a dictionary?
  • How is a tree like a school system?
  • Why is it important to choose the right structure?

๐Ÿ› ๏ธ Mini Project

Build a "library manager" that stores books (title, author, year) and allows you to add, list, and find books by author.

๐Ÿ“‹ Practical Assignment

Create a student database using structs. Write functions to add a student, find a student by name, and calculate the average age of all students.

๐Ÿ† Challenge Exercise

Write a function that takes a binary tree and returns the sum of all values. (Assume each node has a value and left/right children.)

๐Ÿ”‘ Quiz Answers

Multiple choice answers: 1-A, 2-A, 3-A, 4-A, 5-A, 6-A, 7-A, 8-A, 9-A, 10-A, 11-A, 12-A, 13-A, 14-A, 15-A.

๐ŸŽ Key Takeaways

  • Data structures organise information.
  • Structs group related fields.
  • Alists provide key-value lookup.
  • Trees show hierarchy.
  • Choose the right structure for your problem.

๐Ÿš€ Preparation for the Next Module

In Module Five, we will dive into recursion on trees and more advanced data processing. You'll learn to manipulate trees, build decision trees for AI, and combine structures with recursion. Keep practising with structures and lists โ€” they will be your main tools!


๐ŸŽ‰ You've completed Module Four ยท You are now a data structure master! ๐ŸŽ‰

๐Ÿ† Get Certified

๐Ÿ”’

Earn this certificate

Every lesson is already free to read. Sign up, finish the lessons, pass the exam (โ‚ฆ2,000 or 20,000 coins), and get a verified certificate with your name on it.

๐ŸŽ“ Sign Up to Get Certified
๐Ÿ› ๏ธ Practice Tools
Hands-on simulators & labs - subscription required.
โ†’
๐ŸŽฏ Internship Tasks
Real-world tasks to build your portfolio - unlock them with a subscription.
โ†’