Red-Black Trees—Again

Slides:



Advertisements
Similar presentations
Part II. Delete an Node from an AVL Tree Consider to delete
Advertisements

Binary Tries (continued) split(k). Similar to split algorithm for unbalanced binary search trees. Construct S and B on way down the trie. Follow with a.
AA Trees another alternative to AVL trees. Balanced Binary Search Trees A Binary Search Tree (BST) of N nodes is balanced if height is in O(log N) A balanced.
A balanced life is a prefect life.
Red-Black Trees—Again rank(x) = # black pointers on path from x to an external node. Same as #black nodes (excluding x) from x to an external node. rank(external.
Chapter 6: Transform and Conquer Trees, Red-Black Trees The Design and Analysis of Algorithms.
Digital Search Trees & Binary Tries Analog of radix sort to searching. Keys are binary bit strings.  Fixed length – 0110, 0010, 1010,  Variable.
1 /26 Red-black tree properties Every node in a red-black tree is either black or red Every null leaf is black No path from a leaf to a root can have two.
Red Black Trees Colored Nodes Definition Binary search tree.
1 Red Black Trees (Guibas Sedgewick 78). 2 Goal Keep sorted lists subject to the following operations: find(x,L) insert(x,L) delete(x,L) catenate(L1,L2)
Bottom-Up Splay Trees–Analysis Actual and amortized complexity of join is O(1). Amortized complexity of search, insert, delete, and split is O(log n).
Red-Black Trees—Again rank(x) = # black pointers on path from x to an external node. Same as #black nodes (excluding x) from x to an external node. rank(external.
Computer Algorithms Submitted by: Rishi Jethwa Suvarna Angal.
Binary Tree Representation Of Trees Problems with trees. 2- and 3-nodes waste space. Overhead of moving pairs and pointers when changing among.
Balanced Binary Search Tree 황승원 Fall 2010 CSE, POSTECH.
B + -Trees Same structure as B-trees. Dictionary pairs are in leaves only. Leaves form a doubly-linked list. Remaining nodes have following structure:
Data Structures Chapter 10: Efficient Binary Search Trees 10-1.
Bottom-Up Red-Black Trees Top-down red-black trees require O(log n) rotations per insert/delete. Color flips cheaper than rotations. Priority search trees.
Red-Black Tree Insertion Start with binary search insertion, coloring the new node red NIL l Insert 18 NIL l NIL l 1315 NIL l
Balanced Search Trees 2-3 Trees AVL Trees Red-Black Trees
AA Trees.
Balanced Binary Search Trees
G64ADS Advanced Data Structures
Red-Black Trees v z Red-Black Trees Red-Black Trees
Red Black Trees Colored Nodes Definition Binary search tree.
Red Black Trees Colored Nodes Definition Binary search tree.
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.
Red Black Trees
Splay Trees Binary search trees.
Finger search trees.
Lecture 17 Red-Black Trees
Priority Queues © 2010 Goodrich, Tamassia Priority Queues 1
Balanced Trees (AVL and RedBlack)
Red-Black Trees v z Red-Black Trees 1 Red-Black Trees
Binary Search Tree Chapter 10.
Heaps 9/13/2018 3:17 PM Heaps Heaps.
Digital Search Trees & Binary Tries
Splay Trees Binary search trees.
Patricia Practical Algorithm To Retrieve Information Coded In Alphanumeric. Compressed binary trie. All nodes are of the same data type (binary tries use.
Red-Black Trees—Again
original list {67, 33,49, 21, 25, 94} pass { } {67 94}
Balanced Binary Search Trees
Chapter 6 Transform and Conquer.
B-Trees (continued) Analysis of worst-case and average number of disk accesses for an insert. Delete and analysis. Structure for B-tree node.
Red-Black Trees v z Red-Black Trees 1 Red-Black Trees
CS202 - Fundamental Structures of Computer Science II
Red-Black Trees v z /20/2018 7:59 AM Red-Black Trees
Red-Black Trees v z Red-Black Trees Red-Black Trees
Advanced Associative Structures
Binary Tries (continued)
© 2013 Goodrich, Tamassia, Goldwasser
Heaps 12/4/2018 5:27 AM Heaps /4/2018 5:27 AM Heaps.
Bottom-Up Splay Trees–Analysis
Red-Black Trees.
Chapter 6: Transform and Conquer
2-3-4 Trees Red-Black Trees
Red-Black Trees v z /17/2019 4:20 PM Red-Black Trees
CS202 - Fundamental Structures of Computer Science II
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.
Red-Black Trees.
Red Black Trees (Guibas Sedgewick 78)
2-3 Trees Extended tree. Tree in which all empty subtrees are replaced by new nodes that are called external nodes. Original nodes are called internal.
Red-black tree properties
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.
Red Black Trees Colored Nodes Definition Binary search tree.
A Heap Is Efficiently Represented As An Array
Splay Trees Binary search trees.
Red-Black Trees v z /6/ :10 PM Red-Black Trees
Heaps 9/29/2019 5:43 PM Heaps Heaps.
CS210- Lecture 13 June 28, 2005 Agenda Heaps Complete Binary Tree
Presentation transcript:

Red-Black Trees—Again rank(x) = # black pointers on path from x to an external node. Same as #black nodes (excluding x) from x to an external node. rank(external node) = 0.

An Example 3 10 7 8 1 5 30 40 20 25 35 45 60 3 2 2 1 1 1 2 1 1 1 1 1 1

Properties Of rank(x) rank(x) = 0 for x an external node. rank(x) = 1 for x parent of external node. p(x) exists => rank(x) <= rank(p(x)) <= rank(x) + 1. g(x) exists => rank(x) < rank(g(x)).

Red-Black Tree A binary search tree is a red-black tree iff integer ranks can be assigned to its nodes so as to satisfy the stated 4 properties of rank.

Relationship Between rank() And Color (p(x),x) is a red pointer iff rank(x) = rank(p(x)). (p(x),x) is a black pointer iff rank(x) = rank(p(x)) – 1. Red node iff pointer from parent is red. Root is black. Other nodes are black iff pointer from parent is black. Given rank(root) and node/pointer colors, remaining ranks may be computed on way down.

rank(root) Height <= 2 * rank(root). No external nodes at levels 1, 2, …, rank(root). So, #nodes >= S1 <= i <= rank(root) 2i -1 = 2 rank(root) – 1. So, rank(root) <= log2(n+1). So, height(root) <= 2log2(n+1).

Join(S,m,B) Input Output Dictionary S of pairs with small keys. Dictionary B of pairs with big keys. An additional pair m. All keys in S are smaller than m.key. All keys in B are bigger than m.key. Output A dictionary that contains all pairs in S and B plus the pair m. Dictionaries S and B may be destroyed.

Join Binary Search Trees m B O(1) time.

Join Red-black Trees When rank(S) = rank(B), use binary search tree method. S m B rank(root) = rank(S) + 1 = rank(B) + 1.

rank(S) > rank(B) Follow right child pointers from root of S to first node x whose rank equals rank(B). a b x p(x) m B a b x p(x) S If p(x) is a red node, we have 2 red nodes in a sequence. Retrace to root fixing 2 consecutive red nodes problem.

rank(S) > rank(B) If there are now 2 consecutive red pointers/nodes, perform bottom-up rebalancing beginning at m. O(rank(S) – rank(B)). a b x p(x) m B a b x p(x) S

rank(S) < rank(B) Follow left child pointers from root of B to first node x whose rank equals rank(B). Similar to case when rank(S) > rank(B).

Split(k) Inverse of join. Obtain S … dictionary of pairs with key < k. B … dictionary of pairs with key > k. m … pair with key = k (if present).

Split A Binary Search Tree d D e E f m g

Split A Binary Search Tree D c E d m e f g

Split A Binary Search Tree D c E d m e f g

Split A Binary Search Tree D c E d m e f g

Split A Binary Search Tree D b C d c E m e f g

Split A Binary Search Tree D b C d c E e m f g

Split A Binary Search Tree D b C g d c E e f m

Split A Red-Black Tree Previous strategy does not split a red-black tree into two red-black trees. Must do a search for m followed by a traceback to the root. During the traceback use the join operation to construct S and B.

Split A Red-Black Tree S = f B = g b A a B c C d D e E f m g

Split A Red-Black Tree S = f B = g b A a B c C d D e E

Split A Red-Black Tree S = f B = g S = join(e, E, S) b A a B c C d D e

Split A Red-Black Tree S = f B = g S = join(e, E, S) B = join(B, D, d)

Split A Red-Black Tree S = f B = g S = join(e, E, S) B = join(B, D, d) S = join(c, C, S) c

Split A Red-Black Tree S = f B = g S = join(e, E, S) B = join(B, D, d) S = join(c, C, S) B = join(B, B, b)

Split A Red-Black Tree S = f B = g S = join(e, E, S) B = join(B, D, d) S = join(c, C, S) B = join(B, B, b) S = join(a, A, S)

Complexity Of Split O(log n) See text.