2IL50 Data Structures Fall 2015 Lecture 7: Binary Search Trees.

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
CS 332: Algorithms Binary Search Trees. Review: Dynamic Sets ● Next few lectures will focus on data structures rather than straight algorithms ● In particular,
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)
Chapter 12 Binary search trees Lee, Hsiu-Hui Ack: This presentation is based on the lecture slides from Hsu, Lih-Hsing, as well as various materials from.
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.
CS Section 600 CS Section 002 Dr. Angela Guercio Spring 2010.
Binary Search Trees Comp 550.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 10.
2IL50 Data Structures Spring 2015 Lecture 8: Augmenting Data Structures.
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.
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.
Binary Search Trees CIS 606 Spring Search trees Data structures that support many dynamic-set operations. – Can be used as both a dictionary and.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 11.
CS Section 600 CS Section 002 Dr. Angela Guercio Spring 2010.
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,
CS 307 Fundamentals of Computer Science 1 Data Structures Review Session 2 Ramakrishna, PhD student. Grading Assistant for this course.
David Luebke 1 7/2/2015 ITCS 6114 Binary Search Trees.
DAST 2005 Tirgul 7 Binary Search Trees. DAST 2005 Motivation We would like to have a dynamic ADT that efficiently supports the following common operations:
12.Binary Search Trees Hsu, Lih-Hsing. Computer Theory Lab. Chapter 12P What is a binary search tree? Binary-search property: Let x be a node in.
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.
CS 3343: Analysis of Algorithms Lecture 16: Binary search trees & red- black trees.
Mudasser Naseer 1 10/20/2015 CSC 201: Design and Analysis of Algorithms Lecture # 11 Red-Black Trees.
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.
October 3, Algorithms and Data Structures Lecture VII Simonas Šaltenis Nykredit Center for Database Research Aalborg University
Preview  Graph  Tree Binary Tree Binary Search Tree Binary Search Tree Property Binary Search Tree functions  In-order walk  Pre-order walk  Post-order.
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)
CSE 2331/5331 Topic 8: Binary Search Tree Data structure Operations.
Binary Search Trees What is a binary search tree?
Balanced Search Trees Modified from authors’ slides.
Binary Search Trees.
CS 332: Algorithms Red-Black Trees David Luebke /20/2018.
CS200: Algorithms Analysis
Lecture 7 Algorithm Analysis
Ch. 12: Binary Search Trees Ming-Te Chi
Red-Black Trees.
CMSC 341 (Data Structures)
Ch. 12: Binary Search Trees Ming-Te Chi
Lecture 7 Algorithm Analysis
Algorithms and Data Structures Lecture VII
CS 583 Analysis of Algorithms
Lecture 7 Algorithm Analysis
Topic 6: Binary Search Tree Data structure Operations
Binary SearchTrees [CLRS] – Chap 12.
Design and Analysis of Algorithms
Analysis of Algorithms CS 477/677
Binary Search Trees Comp 122, Spring 2004.
Chapter 12&13: Binary Search Trees (BSTs)
Red-Black Trees CS302 Data Structures
Presentation transcript:

2IL50 Data Structures Fall 2015 Lecture 7: Binary Search Trees

Dynamic Sets

Dynamic sets Dynamic sets Sets that can grow, shrink, or otherwise change over time.  Two types of operations: queries return information about the set modifying operationschange the set Common queries Search, Minimum, Maximum, Successor, Predecessor Common modifying operations Insert, Delete

Dictionary Dictionary Stores a set S of elements, each with an associated key (integer value). Operations Search(S, k): return a pointer to an element x in S with key[x] = k, or NIL if such an element does not exist. Insert(S, x): inserts element x into S, that is, S ← S ⋃ {x} Delete(S, x): remove element x from S

SearchInsertDelete linked list sorted array hash table Θ(1) Θ(n) Θ(log n) Implementing a dictionary Hash table Running times are average times and assume (simple) uniform hashing and a large enough table (for example, of size 2n) TodayBinary search trees Θ(1)

Binary Search Trees

Binary search trees  Binary search trees are an important data structure for dynamic sets.  They can be used as both a dictionary and a priority queue.  They accomplish many dynamic-set operations in O(h) time, where h = height of tree.

