/633 Introduction to Algorithms Lecturer: Michael Dinitz Topic: Balanced Search Trees Date: 9/20/18
|
|
- Constance Hunt
- 5 years ago
- Views:
Transcription
1 /633 Introduction to Algorithms Lecturer: Michael Dinitz Topic: Balanced Search Trees Date: 9/20/ Introduction For next few lectures we will be looking at several important data structures. A data structure is a method for storing data so that operations you care about can be performed quickly. Data structures are typically used as part of some larger algorithm or system, and good data structures are often crucial when especially fast performance is needed. Today we will be focusing in particular on what are called dictionary data structures, that support insert and lookup operations (and usually delete as well). Specifically, Definition A dictionary data structure is a data structure supporting following operations: insert(key,object): insert (key, object) pair. For instance, this could be a word and its definition, a name and phone number, etc. The key is what will be used to access object. lookup(key): return associated object delete(key): remove key and its object from data structure. We may or may not care about this operation. One option is we could use a sorted array. Then, a lookup takes O(log n) time using binary search. However, an insert may take Ω(n) time in worst case because we have to shift everything to right in order to make room for new key. Anor option might be an unsorted list. In that case, inserting can be done in O(1) time, but a lookup may take Ω(n) time. Our goal is going to be to construct data structures which let us perform all operations in O(log n) time. A binary search tree is a binary tree in which each node stores a (key, object) pair such that all descendants to left have smaller keys and all descendants to right have larger keys (let s not worry about case of multiple equal keys). To do a lookup operation you simply walk down from root, going left or right depending on wher query is smaller or larger than key in current node, until you get to correct key or walk off tree. We will also talk about non-binary search trees that potentially have more than one key in each node, and nodes may have more than two children. For rest of this discussion, we will ignore object part of things. We will just worry about keys since that is all that matters as far as understanding data structures is concerned. 6.2 Simple Binary Search Trees The simplest way to maintain a binary search tree is to implement insert operations as follows: 1
2 insert(x): If tree is empty n put x in root. Orwise, compare it to root: if x is smaller n recursively insert on left; orwise recursively insert on right. Equivalently: walk down tree as if doing a lookup, and n insert x into a new leaf at end. Example: build a tree by inserting sequence: H O P K I N S. On positive side, this is very easy to implement (although deletes are a bit of a pain think of how you would implement m). However, worst-case performance is very bad. If tree has depth d, n an insert or a lookup could take Ω(d) time. So if tree is very unbalanced, operations could take Ω(n) time each! This is actually similar to what happens with quicksort if input is already sorted, n each recursive call only decreases input size (quicksort) or depth of tree (BSTs) by one. Main idea: The problem with basic binary search tree was that we were not maintaining balance. On or hand, if we try to maintain a perfectly balanced tree, we will spend too much time rearranging things. So, we want to be balanced but also give ourselves some slack. It s a bit like how in median-finding algorithm, we gave ourselves slack by allowing pivot to be near middle. For B-trees, we will make tree perfectly balanced, but give ourselves slack by allowing some nodes to have more children than ors. 6.3 B-trees and trees A B-tree is a search tree where for some pre-specified t 2 (think of t = 2 or t = 3) following three properties hold. 1. Each node has between t 1 and 2t 1 keys in it (except root has between 1 and 2t 1 keys). Keys in a node are stored in a sorted array. 2. Each non-leaf has degree (number of children) equal to number of keys in it plus 1. So, node degrees are in range [t, 2t] except root has degree in range [2, 2t]. If v is a node with keys [a 1, a 2,..., a k ] and children are [v 1, v 2,..., v k+1 ], n tree rooted at v i contains only keys that are at least a i 1 and at most a i (except edge cases: tree rooted at v 1 has keys less than a 1, and tree rooted at v k+1 has keys at least a k ). E.g., if keys are [a 1, a 2,..., a 10 ] n re is one child for keys less than a 1, one child for keys between a 1 and a 2, and so on, until rightmost child has keys greater than a All leaves are at same depth. The idea is that by using flexibility in sizes and degrees of nodes, we will be able to keep trees perfectly balanced (in sense of all leaves being at same level) while still being able to do inserts cheaply. Note that case of t = 2 is called a tree since degrees are 2, 3, or 4. As an example, here is a tree for t = 3 (so, non-leaves have between 3 and 6 children (though root can have fewer) and maximum size of any node is 5). 2
3 The idea is that by using flexibility in sizes and degrees of nodes, we will be able to keep trees perfectly balanced (in sense of all leaves being at same level) while still being able to do inserts cheaply. Note that case of t = 2 is called a tree since degrees are 2, 3, or 4. Example: here is a tree for t = 3 (so, non-leaves have between 3 and 6 children though root can have fewer and maximum size of any node is 5). H M R A B C D K L N O T Y Z Now, rules for lookup and insert turn out to be pretty easy: Now we have to define how to do inserts and lookups. Lookup: Just do binary search in array at root. This will eir return item you are Lookup: looking Just for do(in binary whichsearch case you in arearray done) at or a pointer root. This to will appropriate eir return child, in item which youcase are looking youfor recurse (in which on that casechild. you are done) or a pointer to appropriate child, in which case you recurse on that child. Insert: To insert, walk down tree as if you are doing a lookup, but if you ever encounter a full Insert: node To (a insert, node with walk down maximum tree 2t as 1 if keys you are in it), doing perform a lookup, a split but operation if you ever on it encounter (described a full node below) (a node before with continuing. maximum 2t 1 keys in it), perform a split operation on it immediately (described below) before continuing. When you re at a leaf node, insert new element into 9.4. Finally, B-TREES insert AND new TREES key into leaf reached. 46 array at that node. Split: To split a node, pull median of its keys up to its parent and n split remaining 2t 22t one keys 2with keys into twoelements nodes nodes of greater tof t1 than keys 1 keys each each median). (one (onewith Then elements connect se less than nodes to median ir parent and one with in appropriate elements greater way (one than as median). child tothen left connect of se median nodes and to one irasparent child in to appropriate its right). way (one If asnode child beingtosplit left is of root, median n create and one a fresh as new child root to its node right). to put If node being median split in. is root, n create a fresh new root node to put median in. Let s consider example above. If we insert an E n that will go into leftmost leaf, making Let s consider it full. If weexample now insert above. an F, If we n insert an process E n of walking that willdown go into treeleftmost we will split leaf, making full node, it full. bringing If we now insert C up an to F, n root. inso, after process inserting of walking F down we willtree nowwe have: will split full node, bringing C up to root. So, after inserting F we will now have: C H M R A B D E F K L N O T Y Z Question: We know that performing a split maintains requirement of at least t 1 keys per Sanity non-root check: node We (because know that we split performing at a median) split maintains but is it possible requirement for a split of at to least make t 1 keys parent per non-root over-full? node (because we split at median) but is it possible for a split to make parent over-full? Answer: No, since if if parent were was full already we would full n have wealready would split have split on it onway down. way down. Let s now continue above example, inserting S, U, V : C H M R U A B D E F K L N O S T V Y Z Now, suppose we insert P. Doing this will bring 3 M up to a new root, and n we finally insert P in appropriate leaf node: M
4 Question: We know that performing a split maintains requirement of at least t 1 keys per A B D E F K L N O T Y Z non-root node (because we split at median) but is it possible for a split to make parent Question: over-full? We know that performing a split maintains requirement of at least t 1 keys per non-root Answer: node No, (because since if weparent split at was full median) we would buthave is italready possiblesplit for it a split on toway make down. parent over-full? Let s now continue above example, inserting S, U, V : Answer: No, since if parent was full we would have already split it on way down. Let s now continue above example, inserting C H M S, R U U, V : C H M R U A B D E F K L N O S T V Y Z Now, suppose we insert P. Doing this will bring M up to a new root, and n we finally insert A B D E F K L N O S T V Y Z P in appropriate leaf node: Now, suppose we insert P. Doing this will bring M up to a new root, and n we finally insert Now, P insuppose appropriate we insert leaf P. node: Doing this will bring M up to a new root, and n we finally insert P in appropriate leaf node: M M C H R U C H R U A B D E F K L N O P S T V Y Z Question: is tree always height-balanced (all leaves at same depth)? A B D E F K L N O P S T V Y Z Answer: yes, since we only grow tree up. Question: So, we have is maintained tree always our desired height-balanced properties. (all What leaves about at running same depth)? time? To perform a lookup, Answer: Let s perform prove yes, (somewhat binary sincesearch weinformally) only each grownode tree correctness we pass up. through, of our so insert procedure. total time for In aparticular, lookup is let s O(depth prove that log t). allwhat nodesis store depth right of number tree? ofsince keys (between at each level t 1we and have 2t a 1branching for a non-root, So, we have maintained our desired properties. What about running time? To perform factorand of a at at lookup, least mostt 2t we (except 1 for perform possibly root), binary at that search root), all elements in each node depth in we pass is O(log subtree through, t n). between Combining two adjacent so total se time for toger, keys are a lookup we indeed is see O(depth that least log t smaller t). cancels and What out is in at most depth expression larger, of tree? forand lookup that Since time: all leaves are same depth. at each level we have a branching factor of at least t (except Theorempossibly at Our insert root), algorithm depthmaintains is O(log t all n). three Combining properties se of atoger, B-tree. we see that t Proof: cancels Weout will inprove expression this by induction. for lookup Note time: that if we insert into an empty B-tree we get simply one node with one element in it. All three properties of a B-tree are indeed true. Now suppose that we have a B-tree which satisfies all three properties, and insert an element x. We claim that resulting tree still satisfied all three properties. The third property is obvious since tree grows up : let h be height of tree before insert, which by induction is depth of all leaves. If our insert does not split root n distance between root and all leaves is unchanged (and so all of m are still h), while if we do split root n all leaves are at distance h + 1 from new root. For first property, note that whenever we do a split operation, parent gains one new key while each child has exactly t 1 keys in it. Since when we do an insert we split any node on path which has 2t 1 keys in it, whenever we do a split operation on a node, its parent must have strictly less than 2t 1 keys (or else parent would have already been split). Thus none of our split operations violate degree bounds. And when we do finally insert new key, node we insert into must have strictly less than 2t 1 keys before insert (or else we would split it). Hence when we do an insert we never violate any of degree upper bounds. Similarly, we never 4
5 violate a degree lower bound since we never create a node with less than t 1 keys or than root (which is allowed to have fewer keys). So it remains only to prove that for every node, tree rooted at ith child of node contains keys which are between (i 1) and (i + 1) key in node. Whenever we do a split operation we preserve this property, so it is preserved as we move down tree on our insert, and in end new key that we insert is placed in a leaf node so that this property continues to hold. Hence this property (and this all three properties) continues to hold after inserting a new key. So, we have maintained our desired properties. What about running time? Suppose that depth is d. To perform a lookup, we perform binary search in each node we pass through, so total time for a lookup is O(d log t). What is depth of tree? Since at each level we have a branching factor of at least t (except possibly at root), depth is O(log t n). Combining se toger, we see that t cancels out in expression for lookup time: Time for lookup = O(log t n log t) = O((log n)/(log t) log t) = O(log n) Inserts are similar to lookups except for two issues. First, we may need to split nodes on way down, and secondly we need to insert element into leaf. So, we have: Time for insert = lookup-time + splitting-time + time-to-insert-into-leaf. The time to insert into a leaf is O(t). The splitting time is O(t) per split, which could happen at each step on way down, for a total of O(t log n). So, if t is a constant, n we still get total time O(log n). Note that this motivates us to make t small, which is one reason that trees get ir own name. More facts about B-trees: B-trees are used a lot in databases applications because y fit in nicely with memory hierarchy when you use a large value of t. For instance, if you have 1 billion items and use t = 1, 000, n you can probably keep top two levels in fast memory and only make one disk access at bottom level. The savings in disk accesses more than makes up for extra O(t) cost for insert. From a student three years ago: I was personally asked in interviews why B-trees are well suited for extremely large data sets. If you use t = 2, you have what is known as a tree. What is special about trees is that y can be implemented efficiently as binary trees using an idea called red-black trees, which is what we ll discuss next. 6.4 Red-Black Trees B-trees are great, but it s generally easier to use binary trees. Lookups are simpler, people find m easier to understand, and for general-purpose (non-database) applications, y tend to be a bit faster and a bit more flexible. There have been a whole bunch of balanced binary search trees developed you may have seen AVL trees when you took Data Structures ( ). One of 5
6 Trees most popular and widely used are red-black trees y re default in linux kernel, are used to optimize Java HashMaps, and are generally balanced search tree of choice in practice. I don t have time to go over m in full detail, but you should read book. What I ll try to explain here is how we can view m as essentially binary versions of trees. One quick note: unlike B-trees, for red-black trees (and binary trees in general) we ll use convention that all missing pointers actually point to NULL. In or words, keys are only stored at internal nodes: all leafs are just NULL. This will make it a bit easier to analyze So let s start from a tree. How can we turn it into a binary tree and still have all of nice properties of a tree? Unfortunately, we can t we re not going to be able to get exact height-balance on any binary tree. But recall that we don t need exact balance in order to have efficient lookups; we just need depth to be O(log n). So let s be a bit flexible in balance requirement. Maybe we get lucky, and all nodes of our are actually degree-2 (y have one key stored Red-Black Tree at m). Then we don t need to do anything. But what if we have some nodes with 2 or 3 keys stored at m? Let s start with case of 3 keys stored, since that s simpler: let s just transform Represent tree as a BST. single degree-4 node into three degree-2 nodes!! Use "internal" edges for 3- and 4- nodes. red glue not 1-1 because 3-nodes swing eir way. So now we have two kinds of edges: some which correspond to original edges in tree (we all se black edges), and Red-Black some! Correspondence which Tree are internal between in trees larger and red-black nodes in trees tree (we call se red edges). When we represent a node with 2 keys stored (degree 3), we actually have two choices: Represent tree as a BST.! Use "internal" edges for 3- and 4- nodes. red glue plitting Nodes not 1-1 because 3-nodes swing eir way. So suppose that we start with a tree, and turn it into a binary tree using above correspondences.! Correspondence What important between properties trees and dored-black we have Red-Black trees. that Tree: show this Splitting is a Nodes good binary tree? 1. In every path from root to any or node, every red edge is followed by a black edge Two easy cases. Switch colors. (i.e., two red edges never appear in a row). 2. For each node, all paths to its descendent leaves have same number of black edges. When this node is root r, this value is called black-depth. Note that se are both simple consequences of properties of trees we ve talked about and correspondences we chose. Toger, y imply that path from root to farst 14 Two hard cases. Use rotations. Red-Black Tree: Splitting Nodes 6 Two easy cases. Switch colors. do single rotation do double rotation
7 not not because because 3-nodes 3-nodes swing swing eir eir way. way.! trees and! Correspondence between trees and red-black trees. leaf is no more than twice as long as path from root to nearest leaf. This in turn implies that depth of tree is only O(log n), so lookups will be efficient (see Lemma 13.1 in book). So we just need to figure out a way of inserting which preserves this correspondence (or more importantly, preserves two important properties). This turns out to be a little complicated, and is main reason that red-black trees can seem a bit mysterious. Note that I m going to do this slightly differently than how it s done in book, in order to make correspondence to trees a bit clearer. I strongly suggest you also read book on this. Informally, we re going to split full nodes on way down (like in B-trees), while book (and most real implementations) first insert and n repair by moving up tree. Red-Black Tree: Splitting Nodes So suppose we re trying to insert. The basic idea is to do a normal binary tree insert, where new link is red (since it s like inserting into a node at bottom of tree). If this were a tree, if we saw a full node on our way down when inserting, we would split it. What does that looktwo like Two ineasy red-black cases. trees? Switch Somecolors. cases are easy: we can just switch colors (bold is red) But or Two Two cases hard hard are harder cases. Use switching Use rotations. colors will violate one of red-black requirements: do do single single rotation do do double double rotation In order to handle se cases, we need to use idea of rotations. Tree rotations are a way of changing binary search tree while still preserving search tree property. They re useful all over place, and are used in search tree data structures beyond just red-black trees. The basic idea is simple, as following picture shows. 7
8 p u u p C A A B B C It turns out we can solve annoying cases of red-black trees by using eir one ( first case) or two ( second case) rotations. Let s see this through an example (proofs are in book read m!) Red-Black Tree: Splitting Nodes inserting G change colors X right rotate R! M left rotate E! L 17 We also need to handle some or complications even if we don t see any full nodes on our way down, inserting new nodered-black might mess up Tree: correspondence Insertion to trees and red-black properties. These can also be handled with Red-Black appropriate Tree: Balance tree rotations. For example, suppose we insert F (E in se notes since I ve copied m): Property A. Every path from root to leaf has same number of black links. Sym Property B. At most one red link in-a-row. E Property C. Height of tree is less than 2 lg N + 2. There are a few or complications, but this should illustrate basic idea. A red-black tree is a particular binary tree representation of a tree, and by carefully designing our insert and X A Implementatio Sorted array Unsorted list Hashing BST Randomized BS Splay Red-Black 8 M P Note. Compari 19
9 delete algorithm to respect this representation, we can get a binary search tree with O(log n) for lookups, inserts, and deletes. As mentioned, book does this slightly differently, which obscures relationship to trees (although it s still true) but makes red-black trees mselves quite a bit simpler. Please make sure you understand what s going on in book. 9
/633 Introduction to Algorithms Lecturer: Michael Dinitz Topic: Priority Queues / Heaps Date: 9/27/17
01.433/33 Introduction to Algorithms Lecturer: Michael Dinitz Topic: Priority Queues / Heaps Date: 9/2/1.1 Introduction In this lecture we ll talk about a useful abstraction, priority queues, which are
More information/633 Introduction to Algorithms Lecturer: Michael Dinitz Topic: Sorting lower bound and Linear-time sorting Date: 9/19/17
601.433/633 Introduction to Algorithms Lecturer: Michael Dinitz Topic: Sorting lower bound and Linear-time sorting Date: 9/19/17 5.1 Introduction You should all know a few ways of sorting in O(n log n)
More informationTreaps. 1 Binary Search Trees (BSTs) CSE341T/CSE549T 11/05/2014. Lecture 19
CSE34T/CSE549T /05/04 Lecture 9 Treaps Binary Search Trees (BSTs) Search trees are tree-based data structures that can be used to store and search for items that satisfy a total order. There are many types
More informationLecture 7. Binary Search Trees and Red-Black Trees
Lecture Binary Search Trees and Red-Black Trees Announcements HW released! (Due Friday) Today: binary search trees Brief foray into data structures! See CS 166 for more! What are binary search trees? You
More informationAlgorithms in Systems Engineering ISE 172. Lecture 16. Dr. Ted Ralphs
Algorithms in Systems Engineering ISE 172 Lecture 16 Dr. Ted Ralphs ISE 172 Lecture 16 1 References for Today s Lecture Required reading Sections 6.5-6.7 References CLRS Chapter 22 R. Sedgewick, Algorithms
More informationComputational Optimization ISE 407. Lecture 16. Dr. Ted Ralphs
Computational Optimization ISE 407 Lecture 16 Dr. Ted Ralphs ISE 407 Lecture 16 1 References for Today s Lecture Required reading Sections 6.5-6.7 References CLRS Chapter 22 R. Sedgewick, Algorithms in
More informationAlgorithms. AVL Tree
Algorithms AVL Tree Balanced binary tree The disadvantage of a binary search tree is that its height can be as large as N-1 This means that the time needed to perform insertion and deletion and many other
More informationAn AVL tree with N nodes is an excellent data. The Big-Oh analysis shows that most operations finish within O(log N) time
B + -TREES MOTIVATION An AVL tree with N nodes is an excellent data structure for searching, indexing, etc. The Big-Oh analysis shows that most operations finish within O(log N) time The theoretical conclusion
More informationB-Trees. Version of October 2, B-Trees Version of October 2, / 22
B-Trees Version of October 2, 2014 B-Trees Version of October 2, 2014 1 / 22 Motivation An AVL tree can be an excellent data structure for implementing dictionary search, insertion and deletion Each operation
More informationCSE373: Data Structures & Algorithms Lecture 5: Dictionary ADTs; Binary Trees. Linda Shapiro Spring 2016
CSE373: Data Structures & lgorithms Lecture 5: Dictionary DTs; Binary Trees Linda Shapiro Spring 2016 Today s Outline nnouncements - Homework 1 due TODY at 11:59 pm - Homework 2 out (paper and pencil assignment)
More informationBinary Search Trees. Analysis of Algorithms
Binary Search Trees Analysis of Algorithms Binary Search Trees A BST is a binary tree in symmetric order 31 Each node has a key and every node s key is: 19 23 25 35 38 40 larger than all keys in its left
More informationBST Deletion. First, we need to find the value which is easy because we can just use the method we developed for BST_Search.
BST Deletion Deleting a value from a Binary Search Tree is a bit more complicated than inserting a value, but we will deal with the steps one at a time. First, we need to find the value which is easy because
More informationChapter 12 Advanced Data Structures
Chapter 12 Advanced Data Structures 2 Red-Black Trees add the attribute of (red or black) to links/nodes red-black trees used in C++ Standard Template Library (STL) Java to implement maps (or, as in Python)
More informationLecture 5. Treaps Find, insert, delete, split, and join in treaps Randomized search trees Randomized search tree time costs
Lecture 5 Treaps Find, insert, delete, split, and join in treaps Randomized search trees Randomized search tree time costs Reading: Randomized Search Trees by Aragon & Seidel, Algorithmica 1996, http://sims.berkeley.edu/~aragon/pubs/rst96.pdf;
More informationHash Tables. CS 311 Data Structures and Algorithms Lecture Slides. Wednesday, April 22, Glenn G. Chappell
Hash Tables CS 311 Data Structures and Algorithms Lecture Slides Wednesday, April 22, 2009 Glenn G. Chappell Department of Computer Science University of Alaska Fairbanks CHAPPELLG@member.ams.org 2005
More informationModule 4: Index Structures Lecture 13: Index structure. The Lecture Contains: Index structure. Binary search tree (BST) B-tree. B+-tree.
The Lecture Contains: Index structure Binary search tree (BST) B-tree B+-tree Order file:///c /Documents%20and%20Settings/iitkrana1/My%20Documents/Google%20Talk%20Received%20Files/ist_data/lecture13/13_1.htm[6/14/2012
More informationParallel and Sequential Data Structures and Algorithms Lecture (Spring 2012) Lecture 16 Treaps; Augmented BSTs
Lecture 16 Treaps; Augmented BSTs Parallel and Sequential Data Structures and Algorithms, 15-210 (Spring 2012) Lectured by Margaret Reid-Miller 8 March 2012 Today: - More on Treaps - Ordered Sets and Tables
More informationCIS265/ Trees Red-Black Trees. Some of the following material is from:
CIS265/506 2-3-4 Trees Red-Black Trees Some of the following material is from: Data Structures for Java William H. Ford William R. Topp ISBN 0-13-047724-9 Chapter 27 Balanced Search Trees Bret Ford 2005,
More informationBinary Search Trees, etc.
Chapter 12 Binary Search Trees, etc. Binary Search trees are data structures that support a variety of dynamic set operations, e.g., Search, Minimum, Maximum, Predecessors, Successors, Insert, and Delete.
More informationCSE 332: Data Structures & Parallelism Lecture 7: Dictionaries; Binary Search Trees. Ruth Anderson Winter 2019
CSE 332: Data Structures & Parallelism Lecture 7: Dictionaries; Binary Search Trees Ruth Anderson Winter 2019 Today Dictionaries Trees 1/23/2019 2 Where we are Studying the absolutely essential ADTs of
More informationLecture 21: Red-Black Trees
15-150 Lecture 21: Red-Black Trees Lecture by Dan Licata April 3, 2012 Last time, we talked about the signature for dictionaries: signature ORDERED = sig type t val compare : t * t -> order signature DICT
More informationIntroduction. for large input, even access time may be prohibitive we need data structures that exhibit times closer to O(log N) binary search tree
Chapter 4 Trees 2 Introduction for large input, even access time may be prohibitive we need data structures that exhibit running times closer to O(log N) binary search tree 3 Terminology recursive definition
More informationCISC 235: Topic 4. Balanced Binary Search Trees
CISC 235: Topic 4 Balanced Binary Search Trees Outline Rationale and definitions Rotations AVL Trees, Red-Black, and AA-Trees Algorithms for searching, insertion, and deletion Analysis of complexity CISC
More informationLecture 6. Binary Search Trees and Red-Black Trees
Lecture Binary Search Trees and Red-Black Trees Sorting out the past couple of weeks: We ve seen a bunch of sorting algorithms InsertionSort - Θ n 2 MergeSort - Θ n log n QuickSort - Θ n log n expected;
More information9/29/2016. Chapter 4 Trees. Introduction. Terminology. Terminology. Terminology. Terminology
Introduction Chapter 4 Trees for large input, even linear access time may be prohibitive we need data structures that exhibit average running times closer to O(log N) binary search tree 2 Terminology recursive
More informationRed-black trees (19.5), B-trees (19.8), trees
Red-black trees (19.5), B-trees (19.8), 2-3-4 trees Red-black trees A red-black tree is a balanced BST It has a more complicated invariant than an AVL tree: Each node is coloured red or black A red node
More informationMulti-Way Search Trees
Multi-Way Search Trees Manolis Koubarakis 1 Multi-Way Search Trees Multi-way trees are trees such that each internal node can have many children. Let us assume that the entries we store in a search tree
More informationData Structures Lesson 7
Data Structures Lesson 7 BSc in Computer Science University of New York, Tirana Assoc. Prof. Dr. Marenglen Biba 1-1 Binary Search Trees For large amounts of input, the linear access time of linked lists
More informationComputer Science 210 Data Structures Siena College Fall Topic Notes: Priority Queues and Heaps
Computer Science 0 Data Structures Siena College Fall 08 Topic Notes: Priority Queues and Heaps Heaps and Priority Queues From here, we will look at some ways that trees are used in other structures. First,
More informationCSE100. Advanced Data Structures. Lecture 8. (Based on Paul Kube course materials)
CSE100 Advanced Data Structures Lecture 8 (Based on Paul Kube course materials) CSE 100 Treaps Find, insert, delete, split, and join in treaps Randomized search trees Randomized search tree time costs
More informationCSE 502 Class 16. Jeremy Buhler Steve Cole. March A while back, we introduced the idea of collections to put hash tables in context.
CSE 502 Class 16 Jeremy Buhler Steve Cole March 17 2015 Onwards to trees! 1 Collection Types Revisited A while back, we introduced the idea of collections to put hash tables in context. abstract data types
More informationMulti-way Search Trees! M-Way Search! M-Way Search Trees Representation!
Lecture 10: Multi-way Search Trees: intro to B-trees 2-3 trees 2-3-4 trees Multi-way Search Trees A node on an M-way search tree with M 1 distinct and ordered keys: k 1 < k 2 < k 3
More informationSelf-Balancing Search Trees. Chapter 11
Self-Balancing Search Trees Chapter 11 Chapter Objectives To understand the impact that balance has on the performance of binary search trees To learn about the AVL tree for storing and maintaining a binary
More informationProperties of red-black trees
Red-Black Trees Introduction We have seen that a binary search tree is a useful tool. I.e., if its height is h, then we can implement any basic operation on it in O(h) units of time. The problem: given
More informationCSE373: Data Structures & Algorithms Lecture 4: Dictionaries; Binary Search Trees. Kevin Quinn Fall 2015
CSE373: Data Structures & Algorithms Lecture 4: Dictionaries; Binary Search Trees Kevin Quinn Fall 2015 Where we are Studying the absolutely essential ADTs of computer science and classic data structures
More informationMulti-Way Search Trees
Multi-Way Search Trees Manolis Koubarakis 1 Multi-Way Search Trees Multi-way trees are trees such that each internal node can have many children. Let us assume that the entries we store in a search tree
More informationComputer Science 210 Data Structures Siena College Fall Topic Notes: Binary Search Trees
Computer Science 10 Data Structures Siena College Fall 018 Topic Notes: Binary Search Trees Possibly the most common usage of a binary tree is to store data for quick retrieval. Definition: A binary tree
More informationLecture 6: Analysis of Algorithms (CS )
Lecture 6: Analysis of Algorithms (CS583-002) Amarda Shehu October 08, 2014 1 Outline of Today s Class 2 Traversals Querying Insertion and Deletion Sorting with BSTs 3 Red-black Trees Height of a Red-black
More informationCOMP Analysis of Algorithms & Data Structures
COMP 3170 - Analysis of Algorithms & Data Structures Shahin Kamali Lecture 9 - Jan. 22, 2018 CLRS 12.2, 12.3, 13.2, read problem 13-3 University of Manitoba COMP 3170 - Analysis of Algorithms & Data Structures
More informationCS350: Data Structures B-Trees
B-Trees James Moscola Department of Engineering & Computer Science York College of Pennsylvania James Moscola Introduction All of the data structures that we ve looked at thus far have been memory-based
More informationBinary Heaps in Dynamic Arrays
Yufei Tao ITEE University of Queensland We have already learned that the binary heap serves as an efficient implementation of a priority queue. Our previous discussion was based on pointers (for getting
More informationMultiway Search Trees. Multiway-Search Trees (cont d)
Multiway Search Trees Each internal node v of a multi-way search tree T has at least two children contains d-1 items, where d is the number of children of v an item is of the form (k i,x i ) for 1 i d-1,
More informationCSE 326: Data Structures Splay Trees. James Fogarty Autumn 2007 Lecture 10
CSE 32: Data Structures Splay Trees James Fogarty Autumn 2007 Lecture 10 AVL Trees Revisited Balance condition: Left and right subtrees of every node have heights differing by at most 1 Strong enough :
More informationBinary Trees
Binary Trees 4-7-2005 Opening Discussion What did we talk about last class? Do you have any code to show? Do you have any questions about the assignment? What is a Tree? You are all familiar with what
More informationLaboratory Module X B TREES
Purpose: Purpose 1... Purpose 2 Purpose 3. Laboratory Module X B TREES 1. Preparation Before Lab When working with large sets of data, it is often not possible or desirable to maintain the entire structure
More informationWe will show that the height of a RB tree on n vertices is approximately 2*log n. In class I presented a simple structural proof of this claim:
We have seen that the insert operation on a RB takes an amount of time proportional to the number of the levels of the tree (since the additional operations required to do any rebalancing require constant
More informationCS102 Binary Search Trees
CS102 Binary Search Trees Prof Tejada 1 To speed up insertion, removal and search, modify the idea of a Binary Tree to create a Binary Search Tree (BST) Binary Search Trees Binary Search Trees have one
More informationPart 2: Balanced Trees
Part 2: Balanced Trees 1 AVL Trees We could dene a perfectly balanced binary search tree with N nodes to be a complete binary search tree, one in which every level except the last is completely full. A
More informationCOMP Analysis of Algorithms & Data Structures
COMP 3170 - Analysis of Algorithms & Data Structures Shahin Kamali Lecture 9 - Jan. 22, 2018 CLRS 12.2, 12.3, 13.2, read problem 13-3 University of Manitoba 1 / 12 Binary Search Trees (review) Structure
More informationB-Trees. Based on materials by D. Frey and T. Anastasio
B-Trees Based on materials by D. Frey and T. Anastasio 1 Large Trees n Tailored toward applications where tree doesn t fit in memory q operations much faster than disk accesses q want to limit levels of
More informationTrees. Courtesy to Goodrich, Tamassia and Olga Veksler
Lecture 12: BT Trees Courtesy to Goodrich, Tamassia and Olga Veksler Instructor: Yuzhen Xie Outline B-tree Special case of multiway search trees used when data must be stored on the disk, i.e. too large
More informationWe assume uniform hashing (UH):
We assume uniform hashing (UH): the probe sequence of each key is equally likely to be any of the! permutations of 0,1,, 1 UH generalizes the notion of SUH that produces not just a single number, but a
More informationData Structures and Algorithms
Data Structures and Algorithms CS245-2008S-19 B-Trees David Galles Department of Computer Science University of San Francisco 19-0: Indexing Operations: Add an element Remove an element Find an element,
More informationOutline. Definition. 2 Height-Balance. 3 Searches. 4 Rotations. 5 Insertion. 6 Deletions. 7 Reference. 1 Every node is either red or black.
Outline 1 Definition Computer Science 331 Red-Black rees Mike Jacobson Department of Computer Science University of Calgary Lectures #20-22 2 Height-Balance 3 Searches 4 Rotations 5 s: Main Case 6 Partial
More informationV Advanced Data Structures
V Advanced Data Structures B-Trees Fibonacci Heaps 18 B-Trees B-trees are similar to RBTs, but they are better at minimizing disk I/O operations Many database systems use B-trees, or variants of them,
More informationA dictionary interface.
A dictionary interface. interface Dictionary { public Data search(key k); public void insert(key k, Data d); public void delete(key k); A dictionary behaves like a many-to-one function. The search method
More informationAlgorithms for Memory Hierarchies Lecture 2
Algorithms for emory Hierarchies Lecture Lecturer: odari Sitchianva Scribes: Robin Rehrmann, ichael Kirsten Last Time External memory (E) model Scan(): O( ) I/Os Stacks / queues: O( 1 ) I/Os / elt ergesort:
More informationComputer Science 210 Data Structures Siena College Fall Topic Notes: Binary Search Trees
Computer Science 10 Data Structures Siena College Fall 016 Topic Notes: Binary Search Trees Possibly the most common usage of a binary tree is to store data for quick retrieval. Definition: A binary tree
More informationlecture notes September 2, How to sort?
.30 lecture notes September 2, 203 How to sort? Lecturer: Michel Goemans The task of sorting. Setup Suppose we have n objects that we need to sort according to some ordering. These could be integers or
More informationAdvanced Algorithms. Class Notes for Thursday, September 18, 2014 Bernard Moret
Advanced Algorithms Class Notes for Thursday, September 18, 2014 Bernard Moret 1 Amortized Analysis (cont d) 1.1 Side note: regarding meldable heaps When we saw how to meld two leftist trees, we did not
More informationCOMP 250 Fall recurrences 2 Oct. 13, 2017
COMP 250 Fall 2017 15 - recurrences 2 Oct. 13, 2017 Here we examine the recurrences for mergesort and quicksort. Mergesort Recall the mergesort algorithm: we divide the list of things to be sorted into
More informationBinary search trees (chapters )
Binary search trees (chapters 18.1 18.3) Binary search trees In a binary search tree (BST), every node is greater than all its left descendants, and less than all its right descendants (recall that this
More informationNote that this is a rep invariant! The type system doesn t enforce this but you need it to be true. Should use repok to check in debug version.
Announcements: Prelim tonight! 7:30-9:00 in Thurston 203/205 o Handed back in section tomorrow o If you have a conflict you can take the exam at 5:45 but can t leave early. Please email me so we have a
More informationWhere we are. CSE373: Data Structures & Algorithms Lecture 4: Dictionaries; Binary Search Trees. The Dictionary (a.k.a. Map) ADT
Where we are Studying the absolutely essential DTs of computer science and classic data structures for implementing them SE: Data Structures & lgorithms Lecture : Dictionaries; inary Search Trees Dan rossman
More informationAlgorithms. Deleting from Red-Black Trees B-Trees
Algorithms Deleting from Red-Black Trees B-Trees Recall the rules for BST deletion 1. If vertex to be deleted is a leaf, just delete it. 2. If vertex to be deleted has just one child, replace it with that
More informationV Advanced Data Structures
V Advanced Data Structures B-Trees Fibonacci Heaps 18 B-Trees B-trees are similar to RBTs, but they are better at minimizing disk I/O operations Many database systems use B-trees, or variants of them,
More informationCOMP Analysis of Algorithms & Data Structures
COMP 3170 - Analysis of Algorithms & Data Structures Shahin Kamali Binary Search Trees CLRS 12.2, 12.3, 13.2, read problem 13-3 University of Manitoba COMP 3170 - Analysis of Algorithms & Data Structures
More informationCS 350 : Data Structures B-Trees
CS 350 : Data Structures B-Trees David Babcock (courtesy of James Moscola) Department of Physical Sciences York College of Pennsylvania James Moscola Introduction All of the data structures that we ve
More informationDictionaries. Priority Queues
Red-Black-Trees.1 Dictionaries Sets and Multisets; Opers: (Ins., Del., Mem.) Sequential sorted or unsorted lists. Linked sorted or unsorted lists. Tries and Hash Tables. Binary Search Trees. Priority Queues
More informationCSE 3101: Introduction to the Design and Analysis of Algorithms. Office hours: Wed 4-6 pm (CSEB 3043), or by appointment.
CSE 3101: Introduction to the Design and Analysis of Algorithms Instructor: Suprakash Datta (datta[at]cse.yorku.ca) ext 77875 Lectures: Tues, BC 215, 7 10 PM Office hours: Wed 4-6 pm (CSEB 3043), or by
More informationLecture 6 Sequences II. Parallel and Sequential Data Structures and Algorithms, (Fall 2013) Lectured by Danny Sleator 12 September 2013
Lecture 6 Sequences II Parallel and Sequential Data Structures and Algorithms, 15-210 (Fall 2013) Lectured by Danny Sleator 12 September 2013 Material in this lecture: Today s lecture is about reduction.
More informationUnit III - Tree TREES
TREES Unit III - Tree Consider a scenario where you are required to represent the directory structure of your operating system. The directory structure contains various folders and files. A folder may
More informationBalanced Trees Part One
Balanced Trees Part One Balanced Trees Balanced search trees are among the most useful and versatile data structures. Many programming languages ship with a balanced tree library. C++: std::map / std::set
More informationBalanced Binary Search Trees. Victor Gao
Balanced Binary Search Trees Victor Gao OUTLINE Binary Heap Revisited BST Revisited Balanced Binary Search Trees Rotation Treap Splay Tree BINARY HEAP: REVIEW A binary heap is a complete binary tree such
More informationExtra: B+ Trees. Motivations. Differences between BST and B+ 10/27/2017. CS1: Java Programming Colorado State University
Extra: B+ Trees CS1: Java Programming Colorado State University Slides by Wim Bohm and Russ Wakefield 1 Motivations Many times you want to minimize the disk accesses while doing a search. A binary search
More informationChapter 15. Binary Search Trees (BSTs) Preliminaries
Chapter 15 Binary Search Trees (BSTs) Search trees are data structures that can be used to efficiently support searches and updates on collections of items that satisfy a total order. Probably, the most
More informationCSCI 136 Data Structures & Advanced Programming. Lecture 25 Fall 2018 Instructor: B 2
CSCI 136 Data Structures & Advanced Programming Lecture 25 Fall 2018 Instructor: B 2 Last Time Binary search trees (Ch 14) The locate method Further Implementation 2 Today s Outline Binary search trees
More informationTrees 2: Linked Representation, Tree Traversal, and Binary Search Trees
Trees 2: Linked Representation, Tree Traversal, and Binary Search Trees Linked representation of binary tree Again, as with linked list, entire tree can be represented with a single pointer -- in this
More informationRecall: Properties of B-Trees
CSE 326 Lecture 10: B-Trees and Heaps It s lunch time what s cookin? B-Trees Insert/Delete Examples and Run Time Analysis Summary of Search Trees Introduction to Heaps and Priority Queues Covered in Chapters
More informationCSE 326: Data Structures Lecture #8 Binary Search Trees
CSE 6: Data Structures Lecture #8 Binary Search Trees Alon Halevy Spring Quarter Binary Trees Many algorithms are efficient and easy to program for the special case of binary trees B A C Binary tree is
More informationCS 137 Part 8. Merge Sort, Quick Sort, Binary Search. November 20th, 2017
CS 137 Part 8 Merge Sort, Quick Sort, Binary Search November 20th, 2017 This Week We re going to see two more complicated sorting algorithms that will be our first introduction to O(n log n) sorting algorithms.
More informationPrinciples of Algorithm Design
Principles of Algorithm Design When you are trying to design an algorithm or a data structure, it s often hard to see how to accomplish the task. The following techniques can often be useful: 1. Experiment
More informationLecture Notes on Binary Search Trees
Lecture Notes on Binary Search Trees 15-122: Principles of Imperative Computation Frank Pfenning André Platzer Lecture 15 March 6, 2014 1 Introduction In this lecture, we will continue considering associative
More informationBinary search trees (chapters )
Binary search trees (chapters 18.1 18.3) Binary search trees In a binary search tree (BST), every node is greater than all its left descendants, and less than all its right descendants (recall that this
More informationLecture 4: examples of topological spaces, coarser and finer topologies, bases and closed sets
Lecture 4: examples of topological spaces, coarser and finer topologies, bases and closed sets Saul Glasman 14 September 2016 Let s give the definition of an open subset of R. Definition 1. Let U R. We
More informationLecture 15 Binary Search Trees
Lecture 15 Binary Search Trees 15-122: Principles of Imperative Computation (Fall 2017) Frank Pfenning, André Platzer, Rob Simmons, Iliano Cervesato In this lecture, we will continue considering ways to
More informationQuiz 1 Solutions. (a) f(n) = n g(n) = log n Circle all that apply: f = O(g) f = Θ(g) f = Ω(g)
Introduction to Algorithms March 11, 2009 Massachusetts Institute of Technology 6.006 Spring 2009 Professors Sivan Toledo and Alan Edelman Quiz 1 Solutions Problem 1. Quiz 1 Solutions Asymptotic orders
More informationComputer Science 210 Data Structures Siena College Fall Topic Notes: Trees
Computer Science 0 Data Structures Siena College Fall 08 Topic Notes: Trees We ve spent a lot of time looking at a variety of structures where there is a natural linear ordering of the elements in arrays,
More informationRed-Black Trees. Based on materials by Dennis Frey, Yun Peng, Jian Chen, and Daniel Hood
Red-Black Trees Based on materials by Dennis Frey, Yun Peng, Jian Chen, and Daniel Hood Quick Review of Binary Search Trees n Given a node n... q All elements of n s left subtree are less than n.data q
More informationCSE 332, Spring 2010, Midterm Examination 30 April 2010
CSE 332, Spring 2010, Midterm Examination 30 April 2010 Please do not turn the page until the bell rings. Rules: The exam is closed-book, closed-note. You may use a calculator for basic arithmetic only.
More informationSolutions. Suppose we insert all elements of U into the table, and let n(b) be the number of elements of U that hash to bucket b. Then.
Assignment 3 1. Exercise [11.2-3 on p. 229] Modify hashing by chaining (i.e., bucketvector with BucketType = List) so that BucketType = OrderedList. How is the runtime of search, insert, and remove affected?
More informationData Structures and Algorithms Dr. Naveen Garg Department of Computer Science and Engineering Indian Institute of Technology, Delhi.
Data Structures and Algorithms Dr. Naveen Garg Department of Computer Science and Engineering Indian Institute of Technology, Delhi Lecture 18 Tries Today we are going to be talking about another data
More informationCSE 530A. B+ Trees. Washington University Fall 2013
CSE 530A B+ Trees Washington University Fall 2013 B Trees A B tree is an ordered (non-binary) tree where the internal nodes can have a varying number of child nodes (within some range) B Trees When a key
More informationCSE 373 OCTOBER 11 TH TRAVERSALS AND AVL
CSE 373 OCTOBER 11 TH TRAVERSALS AND AVL MINUTIAE Feedback for P1p1 should have gone out before class Grades on canvas tonight Emails went to the student who submitted the assignment If you did not receive
More informationCS125 : Introduction to Computer Science. Lecture Notes #38 and #39 Quicksort. c 2005, 2003, 2002, 2000 Jason Zych
CS125 : Introduction to Computer Science Lecture Notes #38 and #39 Quicksort c 2005, 2003, 2002, 2000 Jason Zych 1 Lectures 38 and 39 : Quicksort Quicksort is the best sorting algorithm known which is
More informationLecture 11: Multiway and (2,4) Trees. Courtesy to Goodrich, Tamassia and Olga Veksler
Lecture 11: Multiway and (2,4) Trees 9 2 5 7 10 14 Courtesy to Goodrich, Tamassia and Olga Veksler Instructor: Yuzhen Xie Outline Multiway Seach Tree: a new type of search trees: for ordered d dictionary
More informationCSE373: Data Structures & Algorithms Lecture 5: Dictionaries; Binary Search Trees. Aaron Bauer Winter 2014
CSE373: Data Structures & lgorithms Lecture 5: Dictionaries; Binary Search Trees aron Bauer Winter 2014 Where we are Studying the absolutely essential DTs of computer science and classic data structures
More informationSearch Trees - 1 Venkatanatha Sarma Y
Search Trees - 1 Lecture delivered by: Venkatanatha Sarma Y Assistant Professor MSRSAS-Bangalore 11 Objectives To introduce, discuss and analyse the different ways to realise balanced Binary Search Trees
More informationChapter 2: Basic Data Structures
Chapter 2: Basic Data Structures Basic Data Structures Stacks Queues Vectors, Linked Lists Trees (Including Balanced Trees) Priority Queues and Heaps Dictionaries and Hash Tables Spring 2014 CS 315 2 Two
More informationData Structures and Algorithms Dr. Naveen Garg Department of Computer Science and Engineering Indian Institute of Technology, Delhi
Data Structures and Algorithms Dr. Naveen Garg Department of Computer Science and Engineering Indian Institute of Technology, Delhi Lecture 20 Priority Queues Today we are going to look at the priority
More information