You are on page 1of 4

April Nop

Fall 2016
Homework 4

CHAPTER 7
3. What is the maximum number of nodes that can be stored in a binary tree with 10
levels?
= 2 ^ 10 1
=1024 1
=1023
4. What is the minimum number of levels in a binary tree containing 6023 nodes?
= ceiling [log2(N + 1)]
= ceiling [log2(6023 + 1)]
= ceiling [log2(6024)] 12.5
212 6023 213
4096 6023 8192
So at least 0-13 levels (= 14 levels)
5. What is the maximum number of levels in a tree containing 6023 nodes?
= 6023

6. What is the maximum number of nodes that can be stored in level 16 of a binary tree?
216 = 65,536

8. True or false:
a. The highest level of a complete binary tree contains more nodes than all of the
other levels combined.
False
b. The root node is always at the highest level of the tree.
False
c. Level 9 is the highest level in a 10 level tree.
True

11. Draw the binary search tree resulting from inserting the nodes with the integer key
values: 68, 23, 45, 90, 70, 21, and 55 into the tree. Assume key 68 is inserted first, then
key 23, etc.
6
8
2
3
2
1

9
0
4
5

7
0

5
5
20. Give the output produced by an NLR output scan of the tree shown in Figure 7.3f.
68-23-21-45-55-90-70

CHAPTER 8
4. Give three factors to be considered in selecting a sorting algorithm for a particular
application.
How much speed and memory you have available, the type of data that needs to be
stored, and the programmers capability

6. Give an expression for the minimum number of comparisons required to sort n items.
= O(nlog2n)
10. Under what conditions is the Binary Tree Sort fast?
Fast for random data, which produces a balanced tree

11. Calculate the minimum and maximum number of comparisons required to sort 1,000,000
items using the Binary Tree Sort.
Minimum
= O ((1,000,000) log2(1,000,000))
= 19,931,568.5693
Maximum
= O (1,000,000 ^2)
= 1,000,000,000,000

13. The integers 65, 80, 70, 18, 86, 6, and 37 are to be sorted using the Binary Tree Sort.
Assume the integers are processed by the algorithm in the order given.
a. Show the binary tree that results from sorting.
6
5
8
0

1
8

3
7

8
6

7
0

b. As the integers are placed in the tree, count the number of comparisons made.
What is the total number of comparisons?
10

23. Define the term "heap."


A binary tree in which (the value of) each parent is greater than both of its
children

31. A 16 element array stores the integers 81, 16, 2, 89, 54, 23, 76, 25, 37, 107, 1, 74, 45,
16, 31, and 58 in elements 0 through 15, respectively. They are to be sorted using the
merge sort.
a. How many passes will be performed to complete the sort?
4
b. Show the contents of the array at the beginning of the sort and after each pass.
81, 16 2, 89 54, 23 76, 25 37, 107
1, 74 45, 16 31, 58
16, 81 2, 89

23, 54 25, 76 37, 107

1, 74

16, 45 31, 58

2, 16, 81, 89 23, 25, 54, 76

1, 37, 74, 107 16, 31, 45, 58

2, 16, 23, 25, 54, 76, 81, 89

1, 16, 31, 37, 45, 58, 74, 107

1, 2, 16, 16, 23, 25, 31, 37, 45, 54, 58, 74, 76, 81, 89, 107

c. Count the number of comparisons that were made to sort the 16 items.
13

d. Count the number of swaps made to complete the sort of the 16 items.
47
e. Compare your answers in parts (c) and (d) of this question to results obtained
when the formulas presented in Table 8.6 are used.
SE (0.75n) log2n
= (0.75(16)) log2(16)
= 48
PMS = log2n
= log2(16)
=4
CAms = [(n/2) + (n-1)]/2
= [(16/2) + (16-1)]/2
= [(8) + (15)]/2
= [(8) + (15)]/2
=23/2
= 11.5
SEMS = PMS * CAms
= 4 * 11.5
=46
total swaps = 2nlog2n
= 128
=(2*SEMS)/0.75
= (2 * 46) /0.75
=122.67
32. What characteristic of a data set makes the Quicksort slow?
It does not perform quickly when input is sorted in reverse or nearly sorted in
reverse