Search Trees. Chapter 10. CSE 2011 Prof. J. Elder Last Updated: :51 PM

Similar documents
Midterm Review. EECS 2011 Prof. J. Elder - 1 -

Priority Queues & Heaps

ECE250: Algorithms and Data Structures Trees

Priority Queues & Heaps

Priority Queues & Heaps

Midterm Review. EECS 2011 Prof. J. Elder - 1 -

Comparison Sorts. EECS 2011 Prof. J. Elder - 1 -

Maps and Hash Tables. EECS 2011 Prof. J. Elder - 1 -

Maps, Hash Tables and Dictionaries

COMP : DATA STRUCTURES 2/27/14. Are binary trees satisfying two additional properties:

Chapter 8: Recursion

File Systems: Fundamentals

Text UI. Data Store Ø Example of a backend to a real Could add a different user interface. Good judgment comes from experience

Uninformed search. Lirong Xia

Contents. Bibliography 121. Index 123

Many Social Choice Rules

Computational Social Choice: Spring 2007

Minimum Spanning Tree Union-Find Data Structure. Feb 28, 2018 CSCI211 - Sprenkle. Comcast wants to lay cable in a neighborhood. Neighborhood Layout

CS 5523: Operating Systems

c M. J. Wooldridge, used by permission/updated by Simon Parsons, Spring

NEW YORK CITY COLLEGE OF TECHNOLOGY The City University of New York

Notes for Session 7 Basic Voting Theory and Arrow s Theorem

Support Vector Machines

Mathematics and Social Choice Theory. Topic 4 Voting methods with more than 2 alternatives. 4.1 Social choice procedures

CS 4407 Algorithms Greedy Algorithms and Minimum Spanning Trees

Cluster Analysis. (see also: Segmentation)

VOTING DYNAMICS IN INNOVATION SYSTEMS

Event Based Sequential Program Development: Application to Constructing a Pointer Program

Tie Breaking in STV. 1 Introduction. 3 The special case of ties with the Meek algorithm. 2 Ties in practice

TAFTW (Take Aways for the Week) APT Quiz and Markov Overview. Comparing objects and tradeoffs. From Comparable to TreeMap/Sort

Estimating the Margin of Victory for Instant-Runoff Voting

Iowa Voting Series, Paper 6: An Examination of Iowa Absentee Voting Since 2000

Graph Structurings. 16. How to Structure Large Models - Obligatory Reading. Ø T. Fischer, Jörg Niere, L. Torunski, and Albert Zündorf, 'Story

What Not To Do When Served With A Rule 45 Subpoena In The Age of E-Discovery

Random Forests. Gradient Boosting. and. Bagging and Boosting

Iowa Voting Series, Paper 4: An Examination of Iowa Turnout Statistics Since 2000 by Party and Age Group

Combating Friend Spam Using Social Rejections

Title: Adverserial Search AIMA: Chapter 5 (Sections 5.1, 5.2 and 5.3)

Introduction to Game Theory. Lirong Xia

Optimization Strategies

16. How to Structure Large Models and Programs with Graph Structurings

The Effectiveness of Receipt-Based Attacks on ThreeBallot

Constraint satisfaction problems. Lirong Xia

PLS 103 Lecture 3 1. Today we talk about the Missouri legislature. What we re doing in this section we

Thinking back to the Presidential Election in 2016, do you recall if you supported ROTATE FIRST TWO, or someone else?

Voting on combinatorial domains. LAMSADE, CNRS Université Paris-Dauphine. FET-11, session on Computational Social Choice

MATH4999 Capstone Projects in Mathematics and Economics Topic 3 Voting methods and social choice theory

Recall: Properties of ranking rules. Recall: Properties of ranking rules. Kenneth Arrow. Recall: Properties of ranking rules. Strategically vulnerable

Comparison of the Psychometric Properties of Several Computer-Based Test Designs for. Credentialing Exams

