Treaps.  Good (logarithmic) performance with random data  Linear performance if the data is sorted ◦ Solutions – Splay Trees Amortized O( lg n) performance.

Slides:



Advertisements
Similar presentations
Binary Search Tree Smt Genap
Advertisements

DATA STRUCTURES AND ALGORITHMS Lecture Notes 9 Prepared by İnanç TAHRALI.
COL 106 Shweta Agrawal and Amit Kumar
CMSC 341 Binary Heaps Priority Queues. 8/3/2007 UMBC CSMC 341 PQueue 2 Priority Queues Priority: some property of an object that allows it to be prioritized.
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.
Trees Types and Operations
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture20.
IKI 10100I: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100I: Data.
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.
10/22/2002CSE Red Black Trees CSE Algorithms Red-Black Trees Augmenting Search Trees Interval Trees.
Lec 15 April 9 Topics: l binary Trees l expression trees Binary Search Trees (Chapter 5 of text)
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.
Review: Search Linear Search Binary Search Search demos: – ndan/dsal/appldsal.htmlhttp://
1 BST Trees A binary search tree is a binary tree in which every node satisfies the following: the key of every node in the left subtree is.
Chapter 08 Binary Trees and Binary Search Trees © John Urrutia 2013, All Rights Reserved.
1 CSC 427: Data Structures and Algorithm Analysis Fall 2010 transform & conquer  transform-and-conquer approach  balanced search trees o AVL, 2-3 trees,
CS 146: Data Structures and Algorithms June 18 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
Binary Search Trees. BST Properties Have all properties of binary tree Items in left subtree are smaller than items in any node Items in right subtree.
CSCE 3110 Data Structures & Algorithm Analysis Binary Search Trees Reading: Chap. 4 (4.3) Weiss.
Chapter 19 - basic definitions - order statistics ( findkth( ) ) - balanced binary search trees - Java implementations Binary Search Trees 1CSCI 3333 Data.
Lecture Objectives  To learn how to use a tree to represent a hierarchical organization of information  To learn how to use recursion to process trees.
Tree. Basic characteristic Top node = root Left and right subtree Node 1 is a parent of node 2,5,6. –Node 2 is a parent of node.
Building Java Programs Binary Search Trees; TreeSet.
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 Neil Ghani University of Strathclyde. General Trees Recall a tree is * A leaf storing an integer * A node storing a left subtree, an integer.
BINARY SEARCH TREE. Binary Trees A binary tree is a tree in which no node can have more than two children. In this case we can keep direct links to the.
CMSC 341 Binary Heaps Priority Queues. 2 Priority: some property of an object that allows it to be prioritized WRT other objects (of the same type) Priority.
Lec 15 Oct 18 Binary Search Trees (Chapter 5 of text)
DATA STRUCTURES AND ALGORITHMS Lecture Notes 8 Prepared by İnanç TAHRALI.
1 Joe Meehean.  We wanted a data structure that gave us... the smallest item then the next smallest then the next and so on…  This ADT is called a priority.
Data Structure II So Pak Yeung Outline Review  Array  Sorted Array  Linked List Binary Search Tree Heap Hash Table.
Chapter 19: Binary Search Trees or How I Learned to Love AVL Trees and Balance The Tree Group 6: Tim Munn.
David Stotts Computer Science Department UNC Chapel Hill.
Binary Search Trees (BSTs) 18 February Binary Search Tree (BST) An important special kind of binary tree is the BST Each node stores some information.
Binary Tree. Some Terminologies Short review on binary tree Tree traversals Binary Search Tree (BST)‏ Questions.
1/14/20161 BST Operations Data Structures Ananda Gunawardena.
CMSC 341 Binary Heaps Priority Queues. 2 Priority: some property of an object that allows it to be prioritized WRT other objects (of the same type) Priority.
Intro. to Data Structures Chapter 6 Priority Queue (Heap) Veera Muangsin, Dept. of Computer Engineering, Chulalongkorn University 1 Priority Queue.
FALL 2005CENG 213 Data Structures1 Priority Queues (Heaps) Reference: Chapter 7.
Lecture 10COMPSCI.220.FS.T Binary Search Tree BST converts a static binary search into a dynamic binary search allowing to efficiently insert and.
CMSC 341 Binary Search Trees. 8/3/2007 UMBC CMSC 341 BST 2 Binary Search Tree A Binary Search Tree is a Binary Tree in which, at every node v, the values.
Properties: -The value in each node is greater than all values in the node’s subtrees -Complete tree! (fills up from left to right) Max Heap.
(c) University of Washington20c-1 CSC 143 Binary Search Trees.
BSTs, AVL Trees and Heaps Ezgi Shenqi Bran. What to know about Trees? Height of a tree Length of the longest path from root to a leaf Height of an empty.
Lecture 15 Nov 3, 2013 Height-balanced BST Recall:
AA Trees.
Lec 13 Oct 17, 2011 AVL tree – height-balanced tree Other options:
CSCE 3110 Data Structures & Algorithm Analysis
CSCE 3110 Data Structures & Algorithm Analysis
CISC220 Fall 2009 James Atlas Lecture 13: Binary Trees.
Binary Search Tree (BST)
Binary Heaps Priority Queues
CMSC 341 Lecture 13 Leftist Heaps
CMSC 341 Lecture 10 Binary Search Trees
Stacks Linked Lists Queues Heaps Hashes
AVL Trees CENG 213 Data Structures.
Lec 12 March 9, 11 Mid-term # 1 (March 21?)
CMSC 341 Binary Search Trees.
CMSC 341 Splay Trees.
CMSC 341 Binary Search Trees.
CSC 143 Binary Search Trees.
Heaps By JJ Shepherd.
Tree.
CMSC 341 Splay Trees.
CMSC 341 Binary Search Trees 8/3/2007 CMSC 341 BST.
CMSC 341 Splay Trees.
Presentation transcript:

Treaps

 Good (logarithmic) performance with random data  Linear performance if the data is sorted ◦ Solutions – Splay Trees Amortized O( lg n) performance if we do many operations Still get the occasional linear operation – Red-Black Trees Guaranteed O( lg n ) performance for all operations complicated to code 11/5/2011CMSC Treaps2

 First introduced in 1989 by Aragon and Seidel ◦  Randomized Binary Search Tree  A combination of a binary search tree and a min binary heap (a “tree - heap”)  Each node contains ◦ Data / Key (comparable) ◦ Priority (random integer) ◦ Left, right child reference  Nodes are in BST order by data/key and in min binary heap order by priority 11/5/2011CMSC Treaps3

 High probability of O( lg N ) performance for any set of input ◦ Priorities are chosen randomly when node is inserted ◦ High probability of avoiding O( N ) operation that can occur in splay trees  Code is less complex than Red-Black trees ◦ Perhaps the simplest of BSTs that try to improve performance ◦ Priorities don’t have to be updated to keep tree balanced (unlike colors in RB Tree) ◦ Non-recursive implementation possible 11/5/2011CMSC Treaps4

11/5/2011CMSC Treaps5

 Based on original algorithm  private TreapNode class  Use RNG to assign priorities ◦ static in TreapNode class  The same RNG used by all TreapNodes  nullNode ◦ What would ordinarily be null references now refer to nullNode ◦ Every node has two children ◦ nullNode’s children refer back to nullNode ◦ Priority is Integer.MAX_VALUE 11/5/2011CMSC Treaps6

