Design and Analysis of Algorithms

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.
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.
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.
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.
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,
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.
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
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.
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.
CS6045: Advanced Algorithms Data Structures. Dynamic Sets Next few lectures will focus on data structures rather than straight algorithms In particular,
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 10.
Binary Search Trees What is a binary search tree?
Balanced Search Trees Modified from authors’ slides.
Binary Search Trees.
Data Structures Review Session 2
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
Elementary Data Structures
ძებნის ორობითი ხეები BST (Binary Search Trees)
Red-Black Trees.
ძებნის ორობითი ხეები BST (Binary Search Trees)
CMSC 341 (Data Structures)
Red Black Tree Essentials
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
CS6045: Advanced Algorithms
CS 583 Analysis of Algorithms
Red Black Tree Essentials
Lecture 7 Algorithm Analysis
Topic 6: Binary Search Tree Data structure Operations
EE Data Structures and 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:

Design and Analysis of Algorithms Dr. Maheswari Karthikeyan 09/03/2013, Lecture 7

Binary Search Tree

BINARY SEARCH TREE It is a data structure that supports the dynamic-set operations including search, maximum, minimum, predecessor, successor, insert and delete. It can be used both as a dictionary and as a priority queue.

BINARY SEARCH TREE Tree representation: Left child Right child L R parent key data 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

BINARY SEARCH TREE

BST of different heights Draw binary search trees of height 2,3,4,5 and 6 on the set of keys {1,4,5,10,16,17,21}

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 2 3 4 5 7 9

TREE-SEARCH(x,k) if x = nil 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 ≠ NIL and k ≠ key[x] do if k < key[x] then x left[x] else x right[x] return x

MINIMUM TREE-MINIMUM(x) while left[x] ≠ NIL do x left[x] return x

MAXIMUM TREE-MAXIMUM(x) while right[x] ≠ NIL do x right[x] return x

Successor Def: successor (x ) = y, such that key [y] is the smallest key > key [x] E.g.: successor (15) = successor (13) = successor (9) = 3 2 4 6 7 13 15 18 17 20 9 17 15 13 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

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

SUCCESSOR

Predecessor Def: predecessor (x ) = y, such that key [y] is the biggest key < key [x] E.g.: predecessor (15) = predecessor (9) = predecessor (13) = 3 2 4 6 7 13 15 18 17 20 9 13 7 9 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

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 2 1 3 5 9 12 18 15 19 17 13

Example: TREE-INSERT x, y=NIL y Insert 13: 2 1 3 5 9 12 18 15 19 17 2 x = NIL y = 15

Alg: TREE-INSERT(T, z) y ← NIL x ← root [T] 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 2 1 3 5 9 12 18 15 19 17 13 Running time: O(h)

INSERTION 15 18 10 12 16 20 05 1 7 11 TREE-INSERT(T,17)

INSERTION 15 18 10 12 16 20 05 1 7 11 17 TREE-INSERT(T,17)

INSERTION 15 18 10 12 16 20 05 1 7 11 TREE-INSERT(T,4)

INSERTION 15 18 10 12 16 20 05 1 7 11 TREE-INSERT(T,14)

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, instead of to z 15 16 20 18 23 6 5 12 3 7 10 13 delete 15 16 20 18 23 6 5 12 3 7 10 z

Deletion Case 2: z has one child Delete z by making the parent of z point to z’s child, instead of to z Update the parent of z’s child to be z’s parent 15 16 20 18 23 6 5 12 3 7 10 13 delete 15 20 18 23 6 5 12 3 7 10 z

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. 6 15 16 20 18 23 7 6 12 3 10 13 15 16 20 18 23 6 5 12 3 7 10 13 delete z y