Tree terminology Binary tree: every node has 0, 1, or 2 children Root: top node (no parent) Leaf: node without children Subtree rooted at node x: all nodes below and including x Depth of node x: length of path from root to x Depth of tree: max. depth over all nodes Height of node x: length longest path from x to leaf Height of tree: height of root Level: set of nodes with same depth Family tree terminology… Left/right child x

5 Binary search trees  root of T denoted by root[T]  internal nodes have four fields: key (and possible other satellite data) left: points to left child right: points to right child p: points to parent. p[root[T]] = NIL Binary tree T root[T] = NIL

Binary search trees Binary tree T root[T] Keys are stored only in internal nodes! There are binary search trees which store keys only in the leaves …

Binary search trees  a binary tree is a leaf or a root node x with a binary tree as its left and/or right child Binary-search-tree property if y is in the left subtree of x, then key[y] ≤ key[x] if y is in the right subtree of x, then key[y] ≥ key[x] Keys don’t have to be unique … can use stricter property, take care when balancing

Binary search trees height h = 2 height h = 4 Binary-search-tree property if y is in the left subtree of x, then key[y] ≤ key[x] if y is in the right subtree of x, then key[y] ≥ key[x]

Inorder tree walk  binary search trees are recursive structures ➨ recursive algorithms often work well Example: print all keys in order using an inorder tree walk InorderTreeWalk(x) 1. if x ≠ NIL 2. then InorderTreeWalk(left[x]) 3. print key[x] 4. InorderTreeWalk(right[x]) Correctness: follows by induction from the binary search tree property Running time? Intuitively, O(n) time for a tree with n nodes, since we visit and print each node once

Inorder tree walk InorderTreeWalk(x) 1. if x ≠ NIL 2. then InorderTreeWalk(left[x]) 3. print key[x] 4. InorderTreeWalk(right[x]) Theorem If x is the root of an n-node subtree, then the call InorderTreeWalk(x) takes Θ(n) time. Proof: T(n) takes small, constant amount of time on empty subtree ➨ T(0) = c for some positive constant c for n > 0 assume that left subtree has k nodes, right subtree n-k-1 ➨ T(n) = T(k) + T(n-k-1) + d for some positive constant d use substitution method … to show: T(n) = (c+d)n + c ■

Querying a binary search tree TreeSearch(x, k) 1. if x = NIL or k = key[x] 2. then return x 3. if k < key[x] 4. then return TreeSearch(left[x], k) 5. else return TreeSearch(right[x], k) Initial call: TreeSearch(root[T], k) TreeSearch(root[T], 4) TreeSearch(root[T], 2) Running time: Θ(length of search path) worst case Θ(h) k ≤ k≥ k Binary-search-tree property

Querying a binary search tree – iteratively TreeSearch(x, k) 1. if x = NIL or k = key[x] 2. then return x 3. if k < key[x] 4. then return TreeSearch(left[x], k) 5. else return TreeSearch(right[x], k) IterativeTreeSearch(x, k) 1. while x ≠ NIL and k ≠ key[x] 2. do if k < key[x] 3. then x = left[x] 4. else x = right[x] 5. return x  iterative version more efficient on most computers k ≤ k≥ k Binary-search-tree property

Minimum and maximum  binary-search-tree property guarantees that the minimum key is located in the leftmost node the maximum key is located in the rightmost node TreeMinimum(x) 1. while left[x] ≠ NIL 2. do x = left[x] 3. return x TreeMaximum(x) 1. while right[x] ≠ NIL 2. do x = right[x] 3. return x k ≤ k≥ k Binary-search-tree property

Minimum and maximum  binary-search-tree property guarantees that the minimum key is located in the leftmost node the maximum key is located in the rightmost node TreeMinimum(x) 1. while left[x] ≠ NIL 2. do x = left[x] 3. return x TreeMaximum(x) 1. while right[x] ≠ NIL 2. do x = right[x] 3. return x k ≤ k≥ k Binary-search-tree property Running time? Both procedures visit nodes on a downward path from the root ➨ O(h) time

