4a-Searching-More1 More Searching Dan Barrish-Flood.

Slides:



Advertisements
Similar presentations
Chapter 13. Red-Black Trees
Advertisements

Comp 122, Spring 2004 Binary Search Trees. btrees - 2 Comp 122, Spring 2004 Binary Trees  Recursive definition 1.An empty tree is a binary tree 2.A node.
Jan Binary Search Trees What is a search binary tree? Inorder search of a binary search tree Find Min & Max Predecessor and successor BST insertion.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
Red-Black Trees CIS 606 Spring Red-black trees A variation of binary search trees. Balanced: height is O(lg n), where n is the number of nodes.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 11.
CS Section 600 CS Section 002 Dr. Angela Guercio Spring 2010.
Binary Search Trees Briana B. Morrison Adapted from Alan Eugenio.
Lecture 12: Balanced Binary Search Trees Shang-Hua Teng.
Tirgul 10 Rehearsal about Universal Hashing Solving two problems from theoretical exercises: –T2 q. 1 –T3 q. 2.
10/22/2002CSE Red Black Trees CSE Algorithms Red-Black Trees Augmenting Search Trees Interval Trees.
1 Red-Black Trees. 2 Black-Height of the tree = 4.
B + -Trees (Part 1) Lecture 20 COMP171 Fall 2006.
B + -Trees (Part 1). Motivation AVL tree with N nodes is an excellent data structure for searching, indexing, etc. –The Big-Oh analysis shows most operations.
CSE 326: Data Structures B-Trees Ben Lerner Summer 2007.
B + -Trees (Part 2) Lecture 21 COMP171 Fall 2006.
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.
4-Searching1 Searching Dan Barrish-Flood. 4-Searching2 Dynamic Sets Manage a changing set S of elements. Every element x has a key, key[x]. Operations:
David Luebke 1 7/2/2015 ITCS 6114 Red-Black Trees.
B + -Trees (Part 2) COMP171. Slide 2 Review: B+ Tree of order M and of leaf size L n The root is either a leaf or 2 to M children n Each (internal) node.
AVL Trees / Slide 1 Deletion  To delete a key target, we find it at a leaf x, and remove it. * Two situations to worry about: (1) target is a key in some.
Red-Black Trees Lecture 10 Nawazish Naveed. Red-Black Trees (Intro) BSTs perform dynamic set operations such as SEARCH, INSERT, DELETE etc in O(h) time.
Advanced Data Structures and Algorithms COSC-600 Lecture presentation-6.
1 B Trees - Motivation Recall our discussion on AVL-trees –The maximum height of an AVL-tree with n-nodes is log 2 (n) since the branching factor (degree,
Balanced Binary Search Trees
David Luebke 1 9/18/2015 CS 332: Algorithms Red-Black Trees.
Chapter 12. Binary Search Trees. Search Trees Data structures that support many dynamic-set operations. Can be used both as a dictionary and as a priority.
© 2014 by Ali Al Najjar Introduction to Algorithms Introduction to Algorithms Red Black Tree Dr. Ali Al Najjar Day 18 L10.1.
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
October 19, 2005Copyright © by Erik D. Demaine and Charles E. LeisersonL7.1 Introduction to Algorithms 6.046J/18.401J LECTURE 10 Balanced Search.
Introduction to Algorithms Jiafen Liu Sept
Red-Black Trees Red-black trees: –Binary search trees augmented with node color –Operations designed to guarantee that the height h = O(lg n)
Computer Algorithms Submitted by: Rishi Jethwa Suvarna Angal.
Mudasser Naseer 1 10/20/2015 CSC 201: Design and Analysis of Algorithms Lecture # 11 Red-Black Trees.
CSIT 402 Data Structures II
Lecture 10 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
1 Balanced Trees There are several ways to define balance Examples: –Force the subtrees of each node to have almost equal heights –Place upper and lower.
2IL50 Data Structures Fall 2015 Lecture 7: Binary Search Trees.
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.
Binary SearchTrees [CLRS] – Chap 12. What is a binary tree ? A binary tree is a linked data structure in which each node is an object that contains following.
A Study of Balanced Search Trees: Brainstorming a New Balanced Search Tree Anthony Kim, 2005 Computer Systems Research.
Outline Binary Trees Binary Search Tree Treaps. Binary Trees The empty set (null) is a binary tree A single node is a binary tree A node has a left child.
1 Searching Searching in a sorted linked list takes linear time in the worst and average case. Searching in a sorted array takes logarithmic time in the.
CS 473Lecture X1 CS473-Algorithms Lecture RED-BLACK TREES (RBT)
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 9.
Lecture 9 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
Lecture 11COMPSCI.220.FS.T Balancing an AVLTree Two mirror-symmetric pairs of cases to rebalance the tree if after the insertion of a new key to.
Week 8 - Wednesday.  What did we talk about last time?  Level order traversal  BST delete  2-3 trees.
Tree Traversals, TreeSort 20 February Expression Tree Leaves are operands Interior nodes are operators A binary tree to represent (A - B) + C.
Chapter 4: Trees Part I: General Tree Concepts Mark Allen Weiss: Data Structures and Algorithm Analysis in Java.
1 Chapter 7 Objectives Upon completion you will be able to: Create and implement binary search trees Understand the operation of the binary search tree.
Red-Black Trees. Review: Binary Search Trees ● Binary Search Trees (BSTs) are an important data structure for dynamic sets ● In addition to satellite.
Binary Search Trees (BST)
October 19, 2005Copyright © by Erik D. Demaine and Charles E. LeisersonL7.1 Introduction to Algorithms LECTURE 8 Balanced Search Trees ‧ Binary.
Binary Search Trees.  Understand tree terminology  Understand and implement tree traversals  Define the binary search tree property  Implement binary.
1 Joe Meehean. A A B B D D I I C C E E X X A A B B D D I I C C E E X X  Terminology each circle is a node pointers are edges topmost node is the root.
CMSC 202, Version 5/02 1 Trees. CMSC 202, Version 5/02 2 Tree Basics 1.A tree is a set of nodes. 2.A tree may be empty (i.e., contain no nodes). 3.If.
1 Binary Search Trees  Average case and worst case Big O for –insertion –deletion –access  Balance is important. Unbalanced trees give worse than log.
Trees By JJ Shepherd. Introduction Last time we discussed searching and sorting in a more efficient way Divide and Conquer – Binary Search – Merge Sort.
Trees CSIT 402 Data Structures II 1. 2 Why Do We Need Trees? Lists, Stacks, and Queues are linear relationships Information often contains hierarchical.
Binary Search Trees What is a binary search tree?
CS 332: Algorithms Red-Black Trees David Luebke /20/2018.
Red Black Trees
B+-Trees.
B+-Trees.
Binary Trees, Binary Search Trees
Binary Trees, Binary Search Trees
Binary SearchTrees [CLRS] – Chap 12.
Binary Trees, Binary Search Trees
Presentation transcript:

4a-Searching-More1 More Searching Dan Barrish-Flood

4a-Searching-More2 Running time of Dynamic Set Operations we’ve seen that all our dynamic set operations can be done in Θ(h) time (worst-case) on a binary search tree of height h. a binary search tree of n nodes has height Θ(lgn) so all dynamic set operations take Θ(lgn) time on a binary search tree. right? WRONG! insert these numbers to form a binary search tree, in this order: 1, 2, 3, 4, 5

4a-Searching-More3 Binary Search Tree Random Insertion & Deletion What is the average depth of a BST after n insertions of random values? The root is equally likely to be the smallest, 2 nd smallest,..., largest, of the n values. So with equal probability, the two subtrees are of sizes 1 & n-2 (not n-1, don’t forget the root!) 2 & n-3... n-3 & 2 n-2 & 1 Wait til you see the next slide!...

4a-Searching-More4 Random Insertion/Deletion (Cont’d) The analysis is identical to Quicksort! The root key is like the pivot. So n insertions take O(nlgn) on average, thus each insertion takes O(lgn), which is the tree height.

4a-Searching-More5 Balanced Search Trees Careful, I may have used BST in the past for “binary search tree” not “balanced search tree”, I will try to avoid this confusion! The general idea (there are exceptions) –do extra work during INSERT and DELETE to ensure the tree’s height is Θ(lgn) –the rest of the dynamic set operations are unchanged. Two examples: –Red-Black Trees (CLRS ch. 13) elegant definition wicked hairy insert/delete –2-3 Trees simpler to understand not true binary trees

4a-Searching-More6 a taste of Red-Black Trees a Red-Black tree is a binary search tree where 1.the root is BLACK 2.each node is colored RED or BLACK 3.every leaf is black 4.each of a red node’s children are black 5.every path from a node to one of its descendant leaves contains the same number of black nodes (a minor twist: we consider the NIL’s as leaves, and all the nodes with keys as internal nodes) the height of a node (for trees in general) is the # of edges on the longest downward path from that node to a leaf.

4a-Searching-More7

8 RBT, each leaf (NIL) is black the tiny number next to a node is its “black- height”

4a-Searching-More9 Dynamic Set Operations as implemented with RBT’s It’s not that hard to prove (by induction) that the RBT properties imply: a RBT with n internal nodes has height ≤ 2 lg(n+1) don’t worry about the proof, but see CLRS p. 274 if you’re interested in the details The height of a RBT is O(lgn), so all dynamic set operations run in O(lgn) time. But wait! INSERT, DELETE may destroy the RBT property! But, it turns out, these two operations can indeed be supported in O(lgn) time, too.

4a-Searching-More10 More on tree-traversal, here is pre-order traversal (note this is not a binary tree) First “process” (e.g. print) the root (hence pre), then recursively process the root’s left subtree(s), then recursively process the root’s right subtree(s). For this tree, we get 1,2,3,...,9

4a-Searching-More11 More on tree traversal, here is post-order traversal (note this is not a binary tree) First recursively process the root’s left subtree(s), then recursively process the root’s right subtree(s), then lastly (hence post) “process” (e.g. print) the root. For this tree, we again get 1,2,3,...,9

4a-Searching-More Trees Another kind of Balanced Search Tree What are the structural requirements: –every non-leaf (i.e. internal) node has exactly 2 or 3 children –all leaves are at the same level –here are three 2-3 trees (not showing keys!)

4a-Searching-More Trees, more structural info the “thinnest” 2-3 tree is a complete binary tree (see picture on prev. slide) the “fattest” 2-3 tree is a complete 3-ary tree (again see prev. slide). If the tree has height h, (recall all leaves are at the same level), the number of leaves, l, is between 2 h and 3 h. A 2-3 tree of n nodes (total) has a height between log 3 n and log 2 n, and since log 3 n = log 3 2 x log 2 n (why?), the height h is guaranteed to be within a small constant factor of log 2 n.

4a-Searching-More14 Where is the data in a 2-3 tree? All the records (keys, other satellite data) are in the leaves. The records are in sorted order. Each internal node has one or two guides: the greatest value in its leftmost one (or two) subtree(s).

4a-Searching-More15 Searching in a 2-3 Tree : Suppose I’m searching the above 2- 3 tree (the one with the guides 8, 29 in the root) for the key 27. Start at the root. 27 ≤ 8? No, so forget about the root’s leftmost subtree. 27 ≤ 29? Yes, so we know our target (if it exists) is in the root’s middle subtree. Thus proceed to the node directly below the root? 27 ≤ 11? No. So forget about this node’s leftmost subtree. 27 ≤ 21? No, so we know our target (if it exists) must be in this node’s rightmost subtree. So follow our current node’s rightmost pointer down. We’re at a leaf (the good stuff is in here!), and we find our target 27 stored in this leaf. We use this method to always reach the “right” leaf, where we will either find our target in that leaf, or find that our target does not exist. SEARCH(2-3_tree, key) clearly, because of the height of the tree, runs in worst-case time O(lg n). I don’t have any good pseudo-code (maybe I’ll make or find some) for this routine.

4a-Searching-More Tree Insert (let’s insert 15 into the prev. tree) tree has increased in height!

4a-Searching-More Tree Delete; let’s delete 10 from the 1 st tree to get the 2 nd one (one method). here we stole 11 from a sibling

4a-Searching-More Tree Delete; let’s delete 10 from the 1 st tree to get the 2 nd one (other method). here we merged nodes

4a-Searching-More Tree Delete; let’s delete 3 from the 1 st tree to get the 3 rd, there’s just one way). again, we stole from a sibling

4a-Searching-More Tree Delete; let’s delete 5 tree loses a level!