100% found this document useful (4 votes)
315 views

Complete Download Data Structures & Algorithms in Python John Canning PDF All Chapters

John

Uploaded by

kalkenankajt
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (4 votes)
315 views

Complete Download Data Structures & Algorithms in Python John Canning PDF All Chapters

John

Uploaded by

kalkenankajt
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 55

Download the Full Version of textbook for Fast Typing at textbookfull.

com

Data Structures & Algorithms in Python John


Canning

https://textbookfull.com/product/data-structures-algorithms-
in-python-john-canning/

OR CLICK BUTTON

DOWNLOAD NOW

Download More textbook Instantly Today - Get Yours Now at textbookfull.com


Recommended digital products (PDF, EPUB, MOBI) that
you can download immediately if you are interested.

Data Structures and Algorithms in Swift Kevin Lau

https://textbookfull.com/product/data-structures-and-algorithms-in-
swift-kevin-lau/

textboxfull.com

Data Structures Algorithms in Kotlin Implementing


Practical Data Structures in Kotlin 1st Edition
Raywenderlich Tutorial Team
https://textbookfull.com/product/data-structures-algorithms-in-kotlin-
implementing-practical-data-structures-in-kotlin-1st-edition-
raywenderlich-tutorial-team/
textboxfull.com

Data Structures Algorithms in Go 1st Edition Hemant Jain

https://textbookfull.com/product/data-structures-algorithms-in-go-1st-
edition-hemant-jain/

textboxfull.com

Probabilistic data structures and algorithms for big data


applications Gakhov

https://textbookfull.com/product/probabilistic-data-structures-and-
algorithms-for-big-data-applications-gakhov/

textboxfull.com
Algorithms and Data Structures in Action MEAP V12 Marcello
La Rocca

https://textbookfull.com/product/algorithms-and-data-structures-in-
action-meap-v12-marcello-la-rocca/

textboxfull.com

Problem Solving in Data Structures & Algorithms Using C


First Edition Jain

https://textbookfull.com/product/problem-solving-in-data-structures-
algorithms-using-c-first-edition-jain/

textboxfull.com

Learning functional data structures and algorithms learn


functional data structures and algorithms for your
applications and bring their benefits to your work now
Khot
https://textbookfull.com/product/learning-functional-data-structures-
and-algorithms-learn-functional-data-structures-and-algorithms-for-
your-applications-and-bring-their-benefits-to-your-work-now-khot/
textboxfull.com

Fundamentals Of Python: Data Structures 2nd Edition


Kenneth A. Lambert

https://textbookfull.com/product/fundamentals-of-python-data-
structures-2nd-edition-kenneth-a-lambert/

textboxfull.com

Data Structures and Algorithms with the C++ STL: A guide


for modern C++ practitioners 1 / converted Edition John
Farrier
https://textbookfull.com/product/data-structures-and-algorithms-with-
the-c-stl-a-guide-for-modern-c-practitioners-1-converted-edition-john-
farrier/
textboxfull.com
John Canning
Alan Broder
Robert Lafore

Data
Structures &
Algorithms
in Python

Boston • Columbus • New York • San Francisco • Amsterdam • Cape Town


Dubai • London • Madrid • Milan • Munich • Paris • Montreal • Toronto • Delhi • Mexico City
São Paulo • Sydney • Hong Kong • Seoul • Singapore • Taipei • Tokyo

9780134855684.indb 1 05/08/22 1:54 PM


Many of the designations used by manufacturers and sellers to distinguish their Editor-in-Chief
products are claimed as trademarks. Where those designations appear in this book, and Mark Taub
the publisher was aware of a trademark claim, the designations have been printed with
initial capital letters or in all capitals. Director, ITP Product
The authors and publisher have taken care in the preparation of this book, but make Management
no expressed or implied warranty of any kind and assume no responsibility for errors or Brett Bartow
omissions. No liability is assumed for incidental or consequential damages in connec-
tion with or arising out of the use of the information or programs contained herein. Acquisitions Editor
For information about buying this title in bulk quantities, or for special sales opportuni- Kim Spenceley
ties (which may include electronic versions; custom cover designs; and content particu-
Development Editor
lar to your business, training goals, marketing focus, or branding interests), please
contact our corporate sales department at corpsales@pearsoned.com or Chris Zahn
(800) 382-3419.
Managing Editor
For government sales inquiries, please contact governmentsales@pearsoned.com. Sandra Schroeder
For questions about sales outside the U.S., please contact intlcs@pearson.com.
Project Editor
Visit us on the Web: informit.com/aw
Mandie Frank
Library of Congress Control Number: 2022910068
Copyright © 2023 Pearson Education, Inc. Copy Editor
All rights reserved. This publication is protected by copyright, and permission must be Chuck Hutchinson
obtained from the publisher prior to any prohibited reproduction, storage in a retrieval
Indexer
system, or transmission in any form or by any means, electronic, mechanical, photo-
copying, recording, or likewise. For information regarding permissions, request forms, Cheryl Lenser
and the appropriate contacts within the Pearson Education Global Rights & Permissions
Proofreader
Department, please visit www.pearson.com/permissions.
Barbara Mack
No patent liability is assumed with respect to the use of the information contained
herein. Although every precaution has been taken in the preparation of this book, the Editorial Assistant
publisher and author assume no responsibility for errors or omissions. Nor is any liability Cindy Teeters
assumed for damages resulting from the use of the information contained herein.
ISBN-13: 978-0-13-485568-4 Designer
ISBN-10: 0-13-485568-X Chuti Prasertsith
ScoutAutomatedPrintCode Compositor
codeMantra

9780134855684.indb 2 05/08/22 1:54 PM


Contents at a Glance

1 Overview. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
2 Arrays. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
3 Simple Sorting. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
4 Stacks and Queues. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103
5 Linked Lists. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157
6 Recursion. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 229
7 Advanced Sorting.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 285
8 Binary Trees. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 335
9 2-3-4 Trees and External Storage. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 401
10 AVL and Red-Black Trees.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 463
11 Hash Tables. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 525
12 Spatial Data Structures.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 597
13 Heaps. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 665
14 Graphs.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 705
15 Weighted Graphs. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 767
16 What to Use and Why.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 813

Appendixes
A Running the Visualizations. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 833
B Further Reading. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 841
C Answers to Questions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 845

Index. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 859

9780134855684.indb 3 05/08/22 1:54 PM


Table of Contents

1 Overview 1
What Are Data Structures and Algorithms?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
Overview of Data Structures.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
Overview of Algorithms. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
Some Definitions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
Database. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
Record. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
Field. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
Key. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
Databases vs. Data Structures. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
Programming in Python. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
Interpreter. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
Dynamic Typing. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
Sequences. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
Looping and Iteration. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
Multivalued Assignment.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
Importing Modules. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
Functions and Subroutines. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
List Comprehensions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
Exceptions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
Object-Oriented Programming. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
Questions.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
Experiments. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28

2 Arrays 29
The Array Visualization Tool. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
Searching. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
The Duplicates Issue. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
Using Python Lists to Implement the Array Class. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
Creating an Array. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
Accessing List Elements. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
A Better Array Class Implementation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
The OrderedArray Visualization Tool. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
Linear Search. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
Binary Search. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48

9780134855684.indb 4 05/08/22 1:54 PM


Python Code for an OrderedArray Class. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
Binary Search with the find() Method.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
The OrderedArray Class.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
Advantages of Ordered Arrays. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
Logarithms.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58
The Equation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
The Opposite of Raising 2 to a Power. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60
Storing Objects. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60
The OrderedRecordArray Class. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
Big O Notation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
Insertion in an Unordered Array: Constant. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66
Linear Search: Proportional to N. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66
Binary Search: Proportional to log(N). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
Don’t Need the Constant. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
Why Not Use Arrays for Everything?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
Questions.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70
Experiments. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72
Programming Projects. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73

3 Simple Sorting 75
How Would You Do It?.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76
Bubble Sort. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77
Bubble Sort on the Football Players. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77
The SimpleSorting Visualization Tool. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79
Python Code for a Bubble Sort.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81
Invariants. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82
Efficiency of the Bubble Sort.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82
Selection Sort. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83
Selection Sort on the Football Players. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83
A Brief Description. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83
A More Detailed Description.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83
The Selection Sort in the SimpleSorting Visualization Tool. . . . . . . . . . . . . 85
Python Code for Selection Sort. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85
Invariant. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86
Efficiency of the Selection Sort.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86
Insertion Sort. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
Insertion Sort on the Football Players. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
Partial Sorting. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
The Marked Player. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
The Insertion Sort in the SimpleSorting Visualization Tool. . . . . . . . . . . . . 89
Python Code for Insertion Sort. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90

9780134855684.indb 5 05/08/22 1:54 PM


vi Data Structures & Algorithms in Python

Invariants in the Insertion Sort. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91


Efficiency of the Insertion Sort.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
Python Code for Sorting Arrays. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
Stability. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96
Comparing the Simple Sorts. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96
Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98
Questions.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98
Experiments. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100
Programming Projects. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101

4 Stacks and Queues 103


Different Structures for Different Use Cases. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103
Storage and Retrieval Pattern. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103
Restricted Access.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104
More Abstract.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104
Stacks. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104
The Postal Analogy. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105
The Stack Visualization Tool. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106
Python Code for a Stack. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108
Stack Example 1: Reversing a Word. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112
Stack Example 2: Delimiter Matching. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113
Efficiency of Stacks. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116
Queues. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116
A Shifty Problem. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117
A Circular Queue. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118
The Queue Visualization Tool. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119
Python Code for a Queue. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120
Efficiency of Queues. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125
Deques. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125
Priority Queues.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126
The PriorityQueue Visualization Tool. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127
Python Code for a Priority Queue. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129
Efficiency of Priority Queues.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132
What About Search and Traversal?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132
Parsing Arithmetic Expressions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132
Postfix Notation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133
Translating Infix to Postfix. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134
The InfixCalculator Tool. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142
Evaluating Postfix Expressions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148
Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151
Questions.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152
Experiments. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154
Programming Projects. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155