Successor and predecessor  Assume that all keys are distinct Successor of a node x node y such that key[y] is the smallest key > key[x] We can find y based entirely on the tree structure, no key comparisons are necessary … if x has the largest key, then we say x’s successor is NIL

Successor and predecessor Successor of a node x node y such that key[y] is the smallest key > key[x] Two cases: 1.x has a non-empty right subtree ➨ x’s successor is the minimum in x’s right subtree  x has an empty right subtree ➨ x’s successor y is the node that x is the predecessor of (x is the maximum in y’s left subtree) as long as we move to the left up the tree (move up through right children), we’re visiting smaller keys …

Successor and predecessor TreeSucessor(x) 1. if right[x] ≠ NIL 2. then return TreeMinimum(right[x]) 3. y = p[x] 4. while y ≠ NIL and x = right[y] 5. do x = y 6. y = p[x] 7. return y  TreePredecessor is symmetric Successor of 15? Successor of 6? Successor of 4? Predecessor of 6?

Successor and predecessor TreeSucessor(x) 1. if right[x] ≠ NIL 2. then return TreeMinimum(right[x]) 3. y = p[x] 4. while y ≠ NIL and x = right[y] 5. do x = y 6. y = p[x] 7. return y  TreePredecessor is symmetric Running time? O(h)

Insertion TreeInsert(T, z) 1. y = NIL 2. x = root[T] 3. while x ≠ NIL 4. do y = x 5. if key[z] < key[x] 6. then x = left[x] 7. else x = right[x] 8. p[z] = y 9. if y == NIL 10. then root[T] = z 11. else if key[z] < key[y] 12. then left[y] = z 13. else right[y] = z  to insert value v, insert node z with key[z] = v, left[z] = NIL, and right[z] = NIL  traverse tree down to find correct position for z

Insertion TreeInsert(T, z) 1. y = NIL 2. x = root[T] 3. while x ≠ NIL 4. do y = x 5. if key[z] < key[x] 6. then x = left[x] 7. else x = right[x] 8. p[z] = y 9. if y == NIL 10. then root[T] = z 11. else if key[z] < key[y] 12. then left[y] = z 13. else right[y] = z  to insert value v, insert node z with key[z] = v, left[z] = NIL, and right[z] = NIL  traverse tree down to find correct position for z z x y = NIL y xy xy xy x

Insertion TreeInsert(T, z) 1. y = NIL 2. x = root[T] 3. while x ≠ NIL 4. do y = x 5. if key[z] < key[x] 6. then x = left[x] 7. else x = right[x] 8. p[z] = y 9. if y == NIL 10. then root[T] = z 11. else if key[z] < key[y] 12. then left[y] = z 13. else right[y] = z Running time?  to insert value v, insert node z with key[z] = v, left[z] = NIL, and right[z] = NIL  traverse tree down to find correct position for z z y O(h)

7 Deletion  we want to delete node z TreeDelete has three cases:  z has no children ➨ delete z by having z’s parent point to NIL, instead of to z

18 Deletion  we want to delete node z TreeDelete has three cases:  z has no children ➨ delete z by having z’s parent point to NIL, instead of to z  z has one child ➨ delete z by having z’s parent point to z’s child, instead of to z

 we want to delete node z TreeDelete has three cases:  z has no children ➨ delete z by having z’s parent point to NIL, instead of to z  z has one child ➨ delete z by having z’s parent point to z’s child, instead of to z  z has two children ➨ z’s successor y has either no or one child ➨ delete y from the tree and replace z’s key and satellite data with y’s 6 7 Deletion

 we want to delete node z TreeDelete has three cases:  z has no children ➨ delete z by having z’s parent point to NIL, instead of to z  z has one child ➨ delete z by having z’s parent point to z’s child, instead of to z  z has two children ➨ z’s successor y has either no or one child ➨ delete y from the tree and replace z’s key and satellite data with y’s Running time? 7 Deletion O(h)

Minimizing the running time  all operations can be executed in time proportional to the height h of the tree (instead of proportional to the number n of nodes in the tree) Worst case: Solution: guarantee small height (balance the tree) ➨ h = Θ(log n) Θ(n)

Balanced Search Trees

