Design Patterns for Self-Balancing Trees Dung “Zung” Nguyen Stephen Wong Rice University.

Slides:



Advertisements
Similar presentations
AVL-Trees (Part 2) COMP171. AVL Trees / Slide 2 A warm-up exercise … * Create a BST from a sequence, n A, B, C, D, E, F, G, H * Create a AVL tree for.
Advertisements

S. Sudarshan Based partly on material from Fawzi Emad & Chau-Wen Tseng
Tree Data Structures &Binary Search Tree 1. Trees Data Structures Tree  Nodes  Each node can have 0 or more children  A node can have at most one parent.
Advanced Database Discussion B Trees. Motivation for B-Trees So far we have assumed that we can store an entire data structure in main memory What if.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Data Abstraction and Problem Solving with JAVA Walls and Mirrors Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Data Abstraction and Problem.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
1 Prof. Dr. Th. Ottmann Theory I Algorithm Design and Analysis (12 - Text search: suffix trees)
They’re not just binary anymore!
CS 171: Introduction to Computer Science II
4.5 AVL Trees  A tree is said to be balanced if for each node, the number of nodes in the left subtree and the number of nodes in the right subtree differ.
CSE332: Data Abstractions Lecture 10: More B-Trees Tyler Robison Summer
CSE332: Data Abstractions Lecture 9: B Trees Dan Grossman Spring 2010.
Tirgul 6 B-Trees – Another kind of balanced trees Some notes regarding Home Work.
Data Structures and Algorithms1 B-Trees with Minimum=1 2-3 Trees.
Department of Computer Science University of Maryland, College Park
Trees, Binary Trees, and Binary Search Trees COMP171.
Advanced Tree Data Structures Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
1 Trees. 2 Outline –Tree Structures –Tree Node Level and Path Length –Binary Tree Definition –Binary Tree Nodes –Binary Search Trees.
Lec 15 April 9 Topics: l binary Trees l expression trees Binary Search Trees (Chapter 5 of text)
Advanced Tree Data Structures Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Tirgul 6 B-Trees – Another kind of balanced trees Problem set 1 - some solutions.
General Trees and Variants CPSC 335. General Trees and transformation to binary trees B-tree variants: B*, B+, prefix B+ 2-4, Horizontal-vertical, Red-black.
Fundamentals of Python: From First Programs Through Data Structures
Data Structures Using C++ 2E Chapter 11 Binary Trees and B-Trees.
Tirgul 6 B-Trees – Another kind of balanced trees.
CPSC 335 BTrees Dr. Marina Gavrilova Computer Science University of Calgary Canada.
IntroductionIntroduction  Definition of B-trees  Properties  Specialization  Examples  2-3 trees  Insertion of B-tree  Remove items from B-tree.
B-Tree. B-Trees a specialized multi-way tree designed especially for use on disk In a B-tree each node may contain a large number of keys. The number.
CSCE 3110 Data Structures & Algorithm Analysis Binary Search Trees Reading: Chap. 4 (4.3) Weiss.
Spring 2006 Copyright (c) All rights reserved Leonard Wesley0 B-Trees CMPE126 Data Structures.
B-trees (Balanced Trees) A B-tree is a special kind of tree, similar to a binary tree. However, It is not a binary search tree. It is not a binary tree.
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
Design Patterns for Self-Balancing Trees Dung “Zung” Nguyen Stephen Wong Rice University.
Data Structures Balanced Trees 1CSCI Outline  Balanced Search Trees 2-3 Trees Trees Red-Black Trees 2CSCI 3110.
OOP in Introductory CS Stephen Wong and “Zung” Nguyen Rice University Better students though abstraction.
2-3 Tree. Slide 2 Outline  Balanced Search Trees 2-3 Trees Trees.
Starting at Binary Trees
Binary Search Tree vs. Balanced Search Tree. Why care about advanced implementations? Same entries, different insertion sequence: 10,20,30,40,50,60,70,
 Trees Data Structures Trees Data Structures  Trees Trees  Binary Search Trees Binary Search Trees  Binary Tree Implementation Binary Tree Implementation.
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of CHAPTER 12: Multi-way Search Trees Java Software Structures: Designing.
Chapter 11 B Trees. © 2004 Pearson Addison-Wesley. All rights reserved 11 B-2 The ADT Binary Search Tree A deficiency of the ADT binary tree which is.
Tree Traversals, TreeSort 20 February Expression Tree Leaves are operands Interior nodes are operators A binary tree to represent (A - B) + C.
CSS446 Spring 2014 Nan Wang.  to study trees and binary trees  to understand how binary search trees can implement sets  to learn how red-black trees.
Find Find 13: Find Min and Find Max Find Min: Find Max:
Patterns for Decoupling Data Structures and Algorithms or How visitors can help you grow! Stephen Wong, Oberlin College Dung “Zung” Nguyen, Pepperdine.
Patterns for Decoupling Data Structures and Algorithms Dung “Zung” Nguyen Pepperdine University / University of Houston Stephen Wong Oberlin College
Data Structures Using C++ 2E Chapter 11 Binary Trees.
Question 4 Tutorial 8. Part A Insert 20, 10, 15, 5,7, 30, 25, 18, 37, 12 and 40 in sequence into an empty binary tree
ITEC 2620M Introduction to Data Structures Instructor: Prof. Z. Yang Course Website: ec2620m.htm Office: TEL 3049.
SUYASH BHARDWAJ FACULTY OF ENGINEERING AND TECHNOLOGY GURUKUL KANGRI VISHWAVIDYALAYA, HARIDWAR.
Data Structure and Algorithms
Trees.
Design Patterns for Self-Balancing Trees
Design Patterns for Self-Balancing Trees
COP3530- Data Structures B Trees
Lecture 26 Multiway Search Trees Chapter 11 of textbook
Design Patterns for Self-Balancing 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.
B-Trees This presentation shows you the potential problem of unbalanced tree and show one way to fix it This lecture introduces heaps, which are used.
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.
Binary Search Trees.
B+Tree Example n=3 Root
Design Patterns for Self-Balancing Trees
Binary Trees, Binary Search Trees
Design Patterns for Self-Balancing Trees
Trees.
B-Trees This presentation shows you the potential problem of unbalanced tree and show one way to fix it This lecture introduces heaps, which are used.
Binary Trees, Binary Search Trees
Data Structures Using C++ 2E
Presentation transcript:

