Red Black Trees Colored Nodes Definition Binary search tree.

Slides:



Advertisements
Similar presentations
Splay Trees Binary search trees.
Advertisements

AVL Trees binary tree for every node x, define its balance factor
Part II. Delete an Node from an AVL Tree Consider to delete
AVL Trees1 Part-F2 AVL Trees v z. AVL Trees2 AVL Tree Definition (§ 9.2) AVL trees are balanced. An AVL Tree is a binary search tree such that.
Binary Search Trees Data Structures & Problem Solving Using JAVA Second Edition Mark Allen Weiss Chapter 19 (continued) © 2002 Addison Wesley.
1 CSE 373 AVL trees, continued read: Weiss Ch. 4, section slides created by Marty Stepp
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.
1 Balanced Search Trees  several varieties  AVL trees  trees  Red-Black trees  B-Trees (used for searching secondary memory)  nodes are added.
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.
Dynamic Dictionaries Primary Operations:  Get(key) => search  Insert(key, element) => insert  Delete(key) => delete Additional operations:  Ascend()
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.
1 Binary Search Trees Implementing Balancing Operations –AVL Trees –Red/Black Trees Reading:
Trees and Red-Black Trees Gordon College Prof. Brinton.
Dynamic Set AVL, RB Trees G.Kamberova, Algorithms Dynamic Set ADT Balanced Trees Gerda Kamberova Department of Computer Science Hofstra University.
CS2420: Lecture 31 Vladimir Kulyukin Computer Science Department Utah State University.
AVL Trees v z. 2 AVL Tree Definition AVL trees are balanced. An AVL Tree is a binary search tree such that for every internal node v of T, the.
Balanced Binary Search Trees height is O(log n), where n is the number of elements in the tree AVL (Adelson-Velsky and Landis) trees red-black trees get,
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.
Binary Tree Representation Of Trees Problems with trees. 2- and 3-nodes waste space. Overhead of moving pairs and pointers when changing among.
Red Black Tree Smt Genap Outline Red-Black Trees ◦ Motivation ◦ Definition ◦ Operation Smt Genap
CSIT 402 Data Structures II
Beyond (2,4) Trees What do we know about (2,4)Trees? Balanced
Balanced Binary Search Tree 황승원 Fall 2010 CSE, POSTECH.
Min Chen School of Computer Science and Engineering Seoul National University Data Structure: Chapter 8.
Chapter 13 B Advanced Implementations of Tables – Balanced BSTs.
B + -Trees Same structure as B-trees. Dictionary pairs are in leaves only. Leaves form a doubly-linked list. Remaining nodes have following structure:
1 Balanced Trees There are several ways to define balance Examples: –Force the subtrees of each node to have almost equal heights –Place upper and lower.
Search Trees. Binary Search Tree (§10.1) A binary search tree is a binary tree storing keys (or key-element pairs) at its internal nodes and satisfying.
1 Trees 4: AVL Trees Section 4.4. Motivation When building a binary search tree, what type of trees would we like? Example: 3, 5, 8, 20, 18, 13, 22 2.
M-ary Trees. m-ary trees Some trees need to be searched efficiently, but have more than two children l parse trees l game trees l genealogical trees,
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
Dynamic Dictionaries Primary Operations:  get(key) => search  put(key, element) => insert  remove(key) => delete Additional operations:  ascend()
1 Binary Search Trees  Average case and worst case Big O for –insertion –deletion –access  Balance is important. Unbalanced trees give worse than log.
Red-Black Trees Bottom-Up Deletion. Recall “ordinary” BST Delete 1.If vertex to be deleted is a leaf, just delete it. 2.If vertex to be deleted has just.
More Trees. Outline Tree B-Tree 2-3 Tree Tree Red-Black Tree.
Red-Black Trees an 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.
Lecture 15 Nov 3, 2013 Height-balanced BST Recall:
AA Trees.
Balanced Binary Search Trees
Lec 13 Oct 17, 2011 AVL tree – height-balanced tree Other options:
Red-Black Tree Neil Tang 02/07/2008
Red-Black Tree Neil Tang 02/04/2010
G64ADS Advanced Data Structures
Red Black Trees Colored Nodes Definition Binary search tree.
Red Black Trees Colored Nodes Definition Binary search tree.
Red Black Trees
Splay Trees Binary search trees.
AVL Trees binary tree for every node x, define its balance factor
AVL DEFINITION An AVL tree is a binary search tree in which the balance factor of every node, which is defined as the difference between the heights of.
AVL Trees A BST in which, for any node, the number of levels in its two subtrees differ by at most 1 The height of an empty tree is -1. If this relationship.
Red-Black Trees 9/12/ :44 AM AVL Trees v z AVL Trees.
Red-Black Trees Bottom-Up Deletion.
Red-Black Trees Bottom-Up Deletion.
Splay Trees Binary search trees.
Red-Black Trees 11/13/2018 2:07 AM AVL Trees v z AVL Trees.
Balanced Binary Search Trees
Red-Black Trees v z Red-Black Trees 1 Red-Black Trees
Red-Black Trees 11/26/2018 3:42 PM AVL Trees v z AVL Trees.
Red-Black Trees 2018年11月26日3时46分 AVL Trees v z AVL Trees.
Red-Black Trees Bottom-Up Deletion.
AVL Search Tree put(9)
Dynamic Dictionaries Primary Operations: Additional operations:
Red-Black Trees Bottom-Up Deletion.
Red-Black Trees 2/24/ :17 AM AVL Trees v z AVL Trees.
Lecture 10 Oct 1, 2012 Complete BST deletion Height-balanced BST
Red-Black Trees 5/19/2019 6:39 AM AVL Trees v z AVL Trees.
Red Black Trees.
Red Black Trees Colored Nodes Definition Binary search tree.
Presentation transcript:

