Treaps.

Slides:



Advertisements
Similar presentations
Chapter 13. Red-Black Trees
Advertisements

AVL Trees Balanced Trees. AVL Tree Property A Binary search tree is an AVL tree if : –the height of the left subtree and the height of the right subtree.
AVL Trees / Slide 1 Delete Single rotation Deletion.
6/14/2015 6:48 AM(2,4) Trees /14/2015 6:48 AM(2,4) Trees2 Outline and Reading Multi-way search tree (§3.3.1) Definition Search (2,4)
TCSS 342 AVL Trees v1.01 AVL Trees Motivation: we want to guarantee O(log n) running time on the find/insert/remove operations. Idea: keep the tree balanced.
1 Red-Black Trees. 2 Black-Height of the tree = 4.
AVL trees. AVL Trees We have seen that all operations depend on the depth of the tree. We don’t want trees with nodes which have large height This can.
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.
Outline Binary Trees Binary Search Tree Treaps. Binary Trees The empty set (null) is a binary tree A single node is a binary tree A node has a left child.
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.
Lecture 15 Jianjun Hu Department of Computer Science and Engineering University of South Carolina CSCE350 Algorithms and Data Structure.
AVL trees1 AVL Trees Height of a node : The height of a leaf is 1. The height of a null pointer is zero. The height of an internal node is the maximum.
Priority Search Trees Keys are pairs (x,y). Basic (search, insert, delete) and rectangle operations. Two varieties.  Based on a balanced binary search.
AVL TREES By Asami Enomoto CS 146 AVL Tree is… named after Adelson-Velskii and Landis the first dynamically balanced trees to be propose Binary search.
AVL Tree: Balanced Binary Search Tree 9.
1 Red-Black Trees. 2 A Red-Black Tree with NULLs shown Black-Height of the tree = 4.
Part-D1 Binary Search Trees
Lecture 15 Nov 3, 2013 Height-balanced BST Recall:
AA Trees.
Lec 13 Oct 17, 2011 AVL tree – height-balanced tree Other options:
G64ADS Advanced Data Structures
Lecture 15 AVL Trees Slides modified from © 2010 Goodrich, Tamassia & by Prof. Naveen Garg’s Lectures.
Topics covered (since exam 1):
CS 332: Algorithms Red-Black Trees David Luebke /20/2018.
Red Black Trees
Splay Trees Binary search trees.
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.
Data Structures and Algorithms (AT70. 02) Comp. Sc. and Inf. Mgmt
Binary Search Tree Chapter 10.
Red-Black Trees 9/12/ :44 AM AVL Trees v z AVL Trees.
Summary of General Binary search tree
Dynamic Dictionaries Primary Operations: Additional operations:
Slide Sources: CLRS “Intro. To Algorithms” book website
Splay Trees Binary search trees.
CS200: Algorithms Analysis
Red-Black Trees 11/13/2018 2:07 AM AVL Trees v z AVL Trees.
Randomized Algorithms: Data Structures
(2,4) Trees (2,4) Trees 1 (2,4) Trees (2,4) Trees
Chapter 6 Transform and Conquer.
Priority Search Trees Keys are pairs (x,y).
Topics covered (since exam 1):
Slide Sources: CLRS “Intro. To Algorithms” book website
Topics covered (since exam 1):
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.
Search Sorted Array: Binary Search Linked List: Linear Search
(2,4) Trees /26/2018 3:48 PM (2,4) Trees (2,4) Trees
Red-Black Trees.
Priority Queues (Chapter 6.6):
(2,4) Trees (2,4) Trees (2,4) Trees.
CSE 332: Data Abstractions AVL Trees
Topics covered (since exam 1, excluding PQ):
(2,4) Trees 2/15/2019 (2,4) Trees (2,4) Trees.
(2,4) Trees /24/2019 7:30 PM (2,4) Trees (2,4) Trees
AVL Trees 2/23/2019 AVL Trees v z AVL Trees.
Red-Black Trees 2/24/ :17 AM AVL Trees v z AVL Trees.
Balanced binary search trees
(2,4) Trees (2,4) Trees (2,4) Trees.
Definition Applications Implementations Heap Comparison
AVL-Trees (Part 1).
CSE2331/5331 Topic 7: Balanced search trees Rotate operation
Binary SearchTrees [CLRS] – Chap 12.
Lecture 10 Oct 1, 2012 Complete BST deletion Height-balanced BST
AVL Trees B.Ramamurthy 4/27/2019 BR.
Priority Queues (Chapter 6):
Topics covered (since exam 1):
Search Sorted Array: Binary Search Linked List: Linear Search
Red-Black Trees 5/19/2019 6:39 AM AVL Trees v z AVL Trees.
Tutorial 2 - Daniel Razavi
CS210- Lecture 19 July 18, 2005 Agenda AVL trees Restructuring Trees
Presentation transcript:

Treaps

Definition A treap is a binary search tree in which each node has both a key and a priority. Nodes are ordered in an in-order fashion by their keys and are heap-ordered by their priorities.

Keys a b c d e f g Priorities 3 7 4 1 5 2 6 d 1 a3 f 2 c 4 e 5 g 6 b 7

Existence Why must a treap with any priorities exist? We can always create one by sorting by priority and then inserting as a binary search tree!

Insertion How do we insert into an existing treap? Insert as a BST, then perform a series of tree rotations to enforce the heap ordering invariant.

Rotation Review – R to Root x r B C C r x B A

Rotation Review – R to Root x r B C C r x B A

Deletion Given an existing treap, how do we delete a node? Rotate it down to a leaf, and then delete it.

Theorem Expected # of comparisons for insert, search, delete is Θ(log n) Equivalent to showing the expected height is Θ(log n) We will prove a weaker statement: Expected height of a single node is O(log n)

Oh god, a proof Suppose we search for a node m.

Oh god, a proof Xi = 1 if i is an ancestor of m 0 otherwise

Oh god, a proof depth(m) = Σ1 ≤ i ≤ nXi

E[depth(m)] = E[Σ1 ≤ i ≤ nXi] Oh god, a proof E[depth(m)] = E[Σ1 ≤ i ≤ nXi]

E[depth(m)] = Σ1 ≤ i ≤ nE[Xi] Oh god, a proof E[depth(m)] = Σ1 ≤ i ≤ nE[Xi]

Oh god, a proof E[depth(m)] = Σ1 ≤ i ≤ nPr[ i is an ancestor of m ]

Finding Pr[ i is an ancestor of m ] Suppose i ≤ m. Consider [i, i+1, …, m-1, m] i must have the highest priority out of these

Finding Pr[ i is an ancestor of m ] Suppose i > m. By symmetry:

Oh god, a proof E[depth(m)] = Σ1 ≤ i ≤ nPr[ i is an ancestor of m ]

Oh god, a proof E[depth(m)] = Σ1 ≤ i ≤ m + Σm+1 ≤ i ≤ n

Oh god, a proof E[depth(m)] = Σ1 ≤ i ≤ m + Σm+1 ≤ i ≤ n ≤ 2 log n

COUNTING ROTATIONS Theorem. The expected number of rotations for insertion or deletion is < 2 Right-most nodes in the left subtree – L(x) Left-most nodes in the right subtree – R(x) x R(x) L(x)

COUNTING ROTATIONS Lemma. Consider the treap obtained right after insertion of node x. Then the number of rotations performed during insertion of x is L(x)+R(x) x x

COUNTING ROTATIONS Let Di be the event that i is in the left subtree (red) m CLAIM:

Informal proof or Game of Darts 1 i m n Don’t care Don’t care Remaining darts Second dart First dart

COUNTING ROTATIONS Let D*i be the event that i is in the right subtree (blue) m CLAIM:

COUNTING ROTATIONS Lemma. Consider the treap obtained right after insertion of node x. Then the number of rotations performed during insertion of x is L(x)+R(x)