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

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

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

190 Chap. 5 Binary Trees5.12 Write a recursive function th<strong>at</strong> traverses a binary tree, <strong>and</strong> prints the value ofevery node which has <strong>at</strong> least four gre<strong>at</strong>-gr<strong>and</strong>children.5.13 Compute the overhead fraction for each of the following full binary tree implement<strong>at</strong>ions.(a) All nodes store d<strong>at</strong>a, two child pointers, <strong>and</strong> a parent pointer. The d<strong>at</strong>afield requires four bytes <strong>and</strong> each pointer requires four bytes.(b) All nodes store d<strong>at</strong>a <strong>and</strong> two child pointers. The d<strong>at</strong>a field requiressixteen bytes <strong>and</strong> each pointer requires four bytes.(c) All nodes store d<strong>at</strong>a <strong>and</strong> a parent pointer, <strong>and</strong> internal nodes store twochild pointers. The d<strong>at</strong>a field requires eight bytes <strong>and</strong> each pointer requiresfour bytes.(d) Only leaf nodes store d<strong>at</strong>a; internal nodes store two child pointers. Thed<strong>at</strong>a field requires eight bytes <strong>and</strong> each pointer requires four bytes.5.14 Why is the BST Property defined so th<strong>at</strong> nodes with values equal to the valueof the root appear only in the right subtree, r<strong>at</strong>her than allow equal-valuednodes to appear in either subtree?5.15 (a) Show the BST th<strong>at</strong> results from inserting the values 15, 20, 25, 18, 16,5, <strong>and</strong> 7 (in th<strong>at</strong> order).(b) Show the enumer<strong>at</strong>ions for the tree of (a) th<strong>at</strong> result from doing a preordertraversal, an inorder traversal, <strong>and</strong> a postorder traversal.5.16 Draw the BST th<strong>at</strong> results from adding the value 5 to the BST shown inFigure 5.13(a).5.17 Draw the BST th<strong>at</strong> results from deleting the value 7 from the BST of Figure5.13(b).5.18 Write a function th<strong>at</strong> prints out the node values for a BST in sorted orderfrom highest to lowest.5.19 Write a recursive function named smallcount th<strong>at</strong>, given the pointer tothe root of a BST <strong>and</strong> a key K, returns the number of nodes having keyvalues less than or equal to K. Function smallcount should visit as fewnodes in the BST as possible.5.20 Write a recursive function named printRange th<strong>at</strong>, given the pointer tothe root of a BST, a low key value, <strong>and</strong> a high key value, prints in sortedorder all records whose key values fall between the two given keys. FunctionprintRange should visit as few nodes in the BST as possible.5.21 Write a recursive function named checkBST th<strong>at</strong>, given the pointer to theroot of a binary tree, will return true if the tree is a BST, <strong>and</strong> false if it isnot.5.22 Describe a simple modific<strong>at</strong>ion to the BST th<strong>at</strong> will allow it to easily supportfinding the Kth smallest value in Θ(log n) average case time. Then writea pseudo-code function for finding the Kth smallest value in your modifiedBST.

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

Saved successfully!

Ooh no, something went wrong!