9780134855684.indb 6 05/08/22 1:54 PM


Contents vii

5 Linked Lists 157


Links. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158
References and Basic Types. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 160
Relationship, Not Position. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 164
The LinkedList Visualization Tool. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 164
The Search Button. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 166
The Delete Button. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 166
The New Button. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167
The Other Buttons. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167
A Simple Linked List. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167
The Basic Linked List Methods. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168
Traversing Linked Lists. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 169
Insertion and Search in Linked Lists. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 170
Deletion in Linked Lists. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 174
Double-Ended Lists. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177
Linked List Efficiency. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183
Abstract Data Types and Objects. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 184
A Stack Implemented by a Linked List. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 184
A Queue Implemented by a Linked List. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187
Data Types and Abstraction. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189
ADT Lists. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 191
ADTs as a Design Tool. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 191
Ordered Lists. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 192
Python Code for Ordered Lists. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193
Efficiency of Ordered Linked Lists. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 198
List Insertion Sort. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 198
Doubly Linked Lists. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 198
Insertion and Deletion at the Ends. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201
Insertion and Deletion in the Middle. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 204
Doubly Linked List as Basis for Deques. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 208
Circular Lists. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 209
Iterators. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 211
Basic Iterator Methods. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 212
Other Iterator Methods. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 216
Iterators in Python. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 217
Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 222
Questions.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 224
Experiments. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 226
Programming Projects. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 227

6 Recursion 229
Triangular Numbers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 230
Finding the nth Term Using a Loop. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 231

9780134855684.indb 7 05/08/22 1:54 PM


viii Data Structures & Algorithms in Python

Finding the nth Term Using Recursion.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 232


What’s Really Happening?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 234
Characteristics of Recursive Routines. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 235
Is Recursion Efficient?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 236
Mathematical Induction.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 237
Factorials. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 237
Anagrams.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 239
A Recursive Binary Search. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 242
Recursion Replaces the Loop.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 243
Divide-and-Conquer Algorithms. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 245
The Tower of Hanoi. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 245
The TowerofHanoi Visualization Tool. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 246
Moving Pyramids. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 247
The Recursive Implementation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 250
Sorting with mergesort. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 255
Merging Two Sorted Arrays. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 255
Sorting by Merging. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 257
Merging Subranges. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 260
Testing the Code. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 262
The Mergesort Visualization Tool. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 263
Efficiency of the mergesort. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 264
Eliminating Recursion. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 267
Recursion and Stacks.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 267
Simulating a Recursive Function: Triangular. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 268
Rewriting a Recursive Procedure: mergesort.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 270
Some Interesting Recursive Applications. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 275
Raising a Number to a Power. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 275
The Knapsack Problem. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 277
Combinations: Picking a Team.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 278
Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 280
Questions.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 281
Experiments. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 283
Programming Projects. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 283

7 Advanced Sorting 285


Shellsort.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 285
Insertion Sort: Too Many Copies. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 286
N-Sorting. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 286
Diminishing Gaps. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 288
The AdvancedSorting Visualization Tool. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 289

9780134855684.indb 8 05/08/22 1:54 PM


Contents ix

Python Code for the Shellsort.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 291


Other Interval Sequences.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 293
Efficiency of the Shellsort. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 294
Partitioning. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 294
The Partition Process.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 295
The General Partitioning Algorithm. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 297
Efficiency of the Partition Algorithm. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 301
Quicksort. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 302
The Basic Quicksort Algorithm.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 302
Choosing a Pivot Value. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 304
A First Quicksort Implementation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 306
Running Quicksort in the AdvancedSorting Visualization Tool. . . . . . 309
The Details.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 310
Degenerates to O(N2) Performance. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 312
Median-of-Three Partitioning.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 313
Handling Small Partitions.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 315
The Full Quicksort Implementation.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 315
Removing Recursion. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 318
Efficiency of Quicksort. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 318
Radix Sort. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 320
Algorithm for the Radix Sort.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 321
Designing a Radix Sort Program. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 321
Efficiency of the Radix Sort. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 322
Generalizing the Radix Sort. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 322
Using a Counting Sort. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 323
Timsort. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 324
Efficiency of Timsort. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 327
Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 327
Questions.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 329
Experiments. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 331
Programming Projects. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 332

8 Binary Trees 335


Why Use Binary Trees?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 335
Slow Insertion in an Ordered Array. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 335
Slow Searching in a Linked List. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 336
Trees to the Rescue. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 336
What Is a Tree?.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 336
Tree Terminology. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 337
Root. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 338
Path. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 338
Parent.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 338

9780134855684.indb 9 05/08/22 1:54 PM


x Data Structures & Algorithms in Python

Child.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 338
Sibling. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 339
Leaf. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 339
Subtree. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 339
Visiting.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 339
Traversing. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 339
Levels. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 339
Keys. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 339
Binary Trees. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 339
Binary Search Trees. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 340
An Analogy. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 340
How Do Binary Search Trees Work?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 341
The Binary Search Tree Visualization Tool. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 341
Representing the Tree in Python Code.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 344
Finding a Node. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 346
Using the Visualization Tool to Find a Node. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 346
Python Code for Finding a Node.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 348
Tree Efficiency. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 350
Inserting a Node. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 350
Using the Visualization Tool to Insert a Node.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 351
Python Code for Inserting a Node. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 352
Traversing the Tree.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 353
In-order Traversal. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 353
Pre-order and Post-order Traversals. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 355
Python Code for Traversing. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 356
Traversing with the Visualization Tool. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 361
Traversal Order. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 363
Finding Minimum and Maximum Key Values. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 365
Deleting a Node.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 366
Case 1: The Node to Be Deleted Has No Children. . . . . . . . . . . . . . . . . . . . . . . . . 367
Case 2: The Node to Be Deleted Has One Child. . . . . . . . . . . . . . . . . . . . . . . . . . . . 367
Case 3: The Node to Be Deleted Has Two Children. . . . . . . . . . . . . . . . . . . . . . . 370
The Efficiency of Binary Search Trees. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 375
Trees Represented as Arrays.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 377
Tree Levels and Size. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 378
Printing Trees. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 379
Duplicate Keys.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 381
The BinarySearchTreeTester.py Program. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 382
The Huffman Code. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 386
Character Codes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 386
Decoding with the Huffman Tree. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 388
Creating the Huffman Tree. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 389

9780134855684.indb 10 05/08/22 1:54 PM


Contents xi

Coding the Message. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 391


Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 393
Questions.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 394
Experiments. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 396
Programming Projects. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 397

9 2-3-4 Trees and External Storage 401


Introduction to 2-3-4 Trees. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 401
What’s in a Name?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 402
2-3-4 Tree Terminology. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 403
2-3-4 Tree Organization.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 403
Searching a 2-3-4 Tree. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 404
Insertion. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 404
Node Splits. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 405
Splitting the Root. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 406
Splitting on the Way Down. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 407
The Tree234 Visualization Tool. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 408
The Random Fill and New Tree Buttons. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 409
The Search Button. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 409
The Insert Button. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 409
Zooming and Scrolling. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 410
Experiments. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 411
Python Code for a 2-3-4 Tree. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 412
The __Node Class. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 412
The Tree234 Class. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 415
Traversal. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 421
Deletion. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 423
Efficiency of 2-3-4 Trees.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 430
Speed. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 431
Storage Requirements.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 432
2-3 Trees. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 432
Node Splits. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 433
Promoting Splits to Internal Nodes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 435
Implementation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 437
Efficiency of 2-3 Trees. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 438
External Storage.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 438
Accessing External Data. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 439
Sequential Ordering. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 442
B-Trees.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 444
Indexing. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 450
Complex Search Criteria.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 452
Sorting External Files. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 453

9780134855684.indb 11 05/08/22 1:54 PM


xii Data Structures & Algorithms in Python

Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 456
Questions.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 458
Experiments. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 459
Programming Projects. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 460

10 AVL and Red-Black Trees 463


Our Approach to the Discussion.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 463
Balanced and Unbalanced Trees. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 464
Degenerates to O(N). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 464
Measuring Tree Balance. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 465
How Much Is Unbalanced?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 468
AVL Trees. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 470
The AVLTree Visualization Tool. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 471
Inserting Items with the AVLTree Visualization Tool. . . . . . . . . . . . . . . . . . . . . 472
Python Code for the AVL Tree. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 474
The Efficiency of AVL Trees.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 486
Red-Black Trees. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 487
Conceptual. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 487
Top-Down Insertion.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 487
Bottom-Up Insertion. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 488
Red-Black Tree Characteristics. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 488
Using the Red-Black Tree Visualization Tool. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 489
Flipping a Node’s Color. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 490
Rotating Nodes.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 491
The Insert Button. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 492
The Search Button. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 492
The Delete Button. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 492
The Erase & Random Fill Button. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 492
Experimenting with the Visualization Tool.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 492
Experiment 1: Inserting Two Red Nodes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 493
Experiment 2: Rotations. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 493
Experiment 3: Color Swaps. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 494
Experiment 4: An Unbalanced Tree. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 495
More Experiments. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 496
The Red-Black Rules and Balanced Trees. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 496
Null Children. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 496
Rotations in Red-Black Trees. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 497
Subtrees on the Move. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 497
Inserting a New Node.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 498
Preview of the Insertion Process. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 499
Color Swaps on the Way Down.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 499
Rotations After the Node Is Inserted. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 501

9780134855684.indb 12 05/08/22 1:54 PM


Contents xiii

Rotations on the Way Down.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 505


Deletion.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 508
The Efficiency of Red-Black Trees.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 509
2-3-4 Trees and Red-Black Trees. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 510
Transformation from 2-3-4 to Red-Black. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 510
Operational Equivalence. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 512
Red-Black Tree Implementation.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 514
Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 515
Questions.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 517
Experiments. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 520
Programming Projects. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 521

11 Hash Tables 525


