Animation | What is the 2-3 tree?

We recall AVL tree, which when inserting and removing nodes, they want to ensure that any difference in height of about child node of the tree does not exceed 1. It is because of this limitation, insert and delete a node has a node may adjust the unbalanced status of multiple nodes. In the case of frequent rotation left and right rotation operation will affect the performance of the entire AVL tree, unless it is rarely balanced and unbalanced changes, or AVL tree search brought performance gains are insufficient to cover the balance of properties caused by tree loss.

That there is no absolute balance a tree it? There will be no difference in height balance factor, there is no balance factor will not adjust the rotation operation. 2-3 Tree It is an absolute balance of the tree, the depth of any node to its leaf nodes are all equal.

Numerals 2-3 tree node has a subtree 2-3. It also satisfies the basic properties of the binary search tree, but it does not belong to the binary search tree.

2-3 definition of tree

2-3 is a tree or a tree empty, or has the following nodes:

2- node element and comprising a two subtrees (subtrees left), the value of all the elements is less than the left subtree of its parent node, the values ​​of all the elements are larger than the right subtree of its parent node;

3- nodes, there are two elements and three subtree (left, right subtree), the values ​​of all elements of the left subtree is less than its parent node subtree of all elements are positioned between the two parent element , the values ​​of all the elements are larger than the right subtree of its parent node;

Sub-tree is empty tree node 2- or 3- node;

No node equal elements.

file

2-3 Finding Elements tree

Find in a similar binary search tree of 2-3 trees, according to the size of the element to determine the direction of the search. To determine whether there is an element, we first look to be the root element and compare, and if it is equal to either one, to find that the hit; otherwise, select the search direction based on the results of the comparison.

file

2-3 Tree insert elements

First insert elements to find a hit, if it can find the hit is not inserted into this element, if repeated elements needed to support this element will add an attribute the object count. If the lookup misses, then insert this element in a leaf node.

Insert an empty tree is very simple, you can create a node. If the tree is not empty, insert the case into four kinds:

1. Insert 2- node element;

2. to insert elements into a tree, containing only a 3- node;

3. a parent node to node 2- 3- node insert elements;

4. 3- parent node to a node in the node 3- insert elements.

Insert element node 2-

如果未命中查找结束于2-节点,直接将2-节点替换为3-节点,并将待插入元素添加到其中。

file

向一颗只含有一个3-节点的树中插入元素

如果命中查找结束于3-节点,先临时将其成为4-节点,把待插入元素添加到其中,然后将4-节点转化为3个2-节点,中间的节点成为左右节点的父节点。如果之前临时4-节点有父节点,就会变成向一个父节点为2-节点的3-节点中插入元素,中间节点与父节点为2-节点的合并。

file

向一个父节点为3-节点的3-节点中插入元素

插入元素后一直向上分解临时的4-节点,直到遇到2-节点的父节点变成3-节点不再分解。如果达到树根节点还是4-节点,则进行分解根节点,此时树高 1(只有分解根节点才会增加树高),下面动画2-3树插入会出这个例子。

file

动画:2-3树插入

动画视频地址

2-3树删除

算法4红黑树删除最小键这一小结里没有特别详细地介绍,但给到了沿着左链接向下进行变换的三种情况:

如果左子节点不是2-节点,完成;

如果左子节点是2-节点,而兄弟节点不是2-节点,将兄弟节点的最小元素移到父节点,父节点的最小元素移到左子节点;

如果左子节点是2-节点,而兄弟节点是2-节点,则左子结点、父节点中最小的元素和兄弟结点合并成4-结点。

file

删除最小元素

我们注意到在叶子节点不是2-节点的时候,删除一个元素是很简单的,而且删除时不考虑自平衡处理。如果删除一个2-节点会留下一个空节点,破坏了2-3树的绝对平衡。所以,为了保证不会删除一个2-节点,可以设定最左边或者最右边进行向下变换节点。这里设置沿最左边的链接,进行向下变换的三种情况正是如上图中,左子节点的父节点除了根节点都会变换成3-节点或4-节点。

删除任意元素

删除任意元素需要进行命中查找。如果查找未命中则忽略之;如果查找命中则向二叉堆一样删除任意元素,将带删除元素右子树的最小元素替换到待删除元素上,然后对右子树进行删除最小元素。

动画:2-3树删除

动画视频地址喜欢本文的朋友,欢迎关注公众号「算法无遗策」,收看更多精彩内容

Published 14 original articles · won praise 5 · Views 1713

Guess you like

Origin blog.csdn.net/qq_32539227/article/details/104087289