Skip to content
Unknown edited this page Feb 19, 2016 · 5 revisions

The idea is to create a Binary Tree, since Froggie can either make one Step or Jump (which equals to two steps), so each Parent Node would have two child nodes with value of Jump/Step (Left Child Node would be the Jump, Right Child Node would be the Step)...In case there is only one remain step for froggie to walk, we add that value in a single child Node called center node (CNode).

We create an Enum with Froggie walk options which are (a step, jump, none as the default).

We create a Node object, each Node would have 3 child nodes (Left node, Right node, Center node)...a parent node can only have values in two nodes (LNode & RNode) or in case of 1 step remaining, the CNode would be occupied).

below is a diagram demonstrating the Algorithm:

-Say we have a distance of 3 inches: J=Jump (2 steps); S=Step (single step); R=remaining steps

                                           3
                                          / \
                                 (R=1)Jump    Step (R=2)
                                    |         /^^^\  
                            CNode(R=0)Step (R=0)J  S(R=1)
                                                     |
                                                  (R=0)Step  

Say we have a distance of 5

                                      5
                   /^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\
                (R=3)J                                   (R=4)S
       /^^^^^^^^^^^^^^^^^^^^\                  /^^^^^^^^^^^^^^^^^^^^^^^^\        
    (R=1)J                (R=2)S            (R=2)J                     (R=3)S
      |               /^^^^^^^^^^^^\    /^^^^^^^^^^^\           /^^^^^^^^^^^^^^^^^^^\
    (R=0)S          (R=0)J     (R=1)S  (R=0)J      (R=1)S    (R=1)J              (R=2)S
                                  |                  |         |                 /^^^^^\
                               (R=0)S              (R=0)S    (R=0)S          (R=0)J   (R=1)S
                                                                                        |
                                                                                      (R=0)S

Clone this wiki locally