Advanced Data Structures and Implementation Top-Down Splay Trees Top-Down Splay Trees Red-Black Trees Red-Black Trees Top-Down Red Black Trees Top-Down.

Slides:



Advertisements
Similar presentations
Chapter 13. Red-Black Trees
Advertisements

Comp 122, Spring 2004 Binary Search Trees. btrees - 2 Comp 122, Spring 2004 Binary Trees  Recursive definition 1.An empty tree is a binary tree 2.A node.
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.
Chapter 4: Trees Part II - AVL Tree
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture.
Outline Scapegoat Trees ( O(log n) amortized time)
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.
Balanced Binary Search Trees
Splay Trees CSIT 402 Data Structures II. Motivation Problems with other balanced trees – AVL: extra storage/complexity for height fields Periulous delete.
November 5, Algorithms and Data Structures Lecture VIII Simonas Šaltenis Nykredit Center for Database Research Aalborg University
A balanced life is a prefect life.
Lecture 12: Balanced Binary Search Trees Shang-Hua Teng.
Chapter 6: Transform and Conquer Trees, Red-Black Trees The Design and Analysis of Algorithms.
1 Red-Black Trees. 2 Black-Height of the tree = 4.
1 Red-Black Trees. 2 Black-Height of the tree = 4.
1 COSC 2P03 Lecture #5 – Trees Part III, Heaps. 2 Today Take up the quiz Assignment Questions Red-Black Trees Binary Heaps Heap sort D-Heaps, Leftist.
Trees and Red-Black Trees Gordon College Prof. Brinton.
CS2420: Lecture 31 Vladimir Kulyukin Computer Science Department Utah State University.
CSC 212 Lecture 19: Splay Trees, (2,4) Trees, and Red-Black Trees.
Advanced Trees Part III Briana B. Morrison Adapted from Alan Eugenio & William J. Collins.
TOP – DOWN Splay Trees Bottom-up splaying requires traversal from root to the node that is to be splayed, and then rotating back to the root – in other.
Splay Trees and B-Trees
CS 61B Data Structures and Programming Methodology Aug 11, 2008 David Sun.
1 Red-Black Trees. 2 Definition: A red-black tree is a binary search tree where: –Every node is either red or black. –Each NULL pointer is considered.
Advanced Data Structures and Algorithms COSC-600 Lecture presentation-6.
CMSC 341 Splay Trees. 8/3/2007 UMBC CMSC 341 SplayTrees 2 Problems with BSTs Because the shape of a BST is determined by the order that data is inserted,
AVL Trees Amanuel Lemma CS252 Algoithms Dec. 14, 2000.
10/20/2015 2:03 PMRed-Black Trees v z. 10/20/2015 2:03 PMRed-Black Trees2 Outline and Reading From (2,4) trees to red-black trees (§9.5) Red-black.
© 2004 Goodrich, Tamassia Red-Black Trees v z.
Red Black Tree Smt Genap Outline Red-Black Trees ◦ Motivation ◦ Definition ◦ Operation Smt Genap
CSIT 402 Data Structures II
Balanced Search Trees 3.4 – 3.7 Team names Majed Suhaim Ahmed Sulaiman M Alharbi.
Min Chen School of Computer Science and Engineering Seoul National University Data Structure: Chapter 8.
Chapter 13 B Advanced Implementations of Tables – Balanced BSTs.
CSCE 3110 Data Structures & Algorithm Analysis AVL Trees Reading: Chap. 4, Weiss.
Lecture 2 Red-Black Trees. 8/3/2007 UMBC CSMC 341 Red-Black-Trees-1 2 Red-Black Trees Definition: A red-black tree is a binary search tree in which: 
AVL Tree Definition: Theorem (Adel'son-Vel'skii and Landis 1962):
Red–black trees.  Define the red-black tree properties  Describe and implement rotations  Implement red-black tree insertion  We will skip red-black.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 9.
Fall 2006 CSC311: Data Structures 1 Chapter 10: Search Trees Objectives: Binary Search Trees: Search, update, and implementation AVL Trees: Properties.
Lecture 11COMPSCI.220.FS.T Balancing an AVLTree Two mirror-symmetric pairs of cases to rebalance the tree if after the insertion of a new key to.
CS 61B Data Structures and Programming Methodology Aug 7, 2008 David Sun.
Review for Exam 2 Topics covered (since exam 1): –Splay Tree –K-D Trees –RB Tree –Priority Queue and Binary Heap –B-Tree For each of these data structures.
Advanced Data Structures By: Nikhil Bhargava. Outline Introduction to ADT. Introduction to ADT. Binary Search Tree (BST) Binary Search Tree (BST) –Definition.
Red-Black Trees Definitions and Bottom-Up Insertion.
Lecture 10COMPSCI.220.FS.T Binary Search Tree BST converts a static binary search into a dynamic binary search allowing to efficiently insert and.
1 Binary Search Trees  Average case and worst case Big O for –insertion –deletion –access  Balance is important. Unbalanced trees give worse than log.
Review for Exam 2 Topics covered: –Recursion and recursive functions –General rooted trees –Binary Search Trees (BST) –Splay Tree –RB Tree –K-D Trees For.
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.
1 Red-Black Trees. 2 A Red-Black Tree with NULLs shown Black-Height of the tree = 4.
Lecture 23 Red Black Tree Chapter 10 of textbook
Definitions and Bottom-Up Insertion
AA Trees.
File Organization and Processing Week 3
Red-Black Trees v z Red-Black Trees Red-Black Trees
Multiway Search Trees Data may not fit into main memory
Red Black Trees
Red-Black Trees v z Red-Black Trees 1 Red-Black Trees
Red-Black Trees Motivations
Red-Black Trees v z /20/2018 7:59 AM Red-Black Trees
Red-Black Trees v z Red-Black Trees Red-Black Trees
Balanced-Trees This presentation shows you the potential problem of unbalanced tree and show two way to fix it This lecture introduces heaps, which are.
Lecture 9 Algorithm Analysis
Lecture 9 Algorithm Analysis
Lecture 9 Algorithm Analysis
Red-Black Trees.
Balanced-Trees This presentation shows you the potential problem of unbalanced tree and show two way to fix it This lecture introduces heaps, which are.
Red-Black Trees v z /17/2019 4:20 PM Red-Black Trees
(2,4) Trees /6/ :26 AM (2,4) Trees (2,4) Trees
Red-Black Trees v z /6/ :10 PM Red-Black Trees
Presentation transcript:

Advanced Data Structures and Implementation Top-Down Splay Trees Top-Down Splay Trees Red-Black Trees Red-Black Trees Top-Down Red Black Trees Top-Down Red Black Trees Top-Down Deletion Top-Down Deletion Deterministic Skip Lists Deterministic Skip Lists AA-Trees AA-Trees Treaps Treaps k-d Trees k-d Trees Pairing Heaps Pairing Heaps

Top-Down Splay Tree Direct strategy requires traversal from the root down the tree, and then bottom-up traversal to implement the splaying tree. Direct strategy requires traversal from the root down the tree, and then bottom-up traversal to implement the splaying tree. Can implement by storing parent links, or by storing the access path on a stack. Can implement by storing parent links, or by storing the access path on a stack. Both methods require large amount of overhead and must handle many special cases. Both methods require large amount of overhead and must handle many special cases. Initial rotations on the initial access path uses only O(1) extra space, but retains the O(log N) amortized time bound. Initial rotations on the initial access path uses only O(1) extra space, but retains the O(log N) amortized time bound.

X Y YLYr XR If Y should become root, then X and its right sub tree are made left children of the smallest value in R, and Y is made root of “center” tree. Y does not have to be a leaf for the Zig case to apply. LR LR X XR Y YLYr Case 1: Zig

Case 2: Zig-Zig X Y ZL Zr XR L R Z YR The value to be splayed is in the tree rooted at Z. Rotate Y about X and attach as left child of smallest value in R LR X XRYR Y ZL Zr Z

Case 3: Zig-Zag (Simplified) X Y ZL Zr XR L R Z YL The value to be splayed is in the tree rooted at Z. To make code simpler, the Zig-Zag rotation is reduced to a single Zig. This results in more iterations in the splay process. L R X XR Y ZL Zr Z YL

Splay Trees Implementation See page 457 for generic SplayTree class implementation. See page 457 for generic SplayTree class implementation. Page 458 shows Top-Down splaying method. Page 458 shows Top-Down splaying method. Refer to page 459 for insertion method into the Top-Down Splay Tree. Refer to page 459 for insertion method into the Top-Down Splay Tree. Page 460 shows deletion method from the tree. Page 460 shows deletion method from the tree.

Reassembling the Splay Tree X XLXR L R X L R XLXR When the value to be splayed to the root is at the root of the “center” tree, we have reached the point where we are ready to reassemble the tree. This is accomplished by a) making XL the right child of the maximum element in L, b) making XR the left child of the minimum element in R, and then making L and R the left and right children of X

