Sei sulla pagina 1di 4

IJP: Assignment 3

October 1, 2010

1. Deadline: Friday 15 October 2010.


2. Requirements: working, enough commented, structured (prop-
erly tabulated) applications. (Partial solutions also count.)
3. Submission: concatenate all classes and put them in one file
called asg3.java. Do not forget to write your name and login
(but *never* a password!) in the beginning of the file. Copy this
file to your home directory on thor.
Warning: email submissions will not be considered (read How-
tos on the course web page).

1 Smart Exponentiation
The straightforward possibility to raise a number x to a nonnegative integer
power n consists in repeated multiplication:

xn = x
|
· · × x},
× ·{z
n times

which is obvious but very inefficient (avoid at all costs!). Secure web connec-
tions heavily rely on raising big numbers to big powers, and never use the
straightforward approach above (except, maybe M$-made).
There is a smarter approach. Imagine, you want to compute x1024 .
Straightforwardly, you’ll need to make 1023 multiplications, which will keep
you busy for a while (try 31024 by pencil and paper during the weekend).
In contrast, I can do the same with just 10 multiplications, by repeated
squaring:

1. m2 = x × x = x2 ,

2. m4 = m2 × m2 = m22 ,

1
3. m8 = m4 × m4 = m24 ,

4. m16 = m8 × m8 = m28 ,

5. m32 = m16 × m16 = m216 ,

6. m64 = m32 × m32 = m232 ,

7. m128 = m64 × m64 = m264 ,

8. m256 = m128 × m128 = m2128 ,

9. m512 = m256 × m256 = m2256 ,

10. m1024 = m512 × m512 = m2512 .

(Clearly, mi = xi .)
Similarly, you can do the same trick for n not being the power of 2
(1024=210 ). For example, x15 is computed with 6 multiplications as follows:

((x2 × x)2 × x)2 × x,

i.e., when read left to right, x2 , x3 , x6 , x7 , x14 , x15 . Read it backwards


instead:

1. to compute x15 I need x14 × x,

2. to compute x14 I need (x7 )2 ,

3. to compute x7 I need (x6 ) × x,

4. to compute x6 I need (x3 )2 ,

5. to compute x3 I need (x2 ) × x,

6. to compute x2 I need x × x.

(Hint: isn’t it dependent on ...? How the choice between squaring 2 and
multiplication ×x for computing xi depends on i?)
Devise an algorithm and implement. Count (and explain) the number of
multiplications and squarings performed for test inputs from the keyboard.

2
A

C D
B

G H I J
E F

Figure 1: General tree example.

2 General Trees
Binary trees (we extensively studied) are sufficient to represent any tree
structure (trees of arbitrary branching degree, not just 2). An example of a
tree with unary, binary, and ternary nodes is given in Figure 1.
To represent a general tree using binary nodes:

1. use the left reference on each node to indicate the first child of the
current node,
2. use the right reference to indicate a sibling, i.e., a child with the same
parent as the current node.

The tree from Figure 1 would thus be represented as shown in Figure 2;


you better turn the head 45o left to better see the binary tree.
Implement an appropriate class for general trees using binary nodes with:

1. appropriate constructors,
2. method numberOfChildren of a node,
3. method child(i) returning the reference to the i-th child of a node (if
exists),
4. method addChild to a node,
(this will allow you to construct the tree from Figure 1; do it)

3
A

B C D

H I J
E F G

Figure 2: Representation as a binary tree.

5. methods for preorder and postorder tree traversals, to inspect a general


tree and report, for every node, its contents, the number and values
of children; for the tree from the previous step the preorder should
produce output like
node A, 3 children B, C, D
node B, 2 children E, F

6. method mirror to reconstruct a tree into one that you will see in the
mirror you put on the left or right of Figure 1 (you do not need to
invert letters, which would be easy for A, H, I, but more difficult for
other letters).

Good Luck!

Potrebbero piacerti anche