Binary Search Tree in Which the Heights of the Subtrees of a Node Differ by No More Than 1

Total Page:16

File Type:pdf, Size:1020Kb

Binary Search Tree in Which the Heights of the Subtrees of a Node Differ by No More Than 1 MA 513: Data Structures Lecture Note http://www.iitg.ernet.in/psm/indexing_ma513/y09/index.html Partha Sarathi Mandal [email protected] Dept. of Mathematics, IIT Guwahati lTue 9:00-9:55 Wed 10:00-10:55 Thu 11:00-11:55 Class Room : 1G2 lMA514 Data Structure Lab : Tue 14:00-16:55 Constructing an expression Tree • Convert postfix expression to expression tree • a b + c d e + * * a b Constructing an expression Tree • Convert postfix expression to expression tree • a b + c d e + * * + a b Constructing an expression Tree • Convert postfix expression to expression tree • a b + c d e + * * + c d e a b Constructing an expression Tree • Convert postfix expression to expression tree • a b + c d e + * * + c + a b d e Constructing an expression Tree • Convert postfix expression to expression tree • a b + c d e + * * * + a b c + d e Constructing an expression Tree • Convert postfix expression to expression tree • a b + c d e + * * * * + a b c + d e AVL-Trees Objectives • Understand the concept of an AVL tree. • Understand how AVL trees and BSTs differ. • Understand the issues involved in balancing an AVL tree. Introduction • Named for its inventors (Adelson-Velsky and Landis), an AVL tree is a binary search tree in which the heights of the subtrees of a node differ by no more than 1. • It is thus a balanced BST. • To understand the significance of the tree being balanced, let’s look at two different trees containing the same data. Introduction Introduction This “tree” is just a linked list in binary tree clothing. It takes 2 tests to locate 12, 3 to locate 14, and 8 to locate 52. Hence, the search effort for this binary tree is O(n). Introduction This BST is an AVL tree. It takes 2 tests to locate 18, 3 to locate 12, and 4 to locate 8. Hence, the search effort for this binary tree is O(log2n). Introduction • For a tree with 1000 nodes, the worst case for a completely unbalanced tree is 1000 tests. • However, the worst case for a balanced tree is 10 tests. • Hence, balancing a tree can lead to significant improvements. Balanced Binary Search Trees • height is O(log n), where n is the number of elements in the tree • AVL (Adelson-Velsky and Landis) trees • red-black trees • get, put, and remove take O(log n) time Balanced Binary Search Trees • Indexed AVL trees • Indexed red-black trees • Indexed operations also take – O(log n) time Balanced Search Trees • weight balanced binary search trees • 2-3 & 2-3-4 trees • B-trees • etc. AVL Tree • binary tree • for every node x, define its balance factor balance factor of x = height of left subtree of x - height of right subtree of x • balance factor of every node x is -1, 0, or 1 Balance Factors -1 • This is an AVL tree. 1 1 -1 0 0 1 0 0 0 -1 0 0 Height The height of an AVL tree that has n nodes is at most 1.44 log2 (n+2). The height of every n node binary tree is at least log2 (n+1). AVL Search Tree -1 10 1 1 7 40 0 1 -1 0 45 3 8 30 0 -1 0 0 0 35 60 1 5 20 0 25 put(9) -1 10 0 1 1 7 40 -1 0 0 -1 1 45 3 8 30 0 -1 0 0 0 0 35 60 1 5 9 20 0 25 put(29) -1 10 1 1 7 40 0 1 -1 0 45 3 8 30 0 -1 0 0 0 -2 35 60 20 1 5 0 -1 RR imbalance => new node is in right 25 0 subtree of right subtree of blue node (node with bf = -2) 29 put(29) -1 10 1 1 7 40 0 1 -1 0 45 3 8 30 0 0 0 0 0 35 60 1 5 25 0 0 20 29 RR rotation 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 operations can be O(N) in the worst case • We want a tree with small height • A binary tree with N node has height at least (log N) • Thus, our goal is to keep the height of a binary search tree O(log N) • Such trees are called balanced binary search trees. Examples are AVL tree, red-black tree. AVL tree Height of a node • The height of a leaf is zero. • The height of an internal node is the maximum height of its children plus 1. AVL tree • An AVL tree is a binary search tree in which – for every node in the tree, the height of the left and right subtrees differ by at most 1. AVL property violated here AVL tree • Let x be the root of an AVL tree of height h • Let Nh denote the minimum number of nodes in an AVL tree of height h • Clearly, Ni ≥ Ni-1 by definition • We have Nh Nh1 Nh2 1 2Nh2 1 2Nh2 • By repeated substitution, we obtain the general form i Nh 2 Nh2i • The boundary conditions are: N0=1 and N1 =2. This implies that h = O(log Nh). • Thus, many operations (searching, insertion, deletion) on an AVL tree will take O(log N) time. Rotations • When the tree structure changes (e.g., insertion or deletion), we need to transform the tree to restore the AVL tree property. • This is done using single rotations or double rotations. e.g. Single Rotation y x x y C A B C B A Before Rotation After Rotation Rotations • Since an insertion/deletion involves adding/ deleting a single node, this can only increase/ decrease the height of some subtree by 1 • Thus, if the AVL tree property is violated at a node x, it means that the heights of left(x) ad right(x) differ by exactly 2. • Rotations will be applied to x to restore the AVL tree property. Insertion • First, insert the new key as a new leaf just as in ordinary binary search tree. • Then trace the path from the new leaf towards the root. For each node x encountered, check if heights of left(x) and right(x) differ by at most 1. • If yes, proceed to parent(x). If not, restructure by doing either a single rotation or a double rotation [next slide]. • For insertion, once we perform a rotation at a node x, we won’t need to perform any rotation at any ancestor of x. Insertion • Let x be the node at which left(x) and right(x) differ by more than 1. • Assume that the height of x is h+3 • There are 4 cases – Height of left(x) is h+2 (i.e. height of right(x) is h) • LL: Height of left(left(x)) is h+1 single rotate with left child • LR: Height of right(left(x)) is h+1 double rotate with left child – Height of right(x) is h+2 (i.e. height of left(x) is h) • RR: Height of right(right(x)) is h+1 single rotate with right child. • RL: Height of left(right(x)) is h+1 double rotate with right child. AVL Rotations • RR: right-right • LL: left-left • RL: right-left • LR: left-right Single rotation (LL) The new key is inserted in the subtree A. The AVL-property is violated at x o height of left(x) is h+2 o height of right(x) is h. Single rotation (RR) The new key is inserted in the subtree C. The AVL-property is violated at x. Single rotation takes O(1) time. Insertion takes O(log N) time. 5 x AVL Tree 5 8 C 3 y 3 8 1 4 1 4 B A 0.8 Insert 0.8 3 5 1 After rotation 4 8 0.8 Double rotation (LR) The new key is inserted in the subtree B1 or B2. The AVL-property is violated at x. x-y-z forms a zig-zag shape also called left-right rotate Double rotation (RL) The new key is inserted in the subtree B1 or B2. The AVL-property is violated at x. also called right-left rotate 5 x AVL Tree 5 C y 8 3 3 8 A 1 4 z 1 4 B 3.5 Insert 3.5 4 5 3 3.5 8 After Rotation 1 An Extended Example Insert 3,2,1,4,5,6,7, 16,15,14 Single rotation 3 3 2 3 2 1 Fig 1 2 3 Fig 4 Fig 2 2 1 2 Single rotation Fig 3 1 3 1 3 Fig 5 Fig 6 4 4 5 2 2 Single rotation 1 1 4 4 3 5 3 5 Fig 8 Fig 7 6 4 4 Single rotation 2 5 2 5 1 3 6 1 3 6 4 Fig 9 Fig 10 7 2 6 1 3 7 5 Fig 11 4 2 6 1 3 7 5 16 Fig 12 4 Double rotation 4 2 6 2 6 1 3 7 1 3 15 5 16 5 16 Fig 13 15 Fig 14 7 4 4 Double rotation 2 2 6 7 15 1 3 15 1 3 5 6 16 7 5 14 16 Fig 15 14 Fig 16 AVL Tree template class template <class keyType> class AVL; template <class keyType> class AvlNode{ friend class AVL<keyType>; private: keyType data; AvlNode *LiftChild, *RightChild; int bf; }; template <class keyType> class AVL{ AvlNode<keyType>* root; public: AVL(AvlNode<keyType> *init = 0): root(init){}; Boolean Insert(const ketType&); Boolean Delete(const ketType&); AvlNode<keyType>* Search(const ketType&); }; Insert in AVL Tree template<class keyType> Boolean AVL<keyType>::Insert(const ketType& x); AvlNode<keyType> *a, *b, *c, *f, *p, *q, *y, *clchild, *crchild; Boolean Found, Unbalanced; int d; { if(!root){ y = new AvlNode<keyType>; y->data = x; root=y; root->bf=0; root->leftChild=root->RightChild=0; return TRUE;} } //a = most resent node with bf +1/-1, f =p[a], q follows p f=0; a=p=root; q=0; Found = FALSE; While(p && !Found){ if(p->bf){a=p; f=q;} if(x.key < p->data.key){q=p; p=p->LeftChild;} else if(x.key > p->data.key) {q=p; p=p->RightChild;} else{y=p; Found = TRUE; } } If(Unbalanced){ //tree unbalanced if(d==1){ // left imbalance Insert in AVL Tree if(b->bf==1){//rotation LL a->LeftChild = b->RightChild; if(!Found){ b->RightChild=a, a->bf=0; b->bf=0; y = new AvlNode<keyType>; } y->data = x; y->bf=0; else{ //rotation LR y->leftChild= y->RightChild=0; c= b->RightChild; if(x.key < q->data.key) b->RightChild = c->LeftChild; q->LeftChild=y; a->LeftChild = c->RightChild ; else q->RightChild=y; c->LeftChild =b; if(x.key > a->data.key){ c->RightChild =a; p=a->RightChild; b=p; d= -1} switch(c->bf){ else { p=a->LeftChild; b=p; d=1} case 1: a->bf = -1; b->bf =0; break; while(p!=y) case -1: b->bf =1; a->bf =0; break; if(x.key > p->data.key){ case 0 : b->bf =0; a->bf =0; break; p->bf = -1; p=p->RightChild;} } c->bf =0; b=c; else {p->bf = 1; p=p->LeftChild;} } // end of LR } // end of left imbalance Unbalanced = TRUE; else {// right imbalance} If( !(a->bf) || !(a->bf+d)){ if(!f) root = b; a->bf+=d; Unbalanced = FALSE; else if(a==f->LeftChild) f->leftChild =b; } else if(a==f->RightChild) f->RightChild =b; } // end of tree unbalanced return TRUE; } // end of if(!Found) Deletion • Delete a node x as in ordinary binary search tree.
Recommended publications
  • Trees • Binary Trees • Newer Types of Tree Structures • M-Ary Trees
    Data Organization and Processing Hierarchical Indexing (NDBI007) David Hoksza http://siret.ms.mff.cuni.cz/hoksza 1 Outline • Background • prefix tree • graphs • … • search trees • binary trees • Newer types of tree structures • m-ary trees • Typical tree type structures • B-tree • B+-tree • B*-tree 2 Motivation • Similarly as in hashing, the motivation is to find record(s) given a query key using only a few operations • unlike hashing, trees allow to retrieve set of records with keys from given range • Tree structures use “clustering” to efficiently filter out non relevant records from the data set • Tree-based indexes practically implement the index file data organization • for each search key, an indexing structure can be maintained 3 Drawbacks of Index(-Sequential) Organization • Static nature • when inserting a record at the beginning of the file, the whole index needs to be rebuilt • an overflow handling policy can be applied → the performance degrades as the file grows • reorganization can take a lot of time, especially for large tables • not possible for online transactional processing (OLTP) scenarios (as opposed to OLAP – online analytical processing) where many insertions/deletions occur 4 Tree Indexes • Most common dynamic indexing structure for external memory • When inserting/deleting into/from the primary file, the indexing structure(s) residing in the secondary file is modified to accommodate the new key • The modification of a tree is implemented by splitting/merging nodes • Used not only in databases • NTFS directory structure
    [Show full text]
  • Balanced 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 ● Java: TreeMap / TreeSet ● Many advanced data structures are layered on top of balanced trees. ● We’ll see several later in the quarter! Where We're Going ● B-Trees (Today) ● A simple type of balanced tree developed for block storage. ● Red/Black Trees (Today/Thursday) ● The canonical balanced binary search tree. ● Augmented Search Trees (Thursday) ● Adding extra information to balanced trees to supercharge the data structure. Outline for Today ● BST Review ● Refresher on basic BST concepts and runtimes. ● Overview of Red/Black Trees ● What we're building toward. ● B-Trees and 2-3-4 Trees ● Simple balanced trees, in depth. ● Intuiting Red/Black Trees ● A much better feel for red/black trees. A Quick BST Review Binary Search Trees ● A binary search tree is a binary tree with 9 the following properties: 5 13 ● Each node in the BST stores a key, and 1 6 10 14 optionally, some auxiliary information. 3 7 11 15 ● The key of every node in a BST is strictly greater than all keys 2 4 8 12 to its left and strictly smaller than all keys to its right. Binary Search Trees ● The height of a binary search tree is the 9 length of the longest path from the root to a 5 13 leaf, measured in the number of edges. 1 6 10 14 ● A tree with one node has height 0.
    [Show full text]
  • Assignment 3: Kdtree ______Due June 4, 11:59 PM
    CS106L Handout #04 Spring 2014 May 15, 2014 Assignment 3: KDTree _________________________________________________________________________________________________________ Due June 4, 11:59 PM Over the past seven weeks, we've explored a wide array of STL container classes. You've seen the linear vector and deque, along with the associative map and set. One property common to all these containers is that they are exact. An element is either in a set or it isn't. A value either ap- pears at a particular position in a vector or it does not. For most applications, this is exactly what we want. However, in some cases we may be interested not in the question “is X in this container,” but rather “what value in the container is X most similar to?” Queries of this sort often arise in data mining, machine learning, and computational geometry. In this assignment, you will implement a special data structure called a kd-tree (short for “k-dimensional tree”) that efficiently supports this operation. At a high level, a kd-tree is a generalization of a binary search tree that stores points in k-dimen- sional space. That is, you could use a kd-tree to store a collection of points in the Cartesian plane, in three-dimensional space, etc. You could also use a kd-tree to store biometric data, for example, by representing the data as an ordered tuple, perhaps (height, weight, blood pressure, cholesterol). However, a kd-tree cannot be used to store collections of other data types, such as strings. Also note that while it's possible to build a kd-tree to hold data of any dimension, all of the data stored in a kd-tree must have the same dimension.
    [Show full text]
  • Splay Trees Last Changed: January 28, 2017
    15-451/651: Design & Analysis of Algorithms January 26, 2017 Lecture #4: Splay Trees last changed: January 28, 2017 In today's lecture, we will discuss: • binary search trees in general • definition of splay trees • analysis of splay trees The analysis of splay trees uses the potential function approach we discussed in the previous lecture. It seems to be required. 1 Binary Search Trees These lecture notes assume that you have seen binary search trees (BSTs) before. They do not contain much expository or backtround material on the basics of BSTs. Binary search trees is a class of data structures where: 1. Each node stores a piece of data 2. Each node has two pointers to two other binary search trees 3. The overall structure of the pointers is a tree (there's a root, it's acyclic, and every node is reachable from the root.) Binary search trees are a way to store and update a set of items, where there is an ordering on the items. I know this is rather vague. But there is not a precise way to define the gamut of applications of search trees. In general, there are two classes of applications. Those where each item has a key value from a totally ordered universe, and those where the tree is used as an efficient way to represent an ordered list of items. Some applications of binary search trees: • Storing a set of names, and being able to lookup based on a prefix of the name. (Used in internet routers.) • Storing a path in a graph, and being able to reverse any subsection of the path in O(log n) time.
    [Show full text]
  • Search Trees for Strings a Balanced Binary Search Tree Is a Powerful Data Structure That Stores a Set of Objects and Supports Many Operations Including
    Search Trees for Strings A balanced binary search tree is a powerful data structure that stores a set of objects and supports many operations including: Insert and Delete. Lookup: Find if a given object is in the set, and if it is, possibly return some data associated with the object. Range query: Find all objects in a given range. The time complexity of the operations for a set of size n is O(log n) (plus the size of the result) assuming constant time comparisons. There are also alternative data structures, particularly if we do not need to support all operations: • A hash table supports operations in constant time but does not support range queries. • An ordered array is simpler, faster and more space efficient in practice, but does not support insertions and deletions. A data structure is called dynamic if it supports insertions and deletions and static if not. 107 When the objects are strings, operations slow down: • Comparison are slower. For example, the average case time complexity is O(log n logσ n) for operations in a binary search tree storing a random set of strings. • Computing a hash function is slower too. For a string set R, there are also new types of queries: Lcp query: What is the length of the longest prefix of the query string S that is also a prefix of some string in R. Prefix query: Find all strings in R that have S as a prefix. The prefix query is a special type of range query. 108 Trie A trie is a rooted tree with the following properties: • Edges are labelled with symbols from an alphabet Σ.
    [Show full text]
  • Trees, Binary Search Trees, Heaps & Applications Dr. Chris Bourke
    Trees Trees, Binary Search Trees, Heaps & Applications Dr. Chris Bourke Department of Computer Science & Engineering University of Nebraska|Lincoln Lincoln, NE 68588, USA [email protected] http://cse.unl.edu/~cbourke 2015/01/31 21:05:31 Abstract These are lecture notes used in CSCE 156 (Computer Science II), CSCE 235 (Dis- crete Structures) and CSCE 310 (Data Structures & Algorithms) at the University of Nebraska|Lincoln. This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License 1 Contents I Trees4 1 Introduction4 2 Definitions & Terminology5 3 Tree Traversal7 3.1 Preorder Traversal................................7 3.2 Inorder Traversal.................................7 3.3 Postorder Traversal................................7 3.4 Breadth-First Search Traversal..........................8 3.5 Implementations & Data Structures.......................8 3.5.1 Preorder Implementations........................8 3.5.2 Inorder Implementation.........................9 3.5.3 Postorder Implementation........................ 10 3.5.4 BFS Implementation........................... 12 3.5.5 Tree Walk Implementations....................... 12 3.6 Operations..................................... 12 4 Binary Search Trees 14 4.1 Basic Operations................................. 15 5 Balanced Binary Search Trees 17 5.1 2-3 Trees...................................... 17 5.2 AVL Trees..................................... 17 5.3 Red-Black Trees.................................. 19 6 Optimal Binary Search Trees 19 7 Heaps 19
    [Show full text]
  • Simple Balanced Binary Search Trees
    Simple Balanced Binary Search Trees Prabhakar Ragde Cheriton School of Computer Science University of Waterloo Waterloo, Ontario, Canada [email protected] Efficient implementations of sets and maps (dictionaries) are important in computer science, and bal- anced binary search trees are the basis of the best practical implementations. Pedagogically, however, they are often quite complicated, especially with respect to deletion. I present complete code (with justification and analysis not previously available in the literature) for a purely-functional implemen- tation based on AA trees, which is the simplest treatment of the subject of which I am aware. 1 Introduction Trees are a fundamental data structure, introduced early in most computer science curricula. They are easily motivated by the need to store data that is naturally tree-structured (family trees, structured doc- uments, arithmetic expressions, and programs). We also expose students to the idea that we can impose tree structure on data that is not naturally so, in order to implement efficient manipulation algorithms. The typical first example is the binary search tree. However, they are problematic. Naive insertion and deletion are easy to present in a first course using a functional language (usually the topic is delayed to a second course if an imperative language is used), but in the worst case, this im- plementation degenerates to a list, with linear running time for all operations. The solution is to balance the tree during operations, so that a tree with n nodes has height O(logn). There are many different ways of doing this, but most are too complicated to present this early in the curriculum, so they are usually deferred to a later course on algorithms and data structures, leaving a frustrating gap.
    [Show full text]
  • Balanced Binary Search Trees – AVL Trees, 2-3 Trees, B-Trees
    Balanced binary search trees – AVL trees, 2‐3 trees, B‐trees Professor Clark F. Olson (with edits by Carol Zander) AVL trees One potential problem with an ordinary binary search tree is that it can have a height that is O(n), where n is the number of items stored in the tree. This occurs when the items are inserted in (nearly) sorted order. We can fix this problem if we can enforce that the tree remains balanced while still inserting and deleting items in O(log n) time. The first (and simplest) data structure to be discovered for which this could be achieved is the AVL tree, which is names after the two Russians who discovered them, Georgy Adelson‐Velskii and Yevgeniy Landis. It takes longer (on average) to insert and delete in an AVL tree, since the tree must remain balanced, but it is faster (on average) to retrieve. An AVL tree must have the following properties: • It is a binary search tree. • For each node in the tree, the height of the left subtree and the height of the right subtree differ by at most one (the balance property). The height of each node is stored in the node to facilitate determining whether this is the case. The height of an AVL tree is logarithmic in the number of nodes. This allows insert/delete/retrieve to all be performed in O(log n) time. Here is an example of an AVL tree: 18 3 37 2 11 25 40 1 8 13 42 6 10 15 Inserting 0 or 5 or 16 or 43 would result in an unbalanced tree.
    [Show full text]
  • Binary Heaps
    Binary Heaps COL 106 Shweta Agrawal and Amit Kumar Revisiting FindMin • Application: Find the smallest ( or highest priority) item quickly – Operating system needs to schedule jobs according to priority instead of FIFO – Event simulation (bank customers arriving and departing, ordered according to when the event happened) – Find student with highest grade, employee with highest salary etc. 2 Priority Queue ADT • Priority Queue can efficiently do: – FindMin (and DeleteMin) – Insert • What if we use… – Lists: If sorted, what is the run time for Insert and FindMin? Unsorted? – Binary Search Trees: What is the run time for Insert and FindMin? – Hash Tables: What is the run time for Insert and FindMin? 3 Less flexibility More speed • Lists – If sorted: FindMin is O(1) but Insert is O(N) – If not sorted: Insert is O(1) but FindMin is O(N) • Balanced Binary Search Trees (BSTs) – Insert is O(log N) and FindMin is O(log N) • Hash Tables – Insert O(1) but no hope for FindMin • BSTs look good but… – BSTs are efficient for all Finds, not just FindMin – We only need FindMin 4 Better than a speeding BST • We can do better than Balanced Binary Search Trees? – Very limited requirements: Insert, FindMin, DeleteMin. The goals are: – FindMin is O(1) – Insert is O(log N) – DeleteMin is O(log N) 5 Binary Heaps • A binary heap is a binary tree (NOT a BST) that is: – Complete: the tree is completely filled except possibly the bottom level, which is filled from left to right – Satisfies the heap order property • every node is less than or equal to its children
    [Show full text]
  • B-Trees and 2-3-4 Trees Reading
    B-Trees and 2-3-4 Trees Reading: • B-Trees 18 CLRS • Multi-Way Search Trees 3.3.1 and (2,4) Trees 3.3.2 GT B-trees are an extension of binary search trees: • They store more than one key at a node to divide the range of its subtree's keys into more than two subranges. 2, 7 • Every internal node has between t and 2t children. Every internal node has one more child than key. • 1, 0 3, 6 8, 9, 11 • All of the leaves must be at same level. − We will see shortly that the last two properties keep the tree balanced. Most binary search tree algorithms can easily be converted to B-trees. The amount of work done at each node increases with t (e.g. determining which branch to follow when searching for a key), but the height of the tree decreases with t, so less nodes are visited. Databases use high values of t to minimize I/O overhead: Reading each tree node requires a slow random disk access, so high branching factors reduce the number of such accesses. We will assume a low branching factor of t = 2 because our focus is on balanced data structures. But the ideas we present apply to higher branching factors. Such B-trees are often called 2-3-4 trees because their branching factor is always 2, 3, or 4. To guarantee a branching factor of 2 to 4, each internal node must store 1 to 3 keys. As with binary trees, we assume that the data associated with the key is stored with the key in the node.
    [Show full text]
  • 2-3-4 Trees and Red- Black Trees
    CS 16: Balanced Trees 2-3-4 Trees and Red- Black Trees erm 204 CS 16: Balanced Trees 2-3-4 Trees Revealed • Nodes store 1, 2, or 3 keys and have 2, 3, or 4 children, respectively • All leaves have the same depth k b e h nn rr a c d f g i l m p s x 1 ---log()N + 1 ≤≤height log()N + 1 2 erm 205 CS 16: Balanced Trees 2-3-4 Tree Nodes • Introduction of nodes with more than 1 key, and more than 2 children 2-node: a • same as a binary node <a >a a b 3-node: • 2 keys, 3 links <a >b >a <b 4-node: a b c • 3 keys, 4 links <a >c >a >b <b <c erm 206 CS 16: Balanced Trees Why 2-3-4? • Why not minimize height by maximizing children in a “d-tree”? • Let each node have d children so that we get O(logd N) search time! Right? log d N = log N/log d • That means if d = N1/2, we get a height of 2 • However, searching out the correct child on each level requires O(log N1/2) by binary search • 2 log N1/2 = O(log N) which is not as good as we had hoped for! • 2-3-4-trees will guarantee O(log N) height using only 2, 3, or 4 children per node erm 207 CS 16: Balanced Trees Insertion into 2-3-4 Trees • Insert the new key at the lowest internal node reached in the search • 2-node becomes 3-node g d d g • 3-node becomes 4-node f d g d f g • What about a 4-node? • We can’t insert another key! erm 208 CS 16: Balanced Trees Top Down Insertion • In our way down the tree, whenever we reach a 4-node, we break it up into two 2- nodes, and move the middle element up into the parent node e e n f n d f g d g • Now we can perform the insertion using one of the f n previous two cases • Since we follow this method from the root down d e g to the leaf, it is called top down insertion erm 209 CS 16: Balanced Trees Splitting the Tree As we travel down the tree, if we encounter any 4-node we will break it up into 2-nodes.
    [Show full text]
  • Slides-Data-Structures.Pdf
    Data structures ● Organize your data to support various queries using little time an space Example: Inventory Want to support SEARCH INSERT DELETE ● Given n elements A[1..n] ● Support SEARCH(A,x) := is x in A? ● Trivial solution: scan A. Takes time Θ(n) ● Best possible given A, x. ● What if we are first given A, are allowed to preprocess it, can we then answer SEARCH queries faster? ● How would you preprocess A? ● Given n elements A[1..n] ● Support SEARCH(A,x) := is x in A? ● Preprocess step: Sort A. Takes time O(n log n), Space O(n) ● SEARCH(A[1..n],x) := /* Binary search */ If n = 1 then return YES if A[1] = x, and NO otherwise else if A[n/2] ≤ x then return SEARCH(A[n/2..n]) else return SEARCH(A[1..n/2]) ● Time T(n) = ? ● Given n elements A[1..n] ● Support SEARCH(A,x) := is x in A? ● Preprocess step: Sort A. Takes time O(n log n), Space O(n) ● SEARCH(A[1..n],x) := /* Binary search */ If n = 1 then return YES if A[1] = x, and NO otherwise else if A[n/2] ≤ x then return SEARCH(A[n/2..n]) else return SEARCH(A[1..n/2]) ● Time T(n) = O(log n). ● Given n elements A[1..n] each ≤ k, can you do faster? ● Support SEARCH(A,x) := is x in A? ● DIRECTADDRESS: Initialize S[1..k] to 0 ● Preprocess step: For (i = 1 to n) S[A[i]] = 1 ● T(n) = O(n), Space O(k) ● SEARCH(A,x) = ? ● Given n elements A[1..n] each ≤ k, can you do faster? ● Support SEARCH(A,x) := is x in A? ● DIRECTADDRESS: Initialize S[1..k] to 0 ● Preprocess step: For (i = 1 to n) S[A[i]] = 1 ● T(n) = O(n), Space O(k) ● SEARCH(A,x) = return S[x] ● T(n) = O(1) ● Dynamic problems: ● Want to support SEARCH, INSERT, DELETE ● Support SEARCH(A,x) := is x in A? ● If numbers are small, ≤ k Preprocess: Initialize S to 0.
    [Show full text]