Red Black Trees Colored Nodes Definition Binary search tree. Each node is colored red or black. Root and all external nodes are black. No root-to-external-node path has two consecutive red nodes. All root-to-external-node paths have the same number of black nodes Every red node has a black parent.

Red Black Trees Colored Edges Definition Binary search tree. Child pointers are colored red or black. Pointer to an external node is black. No root to external node path has two consecutive red pointers. Every root to external node path has the same number of black pointers.

Example Red-Black Tree 10 7 8 1 5 30 40 20 25 35 45 60 3 Height = 5

Properties The height of a red black tree that has n (internal) nodes is between log2(n+1) and 2log2(n+1).

Properties Start with a red black tree whose height is h; collapse all red nodes into their parent black nodes to get a tree whose node-degrees are between 2 and 4, height is >= h/2, and all external nodes are at the same level.

Properties 10 7 8 1 5 30 40 20 25 35 45 60 3 Collapsed height = 3.

Properties Let h’>= h/2 be the height of the collapsed tree. In worst-case, all internal nodes of collapsed tree have degree 2. Number of internal nodes in collapsed tree >= 2h’-1. So, n >= 2h’-1 So, h <= 2 log2 (n + 1)

Properties At most 1 rotation and O(log n) color flips per insert/delete. Priority search trees. Two keys per element. Search tree on one key, priority queue on other. Color flip doesn’t disturb priority queue property. Rotation disturbs priority queue property. O(log n) fix time per rotation => O(log2n) overall time. Color flip to be defined later. Binary search trees in which each node contains a binary search tree are used, for example, in IP router tables. Each rotation of the outer binary search tree requires changes in the binary search trees of the involved nodes resulting in an O(log n) cost per rotation.

Properties O(1) amortized complexity to restructure following an insert/delete. C++ STL implementation java.util.TreeMap => red black tree

