• Home
  • Textbooks
  • Data Structures and Algorithms in C++
  • BINARY TREES

Data Structures and Algorithms in C++

Adam Drozdek

Chapter 6

BINARY TREES - all with Video Answers

Educators


Chapter Questions

Problem 1

The function search ( ) given in Section 6.3 is well suited for searching binary search trees. Try to adopt all four traversal algorithms so that they become search procedures for any binary tree.

Check back soon!

Problem 2

Write functions to count the number of nodes in a binary tree, the number of leaves, the number of right children, and the height of the tree.

Check back soon!

Problem 3

Write a function that checks whether or not a binary tree is perfectly balanced.
FIGURE 6.64 An example of a binary search tree.
(Figure Cant Copy)

Check back soon!

Problem 4

Design an algorithm to test whether a binary tree is a binary search tree.

Check back soon!

Problem 5

Write a function to delete all leaves from a binary tree.

Check back soon!
01:24

Problem 6

Apply preorder (), inorder ( ), and postorder () to the tree in Figure 6.64, if visit $(p)$ is defined as:
a. if (p->left $1=0$ & & p->key - p->left->key $<2$ )
$$
\text { p->left } \rightarrow \text { key }+=2 \text {; }
$$
b. if $(p->$ left $==0)$
$$
\mathrm{p}->\text { right }=0 \text {; }
$$
c. if $(\mathrm{p}->$ left $==0)$
$$
\mathrm{p}->\text { left }=\text { new IntBSTNode }(\mathrm{p}->\text { key }-1)
$$
d. {
$$
\begin{aligned}
& \text { tmp }=p->r i g h t ; \\
& p->\text { right }=p->\text { left; } \\
& p->\text { left }=\text { tmp; } \\ \}
\end{aligned}
$$

James Kiss
James Kiss
Numerade Educator

Problem 7

Show a tree for which the preorder and inorder traversals generate the same sequence.

Check back soon!

Problem 8

Figure 6.59 indicates that the inorder traversal for different trees can result in the same sequence. Is this possible for the preorder or postorder traversals? If it is, show an example.

Check back soon!

Problem 9

Draw all possible binary search trees for the three elements $A, B$, and $C$.

Check back soon!
01:59

Problem 10

What are the minimum and maximum numbers of leaves in a balanced tree of height $h$ ?

Nick Johnson
Nick Johnson
Numerade Educator

Problem 11

Write a function to create a mirror image of a binary tree.
FIGURE 6.65 Printing a binary search tree (a) and a threaded tree (b) sideways.
(Figure Cant Copy)

Check back soon!

Problem 12

Consider an operation $R$ that for a given traversal method $t$ processes nodes in the opposite order than $t$ and an operation $C$ that processes nodes of the mirror image of a given tree using traversal method $t$. For the tree traversal methods-preorder, inorder, and postorder-determine which of the following nine equalities are true:
$$
\begin{aligned}
& R(\text { preorder })=C(\text { preorder }) \\
& R(\text { preorder })=C(\text { inorder }) \\
& R(\text { preorder })=C \text { (postorder }) \\
& R(\text { inorder })=C(\text { preorder }) \\
& R(\text { inorder })=C(\text { inorder }) \\
& R(\text { inorder })=C(\text { postorder }) \\
& R(\text { postorder })=C(\text { preorder }) \\
& R(\text { postorder })=C(\text { inorder }) \\
& R(\text { postorder })=C(\text { postorder })
\end{aligned}
$$

Check back soon!

Problem 13

Using inorder, preorder, and postorder tree traversal, visit only leaves of a tree. What can you observe? How can you explain this phenomenon?

Check back soon!

Problem 14

(a) Write a function which prints sideways each binary tree with proper indentation, as in Figure 6.65a. (b) Adopt this function to print a threaded tree sideways; if appropriate, print the key in the successor node, as in Figure 6.65b.

Check back soon!

Problem 15

