red-black revision dafcb997e390efa4423883dafd100c975c4095d6
29ed3ddde7ac0c63786946b42e478f24554085d5jeff_schillerCopyright (C) 2004 Internet Systems Consortium, Inc. ("ISC")
29ed3ddde7ac0c63786946b42e478f24554085d5jeff_schillerCopyright (C) 1999-2001 Internet Software Consortium.
1298782991860e0fa520aac3222e0da0fd299a29helixSee COPYRIGHT in the source root or http://isc.org/copyright.html for terms.
654009ff682f4af41ae8ec2260e3562b769cb1fbEduard Braun$Id: red-black,v 1.9 2004/03/05 05:04:46 marka Exp $
654009ff682f4af41ae8ec2260e3562b769cb1fbEduard Braun Red-Black Tree Implementation Notes
654009ff682f4af41ae8ec2260e3562b769cb1fbEduard BraunBIND9's basic name storage mechanism is to use a modified form of
654009ff682f4af41ae8ec2260e3562b769cb1fbEduard Braunbalanced binary tree known as a red-black tree. Red-black trees
654009ff682f4af41ae8ec2260e3562b769cb1fbEduard Braunprovide for relatively efficient storage, retrieval and removal of
654009ff682f4af41ae8ec2260e3562b769cb1fbEduard Braundata while maintaining the lexical order of all stored keys, a
654009ff682f4af41ae8ec2260e3562b769cb1fbEduard Braunnecessary function for DNS security.
654009ff682f4af41ae8ec2260e3562b769cb1fbEduard BraunA red-black tree is a balanced binary tree named for the coloring that
654009ff682f4af41ae8ec2260e3562b769cb1fbEduard Braunis done in the tree, identifying each node as either red or black.
654009ff682f4af41ae8ec2260e3562b769cb1fbEduard BraunThere are two simple rules for maintaining the color of nodes:
654009ff682f4af41ae8ec2260e3562b769cb1fbEduard Braun (1) A red node has only black children.
654009ff682f4af41ae8ec2260e3562b769cb1fbEduard Braun (2) The path from the root to any leaf node always includes the
654009ff682f4af41ae8ec2260e3562b769cb1fbEduard Braun same number of black nodes.
654009ff682f4af41ae8ec2260e3562b769cb1fbEduard BraunWhenever a key is added or removed, adjustments are made to adhere to
654009ff682f4af41ae8ec2260e3562b769cb1fbEduard Braunthose two rules. These adjustments are relatively cheap to make but
654009ff682f4af41ae8ec2260e3562b769cb1fbEduard Braunmaintain the balance of the tree, thus making for efficient addition,
654009ff682f4af41ae8ec2260e3562b769cb1fbEduard Braunlookup and deletion operations, all of which are O(log N). The color
654009ff682f4af41ae8ec2260e3562b769cb1fbEduard Braunof a node is not relevant to external users of the tree; it is needed
654009ff682f4af41ae8ec2260e3562b769cb1fbEduard Braunonly to maintain the balance of the tree.
654009ff682f4af41ae8ec2260e3562b769cb1fbEduard BraunFor more information on basic red-black trees, see _Introduction to
654009ff682f4af41ae8ec2260e3562b769cb1fbEduard BraunAlgorithms_, Cormen, Leiserson, and Rivest, MIT Press / McGraw Hill,
654009ff682f4af41ae8ec2260e3562b769cb1fbEduard Braun1990, ISBN 0-262-03141-8, chapter 14.
654009ff682f4af41ae8ec2260e3562b769cb1fbEduard BraunIn BIND9, the red-black tree implementation uses DNS names as keys,
654009ff682f4af41ae8ec2260e3562b769cb1fbEduard Braunand can store arbitrary data with each key value. "name" and "key"
654009ff682f4af41ae8ec2260e3562b769cb1fbEduard Braunare used interchangably in this document.
654009ff682f4af41ae8ec2260e3562b769cb1fbEduard BraunThe basic red-black tree algorithm is further adapted for use in BIND9
654009ff682f4af41ae8ec2260e3562b769cb1fbEduard Braunto incorporate the notion of hierarchy, creating a tree of red-black
654009ff682f4af41ae8ec2260e3562b769cb1fbEduard Brauntrees. Where there is more than one name with a common suffix, all
654009ff682f4af41ae8ec2260e3562b769cb1fbEduard Braunnames with that suffix are stored in their own red-black tree, with a
654009ff682f4af41ae8ec2260e3562b769cb1fbEduard Braundown pointer from the suffix locating the subtree.
654009ff682f4af41ae8ec2260e3562b769cb1fbEduard BraunFor example, consider storing the following names:
654009ff682f4af41ae8ec2260e3562b769cb1fbEduard BraunNo matter which order the keys were added, this would result in a tree
654009ff682f4af41ae8ec2260e3562b769cb1fbEduard Braunthat can be visualized as:
654009ff682f4af41ae8ec2260e3562b769cb1fbEduard BraunThis tree shows that when there is no key for a particular label, and
654009ff682f4af41ae8ec2260e3562b769cb1fbEduard Braunwhen there is only one known label for its immediate subordinate, then
654009ff682f4af41ae8ec2260e3562b769cb1fbEduard Braunmultiple labels can appear in a single node, such as at d.e.f and g.h.
b6a977ede002f585667208a8730b00835944e954JazzyNicoIt also demonstrates that there can be more nodes in the tree of trees
654009ff682f4af41ae8ec2260e3562b769cb1fbEduard Braunthan there are actual keys (which degrades the O(log N) performance
654009ff682f4af41ae8ec2260e3562b769cb1fbEduard Braunmarginally); the nodes at d.e.f and w.y do not represent keys.
654009ff682f4af41ae8ec2260e3562b769cb1fbEduard BraunAs an aside, remember that when ordering DNS names, labels are
654009ff682f4af41ae8ec2260e3562b769cb1fbEduard Braunexamined from the right, therefore w.y sorts after x and before z.
654009ff682f4af41ae8ec2260e3562b769cb1fbEduard BraunA split can occur not only on a regular label boundary, but also
654009ff682f4af41ae8ec2260e3562b769cb1fbEduard Braunbetween any two bits in an EDNS bitstring label. The common-suffix
654009ff682f4af41ae8ec2260e3562b769cb1fbEduard Braunrules will be applied to keep as many bits together as possible.
654009ff682f4af41ae8ec2260e3562b769cb1fbEduard BraunIn the current implementation of the tree of trees, a node is
654009ff682f4af41ae8ec2260e3562b769cb1fbEduard Braunconsidered to "formally" exist only if it has data associated with
654009ff682f4af41ae8ec2260e3562b769cb1fbEduard Braunit. So if the above tree then had the key d.e.f added to it, the
654009ff682f4af41ae8ec2260e3562b769cb1fbEduard Braunoperation would succeed rather than getting an "already exists"
654009ff682f4af41ae8ec2260e3562b769cb1fbEduard BraunAlong the same lines, if a key is added with a name which is a proper
654009ff682f4af41ae8ec2260e3562b769cb1fbEduard Braunsuperdomain of the name stored in an existing node, the operation will
654009ff682f4af41ae8ec2260e3562b769cb1fbEduard Braunsucceed by splitting the existing node into one node that is the key
654009ff682f4af41ae8ec2260e3562b769cb1fbEduard Braunand another node that is the remaining parts of the name. Adding e.f
654009ff682f4af41ae8ec2260e3562b769cb1fbEduard Braunto the above tree results in the top level red-black tree having a
b6a977ede002f585667208a8730b00835944e954JazzyNiconode named e.f where the current d.e.f is, and a down pointer from
654009ff682f4af41ae8ec2260e3562b769cb1fbEduard Braund.e.f to a "tree" of a single node named d. The down pointer from d
29ed3ddde7ac0c63786946b42e478f24554085d5jeff_schillerwould be kept to the level which has x, w.y, and z.
29ed3ddde7ac0c63786946b42e478f24554085d5jeff_schillerA similar split of d.e.f would occur if the name k.e.f were added.
1298782991860e0fa520aac3222e0da0fd299a29helixThe top level tree would have the node e.f with a down pointer to a
29ed3ddde7ac0c63786946b42e478f24554085d5jeff_schillerlevel that had both d and k, and d would continue to have its down
29ed3ddde7ac0c63786946b42e478f24554085d5jeff_schillerpointer to the x, w.y and z level.
654009ff682f4af41ae8ec2260e3562b769cb1fbEduard BraunIt is guaranteed when splitting that external references to the node
29ed3ddde7ac0c63786946b42e478f24554085d5jeff_schillerthat is split will remain valid --- in the previous examples, anything
29ed3ddde7ac0c63786946b42e478f24554085d5jeff_schillerthat was pointing to the node that was d.e.f will still point to the
When deleting keys, nodes can be rejoined. If both of p.w.y.d.e.f and
would become o.w.y. Unlike splitting, it is _not_ guaranteed that
exist only if data is associated with it, because even if w.y.d.e.f
still be merged with the w.y node when p and q were deleted.
rc.vix.com, but not the full name.
For example, consider a database that had only the names vix.com and
vix.com, the DNSSEC predecessor. Though this might first appear to
match node will be the predecessor itself. In the vix.com/isc.org
example, the search for uu.net finds a partial match at ".", which is
of course also in the path to the vix.com predecessor. A search for
The rbt.h file has more complete descriptions of each of the functions