Red Black Trees

download Red Black Trees

If you can't read please download the document

description

Red Black Trees. Colored Nodes Definition Binary search tree. Each node is colored red or black. Root and all external nodes are black. No root-to-external-node path has two consecutive red nodes. All root-to-external-node paths have the same number of black nodes. Red Black Trees. - PowerPoint PPT Presentation

Transcript of Red Black Trees

  • Red Black TreesColored Nodes DefinitionBinary search tree.Each node is colored red or black.Root and all external nodes are black.No root-to-external-node path has two consecutive red nodes.All root-to-external-node paths have the same number of black nodes

  • Red Black TreesColored Edges DefinitionBinary search tree.Child pointers are colored red or black.Pointer to an external node is black.No root to external node path has two consecutive red pointers.Every root to external node path has the same number of black pointers.

  • Example Red-Black Tree

  • PropertiesThe height of a red black tree that has n (internal) nodes is between log2(n+1) and 2log2(n+1).

  • PropertiesStart with a red black tree whose height is h; collapse all red nodes into their parent black nodes to get a tree whose node-degrees are between 2 and 4, height is >= h/2, and all external nodes are at the same level.

  • Properties

  • PropertiesLet h>= h/2 be the height of the collapsed tree. In worst-case, all internal nodes of collapsed tree have degree 2.Number of internal nodes in collapsed tree >= 2h-1.So, n >= 2h-1So, h
  • PropertiesAt most 1 rotation and O(log n) color flips per insert/delete.Priority search trees.Two keys per element.Search tree on one key, priority queue on other.Color flip doesnt disturb priority queue property.Rotation disturbs priority queue property.O(log n) fix time per rotation => O(log2n) overall time.

  • PropertiesO(1) amortized complexity to restructure following an insert/delete.C++ STL implementationjava.util.TreeMap => red black tree

  • InsertNew pair is placed in a new node, which is inserted into the red-black tree.New node color options.Black node => one root-to-external-node path has an extra black node (black pointer).Hard to remedy.Red node => one root-to-external-node path may have two consecutive red nodes (pointers).May be remedied by color flips and/or a rotation.

  • Classification Of 2 Red Nodes/PointersXYzX => relationship between gp and pp.pp left child of gp => X = L.Y => relationship between pp and p.p right child of pp => Y = R.z = b (black) if d = null or a black node.z = r (red) if d is a red node.

  • XYrColor flip.Move p, pp, and gp up two levels.Continue rebalancing if necessary.

  • LLbRotate.Done!Same as LL rotation of AVL tree.

  • LRbRotate.Done!Same as LR rotation of AVL tree.RRb and RLb are symmetric.

  • DeleteDelete as for unbalanced binary search tree.If red node deleted, no rebalancing needed.If black node deleted, a subtree becomes one black pointer (node) deficient.

  • Delete A Black Leaf Delete 8.

  • Delete A Black Leafy y is root of deficient subtree. py is parent of y.py

  • Delete A Black Degree 1 Node Delete 45.y y is root of deficient subtree.py

  • Delete A Black Degree 2 Node Not possible, degree 2 nodes are never deleted.

  • Rebalancing StrategyIf y is a red node, make it black.

  • Rebalancing StrategyNow, no subtree is deficient. Done!

  • Rebalancing Strategyy is a black root (there is no py).Entire tree is deficient. Done!

  • Rebalancing Strategyy is black but not the root (there is a py).Xcny is right child of py => X = R.Pointer to v is black => c = b.v has 1 red child => n = 1.

  • Rb0 (case 1)Color change.Now, py is root of deficient subtree.Continue!

  • Rb0 (case 2)Color change.Deficiency eliminated.Done!

  • Rb1 (case 1)LL rotation.Deficiency eliminated.Done!

  • Rb1 (case 2)LR rotation.Deficiency eliminated.Done!

  • Rb2LR rotation.Deficiency eliminated.Done!

  • Rr(n)n = # of red children of vs right child w.

  • Rr(0)LL rotation.Done!

  • Rr(1) (case 1)LR rotation.Deficiency eliminated.Done!

  • Rr(1) (case 2)Rotation.Deficiency eliminated.Done!

  • Rr(2)Rotation.Deficiency eliminated.Done!

  • B-Trees

    Large degree B-trees used to represent very large dictionaries that reside on disk.Smaller degree B-trees used for internal-memory dictionaries to overcome cache-miss penalties.

  • AVL Treesn = 230 = 109 (approx).30
  • Red-Black Treesn = 230 = 109 (approx).30
  • m-way Search TreesEach node has up to m 1 pairs and m children. m = 2 => binary search tree.

  • 4-Way Search Tree k < 10 10 < k < 30 30 < k < 35 k > 35

  • Maximum # Of PairsHappens when all internal nodes are m-nodes.Full degree m tree.# of nodes = 1 + m + m2 + m3 + + mh-1 = (mh 1)/(m 1). Each node has m 1 pairs.So, # of pairs = mh 1.

  • Capacity Of m-Way Search Tree

    m = 2

    m = 200

    h = 3

    7

    8 * 106 - 1

    h = 5

    31

    3.2 * 1011 - 1

    h = 7

    127

    1.28 * 1016 - 1

  • Definition Of B-TreeDefinition assumes external nodes (extended m-way search tree).B-tree of order m.m-way search tree.Not empty => root has at least 2 children.Remaining internal nodes (if any) have at least ceil(m/2) children.External (or failure) nodes on same level.

  • 2-3 And 2-3-4 TreesB-tree of order m.m-way search tree.Not empty => root has at least 2 children.Remaining internal nodes (if any) have at least ceil(m/2) children.External (or failure) nodes on same level.2-3 tree is B-tree of order 3. 2-3-4 tree is B-tree of order 4.

  • B-Trees Of Order 5 And 2B-tree of order m.m-way search tree.Not empty => root has at least 2 children.Remaining internal nodes (if any) have at least ceil(m/2) children.External (or failure) nodes on same level.B-tree of order 5 is 3-4-5 tree (root may be 2-node though).B-tree of order 2 is full binary tree.

  • Minimum # Of Pairsn = # of pairs.# of external nodes = n + 1.Height = h => external nodes on level h + 1.112>= 23>= 2*ceil(m/2)h + 1>= 2*ceil(m/2)h-1 n + 1 >= 2*ceil(m/2)h-1, h >= 1

  • Minimum # Of Pairsm = 200. n + 1 >= 2*ceil(m/2)h-1, h >= 12>= 1993>= 19,9994>= 2 * 106 15>= 2 * 108 1 h
  • Choice Of mWorst-case search time.(time to fetch a node + time to search node) * height(a + b*m + c * log2m) * hwhere a, b and c are constants.

  • Insert15 20841 35 630 409 Insertion into a full leaf triggers bottom-up node splitting pass.16 17

  • Split An Overfull Nodeai is a pointer to a subtree.pi is a dictionary pair.

    m a0 p1 a1 p2 a2 pm am ceil(m/2)-1 a0 p1 a1 p2 a2 pceil(m/2)-1 aceil(m/2)-1 m-ceil(m/2) aceil(m/2) pceil(m/2)+1 aceil(m/2)+1 pm ampceil(m/2) plus pointer to new node is inserted in parent.

  • Insert15 20841 35 630 409 Insert a pair with key = 2. New pair goes into a 3-node.16 17

    Every red node has a black parent.Height = 5Collapsed height = 3.Color flip to be defined later.Binary search trees in which each node contains a binary search tree are used, for example, in IP router tables. Each rotation of the outer binary search tree requires changes in the binary search trees of the involved nodes resulting in an O(log n) cost per rotation.Initially, p is newly inserted node. Pp and gp may be null. In each round, p, pp, and gp are moved up by 2 levels. If p is a red root, make it black.If p or pp black done!If pp is a red root, make it black.O.w., gp exists If py doesnt exist, the whole tree is one black node deficient. This is OK.y represents both a subtree and its root.This includes the case when y is null.Broken lines indicate pointers whose colors are unspecified.v must exist because y is one black node/pointer deficient.May also be used internally to reduce cache misses and hence improve performance.43 cache misses possible when searching memory resident AVL treesNatural extension of 2-3 and 2-3-4 trees would leave worst case at m=2.Alternative definition has pairs only in leaves; remaining nodes have keys only. Leaf-pushed B-tree or B+-tree.Not possible for an internal node in an extended tree to have only 1 child.Typically pick m so as to fill a block.2-3 Tree. Insert 10, no problem. Insert 18? Node Splitting