Proving correctness of Stable Matching algorithm Analyzing algorithms Asymptotic running times

Recommendations For Reddit Users Avideh Taalimanesh and Mohammad Aleagha Stanford University, December 2012

twentieth century and early years of the twenty-first century, reversed its net migration result,

Overview. Ø Neural Networks are considered black-box models Ø They are complex and do not provide much insight into variable relationships

Social welfare functions

DHSLCalc.xls What is it? How does it work? Describe in detail what I need to do

Connecting Voting Theory and Graph Theory

Proposals for the introduction of Elements of Direct Democracy in Great Britain and Northern Ireland

Measuring a Gerrymander

Batch binary Edwards. D. J. Bernstein University of Illinois at Chicago NSF ITR

The Effects of Housing Prices, Wages, and Commuting Time on Joint Residential and Job Location Choices

A well known paper, Mark Granovetter wrote about the «strength of weak ties», meaning by that the fact that unimportant relations are often the cause

Plan For the Week. Solve problems by programming in Python. Compsci 101 Way-of-life. Vocabulary and Concepts

Objective Measures of Preferential Ballot Voting Systems

Computational Social Choice: Spring 2017

Evaluating the Role of Immigration in U.S. Population Projections

HASHGRAPH CONSENSUS: DETAILED EXAMPLES

Estimating the Margin of Victory for Instant-Runoff Voting*

PATENT ATTORNEYS EXAMINATION

Voting and Complexity

Analysis of transit signals measured with Corot prior to follow-up observations

Voting Criteria April

Navigating The USPTO First Action Interview Pilot Program

CITIZENSHIP. What We Will Cover Today

Are Second-Best Tariffs Good Enough?

Backoff DOP: Parameter Estimation by Backoff

Complexity of Manipulating Elections with Few Candidates

Programming in Logic: Prolog

NEW PERSPECTIVES ON THE LAW & ECONOMICS OF ELECTIONS

Decomposition and Complexity of Hereditary History Preserving Bisimulation on BPP

Deep Learning and Visualization of Election Data

Arrow s Impossibility Theorem on Social Choice Systems

1 Aggregating Preferences

Title: Solving Problems by Searching AIMA: Chapter 3 (Sections 3.1, 3.2 and 3.3)

Accelerated Examination. Presented by Hans Troesch, Principal Fish & Richardson P.C. March 2, 2010

Voting. Suppose that the outcome is determined by the mean of all voter s positions.

Study Questions (with Answers) Lecture 10. Migration

In this lecture, we will explore weighted voting systems further. Examples of shortcuts to determining winning coalitions and critical players.

4/29/2015. Conditions for Patentability. Conditions: Utility. Juicy Whip v. Orange Bang. Conditions: Subject Matter. Subject Matter: Abstract Ideas

Restorative justice at the level of the police in England: implementing change

DOLOMITE CAPITAL LIMITED. Dated: 07 October 2014 Stock Code: DOL004 ISIN Code: ZAG

Chapter 6 MOTIONS, RESOLUTIONS AND ORDINANCES

Example 8.2 The Economics of Terrorism: Externalities and Strategic Interaction

CENTRAL CATALOGUE OF OFFICIAL DOCUMENTS OF THE REPUBLIC OF CROATIA

By Howard L. Hoffenberg The IP and Business Law Offices of Howard L. Hoffenberg, Esq.

Patents and Cold Fusion

Role of the non-proliferation regime in preventing non-state nuclear proliferation

Bank Reconciliation Script

AGENDAS AND SINCERITY: A SECOND RESPONSE TO SCHWARTZ

Improved Boosting Algorithms Using Confidence-rated Predictions

A Calculus for End-to-end Statistical Service Guarantees

Transcription:

Search Trees Chapter 1 < 6 2 > 1 4 = 8 9-1 -

Outline Ø Binary Search Trees Ø AVL Trees Ø Splay Trees - 2 -

