Binary search trees Definition Binary search trees and dynamic set operations Balanced binary search trees –Tree rotations –Red-black trees Move to front.

Slides:



Advertisements
Similar presentations
Chapter 13. Red-Black Trees
Advertisements

Chapter 4: Trees Part II - AVL Tree
AVL Trees COL 106 Amit Kumar Shweta Agrawal Slide Courtesy : Douglas Wilhelm Harder, MMath, UWaterloo
AVL Tree Smt Genap Outline AVL Tree ◦ Definition ◦ Properties ◦ Operations Smt Genap
Search Trees: BSTs and B-Trees David Kauchak cs302 Spring 2013.
AA Trees another alternative to AVL trees. Balanced Binary Search Trees A Binary Search Tree (BST) of N nodes is balanced if height is in O(log N) A balanced.
Balanced Binary Search Trees
Balanced Search Trees. 2-3 Trees Trees Red-Black Trees AVL Trees.
A balanced life is a prefect life.
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.
CSE332: Data Abstractions Lecture 7: AVL Trees Tyler Robison Summer
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.
Data Types and Data Structures
10/22/2002CSE Red Black Trees CSE Algorithms Red-Black Trees Augmenting Search Trees Interval Trees.
Trees and Red-Black Trees Gordon College Prof. Brinton.
Dynamic Set AVL, RB Trees G.Kamberova, Algorithms Dynamic Set ADT Balanced Trees Gerda Kamberova Department of Computer Science Hofstra University.
Self-Balancing Search Trees Chapter 11. Chapter 11: Self-Balancing Search Trees2 Chapter Objectives To understand the impact that balance has on the performance.
Fall 2007CS 2251 Self-Balancing Search Trees Chapter 9.
Self-Balancing Search Trees Chapter 11. Chapter Objectives  To understand the impact that balance has on the performance of binary search trees  To.
0 Course Outline n Introduction and Algorithm Analysis (Ch. 2) n Hash Tables: dictionary data structure (Ch. 5) n Heaps: priority queue data structures.
CS 61B Data Structures and Programming Methodology Aug 11, 2008 David Sun.
Advanced Data Structures and Algorithms COSC-600 Lecture presentation-6.
Balanced Trees Ellen Walker CPSC 201 Data Structures Hiram College.
Compiled by: Dr. Mohammad Alhawarat BST, Priority Queue, Heaps - Heapsort CHAPTER 07.
CS Data Structures Chapter 10 Search Structures.
October 19, 2005Copyright © by Erik D. Demaine and Charles E. LeisersonL7.1 Introduction to Algorithms 6.046J/18.401J LECTURE 10 Balanced Search.
Course: Programming II - Abstract Data Types Red-Black TreesSlide Number 1 Balanced Search Trees Binary Search Tree data structures can allow insertion,
Analysis of Red-Black Tree Because of the rules of the Red-Black tree, its height is at most 2log(N + 1). Meaning that it is a balanced tree Time Analysis:
Introduction to Algorithms Jiafen Liu Sept
Balancing Binary Search Trees. Balanced Binary Search Trees A BST is perfectly balanced if, for every node, the difference between the number of nodes.
Computer Algorithms Submitted by: Rishi Jethwa Suvarna Angal.
CSIT 402 Data Structures II
Chapter 13 B Advanced Implementations of Tables – Balanced BSTs.
Balanced Search Trees Fundamental Data Structures and Algorithms Margaret Reid-Miller 3 February 2005.
Data Structures Balanced Trees 1CSCI Outline  Balanced Search Trees 2-3 Trees Trees Red-Black Trees 2CSCI 3110.
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.
2-3 Tree. Slide 2 Outline  Balanced Search Trees 2-3 Trees Trees.
1 Trees 4: AVL Trees Section 4.4. Motivation When building a binary search tree, what type of trees would we like? Example: 3, 5, 8, 20, 18, 13, 22 2.
Fall 2006 CSC311: Data Structures 1 Chapter 10: Search Trees Objectives: Binary Search Trees: Search, update, and implementation AVL Trees: Properties.
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.
Oct 26, 2001CSE 373, Autumn A Forest of Trees Binary search trees: simple. –good on average: O(log n) –bad in the worst case: O(n) AVL trees: more.
Binary Search Trees (BSTs) 18 February Binary Search Tree (BST) An important special kind of binary tree is the BST Each node stores some information.
Balanced Search Trees Chapter 19 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
Red-Black Trees. Review: Binary Search Trees ● Binary Search Trees (BSTs) are an important data structure for dynamic sets ● In addition to satellite.
October 19, 2005Copyright © by Erik D. Demaine and Charles E. LeisersonL7.1 Introduction to Algorithms LECTURE 8 Balanced Search Trees ‧ Binary.
Red-Black trees Red-black trees are trees represented as binary trees at the expense of one extra bit per node. The idea is to represent 3- and 4-nodes.
1 Binary Search Trees  Average case and worst case Big O for –insertion –deletion –access  Balance is important. Unbalanced trees give worse than log.
AVL Trees AVL (Adel`son-Vel`skii and Landis) tree = – A BST – With the property: For every node, the heights of the left and right subtrees differ at most.
Balancing Binary Search Trees. Balanced Binary Search Trees A BST is perfectly balanced if, for every node, the difference between the number of nodes.
CSE332: Data Abstractions Lecture 7: AVL Trees
Lecture 15 Nov 3, 2013 Height-balanced BST Recall:
AA Trees.
Binary search trees Definition
Lec 13 Oct 17, 2011 AVL tree – height-balanced tree Other options:
Red-Black Tree Neil Tang 02/04/2010
Balancing Binary Search Trees
Red Black Trees
CO4301 – Advanced Games Development Week 10 Red-Black Trees continued
Lecture 25 Splay Tree Chapter 10 of textbook
Chapter 22 : Binary Trees, AVL Trees, and Priority Queues
Monday, April 16, 2018 Announcements… For Today…
Lecture 9 Algorithm Analysis
Lecture 9 Algorithm Analysis
Lecture 9 Algorithm Analysis
CSE373: Data Structures & Algorithms Lecture 5: AVL Trees
Lecture 10 Oct 1, 2012 Complete BST deletion Height-balanced BST
Presentation transcript:

Binary search trees Definition Binary search trees and dynamic set operations Balanced binary search trees –Tree rotations –Red-black trees Move to front “balancing” technique –Unsorted linked lists –Splay trees

Binary search trees Basic tree property –For any node x left subtree has nodes ≤ x right subtree has nodes ≥ x

BSTs and Dynamic Sets Dynamic set operations and binary search trees –Search(S,k) –Insert(S,x) –Delete(S,x) –Minimum or Maximum(S) –Successor or Predecessor (S,x) –List All(S) –Merge(S 1,S 2 )

Dynamic Set Operations Listall(T)? –time to list? Search(T,k)? –search time? Minimum(T)? Maximum(T)? –search time? Successor(T,x)? Predecessor(T,x)? –Search time Simple Insertion(T,x)

Simple deletion Delete(T,x): Three possible cases: –a) x is a leaf : –b) x has one child : –c) x has two children : Replace x with successor(x). Successor(x) has at most one child (why?); Use step a or b on successor(x)

Simple binary search trees What is the expected height of a binary search tree? Difficult to compute if we allow both insertions and deletions With insertions, analysis of section 12.4 shows that expected height is O(log n) Implications about BSTs as dynamic sets?

Tree-Balancing Algorithms Tree rotations Red-Black Trees Splay Trees Others –AVL Trees –2-3 Trees and Trees

Tree Rotations A B T1 T2T3 B A T1T2 Right Rotate(T,A) Left Rotate(T,B)

Red-Black Trees All nodes in the tree are either red or black. Every null-child is included and colored black. All red nodes must have two black children. Every path from any node x (including the root) to a leaf must have the same number of black nodes. How balanced of a tree will this produce? How hard will it be to maintain?

Example Red-Black Tree

Insertion(T,z) Find place to insert using simple insertion Color node z as red Fix up tree so that it is still a red-black tree What needs to be fixed? –Problem 1: z is root (first node inserted) Minor detail –Problem 2: parent(z) is red

RB-Insert-Fixup Situation: parent(z) is red and z is red –Case 1: uncle(z) is red Then make both uncle(z) and parent(z) black and p(p(z)) red and recurse up tree z z

RB-Insert-Fixup (parent(z) is right child of parent(parent(z))) Situation: parent(z) is red and z is red –Case 2: uncle(z) is black and z is a left child Right rotate to make into case 3 A B T1 T2T3 z B A T1T2 z

RB-Insert-Fixup (parent(z) is right child of parent(parent(z))) Situation: parent(z) is red and z is red –Case 3: uncle(z) is black and z is a left child Left rotate to make B root of tree A B C T1 T2T3 z A B C T1 T2T3

RB-Insert-Fixup Analysis (parent(z) is right child of parent(parent(z))) Situation: parent(z) is red and z is red –Case 1: no rotations, always moving up tree –Cases 2 and 3: At most 2 rotations total and tree ends up balanced No more need to fix up once these cases are met –Total cost: at most 2 rotations and log n operations

Delete(T,z) Find node y to delete using simple deletion Let x be a child of y if such a child exists (otherwise x is a null child) If y is black, fix up tree so that it is still a red- black tree What needs to be fixed? –Problem 1: y was root, so now we might have red root –Problem 2: x and parent(y) are both red –Problem 3: Removal of y violates black height properties of paths that used to go through y

Move To Front (MTF) Technique A powerful “balancing” mechanism is the “move to front” idea This technique is effective in managing both unsorted lists and binary search trees The idea: Whenever an item is accessed (by search or by insertion), it is always moved to the front –In a list, the front is well-defined –In a binary search tree, the front of the tree is the root –A tree that implements this idea is called a splay tree Rotations are not simple single rotations but occur in pairs We give some intuition about the power of MTF

Splay Tree Example

