Team LRN Team LRN Data Structures FM 11/5/02 9:28 AM Page i Data Structures for Game Programmers Team LRN This page intentionally left blank Team LRN Data Structures FM 11/5/02 9:28 AM Page iii Data Structures Ron Penton TM for Game Programmers Team LRN Data Structures FM 11/5/02 9:28 AM Page iv © 2003 by Premier Press, a division of Course Technology. All rights reserved. No part of this book may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or by any information storage or retrieval system without written permission from Premier Press, except for the inclusion of brief quotations in a review. The Premier Press logo and related trade dress are trademarks of Premier Press and may not be used without written permission. TM Publisher: Stacy L. Hiquet Marketing Manager: Heather Hurley Acquisitions Editor: Emi Smith Project Editor: Karen A. Gill Technical Reviewer: André LaMothe Copyeditor: Stephanie Koutek Interior Layout: LJ Graphics, Susan Honeywell Cover Design: Mike Tanamachi Indexer: Kelly Talbot Proofreader: Jenny Davidson Microsoft, Windows, and Visual C++ are trademarks of Microsoft Corporation.Wolfenstein, Doom, and Quake are trademarks of Id Software, Inc. Warcraft and Starcraft are trademarks of Blizzard Entertainment. The artwork used in this book is copyrighted by its respective owners, and you may not use it in your own commercial works. All other trademarks are the property of their respective owners. Important: Premier Press cannot provide software support. Please contact the appropriate software manufacturer’s technical support line or Web site for assistance. Premier Press and the author have attempted throughout this book to distinguish proprietary trade- marks from descriptive terms by following the capitalization style used by the manufacturer. Information contained in this book has been obtained by Premier Press from sources believed to be reliable. However, because of the possibility of human or mechanical error by our sources, Premier Press, or others, the Publisher does not guarantee the accuracy, adequacy, or completeness of any information and is not responsible for any errors or omissions or the results obtained from use of such information. Readers should be particularly aware of the fact that the Internet is an ever-chang- ing entity. Some facts may have changed since this book went to press. ISBN: 1-931841-94-2 Library of Congress Catalog Card Number: 2002111226 Printed in the United States of America 03 04 05 06 07 BH 10 9 8 7 6 5 4 3 2 1 Premier Press, a division of Course Technology 2645 Erie Avenue, Suite 41 Cincinnati, Ohio 45208 Team LRN Data Structures FM 11/5/02 9:28 AM Page v To my family, for always being there for me. Team LRN Data Structures FM 11/5/02 9:28 AM Page vi Acknowledgments I would first like to thank my family for putting up with me for the past nine months. Yes, yes, I’ll start cleaning the house now. I would like to thank all of my friends at school: Jim, James, Dan, Scott, Kevin, and Kelvin, for helping me get through all of those boring classes without falling asleep. I would like to thank everyone at work for supporting me through this endeavor. I especially want to thank Ernest Pazera, André LaMothe, and everyone else at Premier Press for giving me this tremendous opportunity and believing in me. I would like to thank Bruno Sousa for opening the door to writing for me. I want to thank the pioneers of Gamedev.net, Kevin Hawkins and Dave Astle, for paving the road for me and making a book such as this possible. I would like to thank all of you in the #gamedev crew, specifically (in no particular order) Trent Polack, Evan Pipho, April Gould, Joseph Fernald, Andrew Vehlies, Andrew Nguyen, John Hattan, Ken Kinnison, Seth Robinson, Denis Lukianov, Sean Kent, Nicholas Cooper, Ian Overgard, Greg Rosenblatt, Yannick Loitière, Henrik Stuart, Chris Hargrove, Richard Benson, Mat Noguchi, and everyone else! I would like to thank my artists, Steven Seator and Ari Feldman, who made this book’s demos look so much better than they would have been. And finally, I would like to thank the Pepsi Corporation, for making that wonderful “stay awake” juice known as Mountain Dew. Team LRN Data Structures FM 11/5/02 9:28 AM Page vii About the Author Ron Penton’s lifelong dream has always been to be a game programmer. From the age of 11, when his parents bought him his first game programming book on how to make adventure games, he has always striven to learn the most about how games work and how to create them. Ron is currently finishing up his bachelor’s degree in computer science at the State University of New York at Buffalo. He hopes to have a long career in game develop - ment. Team LRN Data Structures FM 11/5/02 9:28 AM Page viii Contents at a Glance Introduction . . . . . . . . . . . . . . . . xxxii Part One Concepts . . . . . . . . . . . . . . . . . . . . . . . . . 1 Chapter 1 Basic Algorithm Analysis . . . . . . . . . 3 Chapter 2 Templates . . . . . . . . . . . . . . . . . . . 13 Part Two The Basics . . . . . . . . . . . . . . . . . . . . . . 37 Chapter 3 Arrays. . . . . . . . . . . . . . . . . . . . . 39 Chapter 4 Bitvectors. . . . . . . . . . . . . . . . . . . 83 Chapter 5 Multi-Dimensional Arrays . . . . . . . . 107 Chapter 6 Linked Lists . . . . . . . . . . . . . . . . . 147 Chapter 7 Stacks and Queues . . . . . . . . . . . . 189 Chapter 8 Hash Tables . . . . . . . . . . . . . . . . . 217 Chapter 9 Tying It Together: The Basics. . . . . . 241 Part Three Recursion and Trees . . . . . . . . . . . . . . 315 Chapter 10 Recursion . . . . . . . . . . . . . . . . . . 317 Chapter 11 Trees . . . . . . . . . . . . . . . . . . . . . 329 Chapter 12 Binary Trees . . . . . . . . . . . . . . . . 359 Chapter 13 Binary Search Trees. . . . . . . . . . . 389 Chapter 14 Priority Queues and Heaps . . . . . . . 407 Team LRN [...]... With that in mind, I wanted a data structures book that was like no other—a book using today’s technology that could live up to my high standards So I set out to find the perfect author and finally Ron Penton came along to take on the challenge Ron, too, had my same vision for a data structures book We couldn’t do something that had been done—there are a zillion boring data structure books—but if we could... sections: the data and the instructions that operate on the data These two sections of a program are commonly called the data structures and the algorithms This book will teach you how to create many data structures, ranging from the very simple to the moderately complex Understanding data structures and algorithms is an essential part of game programming Knowing the most efficient way to store data and... the CD After that, I show you how to actually code the structure or algorithm in C++ The code for these sections is mostly platform free, so it will usually compile on any compiler I mention any sections that are platform-specific in the book All of the code for the data structures and algorithms can be found on the CD in the directory \structures\ for your convenience Copies of the files have also been... and work with the data is an important part of game programming; you want your games to run as quickly as possible so you can pack as many cool features into them as you can I have a few goals with this book: ■ ■ ■ Teach you how the most popular data structures and algorithms work Teach you how to make the structures and algorithms Teach you how to use the data structures in computer games Mark Twain... of nodes in a tree Only a game programmer could bring these and more to you—no one else would have the programming mastery of all the fields necessary to Team LRN xxxi Letter from the Series Editor pull this off On the other hand, if you are a game programmer, then you will greatly appreciate Ron s insight into applications of various data structures and algorithms for game- related programs In fact,... that’s in here, too—but you will love it because for once, you will be able to see the recursion! Finally, the book ends with primers on C++, SDL, and the standard template library, so basically you will be a data structure god when you’re done! In conclusion, this book is for the person who is looking for both a practical and a theoretical base in data structures and algorithms I guarantee that it will... Appendix A).You cannot copy any of the structures because none of them implements proper copy constructors Whenever you pass a structure into a function as a parameter, make absolutely certain that you pass-by-reference or use a pointer; otherwise, it will mess up your structure If you don’t know what this means just yet, look at the functions that use the data structures; they demonstrate how to use... show you an interactive Game Demonstration, which highlights the usage of the structure or algorithm in a game- like atmosphere Most of these games are simple, but they prove a point These demonstrations also use the SDL multimedia library and are located on the CD in the directory \demonstrations\ Some chapters might deviate from the format to show you different versions of the structures I’ve separated... Structure 565 The File Format 566 Game Demonstration 1 9-1 : Adding the New Map Format 567 The DirectionMap 568 Changes to the Game Logic 580 Playing the Game 582 Converting... Multi-Dimensional Arrays 107 What Is a Multi-Dimensional Array? 108 Graphical Demonstration 111 Native Multi-Dimensional Arrays 112 Declaring a Multi-Dimensional Array 112 Accessing a Multi-Dimensional Array 115 Inside a Multi-Dimensional . LRN Data Structures FM 11/5/02 9:28 AM Page i Data Structures for Game Programmers Team LRN This page intentionally left blank Team LRN Data Structures FM 11/5/02 9:28 AM Page iii Data Structures. Team LRN Data Structures FM 11/5/02 9:28 AM Page iii Data Structures Ron Penton TM for Game Programmers Team LRN Data Structures FM 11/5/02 9:28 AM Page iv © 2003 by Premier Press, a division. Avenue, Suite 41 Cincinnati, Ohio 45208 Team LRN Data Structures FM 11/5/02 9:28 AM Page v To my family, for always being there for me. Team LRN Data Structures FM 11/5/02 9:28 AM Page vi Acknowledgments