CSDS233 HW2
CSDS233 HW2
Arrays have constant indexing as the different elements in the array have addresses
varying by a constant value, which is 4 in this case. If the 20th element has an address of
500, then
Stacks and Queues are both linear data structures like arrays and linked lists and both
store collections of elements. They are both dynamic in size, so they can dynamically grow or
shrink in size as elements are added and removed over time. They can both be implemented
using other data structures, and specify the order in which elements are processed. For
example, stacks follow ”Last In, First Out” (LIFO), while queues follow ”First In, First
Out” (FIFO). These behaviors cause them to be useful for many algorithms.
(c) For a binary tree with a depth of 8, what are the maximum and minimum possible num-
bers of nodes it can have? (Give the calculation process or explanation.)
A minimal binary tree has only one node per level as each node only has one child.
Therefore assuming the parent node is at depth 0, then there can be a minimum 9 nodes in
a binary tree with depth 8 .
A full binary tree, on the other hand, has 2 children per node. The number of nodes
on any given level of a binary tree is given by 2h , where h is the current level on the tree
(0-based
P8 index). Therefore the total number of nodes is given by 20 + 21 + 22 + · · · + 28 =
n
n=0 2 = 511 .
1
Trevor Swan CSDS233 - HW2 10/08/24
(d) A complete binary tree contains 256 nodes. What is its depth, and how many leaf nodes
does it have? (Give the calculation process or explanation.)
Generally, the total number of nodes in a tree can be found without using a summation
as Nmax = 2d+1 − 1, where d is the depth of the tree. This is derived from the geometric
rn −1
series relation S = a1r−1 where n is the total number of terms (the depth plus 1).
We can solve for the depth of the tree by taking the floor function of d after solving for
it as d = ⌈ ln(N +1)
ln 2
− 1⌉. Given that N = 256, we find that d = 8 . We can solve for the
total number of leaf nodes by observing that in a full binary tree with a depth of 7, there
are 27 = 128 leaf nodes. Also, a tree with a depth of 7 has 255 total nodes, and ours has
256. Therefore one of the nodes on the 7th level has a child, but there are still a total of
128 leaf nodes on the tree.
(e) If the input sequence of a stack is 1, 2, 3, 4, 5, 6, provide three possible output sequences.
2
Trevor Swan CSDS233 - HW2 10/08/24
P.2) Consider the following binary tree with 15 nodes. Describe the order of
nodes visited in each of the following traversals of the tree:
25
9 33
12 7 11 19
10 17 3 23 27 8
5 6
(a) In-Order
Nodes are visited in Left-Root-Right order. This gives:
(b) Pre-Order
Nodes are visited in Root-Left-Right order. This gives:
(c) Post-Order
Nodes are visited in Left-Right-Root order. This gives:
3
Trevor Swan CSDS233 - HW2 10/08/24
P.3) Convert the following expressions from infix into postfix or from postfix
into infix. (Use PEMDAS to convert expressions)
(a) 7 / 5 * G ˆ 6 * C + A ˆ 3 * B + Z - T
This expression is in infix, and should be converted to postfix as follows:
75/G6ˆ*C*A3ˆB*+Z+T-
(b) X * Y2 * q 7 -Z
2
1− A2
B
We can first rewrite this so that its easier to work with as:
X * Y ˆ 2 * 7 / (1 - A ˆ 2 / B ˆ 2) ˆ 0.5 - Z
This expression is in infix, and should be converted to postfix as follows:
X Y 2 ˆ * 7 * 1 A 2 ˆ B 2 ˆ / - 0.5 ˆ / Z -
(c) X Y Z - / A 8 O P Q / - * G 2 ˆ * / -
This expression is in postfix, and should be converted to infix as follows:
X / (Y - Z) - A / (8 * (O - P / Q) * G ˆ 2)
(d) b 2 - 4 a c * - 2 a * / + 7 c * +
This expression is in postfix, and should be converted to infix as follows:
b - 2 + (4 - a * c) / (2 * a) + 7 * c