Balanced Search trees  There are many methods to balance a search tree. by weight for each node the number of nodes in the left and the right subtree is approximately equal by height for each node the height of the left and the right subtree is approximately equal by degree all leaves have the same height, but the degree of the nodes differs (hence not a binary search tree)

Weight-balanced search trees BB[α]-tree binary search tree where for each pair of nodes x, y, with y being a child of x we have where α is a positive constant with α ≤ 1/3  For the height h(n) holds h(n) ≤ h((1-α)n) + 1 Master theorem ➨ h(n) = Θ(log n)  Ideally: α as close as possible to 1/3 But: α = 1/3 gives too little flexibility for updates α just smaller than 1/3 works fine number of leaves in subtree rooted at y number of leaves in subtree rooted at x ≤ 1-αα ≤ x y

Height-balanced search trees AVL-tree binary search tree where for each node | height left subtree – height right subtree | ≤ 1 Theorem An AVL-tree with n nodes has height Θ(log n). h h-1, h, or h+1

Height-balanced search trees Theorem An AVL-tree with n nodes has height Θ(log n). Proof Let n(h) = minimum number of nodes in an AVL-tree of height h Claim: n(h) ≥ 2 h/2 – 1 Proof of Claim: induction on h h = 1 h = 2 n(1) = 1 ✔ n(2) = 2 ✔

Height-balanced search trees Theorem An AVL-tree with n nodes has height Θ(log n). Proof Let n(h) = minimum number of nodes in an AVL-tree of height h Claim: n(h) ≥ 2 h/2 – 1 Proof of Claim: induction on h h > 2 n(h) = 1 + n(h-1) + n(h-2) ≥ 2n(h-2) ≥ 2 ∙ 2 (h-2)/2 - 1 ≥ 2 h/2 – 1 ■ h-1 h-2

Degree-balanced trees (2, 3)-tree search tree where all leaves have the same height and internal nodes have degree 2 or 3 Theorem A (2, 3)-tree with n nodes has height Θ(log n) , ,14

Red-black Trees Another height-balanced search tree …

Red-black tree binary search tree where each node has a color attribute which is either red or black Red-black properties 1.Every node is either red or black. 2.The root is black 3.Every leaf (nil[T]) is black. 4.If a node is red, then both its children are black. (Hence no two reds in a row on a simple path from the root to a leaf) 5.For each node, all paths from the node to descendant leaves contain the same number of black nodes. NIL 10 NIL NIL Red-black trees

height of a node number of edges on a longest path to a leaf black-height of a node x bh(x) is the number of black nodes (including nil[T]) on the path from x to leaf, not counting x. Red-black properties 1.Every node is either red or black. 2.The root is black 3.Every leaf (nil[T]) is black. 4.If a node is red, then both its children are black. (Hence no two reds in a row on a simple path from the root to a leaf) 5.For each node, all paths from the node to descendant leaves contain the same number of black nodes. NIL 10 NIL NIL Red-black trees: height h = 3 bh = 2 h = 1 bh = 1 h = 4 bh = 2

Red-black trees: implementation detail  It is useful to replace each NIL by a single sentinel nil[T] which is always black. The root’s parent is also the sentinel. NIL NIL 10 NIL NIL

Red-black trees: implementation detail  It is useful to replace each NIL by a single sentinel nil[T] which is always black. The root’s parent is also the sentinel.  NIL will not (always) be drawn on the following slides

Red-black trees Lemma A red-black tree with n nodes has height ≤ 2 log(n+1). Proof the subtree rooted at any node x contains at least 2 bh(x) - 1 internal nodes the complete tree has n internal nodes ➨ 2 bh(root[T]) - 1 ≤ n ➨ bh(root[T]) ≤ log (n+1) ➨ height(T) = number of edges on a longest path to a leaf ≤ 2 ∙ bh(root[T]) ≤ 2 log(n+1) ■ bh(x) x “smallest” subtree with black-height bh(x)

Balanced binary search trees Advantages of balanced binary search trees over linked lists efficient search in Θ(log n) time over sorted arrays efficient insertion and deletion in Θ(log n) time over hash tables can find successor and predecessor efficiently in Θ(log n) time