This page intentionally left blank
Data Structures and Algorithms Using Python Rance D. Necaise Department of Computer Science College of William and Mary JOHN WILEY & SONS, INC.
ACQUISITIONS EDITOR MARKETING MANAGER EDITORIAL ASSISTANT SENIOR DESIGNER MEDIA EDITOR PRODUCTION MANAGER PRODUCTION EDITOR Beth Golub Christopher Ruel Michael Berlin Jeof Vita Thomas Kulesa Micheline Frederick Amy Weintraub This book was printed and bound by Hamilton Printing Company. The cover was printed by Hamilton Printing Company This book is printed on acid free paper. ∞ Copyright ©2011 John Wiley & Sons, Inc. All rights reserved. No part of this publication may be reproduced, stored in a retrieval system or transmitted in any form or by any means, electronic, mechanical, photocopying, recording, scanning or otherwise, except as permitted under Sections 107 or 108 of the 1976 United States Copyright Act, without either the prior written permission of the Publisher, or authorization through payment of the appropriate per-copy fee to the Copyright Clearance Center, Inc. 222 Rosewood Drive, Danvers, MA 01923, website www.copyright.com. Requests to the Publisher for permission should be addressed to the Permissions Department, John Wiley & Sons, Inc., 111 River Street, Hoboken, NJ 07030-5774, (201)748-6011, fax (201)748-6008, website http://www.wiley.com/go/permissions. “Evaluation copies are provided to qualified academics and professionals for review purposes only, for use in their courses during the next academic year. These copies are licensed and may not be sold or transferred to a third party. Upon completion of the review period, please return the evaluation copy to Wiley. Return instructions and a free of charge return shipping label are available at www.wiley.com/go/returnlabel. Outside of the United States, please contact your local representative.” Library of Congress Cataloging-in-Publication Data Necaise, Rance D. Data structures and algorithms using Python / Rance D. Necaise. p. cm. Includes bibliographical references and index. ISBN 978-0-470-61829-5 (pbk.) 1. Python (Computer program language) 2. Algorithms. 3. Data structures (Computer science) I. Title. QA76.73.P98N43 2011 005.13'3—dc22 2010039903 Printed in the United States of America 10 9 8 7 6 5 4 3 2 1