Dale - Computer Science Illuminated 335

Dale - Computer Science Illuminated 335 - right subtree,...

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

View Full Document Right Arrow Icon
308 Chapter 9 Abstract Data Types and Algorithms tree Call Call to Print 1 Print((N)) If Statement (N) != null Action/Call Print the left subtree 2 Print((C)) (C) != null Print the left subtree 3 Print((B)) (B) != null (L) != null Print the left subtree 4 Print((null)) (null) = null (null) = null Return to 3 3 Print B, Print right subtree 5 Print((null)) Return to 2, finished call 3 2 Print C, Print right subtree 6 Print((L)) Print the left subtree 7 Print((null)) (null) = null Return to 6 6 Print L, Print right subtree C N L S B To print the value in the root, we must first print all the values in its left subtree, which by definition are smaller than the value in the root. Once we print the value in the root, we must print all the values in the root’s
Background image of page 1
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: right subtree, which by definition are greater than the value in the root. And we are then finished. Finished? But what about the values in the left and right subtrees? How do we print them? Why, the same way of course. They are, after all, just binary search trees. This algorithm sounds too easy. That’s the beauty of recursive algorithms: They are often short and elegant (though sometimes they take some thought to trace). Let’s write it and trace it, using the tree shown below the algorithm. We number the calls in our trace because there are two recursive calls. Print (tree) If (tree is NOT null) Print (left(tree)) Write info(tree) Print(right(tree))...
View Full Document

This note was uploaded on 01/13/2011 for the course CSE 1550 taught by Professor Marianakant during the Fall '10 term at York University.

Ask a homework question - tutors are online