Insert New pair is placed in a new node, which is inserted into the red-black tree. New node color options. Black node => one root-to-external-node path has an extra black node (black pointer). Hard to remedy. Red node => one root-to-external-node path may have two consecutive red nodes (pointers). May be remedied by color flips and/or a rotation.

Classification Of 2 Red Nodes/Pointers b c d gp pp p LLb XYz X => relationship between gp and pp. pp left child of gp => X = L. Y => relationship between pp and p. p right child of pp => Y = R. z = b (black) if d = null or a black node. z = r (red) if d is a red node. Initially, p is newly inserted node. Pp and gp may be null. In each round, p, pp, and gp are moved up by 2 levels. If p is a red root, make it black. If p or pp black done! If pp is a red root, make it black. O.w., gp exists

XYr Color flip. Move p, pp, and gp up two levels. b c d gp pp p a b c d gp pp p Move p, pp, and gp up two levels. Continue rebalancing if necessary.

LLb Rotate. Done! Same as LL rotation of AVL tree. a b c d a b c d gp y x a b z c d a b c d gp pp p x y z Done! Same as LL rotation of AVL tree.

LRb Rotate. Done! Same as LR rotation of AVL tree. y x a b z c d b c a d gp pp p y x z Done! Same as LR rotation of AVL tree. RRb and RLb are symmetric.

Delete Delete as for unbalanced binary search tree. If red node deleted, no rebalancing needed. If black node deleted, a subtree becomes one black pointer (node) deficient.

Delete A Black Leaf 10 7 8 1 5 30 40 20 25 35 45 60 3 Delete 8.

Delete A Black Leaf y is root of deficient subtree. py is parent of y. 10 7 1 5 30 40 20 25 35 45 60 3 py y If py doesn’t exist, the whole tree is one black node deficient. This is OK. y is root of deficient subtree. py is parent of y.

Delete A Black Degree 1 Node 10 7 8 1 5 30 40 20 25 35 45 60 3 py y Delete 45. y is root of deficient subtree.

Delete A Black Degree 2 Node 10 7 8 1 5 30 40 20 25 35 45 60 3 Not possible, degree 2 nodes are never deleted.

Rebalancing Strategy If y is a red node, make it black. py y 10 7 8 1 5 30 40 20 25 35 45 60 3 y py

Rebalancing Strategy Now, no subtree is deficient. Done! py y 60 10 7 8 1 5 30 40 20 25 35 45 3 y py

Rebalancing Strategy y is a black root (there is no py). Entire tree is deficient. Done! 60 10 7 8 1 5 30 40 20 25 35 45 3 y

Rebalancing Strategy y is black but not the root (there is a py). Xcn v Xcn y is right child of py => X = R. Pointer to v is black => c = b. v has 1 red child => n = 1. y represents both a subtree and its root. This includes the case when y is null. Broken lines indicate pointers whose colors are unspecified. v must exist because y is one black node/pointer deficient.

Rb0 (case 1) Color change. Now, py is root of deficient subtree. v a b y py v Color change. Now, py is root of deficient subtree. Continue!

Rb0 (case 2) Color change. Deficiency eliminated. Done! y a b py v a b

Rb1 (case 1) LL rotation. Deficiency eliminated. Done! a b y v py a b

Rb1 (case 2) LR rotation. Deficiency eliminated. Done! c y w py a b v

Rb2 c y w py a b v a y py v b c w LR rotation. Deficiency eliminated. Done!

Rr(n) n = # of red children of v’s right child w. a y py v b c w Rr(2)

Rr(0) a b y v py a b y py v LL rotation. Done!

Rr(1) (case 1) LR rotation. Deficiency eliminated. Done! y c w py a v b a y py v b w c LR rotation. Deficiency eliminated. Done!

Rr(1) (case 2) y d x py a v b c w a y py v b w c d x Rotation. Deficiency eliminated. Done!

Rr(2) Rotation. Deficiency eliminated. Done! d y x py a v b c w a y py