Chapter 12&13: Binary Search Trees (BSTs)

Slides:



Advertisements
Similar presentations
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.
Advertisements

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.
Analysis of Algorithms CS 477/677 Binary Search Trees Instructor: George Bebis (Appendix B5.2, Chapter 12)
Binary Search Trees Many of the slides are from Prof. Plaisted’s resources at University of North Carolina at Chapel Hill.
Introduction to Algorithms Jiafen Liu Sept
ALGORITHMS THIRD YEAR BANHA UNIVERSITY FACULTY OF COMPUTERS AND INFORMATIC Lecture six Dr. Hamdy M. Mousa.
The complexity and correctness of algorithms (with binary trees as an example)
David Luebke 1 5/4/2015 Binary Search Trees. David Luebke 2 5/4/2015 Dynamic Sets ● Want a data structure for dynamic sets ■ Elements have a key and satellite.
Binary Search Trees Comp 550.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 10.
1 Brief review of the material so far Recursive procedures, recursive data structures –Pseudocode for algorithms Example: algorithm(s) to compute a n Example:
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 12.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 11.
Lecture 12: Balanced Binary Search Trees Shang-Hua Teng.
Sorting. How fast can we sort? All the sorting algorithms we have seen so far are comparison sorts: only use comparisons to determine the relative order.
Analysis of Algorithms CS 477/677 Red-Black Trees Instructor: George Bebis (Chapter 14)
1.1 Data Structure and Algorithm Lecture 12 Binary Search Trees Topics Reference: Introduction to Algorithm by Cormen Chapter 13: Binary Search Trees.
Data Structures, Spring 2006 © L. Joskowicz 1 Data Structures – LECTURE Binary search trees Motivation Operations on binary search trees: –Search –Minimum,
13. Red-Black Tree Hsu, Lih-Hsing. Computer Theory Lab. Chapter 13P.2 One of many search-tree schemes that are “ balanced ” in order to guarantee that.
Design & Analysis of Algorithms Unit 2 ADVANCED DATA STRUCTURE.
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.
Red-Black Trees Many of the slides are from Prof. Plaisted’s resources at University of North Carolina at Chapel Hill.
Mudasser Naseer 1 10/20/2015 CSC 201: Design and Analysis of Algorithms Lecture # 11 Red-Black Trees.
Red-Black Trees Comp 550.
Chapter 13 Red-Black Trees Lee, Hsiu-Hui Ack: This presentation is based on the lecture slides from Hsu, Lih-Hsing, as well as various materials from the.
Lecture 10 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
2IL50 Data Structures Fall 2015 Lecture 7: Binary Search Trees.
October 3, Algorithms and Data Structures Lecture VII Simonas Šaltenis Nykredit Center for Database Research Aalborg University
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.
Red Black Tree Essentials Notes from “Introduction to Algorithms”, Cormen et al.
1 Algorithms CSCI 235, Fall 2015 Lecture 22 Binary Search Trees.
Red-Black Trees. Review: Binary Search Trees ● Binary Search Trees (BSTs) are an important data structure for dynamic sets ● In addition to satellite.
Lecture 19. Binary Search Tree 1. Recap Tree is a non linear data structure to present data in hierarchical form. It is also called acyclic data structure.
Red Black Trees. History The concept of a balancing tree was first invented by Adel’son-Vel’skii and Landis in They came up with the AVL tree. In.
Fundamentals of Algorithms MCS - 2 Lecture # 17. Binary Search Trees.
Lecture 91 Data Structures, Algorithms & Complexity Insertion and Deletion in BST GRIFFITH COLLEGE DUBLIN.
Mudasser Naseer 1 1/25/2016 CS 332: Algorithms Lecture # 10 Medians and Order Statistics Structures for Dynamic Sets.
Analysis of Algorithms CS 477/677 Red-Black Trees Instructor: George Bebis (Chapter 14)
Sept Red-Black Trees What is a red-black tree? -node color: red or black - nil[T] and black height Subtree rotation Node insertion Node deletion.
Binary Search Trees What is a binary search tree?
Balanced Search Trees Modified from authors’ slides.
CS 332: Algorithms Red-Black Trees David Luebke /20/2018.
Red Black Trees
Red-Black Trees.
Red-Black Trees.
Summary of General Binary search tree
Lecture 7 Algorithm Analysis
Red-Black Trees Motivations
Ch. 12: Binary Search Trees Ming-Te Chi
CS200: Algorithms Analysis
Red-Black Trees.
CMSC 341 (Data Structures)
Lecture 9 Algorithm Analysis
Red Black Tree Essentials
Lecture 9 Algorithm Analysis
Lecture 9 Algorithm Analysis
Lecture 7 Algorithm Analysis
Algorithms and Data Structures Lecture VII
CS 583 Analysis of Algorithms
Red Black Tree Essentials
Lecture 7 Algorithm Analysis
Topic 6: Binary Search Tree Data structure Operations
Design and Analysis of Algorithms
Analysis of Algorithms CS 477/677
Algorithms, CSCI 235, Spring 2019 Lecture 22—Red Black Trees
Binary Search Trees Comp 122, Spring 2004.
Red-Black Trees CS302 Data Structures
Presentation transcript:

