Splay Trees Binary search trees.

Slides:



Advertisements
Similar presentations
CS16: Introduction to Data Structures & Algorithms
Advertisements

© 2004 Goodrich, Tamassia Heaps © 2004 Goodrich, Tamassia Heaps2 Recall Priority Queue ADT (§ 7.1.3) A priority queue stores a collection of.
10 -1 Chapter 10 Amortized Analysis A sequence of operations: OP 1, OP 2, … OP m OP i : several pops (from the stack) and one push (into the stack)
AVL Trees CSE 373 Data Structures Lecture 8. 12/26/03AVL Trees - Lecture 82 Readings Reading ›Section 4.4,
CSE Lecture 17 – Balanced trees
§2 Binary Trees Note: In a tree, the order of children does not matter. But in a binary tree, left child and right child are different. A B A B andare.
COL 106 Shweta Agrawal, Amit Kumar
AVL Trees binary tree for every node x, define its balance factor
Part II. Delete an Node from an AVL Tree Consider to delete
Splay Tree Algorithm Mingda Zhao CSC 252 Algorithms Smith College Fall, 2000.
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.
Heaps1 Part-D2 Heaps Heaps2 Recall Priority Queue ADT (§ 7.1.3) A priority queue stores a collection of entries Each entry is a pair (key, value)
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.
1 AVL Trees (10.2) CSE 2011 Winter April 2015.
Chapter 4: Trees Part II - AVL Tree
Solution of Assignment 3 and Midterm CSC2100B. AVL Tree A binary search tree is a binary tree in which every node has larger key than the nodes in its.
AVL Tree Smt Genap Outline AVL Tree ◦ Definition ◦ Properties ◦ Operations Smt Genap
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.
Splay Trees CSIT 402 Data Structures II. Motivation Problems with other balanced trees – AVL: extra storage/complexity for height fields Periulous delete.
© 2004 Goodrich, Tamassia Binary Search Trees   
CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University1 Splay trees CS 202 – Fundamental Structures of Computer Science II.
Digital Search Trees & Binary Tries Analog of radix sort to searching. Keys are binary bit strings.  Fixed length – 0110, 0010, 1010,  Variable.
Dynamic Dictionaries Primary Operations:  Get(key) => search  Insert(key, element) => insert  Delete(key) => delete Additional operations:  Ascend()
DEAPS By: Michael Gresenz Austin Forrest. Deaps A deap is a double-ended heap that supports the double-ended priority operations of insert, delete-min,
Red Black Trees Colored Nodes Definition Binary search tree.
Chapter 4: Trees AVL Trees Lydia Sinapova, Simpson College Mark Allen Weiss: Data Structures and Algorithm Analysis in Java.
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 (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)
Balanced Trees. Binary Search tree with a balance condition Why? For every node in the tree, the height of its left and right subtrees must differ by.
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.
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).
E.G.M. PetrakisTrees in Main Memory1 Balanced BST  Balanced BSTs guarantee O(logN) performance at all times  the height or left and right sub-trees are.
Splay Trees Splay trees are binary search trees (BSTs) that:
0 Course Outline n Introduction and Algorithm Analysis (Ch. 2) n Hash Tables: dictionary data structure (Ch. 5) n Heaps: priority queue data structures.
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,
CSIT 402 Data Structures II
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:
2-3 Tree. Slide 2 Outline  Balanced Search Trees 2-3 Trees Trees.
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.
Balanced Search Trees Problem: Efficiency of BST is related to tree’s height.  search, insert and remove follow a path from root to desired location 
CSE221/ICT221 Analysis and Design of Algorithms CSE221/ICT221 Analysis and Design of Algorithms Analysis of Algorithm using Tree Data Structure Asst.Prof.
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.
Oct 26, 2001CSE 373, Autumn A Forest of Trees Binary search trees: simple. –good on average: O(log n) –bad in the worst case: O(n) AVL trees: more.
Data Structures Chapter 10: Efficient Binary Search Trees 10-1.
1 Chapter 7 Objectives Upon completion you will be able to: Create and implement binary search trees Understand the operation of the binary search tree.
AVL Trees and Heaps. AVL Trees So far balancing the tree was done globally Basically every node was involved in the balance operation Tree balancing can.
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.
Priority Search Trees Keys are pairs (x,y). Basic (search, insert, delete) and rectangle operations. Two varieties.  Based on a balanced binary search.
CSE 3358 NOTE SET 13 Data Structures and Algorithms.
Heaps © 2010 Goodrich, Tamassia. Heaps2 Priority Queue ADT  A priority queue (PQ) stores a collection of entries  Typically, an entry is a.
1 Binary Search Trees  Average case and worst case Big O for –insertion –deletion –access  Balance is important. Unbalanced trees give worse than log.
Leftist Trees Linked binary tree.
Red Black Trees Colored Nodes Definition Binary search tree.
Red Black Trees Colored Nodes Definition Binary search tree.
Search 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.
Introduction Applications Balance Factor Rotations Deletion Example
Chapter 10.1 Binary Search Trees
Heaps © 2010 Goodrich, Tamassia Heaps Heaps
Splay Trees Binary search trees.
Interval Heaps Complete binary tree.
Advanced Associative Structures
AVL Search Tree put(9)
Dynamic Dictionaries Primary Operations: Additional operations:
(2,4) Trees 2/15/2019 (2,4) Trees (2,4) Trees.
Red Black Trees Colored Nodes Definition Binary search tree.
Splay Trees Binary search trees.
Presentation transcript:

Splay Trees Binary search trees. Search, insert, delete, and split have amortized complexity O(log n) & actual complexity O(n). Actual and amortized complexity of join is O(1). Priority queue and double-ended priority queue versions outperform heaps, deaps, etc. over a sequence of operations. Two varieties. Bottom up. Top down.

Bottom-Up Splay Trees Search, insert, delete, and join are done as in an unbalanced binary search tree. Search, insert, and delete are followed by a splay operation that begins at a splay node. When the splay operation completes, the splay node has become the tree root. Join requires no splay (or, a null splay is done). For the split operation, the splay is done in the middle (rather than end) of the operation.

Splay Node – search(k) 20 10 6 2 8 15 40 30 25 If there is a pair whose key is k, the node containing this pair is the splay node. Otherwise, the parent of the external node where the search terminates is the splay node.

Splay Node – insert(newPair) 20 10 6 2 8 15 40 30 25 If there is already a pair whose key is newPair.key, the node containing this pair is the splay node. Otherwise, the newly inserted node is the splay node.

Splay Node – delete(k) 20 10 6 2 8 15 40 30 25 If there is a pair whose key is k, the parent of the node that is physically deleted from the tree is the splay node. Otherwise, the parent of the external node where the search terminates is the splay node.

Splay Node – split(k) Use the unbalanced binary search tree insert algorithm to insert a new pair whose key is k. The splay node is as for the splay tree insert algorithm. Following the splay, the left subtree of the root is S, and the right subtree is B. S m B m is set to null if it is the newly inserted pair.

Splay Let q be the splay node. q is moved up the tree using a series of splay steps. In a splay step, the node q moves up the tree by 0, 1, or 2 levels. Every splay step, except possibly the last one, moves q two levels up.

Splay Step If q = null or q is the root, do nothing (splay is over). If q is at level 2, do a one-level move and terminate the splay operation. b c a q p p q a b c q right child of p is symmetric.

Splay Step If q is at a level > 2, do a two-level move and continue the splay operation. c d b p gp q a p q a b c gp d q right child of right child of gp is symmetric.

2-Level Move (case 2) q left child of right child of gp is symmetric. b gp p q d p q b c a gp d q left child of right child of gp is symmetric.

Per Operation Actual Complexity Start with an empty splay tree and insert pairs with keys 1, 2, 3, …, in this order. 1 1 2 1 2

Per Operation Actual Complexity Start with an empty splay tree and insert pairs with keys 1, 2, 3, …, in this order. 1 2 3 1 2 3 4 1 2 3

Per Operation Actual Complexity Worst-case height = n. Actual complexity of search, insert, delete, and split is O(n).

Top-Down Splay Trees On the way down the tree, split the tree into the binary search trees S (small elements) and B (big elements). Similar to split operation in an unbalanced binary search tree. However, a rotation is done whenever an LL or RR move is made. Move down 2 levels at a time, except (possibly) in the end when a one level move is made. When the splay node is reached, S, B, and the subtree rooted at the splay node are combined into a single binary search tree.

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

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

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

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

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

Split A Binary Search Tree D d m f g

Split A Binary Search Tree g c D f d m

Two-Level Moves Let m be the splay node. RL move from A to C. g E Let m be the splay node. RL move from A to C. RR move from C to E. L move from E to m.

RL Move B e d b A a c C D f m g E S Some go in S, some in B

Same outcome as in split. RL Move S B A B d c C D f m g E e a b Same outcome as in split.

RR Move S B A B d c C D f m g E e a b All go in S

Outcome is different from split. RR Move S B A B a D b C E c d e m Rotation performed. f g Outcome is different from split.

L Move S B A B a D b C E c d e m f g

L Move S B A B E a D b e C c d m f g

Wrap Up S B m f g A B E a D b e C c d

Wrap Up S B m A B E a D b g e C f c d

Wrap Up m A B E a D b g e C f c d

Bottom Up vs Top Down Top down splay trees are faster than bottom up splay trees.