Idea for TREE-DELETE(T, z) Determine a node y that has to be deleted If z has only 1 child  y = z (case 2) If z has 2 children  y = TREE-SUCCESSOR(z) (case 3) In any case y has at most 1 child!!! Set a node x to the non-nil child of y Delete node y: set the parent of x to be the parent of y If the y is the root x becomes the new root otherwise, update parent pointers accordingly If the deleted node was the successor of z: move y’s key and satellite data onto z The deleted node y is returned for recycling

TREE-DELETE(T, z) if left[z] = NIL or right[z] = NIL then y ← z else y ← TREE-SUCCESSOR(z) if left[y]  NIL then x ← left[y] else x ← right[y] if x  NIL then p[x] ← p[y] z has one child z has 2 children 15 16 20 18 23 6 5 12 3 7 10 13 y x

TREE-DELETE(T, z) – cont. 15 16 20 18 23 6 5 12 3 7 10 13 y if p[y] = NIL then root[T] ← x else if y = left[p[y]] then left[p[y]] ← x else right[p[y]] ← x if y  z then key[z] ← key[y] copy y’s satellite data into z return y x

DELETION –case 1 15 18 10 12 16 20 05 1 7 11 z TREE-DELETE(T,11)

DELETION -case 1 15 18 10 12 16 20 05 1 7 TREE-DELETE(T,11)

DELETION –case 2 15 18 z 10 12 16 05 1 7 11 TREE-DELETE(T,18)

DELETION –case 2 15 y 18 z 10 12 16 05 1 7 11 TREE-DELETE(T,18)

DELETION –case 2 15 y 18 z 10 12 16 05 x 1 7 11 TREE-DELETE(T,18)

DELETION –case 2 15 y 18 z 10 12 16 05 x 1 7 11 TREE-DELETE(T,18)

DELETION –case 2 15 y x 16 z 10 12 05 1 7 11 TREE-DELETE(T,18)

DELETION –case 3 15 z 18 10 12 16 20 05 1 7 11 TREE-DELETE(T,10)

DELETION –case 3 15 z 18 10 12 16 20 05 1 7 11 y TREE-DELETE(T,18)

DELETION –case 3 15 z 18 10 12 16 20 05 1 7 11 y x = NIL TREE-DELETE(T,18)

DELETION –case 3 y 15 11 z 18 10 12 16 20 05 1 7 TREE-DELETE(T,18)

DELETION –case 3 15 18 11 12 16 20 05 1 7 TREE-DELETE(T,18)

DELETION 20 25 09 12 23 30 05 1 6 11 26 21 24 TREE-DELETE(T,30) TREE-ELETE(T,09) TREE-DELETE(T,6)

Suppose that we have numbers between 1 and 1000 stored in a binary search tree and we want to search for the number 259. Which of the following sequences could not be the sequence of keys examined? (a) 500, 300, 167, 290, 175, 259 (b) 999, 112, 602, 253, 411, 110, 259 (c) 19, 800, 310, 21, 176, 257, 258, 259

Suppose that we have numbers between 1 and 1000 stored in a binary search tree and we want to search for the number 259. Which of the following sequences could not be the sequence of keys examined? (a) 500, 300, 167, 290, 175, 259 (b) 999, 112, 602, 253, 411, 110, 259 (c) 19, 800, 310, 21, 176, 257, 258, 259

Suppose that we have numbers between 1 and 1000 in binary search tree and want to search for the number 363. Which of the following sequences could not be the sequence of nodes examined? 2,252,401,398,330,344,397,363 924,220,911,244,898,258,362,363 925,202,911,240,912,245,363 2,399,387,219,266,382,381,278,363 935,278,347,621,299,392,358,363

Suppose that we have numbers between 1 and 1000 in binary search tree and want to search for the number 363. Which of the following sequences could not be the sequence of nodes examined? 2,252,401,398,330,344,397,363 924,220,911,244,898,258,362,363 925,202,911,240,912,245,363 2,399,387,219,266,382,381,278,363 935,278,347,621,299,392,358,363

Binary Search Tree - 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