Operation 1: Zig-Zig LR A B C D E F G H X Ar Br Cr Dl Er Fl Gl Hl Xl Xr Rotate B around A and make L child of minimum element in R (which is now empty) LR C D E F G H X Cr Dl Er Fl Gl Hl Xl Xr A B ArBr L is still empty, and R is now the tree rooted at B. Note that R contains nodes > X but not in the right subtree of X.

Operation 2: Zig-Zag LR C D E F G H X Cr Dl Er Fl Gl Hl Xl Xr Just perform Zig (simplified Zig-Zag) D Dl LR C Cr A B ArBr L was previously empty and it now consists of node D and D’s left subtree A B ArBr E F G H X Er Fl Gl Hl Xl Xr

L R After X reaches root: X Xl Xr C Cr A B ArBr E Er G Gl F Fl H Hl D Dl This configuration was achieved by doing Zig Zig (of F, G) followed by a Zig (node H) Reassemble – XL becomes right sub tree of H, XR becomes left sub tree of E, and then L, R reattached to X X Xl G Gl F Fl H Hl D Dl C Cr A B ArBr E Er Note that this is not the same tree as was obtained by doing BU splaying.

Red-Black Tree Popular alternative to the AVL tree. Popular alternative to the AVL tree. Operations take O(log N) time in worst case. Operations take O(log N) time in worst case. Height is at most 2log(N+1). Height is at most 2log(N+1). A red-black tree is a binary search tree with one extra attribute for each node: the color, which is either red or black. A red-black tree is a binary search tree with one extra attribute for each node: the color, which is either red or black. The root is black. The root is black. If node is red, its children must be black. If node is red, its children must be black. Every path from a node to a null reference must contain the same number of black nodes. Every path from a node to a null reference must contain the same number of black nodes. Basic operations to conform with rules are color changes and tree rotations. Basic operations to conform with rules are color changes and tree rotations.

Theorem 1 – In a red-black tree, at least half the nodes on any path from the root to a leaf must be black. Proof – If there is a red node on the path, there must be a corresponding black node.

Theorem 2 – In a red-black tree, no path from any node, N, to a leaf is more than twice as long as any other path from N to any other leaf. Proof: By definition, every path from a node to any leaf contains the same number of black nodes. By Theorem1, a least ½ the nodes on any such path are black. Therefore, there can no more than twice as many nodes on any path from N to a leaf as on any other path. Therefore the length of every path is no more than twice as long as any other path.

Theorem 3 – A red-black tree with n internal nodes has height h <= 2 lg(n + 1). Proof: Let h be the height of the red-black tree with root x. By Theorem 1, bh(x) >= h/2 From Theorem 1, n >= 2bh(x) - 1 Therefore n >= 2 h/2 – 1 n + 1 >= 2h/2 lg(n + 1) >= h/2 2lg(n + 1) >= h

Bottom-Up Insertion Cases: Cases: 0: X is the root – color it black 0: X is the root – color it black 1: Both parent and uncle are red – color parent and uncle black, color grandparent red, point X to grandparent, check new situation 1: Both parent and uncle are red – color parent and uncle black, color grandparent red, point X to grandparent, check new situation 2 (zig-zag): Parent is red, but uncle is black. X and its parent are opposite type children – color grandparent red, color X black, rotate left on parent, rotate right on grandparent 2 (zig-zag): Parent is red, but uncle is black. X and its parent are opposite type children – color grandparent red, color X black, rotate left on parent, rotate right on grandparent 3 (zig-zig): Parent is red, but uncle is black. X and its parent are both left or both right children – color parent black, color grandparent red, rotate right on grandparent 3 (zig-zig): Parent is red, but uncle is black. X and its parent are both left or both right children – color parent black, color grandparent red, rotate right on grandparent

Top-Down Red-Black Trees In T-Down insertion, the corrections are done while traversing down the tree to the insertion point. In T-Down insertion, the corrections are done while traversing down the tree to the insertion point. When the actual insertion is done, no further corrections are needed, so no need to traverse back up the tree. When the actual insertion is done, no further corrections are needed, so no need to traverse back up the tree. So, T-Down insertion can be done iteratively which is generally faster. So, T-Down insertion can be done iteratively which is generally faster. Insertion is always done as a leaf (as in ordinary BST insertion). Insertion is always done as a leaf (as in ordinary BST insertion).

