Suggestions
Use up and down arrows to review and enter to select.Please wait while we process your payment
If you don't see it, please check your spam folder. Sometimes it can end up there.
If you don't see it, please check your spam folder. Sometimes it can end up there.
Please wait while we process your payment
By signing up you agree to our terms and privacy policy.
Don’t have an account? Subscribe now
Create Your Account
Sign up for your FREE 7-day trial
Already have an account? Log in
Your Email
Choose Your Plan
Individual
Group Discount
Save over 50% with a SparkNotes PLUS Annual Plan!
Purchasing SparkNotes PLUS for a group?
Get Annual Plans at a discount when you buy 2 or more!
Price
$24.99 $18.74 /subscription + tax
Subtotal $37.48 + tax
Save 25% on 2-49 accounts
Save 30% on 50-99 accounts
Want 100 or more? Contact us for a customized plan.
Your Plan
Payment Details
Payment Summary
SparkNotes Plus
You'll be billed after your free trial ends.
7-Day Free Trial
Not Applicable
Renews December 7, 2023 November 30, 2023
Discounts (applied to next billing)
DUE NOW
US $0.00
SNPLUSROCKS20 | 20% Discount
This is not a valid promo code.
Discount Code (one code per order)
SparkNotes PLUS Annual Plan - Group Discount
Qty: 00
SparkNotes Plus subscription is $4.99/month or $24.99/year as selected above. The free trial period is the first 7 days of your subscription. TO CANCEL YOUR SUBSCRIPTION AND AVOID BEING CHARGED, YOU MUST CANCEL BEFORE THE END OF THE FREE TRIAL PERIOD. You may cancel your subscription on your Subscription and Billing page or contact Customer Support at custserv@bn.com. Your subscription will continue automatically once the free trial period is over. Free trial is available to new customers only.
Choose Your Plan
For the next 7 days, you'll have access to awesome PLUS stuff like AP English test prep, No Fear Shakespeare translations and audio, a note-taking tool, personalized dashboard, & much more!
You’ve successfully purchased a group discount. Your group members can use the joining link below to redeem their group membership. You'll also receive an email with the link.
Members will be prompted to log in or create an account to redeem their group membership.
Thanks for creating a SparkNotes account! Continue to start your free trial.
Please wait while we process your payment
Your PLUS subscription has expired
Please wait while we process your payment
Please wait while we process your payment
In section 1 of this topic, we provided the fundamental functions for trees, namely those than construct and destroy them. There are, however, some other tree functions that make a tree library more complete. We'll discuss a few of them here.
We have said that it is important to "hide" all of the details of an implementation from the user. With that in mind, if that user is ever going to need to check whether the tree is empty, then having a condition (tree == NULL) would not be allowed. This implies that the programmer knows that a NULL tree means an empty tree, and this is an implementation detail. A more "black box" approach would be to have a boolean function that returned a value indicating whether the tree was empty.
Here we've taken the condition that the programmer would have put into the program and wrapped it in a function that explains what the condition does.
Another boolean function that applies to a condition that comes up often tells whether or not a given node is a leaf. The condition to check is simply whether or not a node has any descendents. In other words, we simply need to check to see if both children of the given node are NULL, which guarantees that it has no descendants.
Here we make use of short circuit evaluation. When evaluating the condition to return, the computer goes through each of the boolean expressions and if any one of them is false, it will return false immediately. This is how we guarantee that we never dereference a NULL pointer.
Another useful function is one to compute the depth of a tree. Again, as we did with the destroy_tree function, we will use recursion. We know that if the tree is empty, then the depth must be zero. Otherwise, the depth will be one more than whichever is greater, the depth of the left subtree or of the right subtree. To produce a function we simply translate these steps into C.
There is almost no end to additional helper functions you could write for trees. Doing the practice problems will hopefully trigger ideas for a few more. The three we have provided should serve as examples for all of the potential functions you may need. In addition, they should provide a framework for how to go about thinking of necessary functions. In general, you should first decide whether you need to go through the entire tree (or a section of it) to solve the function and if so you should try to think of the problem recursively.
Please wait while we process your payment