Analysis of Algorithms CS 477/677 Binary Search Trees Instructor: George Bebis (Appendix B5.2, Chapter 12)

Slides:



Advertisements
Similar presentations
Chapter 12 Binary Search Trees
Advertisements

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.
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.
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.
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.
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.
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.
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,
David Luebke 1 7/2/2015 ITCS 6114 Binary Search Trees.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu.
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.
Design and Analysis of Algorithms Binary search trees Haidong Xue Summer 2012, at GSU.
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.
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.
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.
1 Algorithms CSCI 235, Fall 2015 Lecture 22 Binary Search Trees.
Binary Search Trees Lecture 5 1. Binary search tree sort 2.
CISC 235 Topic 3 General Trees, Binary Trees, Binary Search Trees.
Binary Search Trees (BST)
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,
Fundamentals of Algorithms MCS - 2 Lecture # 17. Binary Search Trees.
Binary Search Trees What is a binary search tree?
Analysis of Algorithms
CS200: Algorithms Analysis
CS 583 Analysis of Algorithms
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
Lecture 7 Algorithm Analysis
Algorithms and Data Structures Lecture VII
Chapter 12: Binary Search Trees
CS6045: Advanced Algorithms
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:

Analysis of Algorithms CS 477/677 Binary Search Trees Instructor: George Bebis (Appendix B5.2, Chapter 12)

2 Binary Search Trees Tree representation: –A linked data structure in which each node is an object Node representation: –Key field –Satellite data –Left: pointer to left child –Right: pointer to right child –p: pointer to parent ( p [root [T]] = NIL ) Satisfies the binary-search-tree property !! Left child Right child LR parent keydata

3 Binary Search Tree Property Binary search tree property: –If y is in left subtree of x, then key [y] ≤ key [x] –If y is in right subtree of x, then key [y] ≥ key [x]

4 Binary Search Trees Support many dynamic set operations –SEARCH, MINIMUM, MAXIMUM, PREDECESSOR, SUCCESSOR, INSERT, DELETE Running time of basic operations on binary search trees –On average:  (lgn) The expected height of the tree is lgn –In the worst case:  (n) The tree is a linear chain of n nodes

5 Worst Case  (n)

6 Traversing a Binary Search Tree Inorder tree walk: –Root is printed between the values of its left and right subtrees: left, root, right –Keys are printed in sorted order Preorder tree walk: –root printed first: root, left, right Postorder tree walk: –root printed last: left, right, root Preorder: Inorder: Postorder:

7 Traversing a Binary Search Tree Alg: INORDER-TREE-WALK (x) 1. if x  NIL 2. then INORDER-TREE-WALK ( left [x] ) 3. print key [x] 4. INORDER-TREE-WALK ( right [x] ) E.g.: Running time: –  (n), where n is the size of the tree rooted at x Output:

8 Searching for a Key Given a pointer to the root of a tree and a key k : –Return a pointer to a node with key k if one exists –Otherwise return NIL Idea –Starting at the root: trace down a path by comparing k with the key of the current node: If the keys are equal: we have found the key If k < key[x] search in the left subtree of x If k > key[x] search in the right subtree of x

9 Example: TREE-SEARCH Search for key 13: –15  6  7 

10 Searching for a Key Alg: TREE-SEARCH (x, k) 1. if x = NIL or k = key [x] 2. then return x 3. if k < key [x] 4. then return TREE-SEARCH (left [x], k ) 5. else return TREE-SEARCH (right [x], k ) Running Time: O (h), h – the height of the tree

11 Finding the Minimum in a Binary Search Tree Goal: find the minimum value in a BST –Following left child pointers from the root, until a NIL is encountered Alg: TREE-MINIMUM (x) 1. while left [x]  NIL 2. do x ← left [x] 3. return x Running time: O(h), h – height of tree Minimum = 2

12 Finding the Maximum in a Binary Search Tree Maximum = 20 Goal: find the maximum value in a BST –Following right child pointers from the root, until a NIL is encountered Alg: TREE-MAXIMUM(x) 1. while right [x]  NIL 2. do x ← right [x] 3. return x Running time: O(h), h – height of tree

