Core code for splitting 2-3-4 trees [JAVA Implementation]

Source: Internet
Author: User

2-3-4 Tree nodes are split into two types.

1: node split; 2: Root split.

Set the data items to be split to A, B, and C.

Node splitting

1. Create a New empty node. It is the brother of the node to be split, on the right of the node to be split.

2. data item C is transferred to the new node.

3. data item B is transferred to the parent node of the node to be split.

4. data item A is retained on the original node.

5. the rightmost two subnodes are disconnected from the node to be split and connected to the new node.

Root split

1. Create a new node as the root node. It is the parent node of the node to be split.

2. Create the second empty node. It is the brother of the node to be split, on the right of the node to be split.

3. data item C is transferred to the new node.

4. data item B is transferred to the parent node of the node to be split.

5. data item A is retained on the original node.

6. the rightmost two subnodes are disconnected from the split node and connected to the new node.

Code Public void split (node thisnode) // split the node <br/> {<br/> // assumes node is full <br/> dataitem itemb, itemc; // data item B, C <br/> node parent, child2, Child3; // The parent node of the thisnode, which contains 3rd and 4 subnodes <br/> int itemindex; </P> <p> itemc = thisnode. removeitem (); // data item C is removed from the thisnode <br/> itemb = thisnode. removeitem (); // data item B is removed from the thisnode </P> <p> child2 = thisnode. disconnectchild (2); // The 3rd sub-nodes of the node are disconnected from the node <br/> Child3 = Thisnode. disconnectchild (3); // 4th sub-nodes of a node are disconnected from the node </P> <p> node newright = new node (); // make new node </P> <p> If (thisnode = root) // create a new node as the root node. It is the parent node of the node to be split. <Br/> {<br/> root = new node (); // make new root <br/> parent = root; // root is our parent <br/> root. connectchild (0, thisnode); // connect to parent <br/>}< br/> else // This node not the root <br/> parent = thisnode. getparent (); // obtain the parent node of the node </P> <p> // deal with parent <br/> itemindex = parent. insertitem (itemb); // insert data item B to the parent node and return the inserted position. <br/> int n = parent. getnumitems (); // total data items of the parent node </P> <p>/* <br/> * move the child node of the parent node to the appropriate location <br/> */<br/> for (Int J = n-1; j> itemindex; j --) // move parent's <br/> {// connections <br/> node temp = parent. disconnectchild (j); // One Child <br/> parent. connectchild (J + 1, temp); // to the right <br/>}< br/> // connect newright to parent <br/> parent. connectchild (itemindex + 1, newright); // The new node is connected to the parent node </P> <p> // deal with newright <br/> newright. insertitem (itemc); // item C to newright <br/> newright. connectchild (0, child2); // connect to 0 and 1 <br/> newright. connectchild (1, Child3); // on newright <br/>}// end split () <br/>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.