Binary Search Trees Ø A binary search tree is a binary tree storing key-value entries at its internal nodes and satisfying the following property: q Let u, v, and w be three nodes such that u is in the left subtree of v and w is in the right subtree of v. We have key(u) key(v) key(w) Ø The tetbook assumes that eternal nodes are placeholders : they do not store entries (makes algorithms a little simpler) Ø An inorder traversal of a binary search trees visits the keys in increasing order Ø Binary search trees are ideal for maps with ordered keys. 6 2 9 1 4 8-3 -

Binary Search Tree All nodes in left subtree Any node All nodes in right subtree 38 25 51 17 31 42 63 4 21 28 35 4 49 55 71-4 -

Ø Maintain a sub-tree. Search: Loop Invariant Ø If the key is contained in the original tree, then the key is contained in the sub-tree. key 17 38 25 51 17 31 42 63 4 21 28 35 4 49 55 71-5 -

Ø Cut sub-tree in half. Search: Define Step Ø Determine which half the key would be in. Ø Keep that half. key 17 38 25 51 17 31 42 63 4 21 28 35 4 49 55 71 If key < root, then key is in left half. If key = root, then key is found If key > root, then key is in right half. - 6 -

Search: Algorithm Ø To search for a key k, we trace a downward path starting at the root Ø The net node visited depends on the outcome of the comparison of k with the key of the current node Ø If we reach a leaf, the key is not found and return of an eternal node signals this. Ø Eample: find(4): q Call TreeSearch(4,root) Algorithm TreeSearch(k, v) if T.isEternal (v) return v if k < key(v) return TreeSearch(k, T.left(v)) else if k = key(v) return v else { k > key(v) } return TreeSearch(k, T.right(v)) < 6 2 > 1 4 = 8 9-7 -

Insertion Ø To perform operation insert(k, o), we search for key k (using TreeSearch) Ø Suppose k is not already in the tree, and let w be the leaf reached by the search Ø We insert k at node w and epand w into an internal node Ø Eample: insert 5 w < 6 6 2 > 1 4 8 > 9 2 1 4 8 w 5 9-8 -

Insertion Ø Suppose k is already in the tree, at node v. Ø We continue the downward search through v, and let w be the leaf reached by the search Ø Note that it would be correct to go either left or right at v. We go left by convention. Ø We insert k at node w and epand w into an internal node Ø Eample: insert 6 2 < > 1 4 8 > 6 9 2 w 1 4 8 w 6 6 9-9 -

Deletion Ø To perform operation remove(k), we search for key k Ø Suppose key k is in the tree, and let v be the node storing k Ø If node v has an eternal leaf child w, we remove v and w from the tree with operation removeeternal(w), which removes w and its parent Ø Eample: remove 4 < 6 6 2 > 1 4 v 8 w w 5 9 2 1 5 8 9-1 -

Deletion (cont.) Ø Now consider the case where the key k to be removed is stored at a node v whose children are both internal q we find the internal node w that follows v in an inorder traversal q we copy the entry stored at w into node v q we remove node w and its left child (which must be a leaf) by means of operation removeeternal() Ø Eample: remove 3 1 2 3 v 8 1 2 5 v 8 w w 5 6 9 6 9-11 -

Performance Ø Consider a dictionary with n items implemented by means of a linked binary search tree of height h q the space used is O(n) q methods find, insert and remove take O(h) time Ø The height h is O(n) in the worst case and O(log n) in the best case Ø It is thus worthwhile to balance the tree (net topic)! - 12 -

AVL Trees v 6 3 8 4-13 -

AVL Trees Ø The AVL tree is the first balanced binary search tree ever invented. Ø It is named after its two inventors, G.M. Adelson-Velskii and E.M. Landis, who published it in their 1962 paper "An algorithm for the organiation of information. - 14 -

