Data structure and algorithm 06 2-3-4 tree

Source: Internet
Author: User

As can be seen from the analysis of the 4th section, binary search tree is a very good data structure, can quickly find a given keyword data items, and can quickly insert and delete data items. But binary search tree has a very troublesome problem, if the tree is inserted in random data, then the performance is good, but if the insertion is ordered or reverse-order data, then the two-fork search tree execution speed becomes very slow. Because the binary tree is unbalanced when inserting values in order, its ability to quickly find, insert, and delete specified data items is lost.

The 2-3-4 tree is a multi-fork tree with a maximum of four child nodes and three data items per node. The 2-3-4 tree, like the red-black tree, is also a balanced tree, which is less efficient than red-Haishi, but easy to program. The meaning of 2, 3, and 4 in the 2-3-4 tree name refers to the number of child nodes that a node may contain. There are three possible scenarios for non-leaf nodes:

• A node with a data item always has two sub-nodes;

• Nodes with two data items always have three sub-nodes;

• Nodes with three data items always have four byte points.

In short, a child node of a non-leaf node is always 1 more than the data item it contains. As shown in the following:


For the sake of convenience, use 0 to 2 to number the data item, and 0 to 3 to number the child node chain. The important point in the structure of a tree is the relationship between its chain and the key value of its own data item. Binary tree all key values are smaller than a node value on the subtree of the root of the left child node of the node, and all the key values are larger than the one and the value of the node at the right child node of the node is the root subtree. The rules in the 2-3-4 tree are the same, plus the following points:

• The root is the child0 of all child nodes of the subtree and the key value is less than key0;

• The root is the child1 of all child nodes of the subtree with the keyword value greater than key0 and less than key1;

• The root is the child2 of all child nodes of the subtree with the keyword value greater than key1 and less than key2;

• The root is the child3 of all child nodes of the subtree with a keyword value greater than key2.

as shown in this relationship, duplicate keyword values are not generally allowed in the 2-3-4 tree, so there is no need to consider comparing the same keyword values.


Inserting nodes in a 2-3-4 tree is sometimes simpler and sometimes more complex. When the insertion is simple when no full node is encountered and the appropriate leaf node is found, the insertion may involve moving one or two other data items in one node so that the key values remain in the correct order after the new data item is inserted. Such as:


If you look down the path where you want to insert it, the node is full and the insertion becomes complicated. In this case, the node must be split. It is this split process that guarantees the balance of the tree. The data items in the set to split node are a, B, C, and the following is the case when splitting (assuming that the split node is not the root node):

• Create a new empty node, which is the sibling to split the node, on the right side of the node to be split;

• Data item C is moved to the new node;

• Data item B is moved to the parent node of the node to be split;

• Data item A remains in its original position;

• The rightmost two child nodes are disconnected from the node to be split and connected to the new node.

shows the process of splitting a node. Another way to describe node splitting is to say that 4-node becomes two 2-node.


If you encounter Mangen when you first look at the insertion point, the insertion process is a little more complicated:

• Create a new root. It is the parent node of the node to be split;

• Create a second new node. It is the sibling node to divide the nodes;

• Data item C is moved to the new sibling node;

• Data item B is moved to the new root node;

• Data item A remains in its original position;

• To split the rightmost two sub-nodes of a node, connect to the new sibling node.

is the process of root splitting. The process creates a new root, which is higher than the old one, so the height of the entire tree is added to the first layer.


Here is the code for the 2-3-4 tree:

public class Tree234 {private Node2 root = new Node2 ();p ublic int find (Long key) {Node2 CurrentNode = Root;int Childnumber ; while (true) {if (Childnumber = Currentnode.finditem (key))! =-1) {return childnumber;} else if (Currentnode.isleaf ()) {return-1;} else {CurrentNode = Getnextchild (CurrentNode, key);}}} Insert a dataitempublic void Insert (Long data) {Node2 CurrentNode = Root;dataitem Tempitem = new DataItem (data); while (tr UE) {if (Currentnode.isfull ()) {split (currentnode);//if node is full, split itcurrentnode = Currentnode.getparent ();// Back Upcurrentnode = Getnextchild (CurrentNode, data),//search Once}else if (Currentnode.isleaf ()) {//if node if leafbreak ;//go Insert}else {currentnode = Getnextchild (CurrentNode, data);}} Currentnode.insertitem (Tempitem);} Display treepublic void DisplayTree () {recdisplaytree (root, 0, 0);} Public Node2 Getnextchild (Node2 currentnode, long key) {int j;//assumes node was not empty, not full and not leafint Numite ms = Currentnode.getnumitems (); for (j = 0; j < NumItems; J + +) {if (Key < Currentnode.getitem (j). DData) {return currentnode.getchild (j);}} Return Currentnode.getchild (j);} public void Split (Node2 currentnode) {//assumes node is Fulldataitem itemb, itemc;//stores the latter two DataItemNode2 the parent to split the node, chil D2, child3;//stores the parent node to split the node and the latter two childint ITEMINDEX;ITEMC = Currentnode.removeitem (); itemb = Currentnode.removeitem ();// Remove items from this nodechild2 = Currentnode.disconnectchild (2); child3 = Currentnode.disconnectchild (3); Remove children from this nodeNode2 newright = new Node2 (); Make a new Nodeif (CurrentNode = = root) {root = new Node2 ();//make a new rootparent = Root;//root are our Parentroot.conn Ectchild (0, CurrentNode);//connect CurrentNode to parent}else {parent = Currentnode.getparent ();} Deal with parentitemindex = Parent.insertitem (ITEMB),//insert B to Parentint n = parent.getnumitems ();//total itemsfor ( int j = n-1; J > ItemIndex; j--) {Node2 temp = Parent.disconnectchild (j);p Arent.connectchild (j+1, temp);} Parent.connectchILD (itemindex+1, newright);//deal with Newrightnewright.insertitem (ITEMC); Newright.connectchild (0, child2); Newright.connectchild (1, child3);} public void Recdisplaytree (Node2 thisnode, int. level, int childnumber) {System.out.print (' level = ' + level + ' child = ' + Childnumber + ""); Thisnode.displaynode ();//call ourselves for each child of this nodeint NumItems = Thisnode.getnumitem s (); for (int j = 0; J < numitems+1; J + +) {Node2 NextNode = Thisnode.getchild (j); if (nextnode! = null) {Recdisplaytree (NEX Tnode, Level+1, j);} else continue;}}} Data item class DataItem {public long ddata;public DataItem (Long data) {dData = data;} public void Displayitem () {System.out.print ("/" + DData);}} Node class Node2 {private static final int ORDER = 4;private int numitems;//indicates how many data items the node contains private Node2 parent;private node 2 childarray[] = new Node2[order]; Stores the array of child nodes, up to four child nodes private DataItem itemarray[] = new dataitem[order-1];//An array of data items in that node, with a maximum of three data items per node//connection child node public void Connectchild (int childnum, Node2 child) {Childarray[childnum] = child;if (child! = null) {child.parent = this;}} Disconnects the child node and returns the child node public Node2 disconnectchild (int childnum) {Node2 Tempnode = childarray[childnum];childarray[ Childnum] = Null;return tempnode;} Public Node2 getchild (int childnum) {return childarray[childnum];} Public Node2 getParent () {return parent;} public boolean isleaf () {return (childarray[0] = = null);} public int Getnumitems () {return numitems;} Public DataItem getItem (int index) {return itemarray[index];} public Boolean isfull () {return (NumItems = = ORDER-1);} public int FindItem (long key) {for (int j = 0; J < ORDER-1; J + +) {if (itemarray[j] = = null) {break;} else if (Itemarray[j].ddata = = key) {return J;}} return-1;} public int InsertItem (DataItem newitem) {//assumes node was not fullnumitems++;long NewKey = newitem.ddata;for (int j = Orde R-2; J >= 0; j--) {//start on rightif (itemarray[j] = = null) {//item is nullcontinue;//get left one cell}else {//not Nulllong Itskey = Itemarray[j].ddata;//get its keyif (NewkeY < Itskey) {//if it ' s biggeritemarray[j+1] = itemarray[j];//shift it right}else {itemarray[j+1] = NewItem;//insert n EW Itemreturn J+1;//return Index to new item}}}itemarray[0] = Newitem;return 0;} Public DataItem RemoveItem () {//assumes node not emptydataitem tempitem = Itemarray[numitems-1];//save itemitemarray[ NUMITEMS-1] = Null;//disconnect Itnumitems--;return tempitem;} public void Displaynode () {for (int i = 0; i < NumItems; i++) {Itemarray[i].displayitem ();} System.out.println ("/");}}

Like the red-black tree, the 2-3-4 tree also accesses one node per layer, but the 2-3-4 tree has a shorter red-black tree than the same data item (few layers). More specifically, each node in the 2-3-4 tree can have up to 4 child nodes, and if each node is full, the height of the tree should be proportional to the log4n. The logarithm of base 2 and the logarithm base of 4 differ by 2, so the height of the 2-3-4 tree is roughly red-Haishi in the case where all nodes are full. But they cannot all be full, the height of the 2-3-4 tree is roughly between log2 (N+1) and log2 (n+1)/2.

On the other hand, each node has more data items to view, which increases the lookup time. Because a linear search is used in a node to view data items, the multiplier to increase the lookup time is proportional to M, which is the average number of data items per node. The total lookup time is proportional to the m*log4n. Some nodes have 1 data items, some 2, some 3, and if calculated by an average of two, the lookup time is proportional to the 2*log4n.

Therefore, increasing the number of data items per node in the 2-3-4 tree can compensate for the decrease in the height of the tree. The lookup time in the 2-3-4 tree is roughly equal to the balanced binary tree (such as red-Haishi), both O (Logn).

2-3-4 Tree to discuss this, if there are errors please leave a message correct, if you like, don't forget to order a good yo ~



Data structure and algorithm 06 2-3-4 tree

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.