(42S,Free42,DM42) Binary Tree
|
01-18-2022, 04:08 AM
(This post was last modified: 01-18-2022 06:24 AM by arhtur.)
Post: #1
|
|||
|
|||
(42S,Free42,DM42) Binary Tree
I searched for a binary tree implementation and didn't see one. How, you might ask, can a binary tree be implemented on the HP42s (or Free42 or DM42)? Some compromises were needed. I limited the tree to 99 elements and each element can have a value from 1 to 99. Each binary tree node is encoded with the left pointer in the thousands and hundreds digits and the right pointer in the tens and ones digits and the value encoded in the tenths and hundredths digits. On the Free42, I was able to make a binary tree with 50 nodes. On the DM42, I had to reduce the size to 25 nodes. This is perhaps due to there being less levels of recursion available on it. This was tested on the Free42 and DM42, but I assume it should also work on the HP42s.
First, in the program FIL, XEQ FIL to fill registers 150 to 199 with the numbers 1 to 50, and initialize registers 1 to 99 with 0. Code: 00 { 95-Byte Prgm } Next, in the program MX, XEQ MX to randomly mix the numbers 1 to 25 in registers 150 to 174. Code: 00 { 61-Byte Prgm } Third, in the program MINS (main insertion), XEQ MINS to fill the binary tree. MINS calls IT. IT uses register C for the address of the insertion point, register D for the data value to insert and register E holds the next free tree node. 1. If the insertion point has a 0 value, then we add the value of register D (divided by 100) to the insertion point. 2. If the insertion point is not 0, we go to IT1 and compare the node value to register D. If register D is greater than the node value, we go to IT2. 3. If register D is not greater than the node value, we check the left pointer (in the thousands and hundreds digits). If the left pointer is not 0, we go to IT3. 4. If the left pointer is 0, we store D in the tenths and hundredths digits of the new node register E points to. We set our current node left pointer to the register E points to, increment E and return. 5. In IT3, if the left pointer (in step 4) was not 0, we set C to the value of the left pointer of the node and recursively call IT. 6. In IT2 (register D > node value), we check the right pointer (in the tens and ones digits). If the right pointer is not 0, we go to IT4. 7. If the right pointer is 0, we store D in the tenths and hundredths digits of the new node register E points to. We set our current node right pointer to the register E points to, increment E and return. 8. In IT4, if the right pointer (in step 7) was not 0, we set C to the value of the right pointer of the node and recursively call iT. Code: 00 { 64-Byte Prgm } Code: 00 { 197-Byte Prgm } Last, we traverse the tree in the program TV with XEQ TV (in MINS, at the end, we set C to 1 (the root of the tree)). The in order tree traversal is basically to traverse left, print the node value and traverse right. Here, I found that a problem is the HP42s/DM42/Free42 does not save variables when a program is recursively called. So I wrote a PUSH function to store the value of C before each TV function call. Here, D is not to be confused with a data value to store (like in the IT program). D is the value of the left or right pointer we are traversing next. After a return from a recursive TV function call, we POP the value of C. Code: 00 { 112-Byte Prgm } Code: 00 { 37-Byte Prgm } If everything is working correctly, you will see the numbers 1 to 25 displayed. |
|||
« Next Oldest | Next Newest »
|
User(s) browsing this thread: 1 Guest(s)