AVL Trees Ø AVL trees are balanced. Ø An AVL Tree is a binary search tree in which the heights of siblings can differ by at most 1. 4 44 2 3 17 78 1 2 32 5 88 1 1 1 48 62 height - 15 -

Height of an AVL Tree Ø Claim: The height of an AVL tree storing n keys is O(log n). - 16 -

Height of an AVL Tree Ø Proof: We compute a lower bound n(h) on the number of internal nodes of an AVL tree of height h. Ø Observe that n(1) = 1 and n(2) = 2 Ø For h > 2, a minimal AVL tree contains the root node, one minimal AVL subtree of height h - 1 and another of height h - 2. Ø That is, n(h) = 1 + n(h - 1) + n(h - 2) Ø Knowing n(h - 1) > n(h - 2), we get n(h) > 2n(h - 2). So n(h) > 2n(h - 2), n(h) > 4n(h - 4), n(h) > 8n(n - 6), > 2 i n(h - 2i) Ø If h is even, we let i = h/2-1, so that n(h) > 2 h/2-1 n(2) = 2 h/2 Ø If h is odd, we let i = h/2-1/2, so that n(h) > 2 h/2-1/2 n(1) = 2 h/2-1/2 Ø In either case, n(h) > 2 h/2-1 Ø Taking logarithms: h < 2log(n(h)) +2 Ø Thus the height of an AVL tree is O(log n) n(2) 3 4 n(1) - 17 -

Insertion height = 2 7 height = 3 7 1 4 Insert(2) 2 4 Problem! 1 2-18 -

Insertion Ø Imbalance may occur at any ancestor of the inserted node. height = 3 7 height = 4 7 2 4 1 8 Insert(2) 3 4 Problem! 1 8 1 3 1 5 2 3 1 5 1 2-19 -

Ø Step 1: Search Insertion: Rebalancing Strategy q Starting at the inserted node, traverse toward the root until an imbalance is discovered. height = 4 7 3 4 Problem! 1 8 2 3 1 5 1 2-2 -

Insertion: Rebalancing Strategy Ø Step 2: Repair q The repair strategy is called trinode restructuring. q 3 nodes, y and are distinguished: ² = the parent of the high sibling ² y = the high sibling ² = the high child of the high sibling q We can now think of the subtree 3 rooted at as consisting of these 3 nodes plus their 4 subtrees 1 2 2 3 height = 4 7 1 4 Problem! 8 1 5-21 -

Ø Step 2: Repair Insertion: Rebalancing Strategy q The idea is to rearrange these 3 nodes so that the middle value becomes the root and the other two becomes its children. q Thus the linear grandparent parent child structure becomes a triangular parent two children structure. q Note that must be either bigger than both and y or smaller than both and y. q Thus either or y is made the root of this subtree, and is lowered by 1. height = h h-1 y q Then the subtrees T are attached at the appropriate places. T T 1 q Since the heights of subtrees T differ by at most 1, the resulting tree is balanced. - 22 - one is & one is h-4

Insertion: Trinode Restructuring Eample Note that y is the middle value. height = h h-1 y Restructure h-1 y T T 1 T T 1 one is & one is h-4 one is & one is h-4-23 -

Insertion: Trinode Restructuring - 4 Cases Ø There are 4 different possible relationships between the three nodes, y and before restructuring: y y y y height = h height = h height = h height = h y h-1 y h-1 T y h-1 y h-1 T T 1 T T T 1 T 1 T 1 one is & one is h-4 one is & one is h-4 one is & one is h-4 one is & one is h-4-24 -

Insertion: Trinode Restructuring - 4 Cases Ø This leads to 4 different solutions, all based on the same principle. y y y y height = h height = h height = h height = h y h-1 y h-1 T y h-1 y h-1 T T 1 T T T 1 T 1 T 1 one is & one is h-4 one is & one is h-4 one is & one is h-4 one is & one is h-4-25 -

Insertion: Trinode Restructuring - Case 1 Note that y is the middle value. height = h h-1 y Restructure h-1 y T T 1 T T 1 one is & one is h-4 one is & one is h-4-26 -