Process On the way down, when we see a node X that has two red children, we make X red and its two children black. On the way down, when we see a node X that has two red children, we make X red and its two children black. If X’s parent is red, we can apply either the single or double rotation to keep us from having two consecutive red nodes. If X’s parent is red, we can apply either the single or double rotation to keep us from having two consecutive red nodes. X’s parent and the parent’s sibling cannot both be red, since their colors would already have been flipped in that case. X’s parent and the parent’s sibling cannot both be red, since their colors would already have been flipped in that case.

Example: Insert Two red children

Example (Cont.) flip colors - two red nodes

Example (Cont.): Do a single rotation

Example (Cont.): Now Insert

Note Since the parent of the newly inserted node was black, we are done. Since the parent of the newly inserted node was black, we are done. Had the parent of the inserted node been red, one more rotation would have had to be performed. Had the parent of the inserted node been red, one more rotation would have had to be performed. Although red-black trees have slightly weaker balancing properties, their performance in experimentally almost identical to that of AVL trees. Although red-black trees have slightly weaker balancing properties, their performance in experimentally almost identical to that of AVL trees.

Implementation of Top-Down Red- Black Trees See pages See pages

Top-Down Deletions Recall that in deleting from a binary search tree, the only nodes which are actually removed are leaves or nodes with exactly one child. Recall that in deleting from a binary search tree, the only nodes which are actually removed are leaves or nodes with exactly one child. Nodes with two children are never removed. Their contents are just replaced. Nodes with two children are never removed. Their contents are just replaced. If the node to be deleted is red, there is no problem - just delete the node. If the node to be deleted is red, there is no problem - just delete the node. If the node to be deleted is black, its removal will violate property. If the node to be deleted is black, its removal will violate property. The solution is to ensure that any node to be deleted is red. The solution is to ensure that any node to be deleted is red.

Deterministic Skip Lists A probabilistically balanced linked list. A probabilistically balanced linked list. Invented in 1986 by William Pugh. Invented in 1986 by William Pugh. Definition: Two elements are linked if there exists at least one link going from one to another. Definition: Two elements are linked if there exists at least one link going from one to another. Definition: The gap size between two elements linked at height h is equal to the number of elements of height h-1 between them. Definition: The gap size between two elements linked at height h is equal to the number of elements of height h-1 between them.

Skip List 3 6 NIL NIL d) xtra pointers every eighth item - full structure e) skip list - same link distribution, random choice

Search time In the deterministic version (a-d): In the deterministic version (a-d): –in a, we need to check at most n nodes –in b, at most  n/2  +1 nodes –in c, at most  n/4  +2 nodes –in general, at most  log N  nodes Efficient search, but impractical insertion and deletion. Efficient search, but impractical insertion and deletion.

Levels A node with k forward pointers is called a level k node. A node with k forward pointers is called a level k node. If every (2 i )th node has a pointer 2 i nodes ahead, they have the following distribution: If every (2 i )th node has a pointer 2 i nodes ahead, they have the following distribution: levelpercent ……

Central idea in skip lists Choose levels of nodes randomly, but in the same proportions (as in e). Choose levels of nodes randomly, but in the same proportions (as in e). A node’s i th forward pointer, points to the next node of level i or higher. A node’s i th forward pointer, points to the next node of level i or higher. Insertions and deletions require only local modifications. Insertions and deletions require only local modifications. A node’s level never changes after first being chosen. A node’s level never changes after first being chosen.

Insertion To perform insertion, we must make sure that when a new node of height h is added, it doesn’t create a gap of four heights of h node (in deterministic skip list). To perform insertion, we must make sure that when a new node of height h is added, it doesn’t create a gap of four heights of h node (in deterministic skip list). See page 269 fig See page 269 fig For implementation of Skip List see pages For implementation of Skip List see pages

AA-Trees Also known as binary B-tree (BB-tree). Also known as binary B-tree (BB-tree). BB-tree is a red-black tree with one extra condition: any node may have at most one red child. BB-tree is a red-black tree with one extra condition: any node may have at most one red child. Some conditions to make it simpler (p.475): Some conditions to make it simpler (p.475): - only right child can be red - code functions recursively - instead of color store information in small integer: - one if the node is a leaf - the level of its parent, if the node is red - one less then the level of its parent, if the node is black

Advantages AA-trees simplify algorithms by: AA-trees simplify algorithms by: - eliminating half of the restructuring cases - simplifying deletion by removing an annoying case if an internal node has only one child, that child must be a red right child if an internal node has only one child, that child must be a red right child We can always replace a node with the smallest child in the right sub tree (it will either be a leaf or have a red child) We can always replace a node with the smallest child in the right sub tree (it will either be a leaf or have a red child)