Introduction to Hashing.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 526
Bank Account Numbers as Keys. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 526
A Dictionary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 527
Hashing. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 530
Collisions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 533
Open Addressing. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 536
Linear Probing.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 536
Python Code for Open Addressing Hash Tables. . . . . . . . . . . . . . . . . . . . . . . . . . . . 544
Quadratic Probing. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 554
Double Hashing. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 559
Separate Chaining.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 565
The HashTableChaining Visualization Tool. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 566
Python Code for Separate Chaining. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 569
Hash Functions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 575
Quick Computation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 575
Random Keys. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 575
Nonrandom Keys.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 576
Hashing Strings. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 578
Folding. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 580
Hashing Efficiency. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 581
Open Addressing. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 581
Separate Chaining. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 583
Open Addressing Versus Separate Chaining.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 587
Hashing and External Storage. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 588
Table of File Pointers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 588
Nonfull Blocks. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 588
Full Blocks. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 589

9780134855684.indb 13 05/08/22 1:54 PM


xiv Data Structures & Algorithms in Python

Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 590
Questions.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 592
Experiments. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 594
Programming Projects. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 595

12 Spatial Data Structures 597


Spatial Data.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 597
Cartesian Coordinates. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 597
Geographic Coordinates. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 598
Computing Distances Between Points. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 599
Distance Between Cartesian Coordinates. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 599
Circles and Bounding Boxes.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 601
Clarifying Distances and Circles. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 601
Bounding Boxes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 603
The Bounding Box of a Query Circle in Cartesian Coordinates. . . . . . 603
The Bounding Box of a Query Circle in Geographic
Coordinates. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 604
Implementing Bounding Boxes in Python. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 605
The CircleBounds Subclass.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 607
Determining Whether Two Bounds Objects Intersect. . . . . . . . . . . . . . . . . . . . 609
Determining Whether One Bounds Object Lies Entirely Within
Another. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 610
Searching Spatial Data. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 611
Lists of Points. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 612
Creating an Instance of the PointList Class. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 612
Inserting Points. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 613
Finding an Exact Match. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 614
Deleting a Point. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 614
Traversing the Points. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 615
Finding the Nearest Match. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 615
Grids. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 617
Implementing a Grid in Python. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 618
Creating an Instance of the Grid Class.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 619
Inserting Points. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 620
Finding an Exact Match. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 621
Big O and Practical Considerations.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 622
Deleting and Traversing. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 623
Finding the Nearest Match. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 624
Does the Query Circle Fall Within a Layer?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 625
Does the Query Circle Intersect a Grid Cell?.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 628
Generating the Sequence of Neighboring Cells to Visit. . . . . . . . . . . . . . . . . 629

9780134855684.indb 14 05/08/22 1:54 PM


Contents xv

Pulling It All Together: Implementing Grid’s findNearest(). . . . . . . . . . . . 630


Quadtrees.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 633
Creating an Instance of the QuadTree Class. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 635
Inserting Points: A Conceptual Overview. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 636
Avoiding Ambiguity.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 638
The QuadTree Visualization Tool. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 639
Implementing Quadtrees: The Node Class.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 640
The insert Method. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 641
Efficiency of Insertion. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 644
Finding an Exact Match. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 644
Efficiency of Exact Search. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 645
Traversing the Points. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 645
Deleting a Point. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 646
Finding the Nearest Match. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 647
Finding a Candidate Node. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 648
Finding the Closest Node. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 649
Pulling It All Together: Implementing QuadTree’s findNearest(). . . . . 652
Efficiency of findNearest(). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 655
Theoretical Performance and Optimizations. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 656
Practical Considerations. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 656
Further Extensions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 658
Other Operations.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 658
Higher Dimensions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 658
Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 659
Questions.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 661
Experiments. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 662
Programming Projects. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 663

13 Heaps 665
Introduction to Heaps. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 666
Priority Queues, Heaps, and ADTs. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 667
Partially Ordered. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 668
Insertion. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 669
Removal. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 670
Other Operations.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 674
The Heap Visualization Tool. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 674
The Insert Button. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 675
The Make Random Heap Button. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 676
The Erase and Random Fill Button. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 676
The Peek Button. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 677
The Remove Max Button. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 677
The Heapify Button. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 677

9780134855684.indb 15 05/08/22 1:54 PM


xvi Data Structures & Algorithms in Python

The Traverse Button. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 677


Python Code for Heaps. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 677
Insertion. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 679
Removal. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 680
Traversal. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 682
Efficiency of Heap Operations.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 683
A Tree-Based Heap. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 684
Heapsort. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 686
Sifting Down Instead of Up. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 686
Using the Same Array.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 688
The heapsort() Subroutine. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 691
The Efficiency of Heapsort.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 693
Order Statistics. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 694
Partial Ordering Assists in Finding the Extreme Values. . . . . . . . . . . . . . . . . . 695
The Efficiency of K Highest. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 696
Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 700
Questions.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 701
Experiments. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 703
Programming Projects. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 703

14 Graphs 705
Introduction to Graphs. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 705
Definitions.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 706
The First Uses of Graphs.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 708
Representing a Graph in a Program. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 709
Adding Vertices and Edges to a Graph. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 713
The Graph Class. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 715
Traversal and Search.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 718
Depth-First.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 719
Breadth-First. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 729
Minimum Spanning Trees.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 734
Minimum Spanning Trees in the Graph Visualization Tool. . . . . . . . . . . . 735
Trees Within a Graph. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 736
Python Code for the Minimum Spanning Tree. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 737
Topological Sorting. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 740
Dependency Relationships.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 741
Directed Graphs. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 742
Sorting Directed Graphs. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 742
The Graph Visualization Tool. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 743
The Topological Sorting Algorithm. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 744
Cycles and Trees. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 745
Python Code for the Basic Topological Sort. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 746

9780134855684.indb 16 05/08/22 1:54 PM


Contents xvii

Improving the Topological Sort.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 748


Connectivity in Directed Graphs. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 753
The Connectivity Matrix. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 753
Transitive Closure and Warshall’s Algorithm. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 753
Implementation of Warshall’s Algorithm. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 758
Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 759
Questions.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 760
Experiments. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 762
Programming Projects. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 763

15 Weighted Graphs 767


Minimum Spanning Tree with Weighted Graphs. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 767
An Example: Networking in the Jungle. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 768
The WeightedGraph Visualization Tool.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 768
Building the Minimum Spanning Tree: Send Out the
Surveyors. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 770
Creating the Algorithm. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 775
The Shortest-Path Problem. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 783
Travel by Rail. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 783
Dijkstra’s Algorithm. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 784
Agents and Train Rides. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 784
Finding Shortest Paths Using the Visualization Tool.. . . . . . . . . . . . . . . . . . . . . 790
Implementing the Algorithm. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 794
Python Code. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 795
The All-Pairs Shortest-Path Problem.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 797
Efficiency. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 800
Intractable Problems. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 801
The Knight’s Tour. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 802
The Traveling Salesperson Problem.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 802
Hamiltonian Paths and Cycles. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 803
Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 805
Questions.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 806
Experiments. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 808
Programming Projects. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 809

16 What to Use and Why 813


Analyzing the Problem. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 814
What Kind of Data?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 814
How Much Data?.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 815
What Operations and How Frequent?.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 816
Who Will Maintain the Software?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 817
Foundational Data Structures. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 818

9780134855684.indb 17 05/08/22 1:54 PM


xviii Data Structures & Algorithms in Python

Speed and Algorithms. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 819


Libraries.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 820
Arrays. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 820
Linked Lists.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 821
Binary Search Trees. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 822
Balanced Search Trees. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 822
Hash Tables. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 823
Comparing the General-Purpose Storage Structures.. . . . . . . . . . . . . . . . . . . . . . 824
Special-Ordering Data Structures. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 824
Stack. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 825
Queue. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 825
Priority Queue. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 826
Comparison of Special-Ordering Structures. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 826
Sorting.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 826
Specialty Data Structures. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 828
Quadtrees and Grids. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 828
Graphs. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 828
External Storage.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 829
Sequential Storage. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 829
Indexed Files. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 829
B-trees. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 830
Hashing. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 830
Choosing Among External Storage Types. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 830
Virtual Memory. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 830
Onward. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 831

Appendixes

A Running the Visualizations 833


For Developers: Running and Changing the Visualizations. . . . . . . . . . . . . . . . . . . . 834
Getting Python. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 834
Getting Git. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 835
Getting the Visualizations. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 835
For Managers: Downloading and Running the Visualizations. . . . . . . . . . . . . . . . . 836
For Others: Viewing the Visualizations on the Internet. . . . . . . . . . . . . . . . . . . . . . . . . 837
Using the Visualizations. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 838

B Further Reading 841


Data Structures and Algorithms.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 841

9780134855684.indb 18 05/08/22 1:54 PM


Contents xix

Object-Oriented Programming Languages. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 842


Object-Oriented Design (OOD) and Software Engineering. . . . . . . . . . . . . . . . . . . . . 842

C Answers to Questions 845


Chapter 1, "Overview". . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 845
Chapter 2, "Arrays". . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 846
Chapter 3, "Simple Sorting". . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 847
Chapter 4, "Stacks and Queues". . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 848
Chapter 5, "Linked Lists". . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 848
Chapter 6, "Recursion". . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 849
Chapter 7, "Advanced Sorting". . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 850
Chapter 8, "Binary Trees". . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 851
Chapter 9, "2-3-4 Trees and External Storage". . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 851
Chapter 10, "AVL and Red-Black Trees". . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 852
Chapter 11, "Hash Tables".. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 853
Chapter 12, "Spatial Data Structures". . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 853
Chapter 13, "Heaps". . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 854
Chapter 14, "Graphs". . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 855
Chapter 15, "Weighted Graphs".. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 856

Index 859

9780134855684.indb 19 05/08/22 1:54 PM


Pearson’s Commitment to Diversity,
Equity, and Inclusion
Pearson is dedicated to creating bias-free content that reflects the diversity of all learners.
We embrace the many dimensions of diversity, including but not limited to race, ethnic-
ity, gender, socioeconomic status, ability, age, sexual orientation, and religious or political
beliefs.

