Data Structures Using Java
D. S. Malik
P. S. Nair
THOMSON
COURSE T E C H N O L O G Y
Australia • Canada • Mexico • Singapore • Spain • United Kingdom • United States
TABLE OF
Contents
PREFACE XXV
1 .Software Engineering Principles and Java Classes 1
Software Life Cycle 2
Software Development Phase 2
Analysis 3
Design 3
Implementation 4
Testing and Debugging 6
Algorithm Analysis: The Big-O Notation 8
User-Defined Classes 15
Constructors 18
Unified Modeling Language Diagrams 19
Variable Declaration and Object Instantiation 20
Accessing Class Members 23
Built-in Operations on Classes 24
The Assignment Operator and Classes: A Precaution 24
Class Scope 26
Definitions of the Constructors and Methods of the c l a s s C l o c k 26
The Copy Constructor 35
Classes and the Method t o S t r i n g 36
Static Members of a Class 37
Data Structures Using Java
Static Variables (Data Members) of a Class 39
Finalizer 40
Creating Your Own Packages 40
Multi-File Programs 41
The Reference t h i s 44
Inner Classes 46
Abstract Data Types 46
Programming Example: Candy Machine 48
Identifying Classes, Objects, and Operations 61
Quick Review 62
Exercises 64
Programming Exercises 71
2.Inheritance and Exception Handling 75
Inheritance 76
Using Methods of the Superclass in a Subclass 78
Constructors of the Superclass and Subclass 83
Protected Members of a Class 92
The c l a s s O b j e c t 96
Objects of Superclasses and Subclasses 98
The Operator i n s t a n c e o f 99
Abstract Methods and Classes 104
Composition 105
Exception Handling 110
Java Exception Hierarchy 111
The Exception Hierarchy 115
Table of Contents xi
Checked and Unchecked Exceptions 117
Handling Exceptions within a Program 118
t r y / c a t c h / f i n a l l y Block 118
Rethrowing and Throwing an Exception 124
Exception Handling: Techniques 129
Creating Your O w n Exception Classes 130
Programming Example: Grade Report 133
Quick Review 155
Exercises 157
Programming Exercises 165
3.Array-Based Lists 169
Typ e of List Elements 171
class IntElement 173
class StringElement 175
class ArrayListClass 177
Unordered List 187
Time-Complexity of List Operations 191
c l a s s Vector 198
Primitive Data Types and the c l a s s V e c t o r 204
Programming Example: Polynomial Operations 204
Quick Review 216
Exercises 217
Programming Exercises 218
Data Structures Using Java
4 . Linked Lists 221
Linked Lists 222
Some Properties of Linked Lists 224
Traversing a Linked List 226
Item Insertion and Deletion 227
Insertion 228
Deletion 231
Building a Linked List 234
Building a Linked List Forward 234
Building a Linked List Backwards 238
Linked List as an ADT 240
Length of the List 245
Retrieve Data from the First and the Last Nodes 245
Insert First Node 246
Insert Last Node 247
Copy 247
Copy Constructor 249
Copy List 249
Definition of the c l a s s L i n k e d L i s t C l a s s 249
Unordered Linked Lists 250
Search List 251
Delete Node 252
Ordered Linked Lists 259
Search List 260
Insert Node 261
Delete Node
Table of Contents xiii
Doubly Linked Lists 271
Default Constructor 274
isEmptyList 274
Initialize List 274
Length of the List 274
Print 275
Reverse Print List 275
Search List 275
First and the Last Element 276
Insert Node 276
Delete Node 279
Linked Lists with Header and Trailer Nodes 282
Circular Linked Lists 283
Programming Example:Video Store 285
Quick Review 307
Exercises 308
Programming Exercises 313
5. Recursion 317
Recursive Definitions 318
Direct and Indirect Recursion 320
Infinite Recursion 320
Problem Solving Using Recursion 321
Programming Example: Converting a Number from Decimal to Binary 336
Programming Example: Sierpinski Gasket 339
Recursion or Iteration? 344
xiv Data Structures Using Java
Recursion and Backtracking: 8-Queens Puzzle 345
Backtracking 346
«-Queens Puzzle 346
Backtracking and 4-Queens Puzzle 348
8-Queens Puzzle 349
Quick Review 353
Exercises 355
Programming Exercises 357
6. Stacks 365
Stacks 366
Stack Operations 368
StackException Class 369
Implementation of Stacks as Arrays 370
Initialize Stack 373
Empty Stack 374
Full Stack 375
Push 375
Top Element 377
Pop 377
Copy 378
Constructors 379
Copy Constructor 380
Copy Stack 380
Programming Example: Highest GPA 382
Table of Contents
Linked Implementation of Stacks 387
Initialize Stack 390
Push 391
Return the Top Element 394
Pop 394
Stack as Derived from the c l a s s L i n k e d L i s t C l a s s 396
Application of Stacks: Postfix Expression Calculator 398
Postfix Expression Calculator: Graphical User Interface (GUI) 410
Removing Recursion: Nonrecursive Algorithm to Print a
Linked List Backwards 417
class Stack 424
Quick Review 426
Exercises 427
Programming Exercises 430
Queues 433
Queues 434
Queue Operations 434
Queue Exception Class 435
Implementation of Queues as Arrays 436
Linked Implementation of Queues 449
Queue Derived from the c l a s s L i n k e d L i s t C l a s s 453
Priority Queues 455
Application of Queues: Simulation 456
Designing a Queuing System 457
Customer 458
xvi Data Structures Using Java
Server 461
Server List 465
Waiting Customers' Queue 469
Quick Review 478
Exercises 479
Programming Exercises 483
8. Search Algorithms 485
Search Algorithms 486
Sequential Search 487
Ordered Lists 489
Binary Search 491
Performance of Binary Search 494
Insertion into an Ordered List 497
Lower Bound on Comparison-Based Search Algorithms 499
Hashing 500
Hash Functions: Some Examples 501
Collision Resolution 502
Collision Resolution: Open Addressing 502
Deletion: Open Addressing 507
Hashing: Implementation Using Quadratic Probing 509
Collision Resolution: Chaining (Open Hashing) 511
Hashing Analysis 513
Quick Review 514
Exercises 517
Programming Exercises 519
Table of Contents xvii
9.Sorting Algorithms 521
Sorting Algorithms 522
Selection Sort: Array-Based Lists 522
Analysis: Selection Sort 528
Insertion Sort: Array-Based Lists 529
Insertion Sort: Linked List-Based Lists 535
Analysis: Insertion Sort 540
Lower Bound on Comparison-Based Sort Algorithms 540
Quick Sort: Array-Based Lists 542
Analysis: Quick Sort 548
Merge Sort: Linked List-Based Lists 549
Divide 551
Merge 553
Analysis: Merge Sort 557
Heap Sort: Array-Based Lists 557
Build Heap 558
Analysis: Heap Sort 566
Priority Queues (Revisited) 567
Programming Example: Election Results 568
Quick Review 587
Exercises 588
Programming Exercises 589
1 0 . Binary Trees 593
Binary Trees 594
Copy Tree 600
xviii Data Structures Using Java
Binary Tree Traversal 601
Inorder Traversal 601
Preorder Traversal 602
Postorder Traversal 602
Implementing Binary Trees 605
Binary Search Trees 611
Search 614
Insert 616
Delete 618
Binary Search Tree: Analysis 625
Nonrecursive Binary Tree Traversal Algorithms 626
Nonrecursive Inorder Traversal 626
Nonrecursive Preorder Traversal 628
Nonrecursive Postorder Traversal 629
AVL (Height-Balanced) Trees 630
Insertion into AVL Trees 633
AVL Tree Rotations 640
Deletion from AVL Trees 653
Analysis: AVL Trees 654
Programming Example: Video Store (Revisited) 655
Quick Review 662
Exercises 665
Programming Exercises 669
1 1 . Graphs 671
Introduction 672
Graph Definitions and Notations 673
xx Data Structures Using Java
APPENDIX D Packages and User-Defined Classes 731
D a t a E l e m e n t and its Subclasses 731
Class: D a t a E l e m e n t 731
Class: I n t E l e m e n t 732
Class: L o n g E l e m e n t 735
Class: C h a r E l e m e n t 736
Class: F l o a t E l e m e n t 738
Class: D o u b l e E l e m e n t 739
Class: B o o l e a n E l e m e n t 741
Class: S t r i n g E l e m e n t 742
Using User-Defined Classes in a Program 744
First Way 744
Second Way 745
Using a Software Development Kit (SDK) 745
APPENDIX E Java Classes 747
Class: B o o l e a n (Package J a v a , l a n g ) 747
Constructors 747
Methods 747
C l a s s : C h a r a c t e r (Package J a v a . l a n g ) 748
Constructor 748
Methods 748
Class: D e c i m a l F o r m a t (Package J a v a . t e x t ) 749
Constructors 749
Methods 749
Table of Contents xxi
Class: Double (Package J a v a , lang) 749
Named Constants 749
Constructors 750
Methods 750
Class: E x c e p t i o n (Package J a v a . lang) 751
Constructors 751
C l a s s : F i l e R e a d e r (Package j a v a . i o ) 751
Constructors 751
Methods 751
Class: F i l e W r i t e r (Package j a v a . i o ) 751
Constructors 751
Methods 752
Class:Float (Package J a v a . l a n g ) 752
Named Constants 752
Constructors 752
Methods 752
Class: InputStreamReader (Package j a v a . i o ) 753
Constructors 753
Methods 754
Class: I n t e g e r (Package J a v a , lang) 754
Named Constants 754
Constructors 754
Methods 754
Class: J B u t t o n (Package j a v a x . swing) 755
Constructors 755
Methods 756
xx ii Data Structures Using Java
Class: JFrame (Package j a v a x . swing) 757
Constructors 757
Methods 758
Class: J L a b e l (Package j a v a x . swing) 759
Constructors 759
Methods 759
Class: J T e x t F i e l d (Package j a v a x . swing) 760
Constructors 760
Methods 761
Class: Long (Package j a v a . lang) 762
Named Constants 762
Constructors 762
Methods 762
Class: Math (Package j a v a . lang) 763
Methods 763
Class: P r i n t W r i t e r (Package j a v a . i o ) 764
Constructors 764
Methods 765
Class: S t a c k (Package j a v a . u t i l ) 766
Constructors 766
Methods 766
Class: S t r i n g (Package J a v a , lang) 766
Constructors 766
Methods 767
Class: S t r i n g T o k e n i z e r (Package j a v a . u t i l ) 769
Constructors 769
Methods 769
Table of Contents xxiii
Class: Throwable (Package J a v a , lang) 770
Constructors 770
Variables 770
Class: V e c t o r (Package j a v a . u t i l ) 770
Variables 770
Constructors 771
Methods 771
APPENDIX F Java for C++ Programmers 773
Data Types 773
Arithmetic Operators and Expressions 774
Named Constants,Variables, and Assignment Statements 774
Parsing Numeric Strings 776
Packages, Classes, Methods, and the import Statement 777
Creating a Java Application Program 777
Objects and Reference Variables 780
class String 781
Input and Output 782
Input 782
Tokenizing a String 784
Output 785
Formatting the Output of Decimal Numbers 786
File Input/Output 788
Storing (Writing) Output to a File 789
Control Structures 791
Methods and Parameters 791
Value-Returning Methods 791
xxiv Data Structures Using Java
v o i d Methods 793
Variables as Parameters 794
Arrays 795
Accessing Array Components 795
Array Index Out of Bounds Exception 796
Arrays and the Instance Variable l e n g t h 796
Arrays as Parameters to Methods 797
APPENDIX G References 799
APPENDIX H Answers to Selected Exercises 801
Chapter 1 801
Chapter 2 802
Chapter 3 803
Chapter 4 804
Chapter 5 804
Chapter 6 805
Chapter 7 805
Chapter 8 806
Chapter 9 807
Chapter 10 808
Chapter 11 810
INDEX 813