Links in AA-tree A horizontal link is a connection between a node and a child with equal levels. A horizontal link is a connection between a node and a child with equal levels. Horizontal links are right references. Horizontal links are right references. There cannot be two consecutive horizontal links. There cannot be two consecutive horizontal links. Nodes at level 2 or higher must have two children. Nodes at level 2 or higher must have two children. If a node has no right horizontal link, its two children are at the same level. If a node has no right horizontal link, its two children are at the same level.

Example

Insertion in AA-tree A new item is always inserted at the bottom level. A new item is always inserted at the bottom level. In the previous example, inserting 2 will create a horizontal left link. In the previous example, inserting 2 will create a horizontal left link. In the previous example, inserting 45 generates consecutive right links. In the previous example, inserting 45 generates consecutive right links. After inserting at the bottom level, we may need to perform rotations to restore the horizontal link properties. After inserting at the bottom level, we may need to perform rotations to restore the horizontal link properties.

skew – remove left horizontal links PX A BC PX A BC

split – remove consecutive horizontal links XP A B G X P A B G

More on skew & split skew removes a left horizontal link. skew removes a left horizontal link. skew might also create consecutive right horizontal links. skew might also create consecutive right horizontal links. First we must apply skew and then use split, if necessary. First we must apply skew and then use split, if necessary. After a split, the middle node increases a level, which may create a problem for the original parent. After a split, the middle node increases a level, which may create a problem for the original parent.

Implementation of AA-trees Refer to pages 476 – 480 for detailed implementation techniques. Refer to pages 476 – 480 for detailed implementation techniques. See page 477 & 479 for more examples of left and right rotations of AA-trees. See page 477 & 479 for more examples of left and right rotations of AA-trees.

Treaps Binary search tree. Binary search tree. Like skip list, it uses random numbers and gives O (log N) expected time for any input. Like skip list, it uses random numbers and gives O (log N) expected time for any input. Slower than balanced search tree. Slower than balanced search tree. Although deletion is much slower, it is still O (log N) expected time. Although deletion is much slower, it is still O (log N) expected time.

Definition of a treap Each node stores an item, left and right link, and a priority that is randomly assigned when the node is created. Each node stores an item, left and right link, and a priority that is randomly assigned when the node is created. Treap is a binary search tree with the property that the node priorities satisfy heap order: any node’s priority must be at least as large as its parents. Treap is a binary search tree with the property that the node priorities satisfy heap order: any node’s priority must be at least as large as its parents. See pages for implementation details. See pages for implementation details.

k – d Trees Multidimensional b-tree. Multidimensional b-tree. Branching of odd levels is done with respect to the first key, and branching on even levels is done with respect to the second key. Branching of odd levels is done with respect to the first key, and branching on even levels is done with respect to the second key. Root is arbitrary chosen to be an odd level. Root is arbitrary chosen to be an odd level. Can be visually represented: Can be visually represented:

Some facts about k-d trees Can have any number of dimensions. Can have any number of dimensions. In practice searches tend to be very efficient. In practice searches tend to be very efficient. For a randomly constructed tree, the average running time of a partial match query is O (M+kN^(1-1/k)). For a randomly constructed tree, the average running time of a partial match query is O (M+kN^(1-1/k)). See pages for implementation details. See pages for implementation details.

Pairing Heaps A min (max) pairing heap is a min (max) tree in which operations are done in a specified manner. A min (max) pairing heap is a min (max) tree in which operations are done in a specified manner

Insert Create 1-element max tree with new item and meld with existing max pairing heap. Create 1-element max tree with new item and meld with existing max pairing heap insert(2) =

Insert (Cont.) Create 1-element max tree with new item and meld with existing max pairing heap. Create 1-element max tree with new item and meld with existing max pairing heap insert(14) =

IncreaseKey (Node, theAmount) Since nodes do not have parent fields, we cannot easily check whether the key in the Node becomes larger than that in its parent. Since nodes do not have parent fields, we cannot easily check whether the key in the Node becomes larger than that in its parent. So, detach the Node from sibling doubly- linked list and meld. So, detach the Node from sibling doubly- linked list and meld.

IncreaseKey (Node, theAmount) If theNode is not the root, remove sub tree rooted at theNode from its sibling list theNode

IncreaseKey (Node, theAmount)

IncreaseKey (Node, theAmount)

Pairing heaps See pages 488 – 491 for implementation details. See pages 488 – 491 for implementation details.