Page 1
Data Abstraction and Problem Solving with JAVA Walls and MirrorsFrank M. Carrano and Janet J. Prichard © 2001 Addison Wesley
Data Abstraction and Problem Solving with JAVA:Data Abstraction and Problem Solving with JAVA:Walls and MirrorsWalls and Mirrors
CarranoCarrano / / PrichardPrichard
Advanced ImplementationsAdvanced Implementationsof Tablesof Tables
Page 2
Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley
Figure 12.1Figure 12.1A binary search tree of a) maximum height; b) minimum height
Page 3
Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley
Figure 12.2Figure 12.2A 2-3 tree of height 3
Page 4
Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley
Figure 12.3Figure 12.3Nodes in a 2-3 tree a) a 2-node; b) a 3-node
Page 5
Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley
Figure 12.4Figure 12.4A 2-3 tree
Page 6
Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley
Figure 12.5Figure 12.5a) A balanced binary search tree; b) a 2-3 tree with the same elements
Page 7
Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley
Figure 12.6Figure 12.6a) The binary search tree of Figure 12.5a after a sequence of insertions; b) the2-3 tree of Figure 12.5b after the same insertions
Page 8
Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley
Figure 12.7Figure 12.7After inserting 39
Page 9
Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley
Figure 12.8Figure 12.8a), b) The steps for inserting 38; c) the resulting tree
Page 10
Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley
Figure 12.9Figure 12.9After inserting 37
Page 11
Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley
Figure 12.10Figure 12.10a), b), c) The steps for inserting 36; d) the resulting tree
Page 12
Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley
Figure 12.11Figure 12.11The tree after the insertion of 35, 34, and 33
Page 13
Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley
Figure 12.12Figure 12.12Splitting a leaf in a 2-3 tree
Page 14
Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley
Figure 12.13Figure 12.13Splitting an internal node in a 2-3 tree
Page 15
Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley
Figure 12.14Figure 12.14Splitting the root of a 2-3 tree
Page 16
Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley
Figure 12.15aFigure 12.15aa), b), c), d) The steps for deleting 70
Page 17
Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley
Figure 12.15b-dFigure 12.15b-da), b), c), d) The steps for deleting 70
Page 18
Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley
Figure 12.15eFigure 12.15ee) the resulting tree
Page 19
Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley
Figure 12.16Figure 12.16a), b), c) The steps for deleting 100; d) the resulting tree
Page 20
Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley
Figure 12.17aFigure 12.17aThe steps for deleting 80
Page 21
Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley
Figure 12.17b and 12.17cFigure 12.17b and 12.17cThe steps for deleting 80
Page 22
Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley
Figure 12.17d and 12.17eFigure 12.17d and 12.17eThe steps for deleting 80
Page 23
Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley
Figure 12.18Figure 12.18Results of deleting 70, 100, and 80 from a) the binary search tree of Figure12.5a and b) the 2-3 tree of Figure 12.5b
Page 24
Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley
Figure 12.19a and 12.19bFigure 12.19a and 12.19ba) Redistributing values; b) merging a leaf
Page 25
Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley
Figure 12.19c and 12.19dFigure 12.19c and 12.19dc) redistributing values and children; d) merging internal nodes
Page 26
Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley
Figure 12.19eFigure 12.19ee) deleting the root
Page 27
Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley
Figure 12.20Figure 12.20A 2-3-4 tree with the same items as the 2-3 tree in Figure 12-6b
Page 28
Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley
Figure 12.21Figure 12.21A 4-node in a 2-3-4 tree
Page 29
Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley
Figure 12.22Figure 12.22Inserting 20 into a one-node 2-3-4 tree
Page 30
Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley
Figure 12.23Figure 12.23After inserting 50 and 40
Page 31
Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley
Figure 12.24Figure 12.24The steps for inserting 70
Page 32
Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley
Figure 12.25Figure 12.25After inserting 80 and 15
Page 33
Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley
Figure 12.26Figure 12.26The steps for inserting 90
Page 34
Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley
Figure 12.27Figure 12.27The steps for inserting 100
Page 35
Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley
Figure 12.28Figure 12.28Splitting a 4-node root during insertion
Page 36
Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley
Figure 12.29Figure 12.29Splitting a 4-node whose parent is a 2-node during insertion
Page 37
Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley
Figure 12.30Figure 12.30Splitting a 4-node whose parent is a 3-node during insertion
Page 38
Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley
Figure 12.31Figure 12.31Red-black representation of a 4-node
Page 39
Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley
Figure 12.32Figure 12.32Red-black representation of a 3-node
Page 40
Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley
Figure 12.33Figure 12.33A red-black tree that represents the 2-3-4 tree in Figure 12.20
Page 41
Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley
Figure 12.34Figure 12.34Splitting a red-black representation of a 4-node that is the root
Page 42
Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley
Figure 12.35Figure 12.35Splitting a red-black representation of a 4-node whose parent is a 2-node
Page 43
Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley
Figure 12.36aFigure 12.36aSplitting a red-black representation of a 4-node whose parent is a 3-node
Page 44
Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley
Figure 12.36bFigure 12.36bSplitting a red-black representation of a 4-node whose parent is a 3-node
Page 45
Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley
Figure 12.36cFigure 12.36cSplitting a red-black representation of a 4-node whose parent is a 3-node
Page 46
Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley
Figure 12.37Figure 12.37a) An unbalanced binary search tree; b) balanced tree after rotation;c) a balanced tree after insertion
Page 47
Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley
Figure 12.38Figure 12.38a) An unbalanced binary search tree; b) a balanced tree after a single leftrotation
Page 48
Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley
Figure 12.39Figure 12.39Before and after a single left rotation that decreases the tree’s height
Page 49
Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley
Figure 12.40Figure 12.40a) An unbalanced binary search tree; b) a balanced tree after a single leftrotation
Page 50
Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley
Figure 12.41Figure 12.41Before and after a single left rotation that does not affect the tree’s height
Page 51
Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley
Figure 12.42Figure 12.42a) Before; b) during; and c) after a double rotation
Page 52
Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley
Figure 12.43Figure 12.43Before and after a double rotation that decreases the tree’s height
Page 53
Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley
Figure 12.44Figure 12.44Address calculator
Page 54
Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley
Figure 12.45Figure 12.45A collision
Page 55
Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley
Figure 12.46Figure 12.46Linear probing with
h(x) = x mod 101
Page 56
Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley
Figure 12.47Figure 12.47Quadratic probing with
h(x) = x mod 101
Page 57
Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley
Figure 12.48Figure 12.48Double hashing during the
insertion of 58, 14, and 91
Page 58
Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley
Figure 12.49Figure 12.49Separate chaining
Page 59
Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley
Figure 12.50Figure 12.50The relative efficiency of four collision-resolution methods
Page 60
Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley
Figure 12.51Figure 12.51Two data structures that share the same data: a) a sorted linked list; b) areference-based queue
Page 61
Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley
Figure 12.52Figure 12.52A queue referencing a sorted linked list
Page 62
Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley
Figure 12.53Figure 12.53A queue referencing into a doubly linked list
Page 63
Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley
Figure 12.54Figure 12.54A queue referencing a double linked binary search tree
Page 64
Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley
Figure 12.55Figure 12.55A red-black tree for Exercise 7