Effectiveness in lists Reference: Amortized efficiency of list update and paging rules, Sleator and Tarjan, CACM 1985 Problem statement: –Suppose you are maintaining an unsorted list where every search must progress from the front of the list to the item (or end of list if search is unsuccessful) –Operations: search, insert, delete Costs: finding or deleting the ith item costs i Inserting a new item costs n+1 Immediately after an insertion or search of an item i, item i may be moved anywhere closer to the front of the list at no extra cost Goal: Find a way to manage list that minimizes total cost of a sequence of operations

Notation for computing costs S: sequence of requests –(insertions, deletions, searches) A: any algorithm for maintaining list –including those those that know S in advance c A (S): cost incurred by algorithm A on sequence S not including paid exchanges x A (S): # of paid exchanges for A on S f A (S): # of free exchanges for A on S Example: –List: 5, 9, 2, 7, 3, 6 and we search for 7 –MTF then has list with 7, 5, 9, 2, 3, 6 –c MTF (S) increases by 4 –x MTF (S) increases by 0 since moving 7 to the front is a free –f MTF (S) increases by 3 since we made 3 free exchanges to move 7

Performance of MTF Thm: For any algorithm A and any sequence S x MTF (S) + c MTF (S) ≤ 2c A (S) + x A (S) – F A (S) – m Observation: x MTF (S) = 0 Interpretation –MTF incurs at most twice the cost of any other algorithm, even those that know the request sequence in advance

Direct Cost Comparison The ideal approach to proving this result is that for each operation, MTF incurs a cost that is at most twice that of algorithm A However, this is clearly not always true Example just before t th operation search(1): –A’s list: 1, 20, 7, 9, 3, 5, 24, 4, 8 A’s cost is just 1 –MTF’s list: 5, 24, 8, 3, 9, 7, 20, 4, 1 MTF’s cost is 9 How can this happen? –Well, since last access to item 1, items 5, 24, 8, 3, 9, 7, 20 and 4 have been accessed. –Thus, A must have done some extra work since last access to 1 in order to have 1 at the current front of the list –This leads to ideas of potential function and amortized analysis

Potential Function Φ Consider A, MTF, and S Let t be the number of operations performed so far (0 ≤ t ≤ |S|) For any t, we define Φ(t) to be the number of inversions between A’s list and MTF’s list –Inversion: a pair of elements x,y s.t. x appears before y in one list and y appears before x in the other list Example –MTF: 1, 7, 2, 5 –A: 2, 1, 5, 7 –Inversions: (1,2), (2,7), (5,7)

Amortized Cost Cost c A (t) is the cost of the t th operation for algorithm A Amortized cost a A (t) of the t th operation is c A (t) + Φ(t) - Φ(t-1) –Cost of t th operation + change in potential fct Key observation Σ t a MTF (t) = Σ t c MTF (t) + Φ(t) - Φ(t-1) = Φ(|S|) - Φ(0) + Σ t c MTF (t) Thus, c MTF (S) = Σ t c MTF (t) = Φ(0) - Φ(|S|) + Σ t a MTF (t) Thus, c MTF (S) ≤ Σ t a MTF (t) Note Φ(0) = 0 and Φ(|S|) ≥ 0

Amortized Cost Comparison Our revised goal is to show that MTF incurs an amortized cost that is at most twice that of algorithm A Example just before t th operation search(1): –A’s list: 1, 20, 7, 9, 3, 5, 24, 4, 8 A’s cost is just 1 –MTF’s list: 5, 24, 8, 3, 9, 7, 20, 4, 1 –MTF’s list afterwards: 1, 5, 24, 8, 3, 9, 7, 20, 4, 1 MTF’s direct cost is 9 The change in potential function is -8 as 8 inversions (all involving 1) are eliminated after 1 is moved to the front of the list MTF’s amortized cost is 1

Amortized Cost Comparison Cont’d General case –We are searching for x which is at position i in A’s list and k in MTF’s list Direct costs –A’s cost to access x is then i –MTF’s cost to access x is k Potential function changes –Let y be the number of items that precede x in MTF’s list but follow x in A’s list –This means k-y-1 items precede x in both lists Note k-y ≤ i. Why? –After x is moved to front of MTF’s list y inversions are eliminated k-y-1 inversions are created –Thus potential function change is k-2y-1 MTF’s amortized cost is thus: k + (k-2y-1) = 2(k-y) -1 ≤ 2i-1 Similar analysis holds for other operations

Splay Tree Performance Analysis of splay trees also uses a potential function and amortized analysis Individual operations may take O(n) time However, it can be shown that any sequence of m operations including n insertions starting with an empty tree take O(m log n) time Static optimality theorem –For any sequence of access operations, a splay tree is asymptotically as efficient as the optimum static search tree (that cannot perform any rotations)

Dynamic Optimality Conjecture Splay trees are as asymptotically fast on any sequence of operations as any other type of search tree with rotations. What does this mean? –Worst case sequence of splay tree operations takes amortized O(log n) time per operation –Some sequences of operations take less. Accessing the same ten items over and over again –Splay tree should then take less on these sequences as well. One special case that has been proven: –search in order from the smallest key to the largest key –the total time for all n operations is O(n)