Chapter 12&13: Binary Search Trees (BSTs) Overview: Definition of dynamic sets Operations on dynamic sets Definition of BST Examples of balanced and unbalanced BSTs support dynamic-set operations T(n) proportional to height of tree Extensions, like Red-Black trees, ensure balance

Basic Definitions dynamic sets = sets that change due to manipulations performed by algorithms. dictionary = dynamic set that supports insertion, deletion, and membership testing. (priority queue is a dictionary) elements of dynamic set = objects with fields that can be examined and manipulated key = a field of an object that identifies it For any 2 keys a and b, either a<b, a=b, or a>b allows relationships to be defined between set members. (member with smallest key, member with next larger key, etc.) satellite data = information not used in searching queries = operations on sets that only extract information

Operations on dynamic sets Search(S,k) = query that returns a pointer x if there exist an element of S such that key[x] = k. Otherwise returns NIL Insert(S,x) = add element with pointer x to S Delete(S,x) = removes element with pointer x from S Minimum(S) = query that returns a pointer to the element of S with the smallest key Maximum(S) = query that returns a pointer to the element of S with the largest key Successor(S,x) = query that returns a pointer to the element with a key that is the next larger than the key(x) Predecessor(S,x) = query that returns a pointer to the element with a key that is the next smaller than the key(x)

Binary Search Trees Binary search trees (BSTs) are data structures that support dictionary operations (Search, Insert and Delete), as well as Minimum, Maximum, Successor, and Predecessor Running time is proportional to h = height of tree (number of edges in longest path from root to a leaf) Balanced BST with n nodes has h ~ lg(n). Extreme case of unbalanced tree is chain of nodes  h = n Performance of codes that use BSTs can be as good as O(lg(n)) or as bad as O(n) Red-Black BSTs are one of many “balanced” search-tree schemes that guarantee O(lg(n)) time for dynamic-set operations in the worst case

Binary-Search-Tree Property BST is a linked data structure Each node represents an object with a key (displayed) and satellite data (not shown) Nodes also contains fields left, right and p that point to left child, right child and parent, respectively (Contain NIL if a child or parent is missing) Root is only node with parent missing For any node x, keys in left subtree of x are no larger than key[x] and keys in right subtree of x are no smaller than key[x]

Examples of BSTs h = number of edges in longest path from root to a leaf h depends on which key is chosen for the root 5 3 2 Keys of BST are sorted to satisfy the BST property: For any node x, keys in left subtree of x are no larger than key[x] and keys in right subtree of x are no smaller than key[x] 5

See text for pseudo codes for Search, Min, Max Successor and Predecessor using BSTs. Lectures focus on Insert and Delete.

Insertion and Deletion When a nodes is added and deleted (as in operation of a priority queue), the binary search tree property must be maintained In algorithm Tree-Insert(T,z), T denotes the tree to which node z is add The key[z] determines where z should be placed in the tree z will be inserted as a leaf to some node y = p(z)  left[z] = right [z] = NIL After finding y, update the fields of y depending on whether z is a left or right child of y

Tree-Insert(T,z) x  root[T] (x will trace a path down tree to the place where z is inserted) y  NIL (y is maintained as the parent of x) while x  NIL (fails when place to insert z has been found) do y  x (candidate for p(z)) if key[z] < key[x] (get new assignment of x) then x  left[x] else x  right[x] (now we have found the parent of z) p[z]  y if y = NIL (T was empty when z was inserted) then root[T]  z else if key[z] < key[y] (update fields of p(z)) then left[y]  z else right[y]  z

Tree-Insert(T,z) x  root[T] y  NIL while x  NIL do y  x if key[z] < key[x] then x  left[x] else x  right[x] p[z]  y if y = NIL then root[T]  z else if key[z] < key[y] then left[y]  z else right[y]  z

Example: Insert a node with key = 13

