[prev] 70 [next]

Red-Black Tree Insertion (cont)

High-level description of insertion algorithm:

insertRedBlack(tree,item):
|  Input  red-black tree, item
|  Output tree with item inserted
|
|  tree=insertRB(tree,item)
|  colour(tree)=BLACK    // root node is always black
|  return tree

insertRB(tree,item):
|  Input  tree, item
|  Output tree with it inserted
|
|  if tree is empty then
|     return newNode(item)
|  else if item=data(tree) then
|     return tree
|  end if
|  if tree is a 4-node then
|     split 4-node
|  end if
|  recursive insert a la BST, re-arrange links/colours after insert
|  return modified tree