{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Data Str &amp; Algorithm HW Solutions 33

# Data Str &amp; Algorithm HW Solutions 33 - 5.4(a...

This preview shows page 1. Sign up to view the full content.

33 5.3 Base Case : For the tree of one leaf node, I = 0 , E = 0 , n = 0 , so the theorem holds. Induction Hypothesis : The theorem holds for the full binary tree containing n internal nodes. Induction Step : Take an arbitrary tree (call it T ) of n internal nodes. Select some internal node x from T that has two leaves, and remove those two leaves. Call the resulting tree T’ . Tree T’ is full and has n 1 internal nodes, so by the Induction Hypothesis E = I + 2( n 1) . Call the depth of node x as d . Restore the two children of x , each at level d +1 . We have now added d to I since x is now once again an internal node. We have now added 2( d + 1) d = d + 2 to E since we added the two leaf nodes, but lost the contribution of x to E . Thus, if before the addition we had E = I + 2( n 1) (by the induction hypothesis), then after the addition we have E + d = I + d + 2 + 2( n 1) or E = I + 2 n which is correct. Thus, by the principle of mathematical induction, the theorem is correct.
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 5.4 (a) template <class Elem> void inorder(BinNode<Elem>* subroot) { if (subroot == NULL) return; // Empty, do nothing preorder(subroot->left()); visit(subroot); // Perform desired action preorder(subroot->right()); } (b) template <class Elem> void postorder(BinNode<Elem>* subroot) { if (subroot == NULL) return; // Empty, do nothing preorder(subroot->left()); preorder(subroot->right()); visit(subroot); // Perform desired action } 5.5 The key is to search both subtrees, as necessary. template <class Key, class Elem, class KEComp> bool search(BinNode<Elem>* subroot, Key K); if (subroot == NULL) return false; if (subroot->value() == K) return true; if (search(subroot->right())) return true; return search(subroot->left()); }...
View Full Document

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern