Types of structures

How they relate and where they live · Part of Complexity and data

Key idea

Practice

6 questions · about 3 min

Explained step by step

Linear structure

Logical structure says how data elements relate to each other. In a linear structure, elements go one after another, in a single line: each one has a previous and a next. Arrays, linked lists, stacks and queues are linear. In a nonlinear one, an element can relate to several: trees and heaps form levels, and graphs form networks. It's like the difference between a checkout line and a family tree.

Graph

Nonlinear structures have 2 common forms. In trees, like heaps, the relationship is one-to-many: each element has a single parent and can have several children. In graphs, the relationship is many-to-many: any element can connect with any other. A company org chart is a tree: each employee has 1 boss. Friendships on a social network are a graph: each person has many friends, and those friends have others.

Memory address

When an algorithm runs, the data lives in memory. You can picture it as a giant spreadsheet where each cell holds a fixed amount of data. Each cell has a unique number, its memory address, and the program uses that number to read or write there. Also, memory is shared among all programs: what one uses another can't use at the same time, so how much each algorithm asks for matters.

Contiguous memory

Physical structure says how data is stored in memory. It can go in adjacent cells, one next to the other, as in an array, or scattered anywhere, joined by references, as in a linked list. Contiguous storage lets you jump straight to position 500 with a calculation. Scattered storage lets you add an element without moving the others and use loose gaps of memory. Each form wins at something and loses at something else.

Data structure

All data structures are built with arrays, with linked lists or with a mix of both. A stack or a queue can be made with either. A hash table usually combines both: an array of buckets where each one has a list. Trees, heaps and graphs can also be built with one or the other. That's why arrays and linked lists are the foundation of everything that follows: it's like learning bricks and beams before designing houses.

Dynamic structure

A static structure has a fixed size from the moment it's created: an array of 10 slots has 10 slots, no more, no less. A dynamic structure can change size while the program runs: a linked list can get new nodes hooked on when needed. Even so, an array can grow by asking for a new, bigger block and copying everything, which is what Python or Java lists do under the hood.

Terms in this lesson

Arki, the beaver mascot

Your next interview starts with 5 minutes today.

Free on iOS and Android. Start from zero, no account needed.

Key idea

How they relate and where they live