词条 | 2–3–4 tree |
释义 |
In computer science, a 2–3–4 tree (also called a 2–4 tree) is a self-balancing data structure that is commonly used to implement dictionaries.{{citation needed |reason=This article and the "associative array" article seem to say that dictionaries are usually implemented using some other structure such as red-black trees.|date=May 2015}} The numbers mean a tree where every node with children (internal node) has either two, three, or four child nodes:
2–3–4 trees are B-trees of order 4;[1] like B-trees in general, they can search, insert and delete in O(log n) time. One property of a 2–3–4 tree is that all external nodes are at the same depth. 2–3–4 trees are an isometry of red–black trees, meaning that they are equivalent data structures. In other words, for every 2–3–4 tree, there exists at least one red–black tree with data elements in the same order. Moreover, insertion and deletion operations on 2–3–4 trees that cause node expansions, splits and merges are equivalent to the color-flipping and rotations in red–black trees. Introductions to red–black trees usually introduce 2–3–4 trees first, because they are conceptually simpler. 2–3–4 trees, however, can be difficult to implement in most programming languages because of the large number of special cases involved in operations on the tree. Red–black trees are simpler to implement,[2] so tend to be used instead. Properties
InsertionTo insert a value, we start at the root of the 2–3–4 tree:
ExampleTo insert the value "25" into this 2–3–4 tree:
DeletionConsider just leaving the element there, marking it "deleted", possibly to be re-used for a future insertion. To remove a value from the 2–3–4 tree:
Make the following adjustments when a 2-node – except the root node – is encountered on the way to the leaf we want to remove:
Once the sought value is reached, it can now be placed at the removed entry's location without a problem because we have ensured that the leaf node has more than 1 key. Deletion in a 2–3–4 tree is O(log n), assuming transfer and fusion run in constant time ( O(1) ).[3][5] See also{{Portal|Computer programming}}
References1. ^{{Cite book |last=Knuth |first=Donald |author-link=Donald Knuth |series=The Art of Computer Programming |title=Sorting and Searching |publisher=Addison–Wesley |year=1998 |volume=Volume 3 |edition=Second |isbn=0-201-89685-0}}. Section 6.2.4: Multiway Trees, pp. 481–491. Also, pp. 476–477 of section 6.2.3 (Balanced Trees) discusses 2-3 trees. 2. ^{{cite web|work = Left-Leaning Red-Black Trees|title = Left-Leaning Red-Black Trees|first = Robert|last = Sedgewick|url = http://www.cs.princeton.edu/~rs/talks/LLRB/RedBlack.pdf|year = 2008|publisher = Department of Computer Science, Purdue University}} 3. ^1 {{citation|last1 = Ford|first1 = William|first2 = William|last2 = Topp|title = Data Structures with C++ Using STL|edition = 2nd|location = New Jersey|publisher = Prentice Hall|year = 2002|isbn = 0-13-085850-1|pages = 683}} 4. ^{{citation|title = Data Structures and Algorithms in C++|first1 = Michael T|last1 = Goodrich|author1-link=Michael T. Goodrich|first2 = Roberto|last2 = Tamassia|author2-link=Roberto Tamassia|first3 = David M|last3 = Mount|author3-link=David Mount|isbn = 0-471-20208-8|publisher = Wiley|year = 2002}} 5. ^{{cite web|work = CS251: Data Structures Lecture Notes|title = (2,4) Trees|first = Ananth|last = Grama|url = http://www.cs.purdue.edu/homes/ayg/CS251/slides/chap13a.pdf|accessdate = 2008-04-10|year = 2004|publisher = Department of Computer Science, Purdue University}} External links{{Commons category|2-3-4-Trees}}
1 : B-tree |
随便看 |
|
开放百科全书收录14589846条英语、德语、日语等多语种百科知识,基本涵盖了大多数领域的百科知识,是一部内容自由、开放的电子版国际百科全书。