Tree-Delete(T, z) has 3 cases case 1: z has no children modify p(z) by replacing z by NIL case 2: z has one child splice out z by making a link between p(z) and it’s child case 3: z has two children splice out z’s successor, y. copy key[y] into z How do I find z’s successor?

Successor node with key=15? Successor node with key=6? If the right[x] is not NIL, then successor of x is its left-most node (min of subtree) If right[x] = NIL, walk up the tree to find the lowest ancestor that whose left child is also an ancestor of x (any node on path from root to x). Successor node with key=15? Successor node with key=6? Successor node with key=4? Successor node with key=13?

If the right[x] is not NIL, then successor of x is its left-most node (min of subtree) If right[x] = NIL, walk up the tree to find the lowest ancestor that whose left child is also an ancestor of x (any node on path from root to x). Successor node with key=15 is node with key=17 (min of right subtree) Successor node with key=6 is node with key=7 (min of right subtree) Successor node with key=4 is node with key=6 Successor node with key=13 is node with key=15

Case 1: delete 13 No children

Case 2: delete 16 1 child

Case 3: delete 5 2 children case 3: z has two children splice out z’s successor, y. copy key[y] into z How do I find z’s successor? case 3: z has two children splice out z’s successor, y. copy key[y] into z Case 3: delete 5 2 children

Red-Black Trees Red-Black Tree is a BST with one extra field per node that denotes whether it is “red” or “black” By constraining the way nodes are colored we can ensure that no path from the root to a leaf is more than twice as long as any other (i.e. balanced tree) All leaves (also called external nodes) are black and do not hold keys May be drawn as single node black-height of node x = number of black nodes in any path between node x and a leaf. The leaf, which is black, is included in the count The node x, which may be red, is not included in the count height of node x = h(x) = number of edges in longest path to a leaf h(T) = h(root)

R-B BST with black heights shown

R-B tree with “sentinel” node All leaves and parent of root

Note: h(x) < 2bh(x) Properties of red-black trees: 1. Every node is either red or black 2. Root is black 3. nil[T] is black 4. If node is red, its children are black 5. All paths from any node to nil[T] have the same number of black nodes (unique bh). Note: h(x) < 2bh(x) h(x) = # edges in longest path to Nil(T)

No restrictions on children of black node 21-1=1 0, 1 or 2 can be red IN=1 22-1=3, IN=7 22-1=3, IN=4 21-1=1, IN=3 A subtree at x has at least 2bh(x) – 1 internal nodes (IN) The more red nodes in subtree, greater deviation from lower bound of 2bh(x) – 1 internal nodes

Proof by induction that subtree at x has at least 2bh(x) – 1 internal nodes Base: x = nil[T], bh(x) = 0, 2bh(x)-1 = 0, no internal nodes in subtree IH: Assume the subtrees of children at x have at least 2bh(child) -1 nodes Analyze case where both children at x are black. Gives the tightest lower bound. bh(black child at x) = bh(x) –1 By IH, each black child at x has at least 2bh(x) -1 – 1 nodes For 2 black children plus the root of subtree, which is x, n= 2(2bh(x) -1 – 1) + 1 = 2bh(x) - 1 nodes

Lemma 13.1: Proof that R-B tree is balanced R-B tree with n internal nodes (i.e. not counting leaves) has a height of at most 2lg(n+1) By property (4) of R-B trees (if node is red, its children are black), at least half of the nodes on the longest path between the root and nil[T] are black. (i.e. bh(root) is at least h(T)/2) Number of nodes in tree, n, is at least 2bh(root) – 1 (inductive proof) n > 2h/2 – 1 n+1 > 2h/2 lg(n +1) > h/2 h < 2 lg(n+1)

Implications of Lemma 13.1: Red-black trees can be used to implement dynamic-set operations Search, Minimum, Maximum, Successor, and Predecessor with running time O(lg n) Tree-Insert and Tree-Delete will also run on red-black trees in O(lg n); however, they may not preserve the red-black tree properties

To restore red-black tree properties after Insert or Delete, we must change color of some nodes and adjust pointer structure Rotation (only update algorithm we will discuss) is a local modification that changes the root of a subtree while preserving the BST property. Left-Rotate(T,x) replaces the root of subtree at x by its right child. x becomes y’s left child so x stays to the left of y y’s left child becomes x’s right child so it stays right of x Right-Rotate(T,y) replaces the root of subtree at y by its left child.

x x y y Example of left rotations in BST Left-Rotate(T,x) replaces the root of subtree at x by its right child. x becomes y’s left child so x stays to the left of y y’s left child becomes x’s right child so it stays right of x y x