Design Patterns for Self-Balancing Trees Dung “Zung” Nguyen Stephen Wong Rice University

Resources ( NTree/presentation/NTree.ppt)

Motivations  Classic self-balancing tree structure –Generalizable to B-trees. –Difficult and complex. Who shows real code?  What’s the proper abstraction? –Should be able to extend prior work on decoupling algorithms from data structures. Consider Trees….

A Tree is… AABABC Empty Non-Empty, in 3 possible states: 2-State: 1 data element + 2 sub-trees. 4-State: 3 data elements + 4 sub-trees. 3-State: 2 data elements + 3 sub-trees. or…

Variant vs. Invariant Operations  Self-balancing insertion is not an intrinsic (invariant) operation of a tree.  What are the invariant operations? –Gettors & Constructors. –Constructive and Destructive operations:  Constructive: Splice a tree into another.  Destructive: Split a tree into a 2-state.

B Splittin’ and Splicin’ AC ABC BABC A C B Split Up:Splice:

Structural Operations t t1.splitUpAt(1) t2 βγα t1.spliceAt(1, t2) βγα t1.splitDownAt(1)

Con/De-struction t1 10 t2 t1.splitUpAt(0) 10 t1.splitDownAt(0) t2.spliceAt(0, t1) 10

Visitor Design Pattern Invariant: Host i calls case i of the visitor. AHost execute(v) AVisitor +case1() +case2() +case3() Host1Host2Host3 VisitorAVisitorB Fixed # of methods  fixed # of hosts

Generalized Visitors Invariant: Host i calls caseAt(i) of the visitor. AHost execute(v) AVisitor +caseAt(int i) Host1Host2Host3 VisitorAVisitorB

TreeN and Algorithms

toString() Algorithm public class ToStringAlgo implements ITreeNAlgo { // Constructors omitted public Object caseAt(int idx, TreeN host, Object key) { switch(idx) { case 0: { return "[ ]"; } default: { String sData= "", sTrees=""; for(int i = 0;i<idx;i++) { sData += host.getDat(i)+" "; sTrees += host.getChild(i).execute(toStringHelp,"| ")+"\n"; } sTrees += host.getChild(idx).execute(toStringHelp," ").toString(); return sData +"\n"+sTrees; } } } ITreeNAlgo toStringHelp = …see next slide…. } Empty Tree Non-Empty Tree “Prefix” data

ToString() Helper private final static ITreeNAlgo toStringHelp = new ITreeNAlgo() { public Object caseAt(int idx, TreeN host, Object prefix) { switch(idx) { case 0: { return "|_[ ]"; } default: { String sData= "", sTrees=""; for(int i = 0;i<idx;i++) { sData += host.getDat(i)+" "; sTrees += prefix + (String) host.getChild(i).execute(this, prefix+"| ")+"\n"; } sTrees += prefix + host.getChild(idx).execute(this, prefix+" " ).toString(); return "|_ "+sData +"\n"+sTrees; } } } }; Empty Tree Non-Empty Tree “Prefix” data

Vertical Data Transport Split upSplice Collapse/Splice Split-down No net height change except at root and leaves!

Command Design Pattern ICommand +Object apply(Object inp) Command1Command2Command3 Performs a task. No specified semantic! Well-defined, but unrelated semantics. Invoker invokes

Insertion Heuristics  Insertion must take place at the leaf.  Tree must grow only at the root. Must transport data from the leaves to the root without affecting the height balance.

Insertion Algorithm  Find insertion point at the leaf and splice new data in.  Use Split-and-Apply visitor to transport excess data upwards. –Visitor passed as parameter to recursive call. –Non-root: split-and-splice –Root node: split-and-no-op will cause entire tree to grow in height. –Abstract the splice/no-op as a command passed to the visitor!

Deletion Heuristics  Deletion only well-defined at leaf.  Data might exist anywhere in the tree.  Tree can only shorten at root. Must transport data from the root to the leaves and from the leaves to the root without affecting the height balance.  Push “candidate” data down from the root to the leaves.  Bubble “excess” data back to the root.

Deletion Algorithm  Identify candidate data –split down at candidate and collapse with children. –If root is a 2-node, then tree will shorten.  Data to delete will appear as 2-node below leaves.  Use Split-and-Apply to transport excess data upwards.

Conclusions  Proper abstraction leads to –Decoupling –Simplicity –Flexibility & extensibility  Generalized Visitors open up new possibilities.  Self-balancing trees teach –Abstract decomposition –Design patterns –Component-frameworks –Lambda calculus –Proof-of-correctness & complexity analysis