26.07.2013 Views

Java How to Program Fourth Edition - DCC

Java How to Program Fourth Edition - DCC

Java How to Program Fourth Edition - DCC

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 19 Data Structures 1123<br />

the same “go left” or “go right” decisions on each comparison as the original value did. Thus,<br />

the insertion operation eventually compares the duplicate with a node containing the same<br />

value. At this point, the insertion operation might simply discard the duplicate value.<br />

Searching a binary tree for a value that matches a key value is fast, especially for tightly<br />

packed trees. In a tightly packed tree, each level contains about twice as many elements as<br />

the previous level. Figure 19.19 is a tightly packed binary tree. A binary search tree with n<br />

elements has a minimum of log 2n levels. Thus, at most log 2n comparisons are required<br />

either <strong>to</strong> find a match or <strong>to</strong> determine that no match exists. Searching a (tightly packed)<br />

1000-element binary search tree requires at most 10 comparisons, because 2 10 > 1000.<br />

Searching a (tightly packed) 1,000,000-element binary search tree requires at most 20 comparisons,<br />

because 2 20 > 1,000,000.<br />

The chapter exercises present algorithms for several other binary tree operations, such<br />

as deleting an item from a binary tree, printing a binary tree in a two-dimensional tree<br />

format and performing a level-order traversal of a binary tree. The level-order traversal of<br />

a binary tree visits the nodes of the tree row-by-row, starting at the root node level. On each<br />

level of the tree, a level-order traversal visits the nodes from left <strong>to</strong> right. Other binary tree<br />

exercises include allowing a binary search tree <strong>to</strong> contain duplicate values, inserting string<br />

values in a binary tree and determining how many levels are contained in a binary tree.<br />

SUMMARY<br />

• Dynamic data structures can grow and shrink at execution time.<br />

• Linked lists are collections of data items “lined up in a row”—insertions and deletions can be made<br />

anywhere in a linked list.<br />

• Stacks are important in compilers and operating systems—insertions and deletions are made only<br />

at one end of a stack, its <strong>to</strong>p.<br />

• Queues represent waiting lines; insertions are made at the back (also referred <strong>to</strong> as the tail) of a<br />

queue and deletions are made from the front (also referred <strong>to</strong> as the head) of a queue.<br />

• Binary trees facilitate high-speed searching and sorting of data, eliminating duplicate data items<br />

efficiently, representing file system direc<strong>to</strong>ries and compiling expressions in<strong>to</strong> machine language.<br />

• A self-referential class contains a reference that refers <strong>to</strong> another object of the same class type.<br />

Self-referential objects can be linked <strong>to</strong>gether <strong>to</strong> form useful data structures such as lists, queues,<br />

stacks and trees.<br />

• Creating and maintaining dynamic data structures requires dynamic memory allocation—the ability<br />

for a program <strong>to</strong> obtain more memory space at execution time <strong>to</strong> hold new nodes and <strong>to</strong> release<br />

space no longer needed.<br />

• The limit for dynamic memory allocation can be as large as the available physical memory in the<br />

computer or the amount of available disk space in a virtual-memory system. Often, the limits are<br />

much smaller because the computer’s available memory must be shared among many users.<br />

• Opera<strong>to</strong>r new takes as an operand the type of the object being dynamically allocated and returns<br />

a reference <strong>to</strong> a newly created object of that type. If no memory is available, new throws an Out-<br />

OfMemoryError.<br />

• A linked list is a linear collection (i.e., a sequence) of self-referential class objects, called nodes,<br />

connected by reference links.<br />

• A linked list is accessed via a reference <strong>to</strong> the first node of the list. Each subsequent node is accessed<br />

via the link-reference member s<strong>to</strong>red in the previous node.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!