Outline functions for inserting and deleting a node in a threaded tree in which threads are put only in the leaves in the way illustrated by Figure 6.66.

Check back soon!

Problem 16

The tree in Figure 6.66b includes threads lining predecessors and successors according to the postorder traversal. Are these threads adequate to perform threaded preorder, inorder, and postorder traversals?
FIGURE 6.66 Examples of threaded trees.
(Figure Cant Copy)

Check back soon!
03:19

Problem 17

Apply the function balance () to the English alphabet to create a balanced tree.

Carson Merrill
Carson Merrill
Numerade Educator
01:13

Problem 18

A sentence $D p q$ that uses a Sheffer's alternative is false only if both $p$ and $q$ are true. In 1925, J Lukasiewicz simplified Nicod's axiom from which all theses of propositional logic can be derived. Transform the Nicod-Lukasiewicz axiom into an infix parenthesized sentence and build a binary tree for it. The axiom is $D D p D q r D D s D s s D D s q$ DpsDps.

Dalia Rodriguez
Dalia Rodriguez
Numerade Educator

Problem 19

Write an algorithm for printing a parenthesized infix expression from an expression tree. Do not include redundant parentheses.

Check back soon!

Problem 20

Hibbard's (1962) algorithm to delete a key from a binary search tree requires that if the node containing the key has a right child, then the key is replaced by the smallest key in the right subtree; otherwise, the node with the key is removed. In what respect is Knuth's algorithm (deleteByCopy ing ()) an improvement?

Check back soon!

Problem 21

Which tree of four nodes is the worst case for generating a backbone in the DSW algorithm? Execute this procedure in this tree by hand.

Check back soon!

Problem 22

Define a binary search tree in terms of the inorder traversal.

Check back soon!
04:26

Problem 23

A Fibonacci tree can be considered the worst case AVL tree in that it has the smallest number of nodes among AVL trees of height $h$. Draw Fibonacci trees for $h=1,2,3,4$ and justify the name of the tree.

Victoria Dollar
Victoria Dollar
Numerade Educator

Problem 24

One-sided height-balanced trees are AVL trees in which only two balance factors are allowed: -1 and 0 or 0 and +1 (Zweben and McDonald 1978). What is the rationale for introducing this type of tree?

Check back soon!

Problem 25

In lazy deletion, nodes to be deleted are retained in the tree and only marked as deleted. What are the advantages and disadvantages of this approach?

Check back soon!
01:32

Problem 26

What is the number of comparisons and swaps in the best case for creating a heap using (a) Williams's method and (b) Floyd's method?

James Chok
James Chok
Numerade Educator

Problem 27

A crossover between Floyd's and Williams's methods for constructing a heap is a method in which an empty position occupied by an element is moved down to the bottom of the tree and then the element is moved up, as in Williams's method, the tree from the position that was just moved down. A pseudocode of this function is as follows:
i= $(\mathrm{n}-1) / 2 ; / /$ position of the last parent in the array of $n$ elements;
while $(i>=0)$
$/ /$ Floyd's phase:
tmp = array [ $i] ;$
consider element array [i] empty and move it down to the bottom
$\quad$ swapping it every time with larger child;
put tmp in the leaf at which this process ended;
$/ /$ Williams's phase:
while tmp is not the root and it is larger than its parent
swap tmp with its parent;
i--; // go to the preceding parent;
$i=(n-1) / 2 ; / /$ position of the last parent in the array of $n$ elements;
while $(i>=0)$
// Floyd's phase:
$\mathrm{tmp}=\operatorname{array}[i] ;$
consider element array [i] empty and move it down to the bottom
swapping it every time with larger child;
put tmp in the leaf at which this process ended;
// Williams's phase:
while tmp is not the root and it is larger than its parent
swap tmp with its parent;
i--; // go to the preceding parent;
It has been shown that this algorithm requires $1.65 n$ comparisons in the average case (McDiarmid and Reed 1989). Show changes in the array [ 2861101531211 ] during execution of the algorithm. What is the worst case?

Check back soon!