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 1125<br />

destination. The routing node routes one packet at a time, so additional packets are enqueued until<br />

the router can route them.<br />

• A file server in a computer network handles file-access requests from many clients throughout the<br />

network. Servers have a limited capacity <strong>to</strong> service requests from clients. When that capacity is<br />

exceeded, client requests wait in queues.<br />

• A tree is a nonlinear, two-dimensional data structure.<br />

• Tree nodes contain two or more links.<br />

• A binary tree is a tree whose nodes all contain two links. The root node is the first node in a tree.<br />

• Each link in the root node refers <strong>to</strong> a child. The left child is the first node in the left subtree, and<br />

the right child is the first node in the right subtree.<br />

• The children of a node are called siblings. A node with no children is called a leaf node.<br />

• Computer scientists normally draw trees from the root node down.<br />

• A binary search tree (with no duplicate node values) has the characteristic that the values in any<br />

left subtree are less than the value in its parent node, and the values in any right subtree are greater<br />

than the value in its parent node.<br />

• A node can be inserted only as a leaf node in a binary search tree.<br />

• An inorder traversal of a binary search tree processes the node values in ascending order.<br />

• The process of creating a binary search tree actually sorts the data—and thus this process is called<br />

the binary tree sort.<br />

• In a preorder traversal, the value in each node is processed as the node is visited. After the value<br />

in a given node is processed, the values in the left subtree are processed, then the values in the right<br />

subtree are processed.<br />

• In a pos<strong>to</strong>rder traversal, the value in each node is processed after the values of its children.<br />

• The binary search tree facilitates duplicate elimination. As the tree is created, attempts <strong>to</strong> insert a<br />

duplicate value are recognized because a duplicate follows the same “go left” or “go right” decisions<br />

on each comparison as the original value did. Thus, the duplicate eventually is compared<br />

with a node containing the same value. The duplicate value could simply be discarded at this point.<br />

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

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

level. So a binary search tree with n elements has a minimum of log2n levels, and thus at<br />

most log2n, comparisons would have <strong>to</strong> be made either <strong>to</strong> find a match or <strong>to</strong> determine that no<br />

match exists. Searching a (tightly packed) 1000-element binary search tree requires at most 10<br />

comparisons, because 210 > 1000. Searching a (tightly packed) 1,000,000-element binary search<br />

tree requires at most 20 comparisons, because 220 > 1,000,000.<br />

TERMINOLOGY<br />

binary search tree enqueue<br />

binary tree FIFO (first-in, first-out)<br />

binary tree sort head of a queue<br />

child node inorder traversal of a binary tree<br />

children inserting a node<br />

delegating leaf node<br />

deleting a node left child<br />

dequeue left subtree<br />

duplicate elimination level-order traversal of a binary tree<br />

dynamic data structures LIFO (last-in, first-out)

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

Saved successfully!

Ooh no, something went wrong!