11.07.2015 Views

Data Structures and Algorithm Analysis - Computer Science at ...

Data Structures and Algorithm Analysis - Computer Science at ...

Data Structures and Algorithm Analysis - Computer Science at ...

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

192 Chap. 5 Binary Trees(c) Wh<strong>at</strong> is the average number of bits required by a character using theHuffman code for this alphabet?5.32 You must keep track of some d<strong>at</strong>a. Your options are:(1) A linked-list maintained in sorted order.(2) A linked-list of unsorted records.(3) A binary search tree.(4) An array-based list maintained in sorted order.(5) An array-based list of unsorted records.For each of the following scenarios, which of these choices would be best?Explain your answer.(a) The records are guaranteed to arrive already sorted from lowest to highest(i.e., whenever a record is inserted, its key value will always begre<strong>at</strong>er than th<strong>at</strong> of the last record inserted). A total of 1000 inserts willbe interspersed with 1000 searches.(b) The records arrive with values having a uniform r<strong>and</strong>om distribution(so the BST is likely to be well balanced). 1,000,000 insertions areperformed, followed by 10 searches.(c) The records arrive with values having a uniform r<strong>and</strong>om distribution (sothe BST is likely to be well balanced). 1000 insertions are interspersedwith 1000 searches.(d) The records arrive with values having a uniform r<strong>and</strong>om distribution (sothe BST is likely to be well balanced). 1000 insertions are performed,followed by 1,000,000 searches.5.9 Projects5.1 Re-implement the composite design for the binary tree node class of Figure5.11 using a flyweight in place of null pointers to empty nodes.5.2 One way to deal with the “problem” of null pointers in binary trees is touse th<strong>at</strong> space for some other purpose. One example is the threaded binarytree. Extending the node implement<strong>at</strong>ion of Figure 5.7, the threaded binarytree stores with each node two additional bit fields th<strong>at</strong> indic<strong>at</strong>e if the childpointers lc <strong>and</strong> rc are regular pointers to child nodes or threads. If lcis not a pointer to a non-empty child (i.e., if it would be null in a regularbinary tree), then it instead stores a pointer to the inorder predecessor of th<strong>at</strong>node. The inorder predecessor is the node th<strong>at</strong> would be printed immedi<strong>at</strong>elybefore the current node in an inorder traversal. If rc is not a pointer to achild, then it instead stores a pointer to the node’s inorder successor. Theinorder successor is the node th<strong>at</strong> would be printed immedi<strong>at</strong>ely after thecurrent node in an inorder traversal. The main advantage of threaded binary

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

Saved successfully!

Ooh no, something went wrong!