12.07.2015 Views

A Practical Introduction to Data Structures and Algorithm Analysis

A Practical Introduction to Data Structures and Algorithm Analysis

A Practical Introduction to Data Structures and Algorithm Analysis

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

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

200 Chap. 5 Binary Treesprints the root, then all nodes of level 1, then all nodes of level 2, <strong>and</strong> soon. Hint: Preorder traversals make use of a stack through recursive calls.Consider making use of another data structure <strong>to</strong> help implement the levelordertraversal.5.8 Write a recursive function that returns the height of a binary tree.5.9 Write a recursive function that returns a count of the number of leaf nodes ina binary tree.5.10 Assume that a given BST s<strong>to</strong>res integer values in its nodes. Write a recursivefunction that sums the values of all nodes in the tree.5.11 Assume that a given BST s<strong>to</strong>res integer values in its nodes. Write a recursivefunction that traverses a binary tree, <strong>and</strong> prints the value of every node who’sgr<strong>and</strong>parent has a value that is a multiple of five.5.12 Write a recursive function that traverses a binary tree, <strong>and</strong> prints the value ofevery node which has at least four great-gr<strong>and</strong>children.5.13 Compute the overhead fraction for each of the following full binary tree implementations.(a) All nodes s<strong>to</strong>re data, two child pointers, <strong>and</strong> a parent pointer. The datafield requires four bytes <strong>and</strong> each pointer requires four bytes.(b) All nodes s<strong>to</strong>re data <strong>and</strong> two child pointers. The data field requiressixteen bytes <strong>and</strong> each pointer requires four bytes.(c) All nodes s<strong>to</strong>re data <strong>and</strong> a parent pointer, <strong>and</strong> internal nodes s<strong>to</strong>re twochild pointers. The data field requires eight bytes <strong>and</strong> each pointer requiresfour bytes.(d) Only leaf nodes s<strong>to</strong>re data; internal nodes s<strong>to</strong>re two child pointers. Thedata field requires eight bytes <strong>and</strong> each pointer requires four bytes.5.14 Why is the BST Property defined so that nodes with values equal <strong>to</strong> the valueof the root appear only in the right subtree, rather than allow equal-valuednodes <strong>to</strong> appear in either subtree?5.15 (a) Show the BST that results from inserting the values 15, 20, 25, 18, 16,5, <strong>and</strong> 7 (in that order).(b) Show the enumerations for the tree of (a) that result from doing a preordertraversal, an inorder traversal, <strong>and</strong> a pos<strong>to</strong>rder traversal.5.16 Draw the BST that results from adding the value 5 <strong>to</strong> the BST shown inFigure 5.13(a).5.17 Draw the BST that results from deleting the value 7 from the BST of Figure5.13(b).5.18 Write a function that prints out the node values for a BST in sorted orderfrom highest <strong>to</strong> lowest.

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

Saved successfully!

Ooh no, something went wrong!