Selected Solutions for Chapter 12:
Binary Search Trees
Solution to Exercise 12.12
In a heap, a node’s key is
±
both of its children’s keys. In a binary search tree, a
node’s key is
±
its left child’s key, but
²
its right child’s key.
The heap property, unlike the binarysearthtree property, doesn’t help print the
nodes in sorted order because it doesn’t tell which subtree of a node contains the
element to print before that node. In a heap, the largest element smaller than the
node could be in either subtree.
Note that if the heap property could be used to print the keys in sorted order in
O.n/
time, we would have an
O.n/
time algorithm for sorting, because building
the heap takes only
O.n/
time. But we know (Chapter 8) that a comparison sort
must take
.n
lg
n/
time.
Solution to Exercise 12.27
Note that a call to T
REE
M
INIMUM
followed by
n
NUL
1
calls to T
REE
S
UCCESSOR
performs exactly the same inorder walk of the tree as does the procedure I
NORDER

T
REE
W
ALK
. I
NORDER
T
REE
W
ALK
prints the T
REE
M
INIMUM
first, and by
definition, the T
REE
S
UCCESSOR
of a node is the next node in the sorted order
determined by an inorder tree walk.
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
This is the end of the preview.
Sign up
to
access the rest of the document.
 Spring '10
 .
 Tree traversal, NORDER T REE

Click to edit the document details