15
Lesson 25 of 44

Data structures

Learn to organise information with the classic structures of programming. In this lesson you will build and use stacks, queues and deque, linked lists, binary search trees, graphs with breadth-first and depth-first traversals, and priority queues with heapq, and you will learn to choose the right structure for each problem.

  • 5 theory articles
  • 10 practice exercises
Start lesson

Fundamentals concepts 5

Stacks and queues

A data structure is a way of organising information so that certain operations are easy and fast. You already know lists…

Theory
Read more

Linked lists

A Python list stores the elements one next to another. A linked list does something different: each element lives in a node that…

Theory
Read more

Binary search trees

A tree is a structure of nodes organised hierarchically: there is a root node and each node can have children . They appear…

Theory
Read more

Graphs and traversals

A graph is a set of nodes (vertices) joined by edges . It models networks of all kinds: road maps, friendships in a social…

Theory
Read more

Priority queues and heapq

In a normal queue the one who arrived first goes first. In a priority queue the one with the highest priority goes first, even if…

Theory
Read more

Practice exercises 10

Balanced brackets

Create a Python program that checks whether the parentheses, square brackets and curly braces of a text are correctly matched. It…

Exercise Medium
Solve it

Postfix calculator

Create a Python program that evaluates expressions in postfix notation (RPN), where operators come after the operands. They are…

Exercise Medium
Solve it

The Josephus problem

Create a Python program that solves the Josephus problem with a double-ended queue ( deque ): n people stand in a circle and one…

Exercise Medium
Solve it

Browser history

Create a Python program that simulates the back and forward buttons of a browser using two stacks .

Exercise Hard
Solve it

Linked list

Create a Python program that implements a singly linked list with a Node class (value and next) and a LinkedList class (head)…

Exercise Hard
Solve it

Merge sorted lists

Create a Python program that merges two sorted linked lists into a single sorted list by reusing the nodes, without creating an…

Exercise Hard
Solve it

Binary search tree

Create a Python program that builds a binary search tree (BST) and queries it. In a BST, values smaller than a node go to its…

Exercise Hard
Solve it

Shortest path in a graph

Create a Python program that finds the shortest path (fewest edges) between two nodes of an undirected graph using a…

Exercise Hard
Solve it

Connected components

Create a Python program that counts the connected components of an undirected graph (groups of nodes that can reach each other)…

Exercise Hard
Solve it

Task priority queue

Create a Python program that manages a priority queue of tasks with the heapq module: the task with the lowest priority number…

Exercise Hard
Solve it