It took two days to complete the 2-3-4 tree summary. Program -- Inserts a 2-3-4 tree. The main reason for this long time is that many detours have been taken and some common-sense errors have been made. To prevent such errors from being repeated, this note is used to deepen your impression. First of all, I did not understand the concept of the 2-3-4 tree when I started writing a program. Although I checked some information, this very abstract thing is still hard to understand, in Wikipedia, we have actually stored 2-3-4 trees Algorithm I wrote it in step 1, but I read it again and again for 20 or 30 times. Let's simply sum up the experience, which is clear.
- Do not write details before the framework is ready.Code. First use system. Out. println () for testing. After the test is successful, start the specific implementation.
- Do not write any line of code before the algorithm steps are fully understood. It is best to draw a few images first, and then start writing after the figure passes.
- Do not change the parameters of the input function. Otherwise, do not reference the input function.
- Before writing code, use natural language to describe the algorithm step. These natural statements can be added to comments.
- The division of labor between various code segments of the program should be clear.
By the way, insert the Wikipedia description of the 2-3-4 Tree Insertion Algorithm:
To insert a value, we start at the root of the 2-3-4 tree:
- If the current node is a 4-node:
- Push the middle element of the 4-node up into the parent, leaving a 3-node.
- Split the remaining 3-node up into a pair of 2-nodes.
- If this is the root node (which thus has no parent), the middle value becomes the new Root 2-node and the tree height increases by 1. ascend into the root.
- Otherwise, push the middle value up into the parent node. ascend into the parent node.
- Find the child whose interval contains the value to be inserted.
- If the child is empty, insert the value into current node and finish.
- Otherwise, descend into the child and repeat from step 1.