Chapter 5

Graphs and Trees

IN THIS CHAPTER

Bullet Understanding and using graphs

Bullet Creating different types of trees

Bullet Manipulating a tree

Most data structures (such as arrays, dictionaries, and queues) store data in a linear format with one chunk of data neatly sandwiched in between exactly two other chunks of data. Linear data structures can be fine for just storing data, but what if you want a data structure that can model a real-life problem?

Picking the right data structure to model a real-life problem can greatly simplify programming. One advantage of a queue is that it closely mimics a line of people (or orders on a website) that need to be handled one at a time, starting with the oldest item. Using a queue data structure to handle incoming orders from a website makes logical sense, but using a dictionary or even an array makes less sense because dictionaries require keys assigned to each item (which isn’t needed) and arrays need to be resized constantly.

So, if you have a problem that doesn’t mimic a linear data structure, using a linear data structure can just make programming harder, much like trying to use a screwdriver to pound in a nail when you really need a hammer.

For example, ...

Get Beginning Programming All-in-One For Dummies, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.