Data Structures, Spring 2006 © L. Joskowicz 1 Data Structures – LECTURE Binary search trees Motivation Operations on binary search trees: –Search –Minimum,

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)
UNC Chapel Hill Lin/Foskey/Manocha Binary Search Tree Her bir node u bir object olan bir linked data structure ile temsil edilebilir. Her bir node key,
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.
Binary Search Trees Azhar Maqsood School of Electrical Engineering and Computer Sciences (SEECS-NUST)
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.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
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 11.
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.
COMP 171 Data Structures and Algorithms Tutorial 6 Binary Search Trees.
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.
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.
David Luebke 1 7/2/2015 Medians and Order Statistics Structures for Dynamic Sets.
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.
2IL50 Data Structures Fall 2015 Lecture 7: Binary Search 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.
Binary Search Tree Qamar Abbas.
Data Structures Haim Kaplan and Uri Zwick November 2012 Lecture 3 Dynamic Sets / Dictionaries Binary Search Trees.
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.
Lecture 9 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
October 9, Algorithms and Data Structures Lecture VIII Simonas Šaltenis Aalborg University
1 Algorithms CSCI 235, Fall 2015 Lecture 22 Binary Search Trees.
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.
CISC 235 Topic 3 General Trees, Binary Trees, 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.
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.
CS6045: Advanced Algorithms Data Structures. Dynamic Sets Next few lectures will focus on data structures rather than straight algorithms In particular,
Binary Search Trees What is a binary search tree?
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
ძებნის ორობითი ხეები BST (Binary Search Trees)
Red-Black Trees.
CMSC 341 (Data Structures)
Ch. 12: Binary Search Trees Ming-Te Chi
Binary Search Trees (13.1/12.1)
Lecture 7 Algorithm Analysis
Algorithms and Data Structures Lecture VII
Chapter 12: Binary Search Trees
Lecture 7 Algorithm Analysis
Topic 6: Binary Search Tree Data structure Operations
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:

Data Structures, Spring 2006 © L. Joskowicz 1 Data Structures – LECTURE Binary search trees Motivation Operations on binary search trees: –Search –Minimum, Maximum –Predecessor, Successor –Insert, Delete Randomly built binary search trees

Data Structures, Spring 2006 © L. Joskowicz 2 Motivation: binary search trees A dynamic ADT that efficiently supports the following common operations on S: –Search for an element –Minimum, Maximum –Predecessor, Successor –Insert, Delete Use a binary tree! All operations take Θ(lg n) The tree must always be balanced, for otherwise the operations will not take time proportional to the height of the tree!

Data Structures, Spring 2006 © L. Joskowicz 3 Binary search tree A binary search tree has a root, internal nodes with at most two children each, and leaf nodes. Each node x has the following fields: key(x), left(x), right(x), parent(x) Binary-search-tree property: Let x be the root of a subtree, and y a node below it. –left subtree: key(y) ≤ key(x) –right subtree: key(y) > key(x)

Data Structures, Spring 2006 © L. Joskowicz 4 Examples of binary trees In-order, pre-order, and post-order traversal

Data Structures, Spring 2006 © L. Joskowicz 5 Tree-Search(x,k) if x = null or k = key[x] then return x if k < key[x] then return Tree-Search(left[x],k) else return Tree-Search(right[x],k) Iterative-Tree-Search(x,k) while x ≠ null and k ≠ key[x] do if k < key[x] then x  left[x] else x  right[x] return x Tree-Search routine Complexity: O(h) h = tree height

Data Structures, Spring 2006 © L. Joskowicz 6 Example: search in a binary tree Search for 13 in the tree

Data Structures, Spring 2006 © L. Joskowicz 7 Tree traversal Inorder-Tree-Walk(x) if x ≠ null then Inorder-Tree-Walk(left[x]) print key[x] Inorder-Tree-Walk(right[x]) Complexity: Θ(n) Recurrence equation: T(0) = Θ(1) T(n) = T(k) + T(n – k –1) + Θ(1)

Data Structures, Spring 2006 © L. Joskowicz 8 Tree-Minimum(x) while left[x] ≠ null do x  left[x] return x Tree-Maximum(x) while right[x] ≠ null do x  right[x] return x Max and Min-Search routines Complexity: O(h)

Data Structures, Spring 2006 © L. Joskowicz 9 Example: Min and Max search

Data Structures, Spring 2006 © L. Joskowicz 10 Tree-Successor routine (1) The successor of x is the smallest element y with a key greater than that of x The successor of x can be found without comparing the keys. It is either: 1. null if x is the maximum node. 2.the minimum of the right child of t when t has a right child. 3.or else, the lowest ancestor of x whose left child is also an ancestor of x.

Data Structures, Spring 2006 © L. Joskowicz 11 Tree-Successor: cases x Minimum of right child of x y z x Lowest ancestor z of x whose left child y is also an ancestor of x

