October 3, 20021 Algorithms and Data Structures Lecture VII Simonas Šaltenis Nykredit Center for Database Research Aalborg University

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.
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.
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.
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.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu.
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.
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.
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.
The Linked List (LL) Each node x in a linked list contains: Key(x) head key[x]- The value stored at x. next[x]- Pointer to left child of x. The while list.
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.
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?
Binary Search Trees.
Data Structures Review Session 2
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)
ძებნის ორობითი ხეები BST (Binary Search Trees)
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
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)
Presentation transcript:

October 3, Algorithms and Data Structures Lecture VII Simonas Šaltenis Nykredit Center for Database Research Aalborg University

October 3, This Lecture Binary Search Trees Tree traversals Searching Insertion Deletion

October 3, Dictionaries Dictionary ADT – a dynamic set with methods: Search(S, k) – a query method that returns a pointer x to an element where x.key = k Insert(S, x) – a modifier method that adds the element pointed to by x to S Delete(S, x) – a modifier method that removes the element pointed to by x from S An element has a key part and a satellite data part

October 3, Ordered Dictionaries In addition to dictionary functionality, we want to support priority-queue-type operations: Min(S) Max(S) Partial-order supported by priority-queues is not enough. We want to support Predecessor(S, k) Successor(S, k)

October 3, A List-Based Implementation Unordered list searching takes O(n) time inserting takes O(1) time Ordered list searching takes O(n) time inserting takes O(n) time Using array would definitely improve search time.

October 3, Binary Search Narrow down the search range in stages findElement(22)

October 3, Running Time The range of candidate items to be searched is halved after comparing the key with the middle element Binary search runs in O(lg n) time (remember recurrence...) What about insertion and deletion?

October 3, Binary Search Trees A binary search tree is a binary tree T such that each internal node stores an item (k,e) of a dictionary keys stored at nodes in the left subtree of v are less than or equal to k keys stored at nodes in the right subtree of v are greater than or equal to k Example sequence 2,3,5,5,7,8

October 3, The Node Structure Each node in the tree contains key[x] – key left[x] – pointer to left child right[x] – pt. to right child p[x] – pt. to parent node

October 3, Tree Walks Keys in the BST can be printed using "tree walks" Keys of each node printed between keys in the left and right subtree – inroder tree traversal Prints elements in monotonically increasing order Running time  (n) InorderTreeWalk(x) 01 if x  NIL then 02 InorderTreeWalk(left[x]) 03 print key[x] 04 InorderTreeWalk(right[x])

October 3, Tree Walks (2) ITW can be thought of as a projection of the BST nodes onto a one dimensional interval

October 3, Tree Walks (3) A preorder tree walk processes each node before processing its children A postorder tree walk processes each node after processing its children

October 3, Tree Walks (4) Printing an arithmetic expression - so called Euler’s walk: Print “(“ before traversing the left subtree, traverse it Print the value of a node Traverse the right subtree, print “)” after traversing it

October 3, Searching a BST To find an element with key k in a tree T compare k with key[root[T]] if k < key[root[T]], search for k in left[root[T]] otherwise, search for k in right[root[T]]

October 3, Recursive version Pseudocode for BST Search Search(T,k) 01 x  root[T] 02 if x = NIL then return NIL 03 if k = key[x] then return x 04 if k < key[x] 05 then return Search(left[x],k) 06 else return Search(right[x],k) Search(T,k) 01 x  root[T] 02 while x  NIL and k  key[x] do 03 if k < key[x] 04 then x  left[x] 05 else x  right[x] 06 return x Iterative version

October 3, Search Examples Search(T, 11)

October 3, Search Examples (2) Search(T, 6)

October 3, Analysis of Search Running time on tree of height h is O(h) After the insertion of n keys, the worst- case running time of searching is O(n)

October 3, BST Minimum (Maximum) Find the minimum key in a tree rooted at x (compare to a solution for heaps) Running time O(h), i.e., it is proportional to the height of the tree TreeMinimum(x) 01 while left[x]  NIL 02 do x  left[x] 03 return x

October 3, Successor Given x, find the node with the smallest key greater than key[x] We can distinguish two cases, depending on the right subtree of x Case 1 right subtree of x is nonempty successor is leftmost node in the right subtree (Why?) this can be done by returning TreeMinimum(right[x])

October 3, Successor (2) Case 2 the right subtree of x is empty successor is the lowest ancestor of x whose left child is also an ancestor of x (Why?)

October 3, For a tree of height h, the running time is O(h) Successor Pseudocode TreeSuccessor(x) 01 if right[x]  NIL 02 then return TreeMinimum(right[x]) 03 y  p[x] 04 while y  NIL and x = right[y] 05 x  y 06 y  p[y] 03 return y

October 3, BST Insertion The basic idea is similar to searching take an element z (whose left and right children are NIL) and insert it into T find place in T where z belongs (as if searching for z), and add z there The running on a tree of height h is O(h), i.e., it is proportional to the height of the tree

October 3, BST Insertion Pseudo Code TreeInsert(T,z) 01 y  NIL 02 x  root[T] 03 while x  NIL 04 y  x 05 if key[z] < key[x] 06 then x  left[x] 07 else x  right[x] 08 p[z]  y 09 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

October 3, BST Insertion Example Insert 8

October 3, BST Insertion: Worst Case In what kind of sequence should the insertions be made to produce a BST of height n?

October 3, BST Sorting Use TreeInsert and InorderTreeWalk to sort a list of n elements, A TreeSort(A) 01 root[T]  NIL 02 for i  1 to n 03 TreeInsert(T,A[i]) 04 InorderTreeWalk(root[T])

October 3, Sort the following numbers Build a binary search tree Call InorderTreeWalk BST Sorting (2)

October 3, Deletion Delete node x from a tree T We can distinguish three cases x has no children x has one child x has two children

October 3, Deletion Case 1 If x has no children – just remove x

October 3, Deletion Case 2 If x has exactly one child, then to delete x, simply make p[x] point to that child

October 3, Deletion Case 3 If x has two children, then to delete it we have to find its successor (or predecessor) y remove y (note that y has at most one child – why?) replace x with y

October 3, Delete Pseudocode TreeDelete(T,z) 01 if left[z]  NIL or right[z] = NIL 02 then y  z 03 else y  TreeSuccessor(z) 04 if left[y]  NIL 05 then x  left[y] 06 else x  right[y] 07 if x  NIL 08 then p[x]  p[y] 09 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] //copy all fileds of y 16 return y

October 3, Balanced Search Trees Problem: worst-case execution time for dynamic set operations is  (n) Solution: balanced search trees guarantee small height!

October 3, Next Week Balanced Binary Search Trees: Red-Black Trees