Education is a powerful force for equity and change in our world. It has the potential to
deliver opportunities that improve lives and enable economic mobility. As we work with
authors to create content for every product and service, we acknowledge our responsibil-
ity to demonstrate inclusivity and incorporate diverse scholarship so that everyone can
achieve their potential through learning. As the world’s leading learning company, we
have a duty to help drive change and live up to our purpose to help more people create a
better life for themselves and to create a better world.

Our ambition is to purposefully contribute to a world where

▶▶ Everyone has an equitable and lifelong opportunity to succeed through learning

▶▶ Our educational products and services are inclusive and represent the rich diversity
of learners

▶▶ Our educational content accurately reflects the histories and experiences of the
learners we serve

▶▶ Our educational content prompts deeper discussions with learners and motivates
them to expand their own learning (and worldview)

While we work hard to present unbiased content, we want to hear from you about any
concerns or needs with this Pearson product so that we can investigate and address them.

Please contact us with concerns about any potential bias at https://www.pearson.com/


report-bias.html.

9780134855684.indb 20 05/08/22 1:54 PM


To my mother, who gave me a thirst for knowledge,
to my father, who taught me the joys of engineering,
and to June, who made it possible to pursue both.

John Canning

For my father Sol Broder, computer science pioneer,


for leading the way.
To my mother Marilyn Broder, master educator,
for inspiring me to teach.
To Fran, for making my life complete.

Alan Broder

9780134855684.indb 21 05/08/22 1:54 PM


Register your copy of Data Structures & Algorithms in Python at informit.com for conve-
nient access to downloads, updates, and corrections as they become available.​To start the
registration process, go to informit.com/register and log in or create an account. Enter the
product ISBN 9780134855684 and click Submit. Once the process is complete, you will
find any available bonus content under "Registered Products."

Visit the book’s website, https://datastructures.live, to join the conversation with other
readers about the book, and learn more about the visualizations that bring data structures
to life.

9780134855684.indb 22 05/08/22 1:54 PM


Acknowledgments
From John Canning and Alan Broder

Robert Lafore’s Java-based version of this book has been a mainstay in Data Structures
courses and professionals’ reference shelves around the world for many years. When
Alan’s Data Structures course at Stern College for Women of Yeshiva University moved on
to Python, the inability to use Lafore’s book in the course was a real loss. We’re thus espe-
cially happy to bring this new and revised edition to the world of Python programmers
and students.

We’d like to thank the many students at Stern who contributed to this book either
directly or indirectly over the past several years. Initial Python versions of Lafore’s Java
implementations were central to Alan’s Python-based courses, and Stern student feedback
helped improve the code’s clarity, enhanced its performance, and sometimes even identi-
fied and fixed bugs!

For their valuable feedback and recommendations on early drafts of this new edition, we
are grateful to many students in Alan’s Data Structures courses, including Estee Brooks,
Adina Bruce, Julia Chase, Hanna Fischer, Limor Kohanim, Elisheva Kohn, Shira Orlian,
Shira Pahmer, Jennie Peled, Alexandra Roffe, Avigail Royzenberg, Batia Segal, Penina
Waghalter, and Esther Werblowsky. Our apologies if we’ve omitted anyone’s name.

An open-source package of data structure visualizations is available to enhance your study


of this book, and Stern students played an active role in the development of the visual-
ization software. John and Alan extend many thanks to the Stern student pioneers and
leaders of this project, including Ilana Radinsky, Elana Apfelbaum, Ayliana Teitelbaum, and
Lily Polonetsky, as well as the following past and present Stern student contributors and
mentors: Zoe Abboudi, Ayelet Aharon, Lara Amar, Natania Birnbaum, Adina Bruce, Chani
Dubin, Sarah Engel, Sarah Graff, Avigayil Helman, Michal Kaufman, Sarina Kofman, Rachel
Leiser, Talia Leitner, Shani Lewis, Rina Melincoff, Atara Neugroschl, Shira Pahmer, Miriam
Rabinovich, Etta Rapp, Shira Sassoon, Shira Schneider, Mazal Schoenwald, Shira Smith, Riva
Tropp, Alexandra Volchek, and Esther Werblowsky. Also, many thanks to the Stern faculty
who mentored student participants: Professor Ari Shamash, Professor Lawrence Teitelman,
and Professor Joshua Waxman. Our apologies if we have left anyone off this list.

Many thanks go to Professor David Matuszek of the University of Pennsylvania for his
early contributions of ideas and PowerPoint slides when Alan first started teaching Data
Structures at Stern. Many of the slides available in the Instructors Resources section have
their origin in his clear and well-designed slides. Also, we are grateful to Professor Marian
Gidea of the Department of Mathematics of Yeshiva University for his insights into spher-
ical trigonometry.

Finally, we owe a great debt to the talented editors at Pearson who made this book a
reality: Mark Taber, Kim Spenceley, Mandie Frank, Chuti Prasertsith, and Chris Zahn.
Without their many talents and patient help, this project would just be an odd collection
of text files, drawings, and source code.

9780134855684.indb 23 05/08/22 1:54 PM


From Robert Lafore for the Java-based versions of the book

Acknowledgments to the First Edition,


Data Structures and Algorithms in
Java
My gratitude for the following people (and many others) cannot be fully expressed
in this short acknowledgment. As always, Mitch Waite had the Java thing figured out
before anyone else. He also let me bounce the applets off him until they did the job,
and extracted the overall form of the project from a miasma of speculation. My editor,
Kurt Stephan, found great reviewers, made sure everyone was on the same page, kept the
ball rolling, and gently but firmly ensured that I did what I was supposed to do. Harry
Henderson provided a skilled appraisal of the first draft, along with many valuable sugges-
tions. Richard S. Wright, Jr., as technical editor, corrected numerous problems with his
keen eye for detail. Jaime Niño, Ph.D., of the University of New Orleans, attempted to
save me from myself and occasionally succeeded, but should bear no responsibility for
my approach or coding details. Susan Walton has been a staunch and much-appreciated
supporter in helping to convey the essence of the project to the nontechnical. Carmela
Carvajal was invaluable in extending our contacts with the academic world. Dan Scherf
not only put the CD-ROM together, but was tireless in keeping me up to date on rapidly
evolving software changes. Finally, Cecile Kaufman ably shepherded the book through its
transition from the editing to the production process.

Acknowledgments to the Second


Edition
My thanks to the following people at Sams Publishing for their competence, effort, and
patience in the development of this second edition. Acquisitions Editor Carol Ackerman
and Development Editor Songlin Qiu ably guided this edition through the complex
production process. Project Editor Matt Purcell corrected a semi-infinite number of gram-
matical errors and made sure everything made sense. Tech Editor Mike Kopak reviewed
the programs and saved me from several problems. Last but not least, Dan Scherf, an old
friend from a previous era, provides skilled management of my code and applets on the
Sams website.

9780134855684.indb 24 05/08/22 1:54 PM


About the Authors
Dr. John Canning is an engineer, computer scientist, and researcher. He earned an S.B.
degree in electrical engineering from the Massachusetts Institute of Technology and a
Ph.D. in Computer Science from the University of Maryland at College Park. His varied
professions include being a professor of computer science, a researcher and software
engineer in industry, and a company vice president. He now is president of Shakumant
Software.

Alan Broder is clinical professor and chair of the Department of Computer Science at
Stern College for Women of Yeshiva University in New York City. He teaches introductory
and advanced courses in Python programming, data structures, and data science. Before
joining Stern College, he was a software engineer, designing and building large-scale data
analysis systems. He founded and led White Oak Technologies, Inc. as its CEO, and later
served as the chairman and fellow of its successor company, Novetta, in Fairfax, Virginia.
Robert Lafore has degrees in Electrical Engineering and Mathematics, has worked as a
systems analyst for the Lawrence Berkeley Laboratory, founded his own software company,
and is a best-selling writer in the field of computer programming. Some of his titles are
Object-Oriented Programming in C++ and Data Structures and Algorithms in Java.

9780134855684.indb 25 05/08/22 1:54 PM


xxvi Data Structures & Algorithms in Python

Introduction
What’s in this book? This book is designed to be a practical introduction to data structures
and algorithms for students who have just begun to write computer programs. This intro-
duction will tell you more about the book, how it is organized, what experience we expect
readers will have before starting the book, and what knowledge you will get by reading it
and doing the exercises.

Who This Book Is For


