Presentation is loading. Please wait.

Presentation is loading. Please wait.

CO4301 – Advanced Games Development Week 10 Red-Black Trees continued

Similar presentations


Presentation on theme: "CO4301 – Advanced Games Development Week 10 Red-Black Trees continued"— Presentation transcript:

1 CO4301 – Advanced Games Development Week 10 Red-Black Trees continued
Gareth Bellaby

2 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 are some differing characteristics.

3 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.

4 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.

5 Applications Sets and maps. Why?
STL map is typically implemented using a red-black 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.

6 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) re-colourings.

7 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).

8 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 red-black 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++

9 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.

10 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.

11 Deletion

12 Deletion The approach to deletion is to first delete with BST deletion algorithm, and then fix the red-black properties afterwards. Start with a standard BST delete Search for the node (using a BST search).

13 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.

14 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)

15 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.

16 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.

17 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.

18 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

19 Deletion Number of different approaches to the algorithm.
Look at Goodrich, Tamassia, Mount, (2011), Data Structures and Algorithms in C++, pp


Download ppt "CO4301 – Advanced Games Development Week 10 Red-Black Trees continued"

Similar presentations


Ads by Google