Download presentation
Presentation is loading. Please wait.
Published byJyrki Lehtinen Modified over 5 years ago
1
CO4301 – Advanced Games Development Week 5 Red-Black Trees
Gareth Bellaby
2
Red-Black trees
3
Importance Used in partitioning Z-order
4
Balanced tree A balanced tree is one such that for any two leaves the difference of the depth is at most 1. This is a height balanced tree. There is a also a weight balanced tree. The height of a balanced tree is O(log n) This means therefore that a worst-case complexity is O(log n) This the best possible form that a binary search tree could take.
5
Balanced tree If the tree becomes unbalanced then its efficiency will decrease. Q: what's the worst it can become? Form? Worst-case complexity? Notice that it could only take 1 insertion for the tree to become unbalanced.
6
Balanced tree A balanced tree is an efficient implementation for a list. Priority queues (talked about these last week) As we shall see it can also be used for sets, maps, multimaps and associative arrays.
7
Self-balancing tree Self-balancing tree is one that keeps its height,
There are a number of data structures which implement a self-balancing tree. 2-3 trees Splay trees SVL tree
8
Red-Black tree Not perfect, but a reasonable approximation which means it works near optimally. 1978 paper, "A Dichromatic Framework for Balanced Trees", Leonidas J. Guibas and Robert Sedgewick
9
Red-Black tree Start with binary tree
Each node is given an extra binary tracking value. The metaphor used is painting. It is called a red-black tree because a node is painted red or black. Colouring simplifies the data structure. Every node is coloured.
10
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.
11
Red-Black tree examples
12
Red-Black tree What these rules do is enforce the property that any path from the root to a leaf is no more than twice as long as any other. The tree is therefore efficient. The tree is roughly height balanced.
13
Red-Black tree By convention, Null (or Nil) leaves are black.
Some use the convention that you don't need to draw the Null leaves. A red-black tree has height O(log n) Search is the same as for a binary search tree.
14
Red-Black tree example
15
Implementation observations
Note that the leaf nodes do not have to be explicitly stored. The link to the child node could include the information that it is leaf. Extra data for each node is only binary. A common approach is se a bit to store the value, thereby incurring little or no extra memory. However, remember word size and memory. Note this works both ways: a single bit could mean the incursion of an extra cost. However, a node may well have excess space because it doesn't take up a full word size and so no cost to store the extra bit.
16
Implementation observations
Also note that you could use a single node for all of the child nodes. Since by definition the child is a leaf, it doesn't matter if all of the references to a leaf actually point to the same node.
17
BST Add the node as any binary search tree insertion
BST = binary search tree. For every node n All keys in n's left subtree are less than the key in n, and All keys in n's right subtree are greater than the key in n. (There are other ways to express this !)
18
BST Insertion Insert into BST Tree
New nodes are always inserted as leaves. Use binary search to find a node whereby inserting the new node does not break the BST property. If root empty, then make new node the root and quit.
19
BST Insertion algorithm
New node = n current = root If (key of n) < (key of current) If current has a left child, search left Else add n as a left child to current If (key of n) > (key of current) If current has a right child, search right Else add n as a right child to current
20
Red-Black tree Insertion
Objective is to add a node whilst maintaining the red-black properties of the tree. Need to change the structure of the tree. This is done by "rotation" Rotation will preserve the binary search tree property. Can do the rotation on different nodes within the tree, the nodes can have subtrees.
21
Rotation Left rotation on x Inverse operation Right rotation on y
22
Red-Black tree Insertion
Objective is to add a node whilst maintaining the red-black properties of the tree. Perform a BST insertion. Colour the inserted node red (unless it is the root, in which case colour it black)
23
Red-Black tree Insertion
There are two properties which can be violated. Firstly, the property "Root is black". This is dealt with trivially by adding the proviso that if it is the root node then colour it black instead and quit. The only other possible red-black property which can be violated is the one "Children of a red node are black.“ (Alternatively expressed as “a red node cannot have a red child”).
24
Red-Black tree Insertion
Check colour of the parent. If parent is black then the insertion has not changed the property of the tree and so everything is OK. Quit.
25
Red-Black tree Insertion
If parent is red this breaks the property "Children of a red node are black."
26
Red-Black tree Insertion
Call the inserted node N. Call the parent P. Grandparent: parent of the parent. Call it G. Uncle: the other child of the grandparent. Call it U.
27
Insertion Case 1 A number of different cases: Case 1:
If the uncle of N is red. Recolour the node's parent, grandparent and uncle. Continue recursively with rest of tree (to be explicit: make uncle and parent black, and make node and grandparent red.) Carry on up the tree. Make the grandfather the new node N, i.e. moving up the tree by two levels
28
=> Insertion Case 1 Case 1: The uncle of N is red.
Recolour the node's parent, grandparent and uncle. Continue recursively with rest of tree. =>
29
Insertion Case 2 The other two cases are when the uncle of N is black. Either N is the left child or N is the right child. We want to make N the left child so let’s start with the case where N is a right child: Case 2: the uncle of N is black and N is a right child. Do a left rotation on P to make N a right child. Re-label N and P. Now falls into case 3.
30
Insertion Case 2 Case 2: the uncle of N is black and N is a right child. Do a left rotation on P.
31
Insertion Case 2 Relabel N and P. Now falls into case 3.
32
Insertion Case 3 Case 3: the uncle of N is black and N is a left child. Do a right rotation on G. Swap colours of P and G.
33
Insertion Case 3 Do a right rotation on G.
34
Insertion Case 3 Swap colours of P and G.
35
Red-Black tree Insertion
Nice visualisation: The summary on Wikipedia is quite good, but note that my number of the cases is different. See also the references I’ve given
36
References Leonidas J. Guibas and Robert Sedgewick, "A Dichromatic Framework for Balanced Trees“ T Cormen,, C Leiserson, R Rivest, C Stein, Introduction to Algorithms Frank M. Carrano, Timothy Henry, Data Abstraction & Problem Solving with C++: Walls and Mirrors Paperback
37
Next Few Weeks Patterns 15-20 minute presentation on a pattern
Game Programming Patterns Robert Nystrom, Game Programming Patterns
38
Patters References Robert Nystrom, Game Programming Patterns
Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides (the GangOfFour), Design Patterns: Elements of Reusable Object-Oriented Software Note that Chapter 3: Creational Patterns, is about building a maze
39
Next Week Julian Gold, Object-Oriented Game Development Chapter 2 of Bruce Sutherland, C++ Game Development Primer
40
Topics Abstract Factory Builder: Factory Method Prototype Observer
Chain of responsibility State Composite
41
Topics Adapter Facade Flyweight Proxy Command Mediator Strategy
Template method
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.