Common algorithms and data structures balance find tree (1)--2-3 find tree (animated)

Source: Internet
Author: User

This series of articles mainly introduces the knowledge of commonly used algorithms and data structures, records the contents of "Algorithms i/ii" course, adopts "algorithm (4th edition)" This Red Cookbook as a learning material, language is java. I don't have to say much about the fame of this book. Watercress Rating 9.4, I myself also think is an excellent learning algorithm books.

With this series of articles, you can deepen your understanding of data structures and basic algorithms (individuals think more clearly than schools) and deepen their understanding of Java.

2-3 Tree Introduction

We talked about binary search tree has been very close to our goal, in many cases, performance is very good, but only in the deletion of not, once deleted operation will lose balance, then, this time, we say how to do a balance find tree, first introduce 2-3 find tree

The 2-3 tree is a special two-fork tree that allows 2 keys to appear in a node.

    • 2 nodes: 1 key,2 children

    • 3 nodes, 2 key,3 children

It has several features:

    1. Conforms to the order of the sequence traversal: The left child is less than the left key, the middle child is between key, the right child is greater than the right key

    2. Absolute balance: As long as the path from the root node to all the null nodes (we'll talk about how to maintain it later)

2-3 Tree Search

According to its characteristics, we can easily know how to find, and the two-fork tree to find almost, just a node more.

    • If the child is smaller than the left: go

    • If less than right child: go mid

    • If the child is greater than right: go

    • If equal to key, return value

    • If NULL, returns null

See Animation:

2-3 insertion of the tree

There are several cases of insertion:

    • If the inserted node is a 2-node: Directly turn 2 nodes into 3 nodes

    • If you are inserting a 3-node:

      • Add key to 3-node becomes a temporary 4-node

      • Move the middle key on the 4 node to the Father node

      • Repeat the operation.

      • If Root is reached and a temporary 4 node is formed, the root node is split into 3 nodes and the tree height increases by 1 layers

See Animation:

Constructing animations

Having talked so much, let's look at how to construct a 2-3 tree.

Implementation scenarios

Here first sells a xiaoguanzi, here does not write the code realization, everybody first understood, later will use the red black tree The plan realizes 2-3 trees

Common algorithms and data structures balance find tree (1)--2-3 find tree (animated)

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.