Data Structures and Analysis (COMP 410)

Slides:



Advertisements
Similar presentations
COSC 2007 Data Structures II Chapter 12 Advanced Implementation of Tables II.
Advertisements

AVL Trees CS II – Fall /8/2010. Announcements HW#2 is posted – Uses AVL Trees, so you have to implement an AVL Tree class. Most of the code is provided.
EECS 311: Chapter 4 Notes Chris Riesbeck EECS Northwestern.
Splay Trees CSIT 402 Data Structures II. Motivation Problems with other balanced trees – AVL: extra storage/complexity for height fields Periulous delete.
CSE 326: Data Structures Splay Trees Ben Lerner Summer 2007.
CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University1 Splay trees CS 202 – Fundamental Structures of Computer Science II.
Dynamic Set AVL, RB Trees G.Kamberova, Algorithms Dynamic Set ADT Balanced Trees Gerda Kamberova Department of Computer Science Hofstra University.
CSE 326: Data Structures Lecture #13 Extendible Hashing and Splay Trees Alon Halevy Spring Quarter 2001.
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.
CSC 2300 Data Structures & Algorithms February 16, 2007 Chapter 4. Trees.
Splay Trees Splay trees are binary search trees (BSTs) that:
David Kaplan Dept of Computer Science & Engineering Autumn 2001
Splay Trees and B-Trees
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,
David Stotts Computer Science Department UNC Chapel Hill.
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.
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.
CSC 213 – Large Scale Programming Lecture 18: Zen & the Art of O (log n ) Search.
David Stotts Computer Science Department UNC Chapel Hill.
Tree Rotations & Splay Trees. BST Structure BST's only perform well when balanced But common cases lead to unbalanced trees.
CompSci 100E 41.1 Balanced Binary Search Trees  Pathological BST  Insert nodes from ordered list  Search: O(___) ?  The Balanced Tree  Binary Tree.
IT 60101: Lecture #121 Foundation of Computing Systems Lecture 12 Trees: Part VII.
1 Binary Search Trees  Average case and worst case Big O for –insertion –deletion –access  Balance is important. Unbalanced trees give worse than log.
SPLAY TREE The basic idea of the splay tree is that every time a node is accessed, it is pushed to the root by a series of tree rotations. This series.
COSC 2007 Data Structures II Chapter 13 Advanced Implementation of Tables II.
Data Structures and Analysis (COMP 410)
AVL Trees CSE, POSTECH.
Lecture 15 Nov 3, 2013 Height-balanced BST Recall:
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.
Introduction Applications Balance Factor Rotations Deletion Example
Balanced Trees AVL : Adelson-Velskii and Landis(1962)
Splay Trees.
SPLAY TREE Features Binary Search Tree Self adjusting balanced tree
Splay Trees Binary search trees.
Red Black Trees.
AVL Trees "The voyage of discovery is not in seeking new landscapes but in having new eyes. " - Marcel Proust.
Lecture 25 Splay Tree Chapter 10 of textbook
CSE373: Data Structures & Algorithms Lecture 7: AVL Trees
Red-Black Trees Motivations
AVL Tree A Balanced Binary Search Tree
Topics covered (since exam 1):
Data Structures and Analysis (COMP 410)
CSCI 104 Splay Trees Mark Redekopp.
Tree Rotations & Splay Trees
David Kaplan Dept of Computer Science & Engineering Autumn 2001
CSE373: Data Structures & Algorithms Lecture 5: AVL Trees
SPLAY TREES.
CSE 373: Data Structures and Algorithms
Tree Rotations and AVL Trees
CMSC 341 Splay Trees.
AVL Search Tree put(9)
Topics covered (since exam 1, excluding PQ):
Data Structures and Analysis (COMP 410)
Data Structures and Analysis (COMP 410)
CMSC 341 Splay Trees.
CS202 - Fundamental Structures of Computer Science II
AVL-Trees (Part 1).
BST Insert To insert an element, we essentially do a find( ). When we reach a NULL pointer, we create a new node there. void BST::insert(const Comp &
Richard Anderson Spring 2016
CSE 326: Data Structures Splay Trees
CMSC 341 Splay Trees.
Tree Balancing: AVL Trees
CMSC 341 Splay Trees.
CS 106B Lecture 20: Binary Search Trees Wednesday, May 17, 2017
326 Lecture 9 Henry Kautz Winter Quarter 2002
Splay Trees Binary search trees.
More on Randomized Data Structures
CS202 - Fundamental Structures of Computer Science II
Presentation transcript:

Data Structures and Analysis (COMP 410) David Stotts Computer Science Department UNC Chapel Hill

Splay Tree Another Balanced BST

SPLT: BST with Balance A Splay Tree (SPLT) is another type of balanced BST. Conceptually less complex than AVL tree No need for subtree balance information Easier programming No “balance property” guiding the balancing Rather we perform a “splaying” operation at various times to re-arrange the tree

No Balance Condition AVL tree has condition that height of two subtrees must differ by 1 or 0… when it gets to 2, there is imbalance In a splay tree the splaying operation tends to balance the tree somewhat in many cases, but sometime it stretches it a bit Over time… when we do a sequence of splays… the good will “balance out” the bad and give us overall acceptable collective behavior

Splaying The Basic Idea After we access a node (insert, search) it gets pushed upward to the root by a series of AVL rotations Has the effect of creating “locality” Once we access a node, if it is deep on a path, then subsequent accesses to that node will find it at or near the root (for a while)

Splaying 3 cases of rotation (and symmetries) Assume Node X is the one that needs to be splayed to the root (zig) Node X has a parent P, no grandparent This means X is almost the tree root… it is one level down Use normal AVL single rotation splay 15 P 10 9 7 2 20 12 15 25 X Path from X back to root is what give the pattern

Splaying 3 cases of rotation (and symmetries) Assume Node X is the one that needs to be splayed to the root (zig-zig RR) Node X is R-child of a parent P, and P is R-child of grandparent G ( symm. L-L ) 7 2 3 1 9 13 17 15 20 splay 17 G 10 Path from X back to root is what gives the pattern P X

Splaying 3 cases of rotation (and symmetries) Assume Node X is the one that needs to be splayed to the root (zig-zag RL) Node X is R-child of parent P, and P is L-child of grandparent G ( symm. L-R ) 7 2 3 1 9 10 15 12 20 17 splay 15 G Path from X back to root is what gives the pattern P X use double AVL rotation

A Reminder Single AVL rotations

Zig R Pattern Node X has a parent P, no grandparent AVL single rotation X P A B C X C B A P

Zig R Example Node X has parent P no grandparent P X 10 9 7 2 20 12 15 find ( 15 ) Means we splay 15 to root Use single AVL P X 10 9 7 2 20 12 15 25 31 15 10 7 2 9 12 20 25 31 X P C B B C A A

Zig Zig R Pattern Use the pattern X is R-child of parent P P is R-child of grandparent G X D B A P G C P G A B C X D Use the pattern

Zig-Zig L Example X is L-child of parent P P is L-child of grandparent 15 10 7 2 9 12 20 25 31 1 3 7 2 3 1 9 10 15 12 20 31 25 P P X G A C B D B C A find ( 7 ) Means we splay 7 to root Use double AVL D

Zig Zag L Pattern X is R-child of parent P P is L-child of grandparent G AVL double rotation P G A B C X D X D B A P G C

Example: do Zig-Zag L X is R-child of parent P P is L-child of grandparent 9 8 5 2 23 12 20 31 37 27 10 19 17 9 8 5 2 23 12 20 31 37 27 10 19 17 X G P G P X C A A B C D B D find ( 19 ) we splay 19 to root use double AVL

Now do a Zig R X is R-child of parent P No grandparent P X 9 8 5 2 12 Splay again Now 19 is one level below root rotate left P X 9 8 5 2 12 10 17 37 27 23 20 31 19 9 8 5 2 23 12 20 31 37 27 10 19 17 X P C C B B A A

When to Splay? insert contains findMin (Max) Insert BST style, then splay the new node to root Each insert causes new root contains If node is found, splay it to root findMin (Max) Node will be found, splay it to root

When to Splay? remove remove (alternate) Remove node BST-style Splay parent of removed node to root What do we do if we must remove the root? remove (alternate) Splay node to be removed to root Remove the root, leaving two disconnected subtrees “join” these 2 trees

ADT: Splay Tree SPLT SPLT is a BST… with… Signature new:  BST insert: BST x Elt  BST remove: BST x Elt  BST findMin: BST  Elt findMax: BST  Elt contains: BST x Elt  Boolean (searching) get: BST x Elt  BST (return a cell) val: BST  Elt (get root value) size: BST  Nat (natural number) empty: BST  Boolean splay: BST  BST (might want this instead of code dupe) splay happens on insert, access, remove

SPLT Complexity Splays might lengthen the path for some nodes (temporarily make balance worse) Cannot guarantee O(log N) bound for any single operation O(N) worst case still holds for insert, find, etc. Has amortized complexity O(log N) Any sequence of M operations takes total time O(M log N) No bad sequences like BST has

SPLT Complexity Linked: Time complexity of operations worst avg insert amort O(log n), O(log n) remove amort O(log n), O(log n) findMin amort O(log n), O(log n) findMax amort O(log n), O(log n) contains amort O(log n), O(log n) get amort O(log n), O(log n) splay amort O(log n), O(log n) empty O(1) size O(1) (keep counter) val O(1) (root access)

SPLT Implementation Linked structure Class SPLTCell { String value; SPLTCell LC; SPLTCell RC; SPLTCell parent; // others like height, etc. // as needed for assignment }

Coding it Up X is R-child of parent P P is L-child of grandparent 9 8 5 2 23 12 20 31 37 27 10 19 17 9 8 5 2 23 12 20 31 37 27 10 19 17 X G P G P X C A A B C D B D find ( 19 ) we splay 19 to root use double AVL

Coding it Up X is R-child of parent P P is L-child of grandparent Splaying 19 to root N 9 8 5 2 23 12 20 31 37 27 10 19 17 G Capture the pattern parts X is node of interest P = X.parent G = P.parent N = G.parent A = P.Lchild B = X.Lchild C = X.Rchild D = G.Rchild P X A C D B

Coding it Up Rearrange pattern parts N.Rchild = X X.Lchild = P X.Rchild = G P.Lchild = A P.Rchild = B G.Lchild = C G.Rchild = D And adjust parent pointers for nodes that move … P.parent = X etc. D.Parent = G etc. N 9 8 5 2 23 12 20 31 37 27 10 19 17 X P G C A B D

Beyond this is just templates END Beyond this is just templates

Splaying 3 cases of rotation (and symmetries) Assume Node X is the one that needs to be splayed to the root (zig) Node X has a parent, no grandparent This means X is almost the tree root… it is one level down Use normal AVL single rotation (zig-zig) Node X is L-child of a parent P, and P is L-child of grandparent G ( or R-R ) (zig-zag) Node X is R-child of parent P, and P is L- child of grandparent G ( or L-R )