Insertion: Trinode Restructuring - Case 2 height = h h-1 y y h-1 T Restructure T 1 T T 1 one is & one is h-4 one is & one is h-4-27 -

Insertion: Trinode Restructuring - Case 3 height = h h-1 h-1 y Restructure y T T T 1 T 1 one is & one is h-4 one is & one is h-4-28 -

Insertion: Trinode Restructuring - Case 4 height = h h-1 y h-1 T Restructure y T T 1 T 1 one is & one is h-4 one is & one is h-4-29 -

Insertion: Trinode Restructuring - The Whole Tree Ø Do we have to repeat this process further up the tree? Ø No! q The tree was balanced before the insertion. q Insertion raised the height of the subtree by 1. q Rebalancing lowered the height of the subtree by 1. q Thus the whole tree is still balanced. height = h h-1 y Restructure h-1 y T T 1 T T 1 one is & one is h-4 one is & one is h-4-3 -

Removal Ø Imbalance may occur at an ancestor of the removed node. height = 3 7 height = 3 7 2 4 1 8 Remove(8) 2 4 Problem! 1 3 1 5 1 3 1 5-31 -

Removal: Rebalancing Strategy Ø Step 1: Search q Let w be the node actually removed (i.e., the node matching the key if it has a leaf child, otherwise the node following in an in-order traversal. q Starting at w, traverse toward the root until an imbalance is discovered. height = 3 7 2 4 Problem! 1 3 1 5-32 -

Removal: Rebalancing Strategy Ø Step 2: Repair q We again use trinode restructuring. q 3 nodes, y and are distinguished: height = 3 7 ² = the parent of the high sibling ² y = the high sibling ² = the high child of the high sibling (if children are equally high, keep chain linear) 1 3 2 4 1 Problem! 5-33 -

Removal: Rebalancing Strategy Ø Step 2: Repair q The idea is to rearrange these 3 nodes so that the middle value becomes the root and the other two becomes its children. q Thus the linear grandparent parent child structure becomes a triangular parent two children structure. q Note that must be either bigger than both and y or smaller than both and y. q Thus either or y is made the root of this subtree, and is lowered by 1. q Then the subtrees T are attached at the appropriate places. q Although the subtrees T can differ in height by up to 2, after restructuring, sibling subtrees will differ by at most 1. T T 1 height = h h-1 y or or & h-4-34 -

Removal: Trinode Restructuring - 4 Cases Ø There are 4 different possible relationships between the three nodes, y and before restructuring: y y y y height = h height = h height = h height = h y h-1 y h-1 T y h-1 y h-1 T or T 1 or T T T 1 T 1 T 1 or & h-4 or & h-4 or & h-4 or & h-4-35 -

Removal: Trinode Restructuring - Case 1 Note that y is the middle value. height = h Restructure h or h-1 y h-1 y h-1 or T T 1 or T T 1 or & h-4 or or & h-4-36 -

Removal: Trinode Restructuring - Case 2 height = h h or h-1 y y h-1 T Restructure h-1 or T 1 or T T 1 or or & h-4 or & h-4-37 -

Removal: Trinode Restructuring - Case 3 height = h h-1 h-1 y Restructure y T T T 1 T 1 or & h-4 or & h-4-38 -

Removal: Trinode Restructuring - Case 4 height = h h-1 y h-1 T Restructure y T T 1 T 1 or & h-4 or & h-4-39 -

Ø Step 2: Repair Removal: Rebalancing Strategy q Unfortunately, trinode restructuring may reduce the height of the subtree, causing another imbalance further up the tree. q Thus this search and repair process must in the worst case be repeated until we reach the root. - 4 -

End of Lecture March 1, 212-41 -

Java Implementation of AVL Trees Ø Please see tet - 42 -

Running Times for AVL Trees Ø a single restructure is O(1) q using a linked-structure binary tree Ø find is O(log n) q height of tree is O(log n), no restructures needed Ø insert is O(log n) q initial find is O(log n) q Restructuring is O(1) Ø remove is O(log n) q initial find is O(log n) q Restructuring up the tree, maintaining heights is O(log n) - 43 -

AVLTree Eample - 44 -

Splay Trees v 6 3 8 4-45 -

Splay Trees Ø Self-balancing BST Ø Invented by Daniel Sleator and Bob Tarjan Ø Allows quick access to recently accessed elements D. Sleator Ø Bad: worst-case O(n) Ø Good: average (amortied) case O(log n) Ø Often perform better than other BSTs in practice R. Tarjan - 46 -

Splaying Ø Splaying is an operation performed on a node that iteratively moves the node to the root of the tree. Ø In splay trees, each BST operation (find, insert, remove) is augmented with a splay operation. Ø In this way, recently searched and inserted elements are near the top of the tree, for quick access. - 47 -

3 Types of Splay Steps Ø Each splay operation on a node consists of a sequence of splay steps. Ø Each splay step moves the node up toward the root by 1 or 2 levels. Ø There are 2 types of step: q Zig-Zig q Zig-Zag q Zig - 48 -

Zig-Zig Ø Performed when the node forms a linear chain with its parent and grandparent. q i.e., right-right or left-left y y T 4 ig-ig T 1 T 1 T 4-49 -

Zig-Zag Ø Performed when the node forms a non-linear chain with its parent and grandparent q i.e., right-left or left-right y ig-ag y T 1 T 4 T 1 T 4-5 -

Zig Ø Performed when the node has no grandparent q i.e., its parent is the root y ig T 4 w y w T 1 T 4 T 1-51 -

Splay Trees & Ordered Dictionaries Ø which nodes are splayed after each operation? method find(k) insert(k,v) splay node if key found, use that node if key not found, use parent of eternal node where search terminated use the new node containing the entry inserted remove(k) use the parent of the internal node w that was actually removed from the tree (the parent of the node that the removed item was swapped with) - 52 -

Recall BST Deletion Ø Now consider the case where the key k to be removed is stored at a node v whose children are both internal q we find the internal node w that follows v in an inorder traversal q we copy key(w) into node v q we remove node w and its left child (which must be a leaf) by means of operation removeeternal() Ø Eample: remove 3 which node will be splayed? 1 3 v 1 5 v 2 8 2 8 w 5 6 9 6 9-53 -

Note on Deletion Ø The tet (Goodrich, p. 463) uses a different convention for BST deletion in their splaying eample q Instead of deleting the leftmost internal node of the right subtree, they delete the rightmost internal node of the left subtree. q We will stick with the convention of deleting the leftmost internal node of the right subtree (the node immediately following the element to be removed in an inorder traversal). - 54 -

Splay Tree Eample - 55 -

Performance Ø Worst-case is O(n) q Eample: ² Find all elements in sorted order ² This will make the tree a left linear chain of height n, with the smallest element at the bottom ² Subsequent search for the smallest element will be O(n) - 56 -

Ø Average-case is O(log n) Performance q Proof uses amortied analysis q We will not cover this Ø Operations on more frequently-accessed entries are faster. q Given a sequence of m operations, the running time to access entry i is: ( ) ( ) O log m / f (i) where f(i) is the number of times entry i is accessed. - 57 -

Ø (2, 4) Trees Other Forms of Search Trees q These are multi-way search trees (not binary trees) in which internal nodes have between 2 and 4 children q Have the property that all eternal nodes have eactly the same depth. q Worst-case O(log n) operations q Somewhat complicated to implement Ø Red-Black Trees q Binary search trees q Worst-case O(log n) operations q Somewhat easier to implement q Requires only O(1) structural changes per update - 58 -

Summary Ø Binary Search Trees Ø AVL Trees Ø Splay Trees - 59 -