13 Successor Def: successor ( x ) = y, such that key [y] is the smallest key > key [x] E.g.: successor (15) = successor (13) = successor (9) = Case 1: right (x) is non empty –successor ( x ) = the minimum in right (x) Case 2: right (x) is empty –go up the tree until the current node is a left child: successor ( x ) is the parent of the current node –if you cannot go further (and you reached the root): x is the largest element x y

14 Finding the Successor Alg: TREE-SUCCESSOR(x) 1. if right [x]  NIL 2. then return TREE-MINIMUM( right [x] ) 3. y ← p[x] 4. while y  NIL and x = right [y] 5. do x ← y 6. y ← p[y] 7. return y Running time: O (h), h – height of the tree y x

15 Predecessor Def: predecessor ( x ) = y, such that key [y] is the biggest key < key [x] E.g.: predecessor (15) = predecessor (9) = predecessor (7) = Case 1: left (x) is non empty –predecessor ( x ) = the maximum in left (x) Case 2: left (x) is empty –go up the tree until the current node is a right child: predecessor ( x ) is the parent of the current node –if you cannot go further (and you reached the root): x is the smallest element x y

16 13 Insertion Goal: –Insert value v into a binary search tree Idea: –If key [x] < v move to the right child of x, else move to the left child of x –When x is NIL, we found the correct position –If v < key [y] insert the new node as y ’s left child else insert it as y ’s right child –Begining at the root, go down the tree and maintain: Pointer x : traces the downward path (current node) Pointer y : parent of x (“trailing pointer” ) Insert value 13

17 Example: TREE-INSERT x=root[T], y=NIL Insert 13: x x x = NIL y = y y

18 Alg: TREE-INSERT (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 Tree T was empty 11. else if key [z] < key [y] 12. then left [y] ← z 13. else right [y] ← z Running time: O(h)

19 Deletion Goal: –Delete a given node z from a binary search tree Idea: –Case 1: z has no children Delete z by making the parent of z point to NIL delete z

20 Deletion Case 2: z has one child –Delete z by making the parent of z point to z ’s child, instead of to z delete z

21 Deletion Case 3: z has two children –z ’s successor (y) is the minimum node in z ’s right subtree –y has either no children or one right child (but no left child) –Delete y from the tree (via Case 1 or 2) –Replace z’s key and satellite data with y’s delete z y

22 TREE-DELETE (T, z) 1. if left[z] = NIL or right[z] = NIL 2. then y ← z 3. else y ← TREE-SUCCESSOR( z ) 4. if left[y]  NIL 5. then x ← left[y] 6. else x ← right[y] 7. if x  NIL 8. then p[x] ← p[y] z has one child z has 2 children y x

23 TREE-DELETE (T, z) – cont. 9. if p[y] = NIL 10. then root[T] ← x 11. else if y = left[p[y]] 12. then left[p[y]] ← x 13. else right[p[y]] ← x 14. if y  z 15. then key[z] ← key[y] 16. copy y’s satellite data into z 17. return y y x Running time: O(h)

24 Binary Search Trees - Summary Operations on binary search trees: –SEARCH O(h) –PREDECESSOR O(h) –SUCCESOR O(h) –MINIMUM O(h) –MAXIMUM O(h) –INSERT O(h) –DELETE O(h) These operations are fast if the height of the tree is small – otherwise their performance is similar to that of a linked list

25 Problems Exercise (page 256) What is the difference between the MAX-HEAP property and the binary search tree property? Exercise (page 256) Can the min-heap property be used to print out the keys of an n - node tree in sorted order in O(n) time? Can you use the heap property to design an efficient algorithm that searches for an item in a binary tree?

26 Problems Let x be the root node of a binary search tree (BST). Write an algorithm BSTHeight(x) that determines the height of the tree. What would be its running time? Alg: BSTHeight(x) if (x==NULL) return -1; else return max (BSTHeight(left[x]), BSTHeight(right[x]))+1;

27 Problems (Exercise , page 264) In a binary search tree, are the insert and delete operations commutative? Insert: –Try to insert 4 followed by 6, then insert 6 followed by 4 Delete –Delete 5 followed by 6, then 6 followed by 5 in the following tree