Data structures and algorithms are the core of computer science. If you’ve ever wanted
to understand what computers can do, how they do it, and what they can’t do, then you
need a deep understanding of both (it’s probably better to say "what computers have
difficulty doing" instead of what they can’t do). This book may be used as a text in a data
structures and/or algorithms course, frequently taught in the second year of a univer-
sity computer science curriculum. The text, however, is also designed for professional
programmers, for high school students, and for anyone else who needs to take the next
step up from merely knowing a programming language. Because it’s easy to understand,
it is also appropriate as a supplemental text to a more formal course. It is loaded with
examples, exercises, and supplemental materials, so it can be used for self-study outside of
a classroom setting.

Our approach in writing this book is to make it easy for readers to understand how data
structures operate and how to apply them in practice. That’s different from some other
texts that emphasize the mathematical theory, or how those structures are implemented
in a particular language or software library. We’ve selected examples with real-world appli-
cations and avoid using math-only or obscure examples. We use figures and visualization
programs to help communicate key ideas. We still cover the complexity of the algorithms
and the math needed to show how complexity impacts performance.

What You Need to Know Before You Read This Book


The prerequisites for using this book are: knowledge of some programming language and
some mathematics. Although the sample code is written in Python, you don’t need to
know Python to follow what’s happening. Python is not hard to understand, if you’ve
done some procedural and/or object-oriented programming. We’ve kept the syntax in the
examples as general as possible,

More specifically, we use Python version 3 syntax. This version differs somewhat from
Python 2, but not greatly. Python is a rich language with many built-in data types and librar-
ies that extend its capabilities. Our examples, however, use the more basic constructs for two
reasons: it makes them easier to understand for programmers familiar with other languages,
and it illustrates the details of the data structures more explicitly. In later chapters, we do
make use of some Python features not found in other languages such as generators and list
comprehensions. We explain what these are and how they benefit the programmer.

9780134855684.indb 26 05/08/22 1:54 PM


Introduction xxvii

Of course, it will help if you’re already familiar with Python (version 2 or 3). Perhaps
you’ve used some of Python’s many data structures and are curious about how they are
implemented. We review Python syntax in Chapter 1, "Overview," for those who need
an introduction or refresher. If you’ve programmed in languages like Java, C++, C#,
JavaScript, or Perl, many of the constructs should be familiar. If you’ve only programmed
using functional or domain-specific languages, you may need to spend more time becom-
ing familiar with basic elements of Python. Beyond this text, there are many resources
available for novice Python programmers, including many tutorials on the Internet.

Besides a programming language, what should every programmer know? A good knowl-
edge of math from arithmetic through algebra is essential. Computer programming is
symbol manipulation. Just like algebra, there are ways of transforming expressions to
rearrange terms, put them in different forms, and make certain parts more prominent,
all while preserving the same meaning. It’s also critical to understand exponentials in
math. Much of computer science is based on knowing what raising one number to a
power of another means. Beyond math, a good sense of organization is also beneficial
for all programming. Knowing how to organize items in different ways (by time, by
function, by size, by complexity, and so on) is crucial to making programs efficient and
maintainable. When we talk about efficiency and maintainability, they have particular
meanings in computer science. Efficiency is mostly about how much time it takes to
compute things but can also be about the amount of space it takes. Maintainability refers
to the ease of understanding and modifying your programs by other programmers as
well as yourself.

You’ll also need knowledge of how to find things on the Internet, download and install
software, and run them on a computer. The instructions for downloading and running the
visualization programs can be found in Appendix A of this book. The Internet has made
it very easy to access a cornucopia of tools, including tools for learning programming and
computer science. We expect readers to already know how to find useful resources and
avoid sources that might provide malicious software.

What You Can Learn from This Book


As you might expect from its title, this book can teach you about how data structures make
programs (and programmers) more efficient in their work. You can learn how data organi-
zation and its coupling with appropriate algorithms greatly affect what can be computed
with a given amount of computing resources. This book can give you a thorough under-
standing of how to implement the data structures, and that should enable you to imple-
ment them in any programming language. You can learn the process of deciding what data
structure(s) and algorithms are the most appropriate to meet a particular programming
request. Perhaps most importantly, you can learn when an algorithm and/or data structure
will fail in a given use case. Understanding data structures and algorithms is the core of
computer science, which is different from being a Python (or other language) programmer.

The book teaches the fundamental data structures that every programmer should know.
Readers should understand that there are many more. These basic data structures work in
a wide variety of situations. With the skills you develop in this book, you should be able

9780134855684.indb 27 05/08/22 1:54 PM


xxviii Data Structures & Algorithms in Python

to read a description of another data structure or algorithm and begin to analyze whether
or not it will outperform or perform worse than the ones you’ve already learned in partic-
ular use cases.

This book explains some Python syntax and structure, but it will not teach you all its
capabilities. The book uses a subset of Python’s full capabilities to illustrate how more
complex data structures are built from the simpler constructs. It is not designed to teach
the basics of programming to someone who has never programmed. Python is a very
high-level language with many built-in data structures. Using some of the more primi-
tive types such as arrays of integers or record structures, as you might find in C or C++,
is somewhat more difficult in Python. Because the book’s focus is the implementation
and analysis of data structures, our examples use approximations to these primitive types.
Some Python programmers may find these examples unnecessarily complex, knowing
about the more elegant constructs provided with the language in standard libraries. If you
want to understand computer science, and in particular, the complexity of algorithms,
you must understand the underlying operations on the primitives. When you use a data
structure provided in a programming language or from one of its add-on modules, you
will often have to know its complexity to know whether it will work well for your use
case. Understanding the core data structures, their complexities, and trade-offs will help
you understand the ones built on top of them.

All the data structures are developed using object-oriented programming (OOP). If
that’s a new concept for you, the review in Chapter 1 of how classes are defined and used
in Python provides a basic introduction to OOP. You should not expect to learn the full
power and benefits of OOP from this text. Instead, you will learn to implement each data
structure as a class. These classes are the types of objects in OOP and make it easier to
develop software that can be reused by many different applications in a reliable way.

The book uses many examples, but this is not a book about a particular application area of
computer science such as databases, user interfaces, or artificial intelligence. The examples
are chosen to illustrate typical applications of programs, but all programs are written in a
particular context, and that changes over time. A database program written in 1970 may
have appeared very advanced at that time, but it might seem very trivial today. The exam-
ples presented in this text are designed to teach how data structures are implemented,
how they perform, and how to compare them when designing a new program. The exam-
ples should not be taken as the most comprehensive or best implementation possible of
each data structure, nor as a thorough review of all the potential data structures that could
be appropriate for a particular application area.

Structure
Each chapter presents a particular group of data structures and associated algorithms.
At the end of the chapters, we provide review questions covering the key points in the
chapter and sometimes relationships to previous chapters. The answers for these can be
found in Appendix C, "Answers to Questions." These questions are intended as a self-test
for readers, to ensure you understood all the material.

9780134855684.indb 28 05/08/22 1:54 PM


Random documents with unrelated
content Scribd suggests to you:
crew of the scow left and took to the woods. The Brazilians then
lowered and manned three boats, and sent them to take possession
of the scow and her gun. As they reached her, some infantry,
numbering about one hundred, who were concealed in the woods,
gave the boats a volley, which killed or wounded about half of their
crews; the rest made off and returned to their vessels.
The ironclads then continued to fire at the abandoned scow, and at
last blew up the magazine, and she sunk. The gun was not injured,
and was recovered by the Paraguayans.
On the 27th they towed the other gun-boat to the same place, and
opened upon the Brazilian fleet again, and the ironclads renewed the
same tactics as before. This time the Paraguayans had their boat
very close in to the bank, and kept their cartridges on shore, to avoid
being blown up. Most of their 68-pound shot struck the ironclads, but
flew in pieces. Some penetrated, however. One struck the
Tamandaré at the edge of a port, broke in pieces, and the fragments
entered, killing every one in that part of the casemate, including the
first and second Captains, three other officers, and eighteen men
killed, and fifteen wounded. The Tamandaré was driven off by this
shot. The two other ironclads kept up the fire, responded to by the
Paraguayan musketry from the woods, and at nine o’clock at night
the Brazilians retired, having effected nothing. Next day four
ironclads and four wooden gun-boats came up to engage this
doughty Paraguayan 8-pounder. On this day the ironclad Barroso got
four holes through her plates, and all the rest of them were more or
less damaged, until, at last, the Paraguayan gun was struck, and
fairly broken in two. Strange to say, not a Paraguayan was hurt.
On the night of the 29th, these irrepressible people, having
recovered the 8-inch gun from the first scow, endeavored to bring a
boat from Humáitá, to mount it upon. Their audacity was such that
they towed it, with canoes, down the Paraguay to the confluence,
and then up the Parana, and all this under a bright moonlight. At last
the Brazilians saw them, before they had reached their goal, and the
gun-boats steamed up, to capture an empty scow. The men had
made off, in the canoes, up the Parana.
These Paraguayan gun-boats thus constantly engaged the whole
Brazilian fleet. But it must be remembered that a mere float, of this
kind, bearing a gun, was very difficult to hit. For a week after this, the
steamer Gualeguay went out every afternoon, and fired at the
Brazilian fleet with her two 12-pounders. This was done principally
for Lopez’ amusement, and he, at a safe distance, had excellent long
glasses mounted, with which he watched the performances. The
Brazilian fleet would dash up the water, all about the Gualeguay, with
every kind of missile, from a 68 to a 150-pounder, and yet this
steamboat never received any damage but one hole in her smoke-
stack.
In some of the subsequent bombardments, Lopez would take up
his quarters in a secure bomb-proof, and receive exact reports of
every gun fired; what it had effected, and so forth. But he never
exposed himself for a moment.
The Allied artillery, on the left bank of the Parana, kept up a heavy
fire upon the post of Itapirú. But there was nothing there to receive
any damage, the 12-pounder being snugly stowed away for an
occasion. This continued for some time; until, at last, the Brazilians
occupied a sand bank, or bar, in the river, opposite Itapirú, and
mounted eight guns there, with two thousand men in trenches. From
this point they reopened a fire upon the work, which seemed a
perfect bugbear to them.
On the 10th of April the Paraguayans attacked this bank, or bar;
and the naval part of the enterprise consisted in their coming in
canoes.
Nine hundred men were embarked, in divisions of four hundred
and fifty each; with a reserve of four hundred at Itapirú. It was a dark
night, and the canoes, propelled by paddles, arrived at the bank, or
bar, at four o’clock in the morning. It was a complete surprise; and
the Paraguayans delivered one volley, and then charged with the
bayonet, taking the trenches. They were soon driven out of them
again, however, by overwhelming numbers; retook them, and were
again driven out. The Brazilian guns opened with canister, and the
Paraguayans lost heavily from this source. Two hundred of them
were dismounted cavalrymen, armed only with their swords, but they
did great execution, charging up to the guns, and taking them; but
being again driven off by heavy musketry fire.
As soon as the firing was heard below several gunboats and
ironclads came up, and surrounded the island, while the garrison
was reinforced from the left bank.
At last the Paraguayans were almost all killed or wounded, and
those who could move pushed off in their canoes, some paddling
with one arm who had the other one wounded. The daylight had
appeared, and they were forced to stem a heavy current, under the
fire of the Brazilian vessels, at close quarters; and yet fifteen canoes
got back to their own shore.
The Paraguayans lost fourteen officers killed, and seven wounded.
Of the soldiers three hundred returned, almost all wounded, and they
left five hundred men on the bank, or bar. Among the prisoners taken
by the Brazilians was a Lieutenant Roméro; and Lopez forced his
wife to write a letter disowning him as a traitor to Paraguay, because
he had allowed himself to be taken alive.
In this affair the Brazilians lost about a thousand, killed and
wounded, many more than the whole attacking force. The fire of their
own steamers destroyed a number of these.
Six Brazilians were afterwards tried for cowardice in this
engagement, and were shot.
In February, 1868, the Brazilian iron-clad vessels succeeded in
passing Humáitá, the extensive works above the confluence of the
rivers, which had so long kept them in check.
On the 13th three new monitors had arrived from Rio Janeiro, and
joined their squadron. They were built in Rio Janeiro, and had twin
screws, with four inches of iron on the hull, which was only one foot
out of the water, when prepared for action in fresh water. They had
each one revolving turret, six inches thick, with one heavy Whitworth
gun in each. The circular port for the gun was barely larger than the
muzzle, and when run out was flush with the face of the turret.
Elevation and depression of a gun so placed was obtained by means
of a double carriage, which raised or lowered the trunnions.
On February 18th everything was ready, and at half past three in
the morning the Brazilians began to bombard the Paraguayan works
most furiously.
The large casemate ironclads, each with a monitor lashed
alongside, then steamed up to the batteries at Humáitá. The fire of
these batteries was well sustained, and true, as the Paraguayan fire
had always been, but their cast-iron shot flew to pieces on the armor
of the ironclads, which passed without serious damage. After
passing the works they continued straight on, past more batteries, at
Timbó, to Tayi. The batteries at Timbó were water batteries, and
injured the ironclads more than all the others they had passed. In
this passage one of the Brazilian monitors received no less than one
hundred and eighty shot; and another one was hit one hundred and
twenty times. Their plates were dented and bent, and the bolts
started, but there was little or no loss of life on board them.
If one or two of the Brazilian ironclads had remained between
Humáitá and Timbó, instead of all running by the latter place, the
works of the former would have been really closely invested; and as
the object of running the batteries was to cause the surrender of
Humáitá, the movement was to that extent a failure. The
Paraguayans evacuated their works at their leisure, taking guns and
stores.

THE RIVER FIGHT AT TAYI.

Twice, in the year 1868, the Paraguayans attacked the Brazilian


monitors lying off Tayi, just above the influx of the river Bermejo.
These desperate attacks showed the most heroic bravery and
devotion, but were never successful.
Upon one occasion the iron-clad vessels Lima-Barros and Cabral
were attacked, and on another the Barroso and the monitor Rio
Grande.
After the last attack, in July, the Brazilians threw a boom across
the stream, which would detain their enemies, descending in their
canoes, long enough to give time for preparation.
These Paraguayan boats were admirably adapted for navigating
those waters, where there was a rapid current and many sand-bars,
constantly shifting, with channels, more or less deep, between them.
The canoes were built so that only a part of the central section
was borne by the water, and they were consequently easily turned,
while they glided over the water, propelled by spoon-shaped
paddles. Some of these craft were very large, and would carry a
cargo of many tons.
On the first occasion that the ironclads were attacked by means of
these canoes, an expedition consisting of twelve hundred men was
organized, under the command of a Captain Xenes, and armed with
swords and hand-grenades only.
The men were all paraded before Madame Lynch, the mistress of
Lopez, who, after distributing cigars among them, with great
condescension, told them to “go, and bring me back my ironclads.”
The men answered her with “vivas,” and went contentedly off upon
their desperate undertaking.
It was a dark night. The canoes were lashed in pairs, with eighteen
or twenty feet of slack rope between each pair. By this means they
hoped to make sure of boarding, the canoes of each pair swinging
round on opposite sides of the bows of the Brazilians.
There were forty-eight canoes, each carrying twenty-five men. The
Lima-Barros and Cabral were in advance of the main body, up the
stream. Many of the canoes were carried past them by the current,
into the midst of the Brazilian fleet. But about half of them hit the
advanced vessels, and the Paraguayans sprang on board,
unperceived. The crews were sleeping on the decks, outside, and
some fifty were at once killed by the boarders. The remainder rushed
below, and into the turrets, and secured the ports and hatches. The
Paraguayans attempted to throw hand-grenades into the port-holes,
and “ran about seeking ingress, like a cat attacking a trapped
mouse,” in the meantime loading the Brazilians with all sorts of
epithets, and daring them to come out and fight with the sword, like
men.
The Lima-Barros and Cabral were thus virtually captured, but by
this time the rest of the fleet were aroused, and soon two more
ironclads came steaming up to their relief. They swept the
Paraguayans from the decks with grape and canister, and those who
were not blown to pieces in this manner were obliged to take to the
water, and swim for life. Very few of them survived to tell the tale.
It is surprising that people so utterly fearless and devoted never
made very serious attempts to blow up the Brazilian ironclads,
especially as there were so many ways of doing so, and plenty who
were ready to attempt it, even at the sacrifice of their own lives.
The best informed foreigners who were in Paraguay at that time
thought that they wanted the vessels themselves so much that they
hesitated to destroy them, hoping that an opportunity to board them
successfully would occur at some time. The same persons thought
that if they had even had one fairly good ironclad they would have
completely cleared the river of the Brazilians. On the part of
Paraguay the war was premature. Lopez had ordered armored
vessels and rifled guns of heavy calibre in Europe, but so late that
the war was upon him, and the river blockaded, before they could be
delivered.

THE DREADNOUGHT.
(The most powerful Ironclad of the English Navy.)
THE CAPTURE OF THE HUASCAR. OCTOBER
8th, 1879.

his recent and important action between iron-clad


vessels, at sea, is remarkable in many ways, and is
especially interesting to naval men, as armored
vessels had, with perhaps a single exception, not
come together before, upon the high seas.
Fortunately, we have accounts of the battle from
Lieutenants Mason and Ingersoll, of the United
States Navy, Clements Markham, Lieutenant
Madan, R. N., and others—of whose accounts this
article will be a condensation.
The action took place in the forenoon, off
Mexillones de Bolivia.
The “Huascar,” a Peruvian man-of-war, was of
the old type of English turret ships, and had been employed
continuously, at sea, for many months, so that her bottom was very
foul, while her boilers were not in condition to make steam properly.
These two causes had very much reduced her speed. Her
commander, Admiral Grau, had wished to overhaul her, but his
representations were overruled, from considerations of policy, and
she was despatched to the south, upon what proved to be her last
cruise under the Peruvian flag.
When in order, she was known to be much faster than the Chilian
vessels which she was to encounter, and her loss may fairly be put
down to this disregard of professional advice and warning.
The Chilian ironclad, “Almirante Cochrane,” her principal
opponent, as well as the “Blanco Encalada,” which participated in the
latter part of the action, were nearly new casemated vessels; and
their constructor, Mr. Reed, had said that they ought to sink the
“Huascar” in five minutes.
This latter vessel had done good service in the war between Peru
and Chili, in interfering with the only transportation possible for the
Chilians, who were the aggressors, and who carried the war into the
Peruvian boundaries. Her commander, Rear-Admiral Grau, was an
excellent officer, and rendered himself quite famous by his sudden
dashes into Chilian ports, capturing transports and lighters, and
interfering with the submarine cable, so necessary for the success of
the Chilian operations.
The “Huascar” had made four successful cruises, or rather “raids,”
to the southward, in one of which she had captured the “Rimac,” a
fine steamer, having on board a fully-equipped battalion of cavalry
and a large amount of military stores. Among other curious things
which came under the latter head was a complete outfit of water-
skins, which were being sent to the Chilian Army at Antofagasta, to
enable it to carry water, in crossing the desert of Atacama, to operate
upon the province of Tarapaca.
This desert had been discovered to contain an immense deposit of
nitrate of soda, and the struggle which had been impending for some
years between the two nations was precipitated by the desire of Chili
to possess this source of wealth.
Soon after this success the “Huascar” appeared off the harbor of
Antofagasta, at night, and with a “Lay” torpedo attacked a Chilian
wooden corvette lying there. Instead of striking the enemy’s vessel,
the torpedo made a half circle, and came back upon the “Huascar.” A
lieutenant of the latter vessel, seeing the imminent danger, jumped
overboard, and swam to meet the torpedo, which was moving slowly,
and diverted it from its course, saving the “Huascar.” The name of
this lieutenant was Firmin Diaz Canséco.
The next day after this unsuccessful attempt she had an
engagement with the shore batteries and two corvettes, in which she
did much damage, and received some herself. She, at this time had
exchanged a native crew for one composed mostly of foreigners,
and men trained as gunners, who could give a better account of the
300-pound shells thrown by her guns.
In September, 1879, there had been a very considerable change
in the officers of the Chilian squadron, and a change of policy, as
well. The “Almirante Cochrane,” and “Blanco Encalada” (the latter
the flag-ship of Commodore Riveros), proceeded north,
accompanied by the wooden corvettes “O’Higgins” and
“Covadonga,” and the armed transports “Loa” and “Mathias
Cousiño.” They expected to find the “Huascar” in Arica, but arrived
there only to find that she had sailed for the south; and they at once
returned to Mexillones Bay, where they coaled, and awaited
instructions and events.
On the morning of October 5th the “Huascar” appeared in the
harbor of Coquimbo, in company with the corvette “Union.” There
were a number of foreign men-of-war lying there, and their officers
were much struck by the handling of the Peruvian vessels; so quiet,
able, and seamanlike, opposed to the usual noisy manner of carrying
on duty to be observed in South American men-of-war. They did not
even blow off steam when slowing down.
There was corresponding quiet on shore, where were batteries
armed with the heaviest modern guns; and the Peruvians were
allowed to retire unscathed, after thoroughly searching the harbor.
They went out again before daylight, but hung about to the
southward of the port, getting news, from mail steamers, of the
Chilian vessels coming to the northward.
During the next two days they moved up the coast; and Admiral
Grau determined to look into Arica, where the Chilian squadron was
supposed to be. Leaving the “Union” on the look-out, the “Huascar”
ran in towards the anchorage of Antofagasta, at about half-past one,
on the morning of October 8th. Finding nothing there, she ran out,
and joined the “Union” again, in about two hours. Both vessels now
headed north. Soon after they made out the smoke of three vessels
coming down the coast, southward, and about six miles distant.
These were quickly recognized as vessels of war, and the
“Huascar’s” course was changed, at about 3.30 a. m., to southwest.
The Chilian squadron at Mexillones, having coaled, put to sea on
the night of the 7th, in two divisions. The first division, consisting of
the slower vessels, the “Blanco,” “Covadonga,” and “Mathias
Cousiño,” sailed at 10 p. m., and steered down the coast towards
Antofagasta; the second division, under commander La Farré,
consisting of the “Cochrane,” “O’Higgins,” and “Loa,” sailing on the
morning of the 8th, before daylight, with orders to cruise twenty-five
miles off Point Angamos. This was done in consequence of
telegraphic instruction from the Chilian authorities. The Commodore
had intended to move south, along the coast, in similar divisions, the
first skirting the coast, and looking in at the bays, while the second
kept pace with them, about forty miles off shore.
The result was the same, whichever plan had been followed.
At half-past three in the morning of the 8th of October, the weather
being fine and clear, the smoke of two vessels approaching, under
Point Letas, and distant about six miles, was reported from the top of
the “Blanco.”
At daylight the enemies recognized each other.
The “Huascar” ran to the southwest for an hour, under full speed,
making nearly eleven knots; the “Blanco” and “Covadonga” following,
and making less than eight knots. The “Mathias Cousiño” was first
sent in towards Antofagasta, but, later, turned and followed her
consorts. Riveros, the Chilian Commodore, soon saw that such a
chase was hopeless; but still, on the chance of an accident to the
machinery of the “Huascar,” or her consort, or of their turning to the
northward, and being cut off by his second division, he determined to
continue it.
The Peruvians could not afford to run any risks with their ships. If
the “Huascar” was lost, it would entail serious consequences to the
Peruvian cause; and it was therefore proper for Grau to attempt
escape. Finding that he could outrun his pursuers, he reduced his
speed, and turned his ships’ heads to the northward. Not very long
after this, smoke was seen to the northwest, and, having diverged a
little from her course, to reconnoitre, the “Huascar” recognized the
Chilian “Cochrane,” and her consorts. At about the same time the
“Huascar” was seen from the “Cochrane,” and the “Loa” was sent to
reconnoitre.
Grau had supposed that the “Cochrane” only steamed eight knots,
and thought he could easily run away from her, so he stood toward
the “Loa,” for a short time Finding, however, that the “Cochrane” was
changing her bearings more rapidly than he had anticipated, he
stood more to the eastward, and ordered “full speed.”
The “Union,” which had been on the “Huascar’s” port quarter, now,
at about 8 a. m., crossed her stern, and passed to starboard of her,
at full speed. This vessel made the best of her way to Arica;
followed, until dark, by the Chilian ships “O’Higgins” and “Loa.”
There appears to have been a good deal of criticism upon the
commanding officers of these three ships—the first for not engaging,
and the others for not continuing the pursuit.
The three ironclads were now comparatively close to each other,
and Grau saw that his only chance of escape lay in his speed. There
were but three courses open to him.
First—to turn boldly and meet the “Cochrane,” and, though inferior
in gun power, endeavor to ram or cripple her, before the “Blanco”
could come up.
Secondly—to endeavor to escape to the northeast, between the
“Cochrane” and the shore, trusting to have superior speed.
Thirdly—to turn round and engage, or escape past, the “Blanco.”
Grau chose the second course.
At nine a. m., the “Cochrane” having approached within about four
thousand yards, and it being evident that she could cross his bows,
Grau ordered his crew to quarters and entered his iron-cased
conning tower, where he was alone. In going to quarters, on board
the “Huascar,” an accident took place, in shifting the steering-gear
from the usual situation to the protected one, in the turret chamber,
under the conning tower.
While a make-shift tackle was being rove the “Huascar” yawed
considerably.
At half-past nine, the “Cochrane” being about 3000 yards distant,
the “Huascar” opened fire with her turret guns. The second shot
ricochetted, and entered the “Cochrane’s” unarmored bow, doing
some damage, but not exploding. At this time the Chilian “Blanco”
was about six miles astern. The “Cochrane” did not answer the
“Huascar’s” two shots; but stood on until within two thousand yards,
when she opened fire. The first shot penetrated the “Huascar’s”
armor on the port side, entered the turret-chamber, and exploded,
set fire to the wood-work, killed or wounded twelve men, and
jammed the wheels on which the turret revolved, for the time being.
The “Huascar” fired a 300-pound Palliser chilled shell, and struck the
“Cochrane’s” side armor, at an angle of about thirty degrees. The
plate struck was six inches thick, and was indented, and scored out
to a depth of three inches, the bolts started, and the backing forced
in.
The “Huascar” soon after stood a little to port, with the intention of
ramming the “Cochrane,” but the latter avoided this, by turning an
equal amount to port, and keeping parallel with her antagonist.
Five minutes after the “Huascar’s” conning tower was struck by a
shell, which exploded in it, shattered it, and blew Admiral Grau to
pieces—only one foot and a few fragments of his body being found.
Grau usually directed the movements of his vessel with his head and
shoulders above the tower, and the shell, therefore, probably hit him
at about the waist.
This shot also killed Lieutenant Diego Ferré, the Admiral’s Aid,
who was at the fighting wheel, and only separated from the conning
tower by a light wooden grating. Ferré’s death appears to have been
caused by concussion, as no wounds were found upon his body.
This shot also damaged the fighting wheel, and the ship ran off to
the eastward until the damage was repaired, when she again
headed to the northward.
About this time a shell penetrated the armor of the turret, which
was trained on the port-quarter, in the thickest part, to the left of the
port of the right gun. This shell killed or disabled most of the two
guns’ crews.
Among these were two gun-captains, Englishmen, who had been
trained on board the gunnery-ship, “Excellent,” and Commander
Carbajal, who had come to inform the second in command,
Commander Elias Aguirre, that he was now in command.
The left gun was not injured, and relief crews were sent to it; but
the firing was very wild. The right gun was disabled by the
compressor and cap-square being bent. At this time Lieutenant
Rodriguez, who was looking out of the gun-port, had his head taken
off. This, in connection with the previous casualties, so demoralized
the Peruvians that most of the subsequent fighting was done by the
foreigners of the “Huascar’s” ship’s company. By this time the fire
from the Nordenfelt guns and the small arms of the Chilians had
driven most of the officers and men of the “Huascar” down into the
ward-room. Some of these were wounded, but the most were merely
taking refuge there.
The “Cochrane” now attempted to ram, coming at right angles to
her adversary. She missed the “Huascar,” going close astern of her,
but a shot from one of her port-guns pierced the armor of the
“Huascar,” on the starboard quarter, exploding, and doing much
damage—among other things, carrying away the steering apparatus.
The “Huascar” now again headed to the eastward: but a shell
pierced the armor, abreast of the engine-room, covering the engine
with fragments of all kinds, and killing and wounding many persons.
Among these were Surgeon Tavara, and Mr. Griffiths, the master of
the English schooner “Coquimbo,” captured a few days before, and
whose crew had been forced to render service during the action.
The relieving tackles, by which the “Huascar” was now steered,
were not only exposed to shot, but had a very bad lead, and the
steering was very uncertain, not only from that cause, but because
Commander Aguirre had to command the vessel from one of the
look-out hoods of the turret, and the word had to be passed clear aft,
on the lower deck, to the men at the relieving tackles. There was,
probably, not much real control of the “Huascar” after the conning
tower was destroyed, Grau killed, and the main steering wheel
disabled.
The “Cochrane” now again attempted to ram the “Huascar,” firing
her bow-gun, at two hundred yards, and coming on at right angles.
She again missed her blow, and passed astern.
It was by this time ten o’clock, and the “Blanco” arrived on the
scene of action, passing between the “Huascar” and the “Cochrane,”
just as the latter was preparing to ram, for the third time. The
“Cochrane,” to avoid the imminent danger in which she was placed
by her consort’s ram, was forced to turn to port, and then to run
northward, increasing her distance to about twelve hundred yards.
The “Huascar” then turned to starboard, and headed for the
“Blanco,” with the intention of ramming her, at the same time firing
some ineffectual shots at her. The “Blanco” sheered to starboard
also, and passing close under her stern, poured a broadside into that
vulnerable part, which killed or wounded all the men at the relieving
tackles, as well as many of the wounded, and the others who had
taken refuge in the officers’ quarters. The wounded were now
removed to the coal-bunkers and store-rooms; and the “Huascar”
stood to the westward.
On account of a number of shot having passed through her
smoke-stack, driving down soot, débris of all kinds, and smoke into
the fire-room, it was impossible to see the gauges. In consequence,
the water got too low in one of the boilers, and the tubes were
burned through, which caused a great escape of steam; so that the
Chilians thought they had struck one of the boilers.
There had been four men stationed at a Gatling gun, in the
“Huascar’s” top, but three were killed, and the other driven below, by
the fire from the Chilian tops, although the “Huascar’s” top had a
screen of boiler-iron.
About half-past ten the flag-staff, with the “Huascar’s” colors, was
shot away, and for some moments all firing ceased, as it was
supposed she had surrendered; but a Frenchman, who was a loader
at one of the guns, went aft, and hoisted another flag at her gaff. Just
then another shot penetrated the turret of the “Huascar,” killing or
mortally wounding every man in it, including Commander Aguirre.
This shot had such terrible effect that when this officer’s body was
found and identified, all the upper part of the head was gone, the
lower jaw only remaining. In addition his body was most fearfully
mutilated. Another officer was terribly wounded by this shot.
The command of the “Huascar” had now devolved upon the fourth
officer, Lieutenant Pedro Garezon. The vessel was almost
unmanageable, and on fire in several places, but the engines were
kept going, and an occasional gun fired.
The “Cochrane” now returned, and again tried to ram, and was
only prevented from doing so by a chance movement of the
“Huascar.”
Both Chilian ships then followed up the Peruvian, using great
guns, musketry and machine guns. They were both in good
condition, although the “Cochrane” had been struck on her
unarmored stern, and had some ten men killed and wounded.
The “Covadonga” now came up, and joined the other Chilian
vessels, and Lieutenant Garezon, after a council with the surviving
officers, determined to sink the “Huascar,” by opening her valves,
and thus deprive his enemies of the satisfaction of taking possession
of her.
Chief Engineer McMahon succeeded in partially accomplishing
this, by opening the circulating valve of the condensers, but to do
this he had to stop the engines. They were at work on the main
injection valve when Lieutenant Simpson, of the “Cochrane,” who
had boarded her, interfered with the operation, revolver in hand.
While this was going on, some of the “Huascar’s” men waved
towels and handkerchiefs, and the Chilians, on seeing this, ceased
firing, and the “Huascar’s” flag was then hauled down.
It was at this moment that Simpson boarded her, and then came
surgeons and engineers from the Chilians.
On taking possession they found three or four feet of water in the
hold. Some of the holes made by projectiles in her sides were nearly
awash, and in a few minutes more the vessel would have sunk. She
was also found to be on fire in several places, one of which was
dangerously near the magazine. Fortunately the sea was smooth.
The valves were closed, the steam pumps started, and the fires
extinguished. The wounded and the prisoners were then transferred
to the Chilian ships.
The “Huascar’s” engines were uninjured, as were three out of her
four boilers, and they were able to get her into port, at Mexillones,
that same afternoon; and in two days, after temporary repairs, she
was sent to Valparaiso. Here proper plates were found, which had
been sent out from England for the “O’Higgins;” and by the 8th of the
succeeding December she was in active service again under the
Chilian flag.
The scene presented on board the “Huascar,” when boarded by
her captors, was most terrible. Hardly a square yard of her upper
works had escaped injury, including her smoke-stack, conning tower,
boats, davits, mast and chain-plates. Her bulwarks, poop, forecastle
and hatch-combings were much injured, while her capstan was
struck and knocked entirely overboard. During the latter part of the
fight, indeed, the Peruvian had been little more than a floating target
for the Chilian’s accurate fire.
Eighteen dead bodies were taken out of the cabin, and the turret
was full of the remains of the two sets of guns’ crews.
The light wood-work, ladders and bulkheads were all destroyed.
The ship’s log-book had been destroyed, but complete working
drawings of the “Blanco” and “Cochrane” were found on board.
The action lasted one hour and a half; and during this time the
“Huascar” lost her commander and the three next senior officers,
either killed or disabled, and had twenty-eight officers and men
killed, and forty-eight wounded, out of a crew of about two hundred.

(ENLARGED VIEW OF
TURRET.)

APPEARANCE OF THE HUASCAR AFTER CAPTURE.

Nearly every time she was struck the greatest temporary damage
possible was inflicted, and yet no permanent injury was caused. The
armor was really a disadvantage to her, for it served to explode the
enemy’s projectiles, which only stopped when they struck at the very
smallest angles. The backing and inner skin only served to increase
the number of fragments, which were driven in with deadly effect.
The shell which passed through the thin sides of the forecastle did
not explode, and did but little damage. Each shell which pierced the
armor exploded, and each explosion set the ship on fire in a new
place. The Chilian small-arm men and the Nordenfelt machine gun
drove all the Peruvians off the deck, and away from the unprotected
guns there. The “Cochrane” fired forty-five Palliser shells. The
“Blanco” fired thirty-one. It is thought that the “Huascar” fired about
forty projectiles from her turret guns.
The “Cochrane” was hit three times. The “Blanco” was untouched,
while the “Huascar” received at least sixteen large Palliser shells,
besides Nordenfelt bullets and shrapnel. The shot-holes in the
“Huascar” were so jagged and irregular that no ordinary stoppers
could be of any service.
The officers who have given us the account of this action make a
number of practical deductions and suggestions of great importance,
but not necessary to be quoted here.

STEEL TORPEDO BOAT AND POLE.


BOMBARDMENT OF ALEXANDRIA. JULY 11th, A.
D. 1882.

t would be rather presumptuous for any one to


attempt at this time to give the real causes of the
bombardment of Alexandria, and of the subsequent
operations of the British army in Egypt. The
Egyptian leader, Arabi Pasha, has been tried, and
the tribunal, while sparing his life, sentenced him to
be banished to Ceylon, where he is now. Nothing
definite was made public, however, as to the
assurances of support and sympathy which he is
supposed to have had, not only from the Sublime
Porte, but from other nations.
Egyptian politics may be symbolized by a
tangled skein which time alone can unravel. Some
day it may be known whether the ostensible
reasons brought a great calamity about, or whether
secret and less worthy motives caused the action of the British
ministry, and controlled their fleet and army.
In the summer of 1882 Arabi Pasha, who had complete control of
the military force of Egypt, although the Khedive had not been
formally deposed, was strengthening the forts about Alexandria, and
increasing their armament. As he was opposed to English or any
foreign control in Egypt, England naturally felt alarm for the safety of
the Suez Canal, which is so vitally important for her communications
with her great Eastern empire, as well as for her general commerce.
Admiral Sir Beauchamp Seymour, with a powerful squadron of the
largest ironclads and a number of gun-boats, had been ordered to
Alexandria, in observation.
The city of Alexandria, named from its founder, Alexander the
Great, has experienced many vicissitudes. The modern city is built
on a peninsula, which was formerly the Island of Pharos, and on the
isthmus connecting it with the mainland. The ancient city was on the
mainland, where its ruins cover a vast extent of surface.
Founded nearly three and a half centuries before the Christian era,
it rose, under the liberal and beneficent sway of the Ptolemies, to
great eminence as a seat of learning, as well as of commerce. Under
the Roman Empire it continued to be a very splendid and influential
city, second only to Rome herself, and engrossing the lucrative traffic
with India. Its library was one of the wonders of the world—400,000
volumes being in the Museum, and 300,000 in the temple of Serapis.
The former was accidentally destroyed by fire during the war with
Julius Cæsar; and the latter was burnt by command of Caliph Omar,
upon the Mohammedan conquest, in the year 640. After the
discovery by the Portuguese of the route to India by the Cape of
Good Hope, its commerce fell off, and its population dwindled to a
few thousands. Gradually it revived again, and for a long time has
been the most important commercial city of the Levant, with a very
large foreign as well as native population. But, to return: On July 6th,
1882, Admiral Seymour sent an ultimatum to Arabi and his council,
which had the effect of stopping work upon the fortifications for a
short time, and produced a promise that such work should not be
renewed.
But on the following night a powerful electric light, on board the
ironclad Alexandra, of the English squadron, disclosed the fact that,
under cover of the darkness, guns were being mounted on the forts
commanding the entrance to the great port, or western harbor—
there being two harbors, one east and one west of the isthmus. New
guns were placed in position on the north side, also, on the
peninsula where the Harem, or residence of the Khedive is situated,
and which forms the protection to the main anchorage. Earthworks
were also being thrown up there by a very large force.
Seymour telegraphed these facts to the British government,
summoned a council of his officers, and made preparations for
battle. He then sent a demand to the Egyptian authorities for the
surrender of the forts to him within twenty-four hours, with a view to
disarmament, under penalty of bombardment.
There was a French fleet in the port, which had been ordered to
take no part in any aggressive measures; and there were also naval
vessels of several other nations, among which were some of our
own, the commanders of which had been very active in affording
refuge to Americans resident in Egypt, as well as to citizens of other
countries who were not represented by men-of-war.
The French fleet, seeing hostilities imminent, got under way, and
steamed out into the offing, followed by the other foreign men-of-war
and merchant vessels. Many of these were crowded with refugees,
but there were left in the city a large number of Greeks, Italians,
Maltese, and Syrians.
The British ships then proceeded to take up their stations before
the forts, and a panic ensued among the inhabitants, who quitted the
city, as did most of the Europeans who had remained. This they
effected with great difficulty, and there was a prospect of the renewal
of the massacre which had taken place some weeks before. The
cash chest of the European Director of Customs, which were
managed for the benefit of the foreign bondholders, was seized by
Arabi, but the officials managed to get away.
On the 10th of July a deputation of Egyptian notables came off to
the English flag-ship, to know the meaning of these warlike
preparations. They had not heard of the ultimatum, which had not
reached them, by some blunder, whether on the part of the English
or the natives does not appear; and, indeed, the document was
brought off to them while they were still on board the ship, by
messengers who had been searching for them. They then went on
shore, to consider it.
Very early next morning, the 11th of July, a deputation of Egyptian
officials came off to say that they were willing to dismount the guns
of the forts themselves. This would appear to be all that the English
Admiral had originally demanded; but whether he suspected a ruse,
or whether he was determined to take offensive measures at any
rate, he refused to entertain the proposal, saying that the time for
negotiation had expired.
At seven in the morning the first shot was fired from the Alexandra,
and eight English ironclads, of the heaviest description, with five
heavy gun-boats, opened upon the different forts. These were the
heaviest guns and the thickest armor, by far, that had ever been in
action. To mention only one, the Inflexible had four 81-ton guns, and
Welcome to our website – the ideal destination for book lovers and
knowledge seekers. With a mission to inspire endlessly, we offer a
vast collection of books, ranging from classic literary works to
specialized publications, self-development books, and children's
literature. Each book is a new journey of discovery, expanding
knowledge and enriching the soul of the reade

Our website is not just a platform for buying books, but a bridge
connecting readers to the timeless values of culture and wisdom. With
an elegant, user-friendly interface and an intelligent search system,
we are committed to providing a quick and convenient shopping
experience. Additionally, our special promotions and home delivery
services ensure that you save time and fully enjoy the joy of reading.

Let us accompany you on the journey of exploring knowledge and


personal growth!

textbookfull.com

You might also like