Data Structures, Spring 2006 © L. Joskowicz 12 Tree-Successor routine (2) Tree-Successor(x) if right[x] ≠ null /* Case 2 then return Tree-Minimum(right[x]) y  parent[x] while y ≠ null and x = right[y] /* Case 3 do x  y y  parent[y] return y

Data Structures, Spring 2006 © L. Joskowicz 13 Example: finding a successor Find the successors of 15, 13

Data Structures, Spring 2006 © L. Joskowicz 14 Proof (1) Case 1 is trivial; Case 2 is easy. Case 3: If x doesn’t have a right child, then its successor is x’s first ancestor such that its left child is also an ancestor of x. (This includes the case that there is no such ancestor, and then x is the maximum and the successor is null.) Proof: To prove that a node z is the successor of x, we need to show that key[z] > key[x] and that x is the maximum of all elements smaller than z. Start from x and climb up the tree as long as you move from a right child up. Let the node you stopped at be y, and denote z = parent[y].

Data Structures, Spring 2006 © L. Joskowicz 15 Proof (2) Sub-claim: x is the max of the sub-tree rooted at y. Proof of sub-claim: x is the node you reach if you go right all the time from y. Now we claim z = parent(y) is the successor of x. First, key[z] > key[x] because y is the left child of z by the definition of y, so x is in z’s left sub-tree. Now, x is the maximum of all items that are smaller than z, because by the sub-claim x is the maximum of the sub-tree rooted at y, and all elements smaller than z are in this subtree by the property of binary search trees.

Data Structures, Spring 2006 © L. Joskowicz 16 Insert Insert is very similar to search:  find the place in the tree where we want to insert the new node z. The new node z will always be a leaf. Initially, assume that left(z) and right(z) are both null.

Data Structures, Spring 2006 © L. Joskowicz 17 Example: insertion Insert 13 in the tree z

Data Structures, Spring 2006 © L. Joskowicz 18 Tree-insert routine Tree-Insert(T,z) y  null x  root[T] while x ≠ null do y  x if key[z] < key[x] then x  left[x] else x  right[x] parent[z]  y /* When the tree is empty if y = null then root[T]  z else if key[z] < key[y] then left[y]  z else right[y]  z y is the parent of x

Data Structures, Spring 2006 © L. Joskowicz 19 Delete Delete is more complicated than insert. There are three cases to delete node z: 1. z has no children 2. z has one child 3. z has two children Case 1: delete z, update the child’s parent child to null. Case 2: delete z and connect its parent to its child. Case 3: more complex. We can’t just take the node out and reconnect its parent with its children, because the tree will no longer be a binary tree!

Data Structures, Spring 2006 © L. Joskowicz 20 delete Delete case 1: no children! delete

Data Structures, Spring 2006 © L. Joskowicz 21 Delete case 2: one child delete

Data Structures, Spring 2006 © L. Joskowicz 22 Delete case 3: two children Replace the node by its successor, and “pull” the successor, which necessarily has at most one child Claim: if a node has two children, its successor has at most one child. Proof: This is because if the node has two children, its successor is the minimum of its right subtree. This minimum cannot have a left child because then the child would be the minimum… Invariant: in all cases the binary search tree property is preserved after the deletion.

Data Structures, Spring 2006 © L. Joskowicz 23 Delete: case 3 proof z α β δ Delete z y w y α β δ w

Data Structures, Spring 2006 © L. Joskowicz 24 Delete: case 3 delete successor

Data Structures, Spring 2006 © L. Joskowicz 25 Tree-Delete routine Tree-Delete(T,z) if left[z] = null or right[z] = null /* Cases 1 or 2 then y  z /* find a node y to splice else y  Tree-Successor(z) /* to splice out if left[y] ≠ null /* set the child x then x  left[y] else x  right[y] if x ≠ null /* splicing operation then parent[x]  parent[y] if parent[y] = null then root[T]  x else if y = left[parent[y]] then left[parent[y]]  x else right[parent[y]]  x if y ≠ z then key[z]  key[y] return y /* copy y’s satellite data into z

Data Structures, Spring 2006 © L. Joskowicz 26 Complexity analysis Delete: The two first cases take O(1) operations: they involve switching the pointers of the parent and the child (if it exists) of the node that is deleted. The third case requires a call to Tree-Successor, and thus can take O(h) time. Conclusion: all dynamic operations on a binary search tree take O(h), where h is the tree height. In the worst case, the height of the tree can be O(n)

Data Structures, Spring 2006 © L. Joskowicz 27 Randomly-built Binary Search Trees Definition: A randomly-built binary search tree over n distinct keys is a binary search tree that results from inserting the n keys in random order (each permutation of the keys is equally likely) into an initially empty tree. Theorem: The average height of a randomly-built binary search tree of n distinct keys is O(lg n) Corollary: The dynamic operations Successor, Predecessor, Search, Min, Max, Insert, and Delete all have O(lg n) average complexity on randomly-built binary search trees.