11/5/2011CMSC Treaps7 import java.util.Random; public class Treap > { private static class TreapNode { AnyTypeelement; // the data in the node TreapNode right; // right child reference TreapNode left; // left child reference int priority; // for balancing private static Random randomObj = new Random( ); // constructors TreapNode( AnyType x ) { this( x, null, null ); // used only by Treap constructor } TreapNode( AnyType x, TreapNode lt, TreapNode rt) { element = x; left = lt; right = rt; priority = randomObj.nextInt( ); } } ;

// Treap class data members private TreapNode root;// usual root private TreapNode nullNode; // replaces null ref // Default constructor Treap( ) { nullNode = new TreapNode ( null ); nullNode.left = nullNode.right = nullNode; nullNode.priority = Integer.MAX_VALUE; root = nullNode; } 11/5/2011CMSC Treaps8

11/5/2011CMSC Treaps9 root

public void insert( AnyType x ) { root = insert( x, root ); } // recurse down the treap to find where to insert x according to BST order // rotate with parent on the way back if necessary according to min heap order private TreapNode insert( AnyType x, TreapNode t ) { if ( t == nullNode ) return new TreapNode ( x, nullNode, nullNode ); int compare = x.compareTo( t.element ); if ( compare < 0 ) { t.left = insert( x, t.left ); // proceed down the treap if ( t.left.priority < t.priority ) // rotate coming back up the treap t = rotateWithLeftChild( t ); } else if ( compare > 0 ) { t.right = insert(x, t.right ); if ( t.right.priority < t.priority ) t = rotateWithRightChild ( t ); } // else duplicate, do nothing return t; } 11/5/2011CMSC Treaps10

11/5/2011CMSC Treaps11 BST Insert

 Find X via recursive BST search  When X is found, rotate with child that has the smaller priority  If X is a leaf, just delete it  If X is not a leaf, recursively remove X from its new subtree 11/5/2011CMSC Treaps12

11/5/2011CMSC Treaps13

11/5/2011CMSC Treaps14

11/5/2011CMSC Treaps15

11/5/2011CMSC Treaps16

public void remove( AnyType x ) { root = remove( x, root ); } private TreapNode remove( AnyType x, TreapNode t) { if( t != nullNode ) { int compare = x.compareTo( t.element ); if ( compare < 0 ) t.left = remove( x, t.left ); else if ( compare > 0 ) t.right = remove( x, t.right ); // found x, swap x with child with smaller priority until x is a leaf else { if ( t.left.priority < t.right.priority ) t = rotateWithLeftChild( t ); else t = rotateWithRightChild( t ); if( t != nullNode ) // not at the bottom, keep going t = remove( x, t ); else // at the bottom; restore nullNode’s left child t.left = nullNode; } return t; } 11/5/2011CMSC Treaps17

public boolean isEmpty( ) { return root == nullNode; } public void makeEmpty( ) { root = nullNode; } public AnyType findMin( ) { if (isEmpty( ) ) throw new UnderFlowException( ); TreapNode ptr = root; while ( ptr.left != nullNode ) ptr = ptr.left; return ptr.element; } public AnyType findMax( ) { if (isEmpty( ) ) throw new UnderFlowException( ); TreapNode ptr = root; while ( ptr.right != nullNode ) ptr = ptr.right; return ptr.element; } 11/5/2011CMSC Treaps18

 Determined by the height  In theory, the random priority will result in a relatively balanced tree giving O( lg N ) performance  To test the theory we inserted N integers in sorted order 10 times 11/5/2011CMSC Treaps19 N Consecutive Intslg( N )Height

 Given two treaps, T1 and T2, such that all keys in T1 are “less than” all keys in T2  To merge the treaps together ◦ Create a “dummy” root node ◦ Attach T1 as the root’s left subtree ◦ Attach T2 as the root’s right subtree ◦ Remove the root  Used for set union ◦ Each set in its own treap 11/5/2011CMSC Treaps20

11/5/2011CMSC Treaps21

11/5/2011CMSC Treaps22

 To divide a set of objects into subsets based on a key, K ◦ Insert all of the objects into a treap ◦ Insert K with priority of Integer.MIN_VALUE  Because K has the smallest possible priority value, the heap ordering will force K to become the root of the treap  Because of the BST ordering, the left subtree of K will contain all objects “less than” K and the right subtree will contain all objects “greater than” K 11/5/2011CMSC Treaps23

 Insert the characters K, F, P, M, N, L, G into an empty treap with priorities 17, 22, 29, 10, 15, 26, 13 respectively. 11/5/2011CMSC Treaps24

11/5/2011CMSC Treaps25