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.

Sec. 5.8 Exercises 189techniques, see Managing Gigabytes by Witten, Moff<strong>at</strong>, <strong>and</strong> Bell [WMB99], <strong>and</strong>Codes <strong>and</strong> Cryptography by Dominic Welsh [Wel88]. Tables 5.23 <strong>and</strong> 5.24 arederived from Welsh [Wel88].5.8 Exercises5.1 Section 5.1.1 claims th<strong>at</strong> a full binary tree has the highest number of leafnodes among all trees with n internal nodes. Prove th<strong>at</strong> this is true.5.2 Define the degree of a node as the number of its non-empty children. Proveby induction th<strong>at</strong> the number of degree 2 nodes in any binary tree is one lessthan the number of leaves.5.3 Define the internal p<strong>at</strong>h length for a tree as the sum of the depths of allinternal nodes, while the external p<strong>at</strong>h length is the sum of the depths of allleaf nodes in the tree. Prove by induction th<strong>at</strong> if tree T is a full binary treewith n internal nodes, I is T’s internal p<strong>at</strong>h length, <strong>and</strong> E is T’s external p<strong>at</strong>hlength, then E = I + 2n for n ≥ 0.5.4 Explain why function preorder2 from Section 5.2 makes half as manyrecursive calls as function preorder. Explain why it makes twice as manyaccesses to left <strong>and</strong> right children.5.5 (a) Modify the preorder traversal of Section 5.2 to perform an inordertraversal of a binary tree.(b) Modify the preorder traversal of Section 5.2 to perform a postordertraversal of a binary tree.5.6 Write a recursive function named search th<strong>at</strong> takes as input the pointer tothe root of a binary tree (not a BST!) <strong>and</strong> a value K, <strong>and</strong> returns true ifvalue K appears in the tree <strong>and</strong> false otherwise.5.7 Write an algorithm th<strong>at</strong> takes as input the pointer to the root of a binarytree <strong>and</strong> prints the node values of the tree in level order. Level order firstprints 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 d<strong>at</strong>a structure to help implement the levelordertraversal.5.8 Write a recursive function th<strong>at</strong> returns the height of a binary tree.5.9 Write a recursive function th<strong>at</strong> returns a count of the number of leaf nodes ina binary tree.5.10 Assume th<strong>at</strong> a given BST stores integer values in its nodes. Write a recursivefunction th<strong>at</strong> sums the values of all nodes in the tree.5.11 Assume th<strong>at</strong> a given BST stores integer values in its nodes. Write a recursivefunction th<strong>at</strong> traverses a binary tree, <strong>and</strong> prints the value of every node who’sgr<strong>and</strong>parent has a value th<strong>at</strong> is a multiple of five.

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

Saved successfully!

Ooh no, something went wrong!