Download Deletion

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Linked list wikipedia , lookup

Lattice model (finance) wikipedia , lookup

Quadtree wikipedia , lookup

B-tree wikipedia , lookup

Binary tree wikipedia , lookup

Interval tree wikipedia , lookup

Red–black tree wikipedia , lookup

Binary search tree wikipedia , lookup

Transcript
CO4301 – Advanced Games
Development
Week 10
Red-Black Trees continued
Gareth Bellaby
1
Applications
•
•
•
An instance of a self-balancing binary search
tree
As such, other self-balancing BSTs would also
be applicable to any of the examples cited.
However,
there
characteristics.
are
some
differing
2
Red-black, AVL comparison
•
•
•
•
AVL tree search is quite fast.
AVL trees have smaller average depth than
red-black trees, and thus searching for a value
in an AVL tree is consistently faster than in a
red-black tree.
However, since an AVL tree is strictly balanced,
insertion and deletion may take some time.
Bit more work to rebalance an AVL tree than a
red-black tree.
3
Red-black, AVL comparison
•
•
•
Red-black trees make less structural changes
to balance themselves than AVL trees, which
can make them potentially faster for insertion
and deletion.
Red-black insertion and deletion is reasonably
fast because it is a height balanced tree.
As an example of these differences in practice:
typically an AVL tree is used for something like
a language dictionary where you have to build
the data structure just once.
4
Applications
•
•
Sets and maps.
• Why?
STL map is typically implemented using a redblack tree. Do remember that this is
implementation dependent, so can’t guarantee
that this is the case. However, current
implementation of STIL is a red-black tree.
5
Efficiency
•
•
•
•
•
Computational Complexity
Height of a red-black tree is O(log n)
The insertion element of the algorithm is
therefore O(log n)
The addition of the node is just O(1)
The algorithm then calls for the tree to be
rebalanced. This also turns out to be O(log n),
i.e. the algorithm performs O(log n) recolourings.
6
Efficiency
•
•
An instance of the rotation element of the
algorithm is O(1).
May perform a number of the rotations, so the
cost may in practice be slightly higher.
However, we can treat it as close to O(1).
7
Implementation Efficiency
•
•
Note also that there are possible efficiencies to
be gained in the implementation used. For
instance, an alternative approach (to the one
discussed in this lecture) to deletion in a redblack tree is to employ a top-down algorithm
and recolour as you go.
For example, see M. Weiss, Data Structures
and Algorithm Analysis in C++
8
Reminder Red-Black tree
•
•
•
•
•
•
Binary tree.
Root is black
Each leaf is black.
Children of a red node are black.
Every path from a node to all of its descendent
leaf nodes contain the same number of black
nodes. Or alternatively, all of the leaves have
the same black depth (count of black nodes up
to the root).
Leaf does not contain data.
9
References
•
•
•
•
Goodrich, Tamassia, (2015), Algorithm Design
and Applications, Wiley.
Goodrich, Tamassia, Mount, (2011), Data
Structures and Algorithms in C++, Wiley
Cormen, Leiserson, Rivest and Stein, (2009),
Introduction to Algorithms, MIT Press.
Frank M. Carrano, (2007), Data Abstraction &
Problem Solving with C++, Pearson.
10
Deletion
11
Deletion
•
The approach to deletion is to first delete with
BST deletion algorithm, and then fix the redblack properties afterwards.
•
Start with a standard BST delete
•
Search for the node (using a BST search).
12
Deletion
•
•
For the purposes of this algorithm ignore the
null leaf nodes.
Deleting the node has 3 possible cases:
1. Node has no children
Delete it.
13
Deletion
2. Node has one child
•
•
Delete it and replace the node with the child
(In practice copy value from child to node and
then delete child)
14
Deletion
3. If the node has two children
The interesting step in this algorithm is that you
don't delete the node itself. Instead you find
another child to node to delete, having first
copied over the value held by this replacement
node into the found node.
15
Deletion
•
•
•
•
•
•
Call the node N.
Obviously we need to maintain the order of the
tree, but the node's replacement could be
anywhere in the subtree.
Need to find the replacement node. This would
either be the successor or predecessor node
dependent on the tree order.
For our purposes, find the node with the largest
value in the left subtree. Call this node R.
Copy the value from R to N.
Delete R.
16
Deletion
•
•
•
The final step is to fix the tree since it may
have changed.
If the removed node was red, the red-black
properties of the tree are maintained.
Otherwise, fix the child of the node that was
removed.
17
Deletion
•
Pretend that we have another property and that
a node can have another extra black token. If the
node is red, then it just becomes black. If the
node is black, it becomes "doubly black". This
violates red-black properties of the tree. The
extra black token is pushed up the tree until:
• it reaches the root node
• it reaches a red node, in which case the red
node becomes black.
• it can be removed by rotating and recolouring
18
Deletion
•
•
Number of
algorithm.
different
approaches
to
the
Look at Goodrich, Tamassia, Mount, (2011),
Data Structures and Algorithms in C++,
pp.488-491
19