DAST Tirgul 7.

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.
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)
Binary Search Trees Comp 550.
1 Brief review of the material so far Recursive procedures, recursive data structures –Pseudocode for algorithms Example: algorithm(s) to compute a n Example:
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.
Analysis of Algorithms CS 477/677 Red-Black Trees Instructor: George Bebis (Chapter 14)
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 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:
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.
Data Structure & Algorithm II.  Delete-min  Building a heap in O(n) time  Heap Sort.
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
2IL50 Data Structures Fall 2015 Lecture 7: Binary Search Trees.
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.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 9.
Lecture 9 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
Red-Black Trees. Review: Binary Search Trees ● Binary Search Trees (BSTs) are an important data structure for dynamic sets ● In addition to satellite.
Binary Search Trees (BST)
Fundamentals of Algorithms MCS - 2 Lecture # 17. Binary Search Trees.
Lecture 91 Data Structures, Algorithms & Complexity Insertion and Deletion in BST GRIFFITH COLLEGE DUBLIN.
DAST Tirgul 6. What is a Binary Search Tree? The keys in a binary search tree (BST) are always stored in such a way as to satisfy the search property:
"Teachers open the door, but you must enter by yourself. "
Binary Search Trees What is a binary search tree?
BCA-II Data Structure Using C
Data Structures – LECTURE Balanced trees
Heaps, Heapsort, and Priority Queues
Heaps, Heap Sort and Priority Queues
Binary Search Trees.
CS 332: Algorithms Red-Black Trees David Luebke /20/2018.
Binary Search Tree (BST)
Data Structures Review Session 2
Summary of General Binary search tree
CS200: Algorithms Analysis
CS 583 Analysis of Algorithms
Lecture 7 Algorithm Analysis
Binary Trees, Binary Search Trees
Elementary Data Structures
Red-Black Trees.
What is a heap? Always keep the thing we are most interested in close to the top (and fast to access). Like a binary search tree, but less structured.
CS 583 Analysis of Algorithms
Heaps, Heapsort, and Priority Queues
Ch 6: Heapsort Ming-Te Chi
Lecture 3 / 4 Algorithm Analysis
"Teachers open the door, but you must enter by yourself. "
Binary Search Trees (13.1/12.1)
Lecture 7 Algorithm Analysis
Chapter 12: Binary Search Trees
CS6045: Advanced Algorithms
CS 583 Analysis of Algorithms
Lecture 7 Algorithm Analysis
Topic 6: Binary Search Tree Data structure Operations
Topic 5: Heap data structure heap sort Priority queue
HEAPS.
Heapsort Sorting in place
Binary SearchTrees [CLRS] – Chap 12.
Design and Analysis of Algorithms
Analysis of Algorithms CS 477/677
B-Trees.
Algorithms, CSCI 235, Spring 2019 Lecture 22—Red Black Trees
Binary Search Trees Comp 122, Spring 2004.
Chapter 12&13: Binary Search Trees (BSTs)
Asst. Prof. Dr. İlker Kocabaş
Red-Black Trees CS302 Data Structures
Presentation transcript:

DAST Tirgul 7

state exactly what you prove Proofs Most important thing to do in proofs: state exactly what you prove

We proved in ex5: A) Any node in an almost complete tree is a root of an almost complete tree. B) Any node in a max-heap is a root of a max-heap. C) We will show today the correctness proof of max_heapify.

Correctness Proof of max-heapify: ex5 - q 3c Claim: If heapify is called on a node x which is: Inside an almost complete tree And whose children are roots of max heaps, The result is: x becomes a root of a max heap. Nodes outside of the subtree of x are unchanged.

We will prove this by induction: Induction on the height (h) of the node x. (reminder: The height of a node is the length (in edges) of the longest path to a leaf.) Basis: h = 0. A leaf was and stays a valid heap. Assume for h. we now prove for h+1. If value of x is larger than its children – nothing happens, and the heap is already a max-heap, we are done. If the value of x is smaller than one of its children y and z, let y be the maximum of the children, so that x is swapped with y. We need to claim two things: Z is a valid heap. The recursive heapify works: After the swap, and after calling to max-heapify on y, y is a valid heap. We will then deduce that After the swap and max-heapify, x is a valid heap.

Correctness of heapify cont. z is the root of a valid heap: by our claim’s assumptions – z is a child of x and thus the root of a valid heap, and nothing changed in tree(z) during the swap and a the call to heapify on y (by our induction assumption). After the call to heapify, y is the root of a valid heap: Using claim assumptions on x: x’s children y,z are valid heaps. By B), y’s children are valid heaps. After the swap, this is still true. When we call heapify on y, the conditions of the claim hold. We can use the induction assumption since the height of y is smaller from that of x. Therefore, after heapify on y, y is the root of a valid heap. x is now the root of a heap because we swapped the original value of x with the maximum of its children, and by claim assumption, these children were heaps. Finally, Nothing outside the subtree of x changed. heapify on y did not change anything out of tree(y), and the only other thing that changed in tree(x) is the swap, which is inside tree(x).

Correctness of heapify - detailed The fact that z is the root of a valid heap is true by our claim assumptions – Z is a child of x and thus the root of a valid heap, and nothing changed in tree(z) during the swap and during the call to max-heapify on y (by our induction assumption). The fact that after the call to max_heapify y is the root of a valid heap is due to the following. Using claim assumptions: From the requirement on x, x’s children y and z are valid heaps. Therefore, before the swap, y is a root of a max heap. By B), y’s children are therefore valid max-heaps. After the swap, this is still true. When we call max_heapify on y, the conditions of the claim hold. We can use the induction assumption since the height of y is smaller by at least 1 from that of x. and therefore by the induction assumption, after max_heapify on y, y is the root of a max_heap. We now know that after the swap and max_heapify at y, z and y are roots of max_heaps. To deduce that x is now the root of a max_heap we just need to show that value in x is now greater than the value at all nodes in tree(z) and tree(y). but this is because we swapped the original value of x with the maximum of its children, and by claim assumption, these children were max-heaps and thus contained the max value of all values in their tree. so now x contains the maximum value in tree(x), and in particular is greater than the value of its children. Finally, we need to prove that nothing outside the subtree of x changed! This again can be proved by the same induction on h. Max_heapify on y did not change anything out of tree(y), and the only thing other than that that changed in tree(x) is the swap, which is inside tree(x). QED

Induction assumptions Induction assumption are crucial. It is very common that assumptions are simple properties. On the other hand, it is sometimes useful to use more general in the induction (e.g. in build-heap we demanded that at step j all nodes up to n-j are valid heaps).

What is a Binary Search Tree? The keys in a binary search tree (BST) are always stored in such a way as to satisfy the search property: Let x be a node in a BST. If y is a node in the left subtree of x, then key[y]≤ key[x]. If y is a node in the right subtree of x, then key[x] < key[y]. Different BSTs can represent the same set of values. The worst-case running time for most operations in search-tree is proportional to the height of the tree.

The BST can be unbalanced 2 3 5 7 8 5 7 3 2 8 A BST on 6 nodes with height 2. A less efficient BST with height 4 that contains the same keys.

BST-SEARCH TREE-SEARCH (x, k) if x= NIL or k = key[x] if k < key[x] then return x if k < key[x] then return TREE-SEARCH(left[x], k) 3. else return TREE-SEARCH(right[x], k) O(H), H = height of the tree

Successor TREE-SUCCESSOR(x) 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 ← parent[y] 7. return y Time is O(D); we either follow a path up the tree or follow a path down the tree

Predecessor Note: the structure of a BST allows us to determine the successor of a node without ever comparing keys. The procedure TREE-PREDECESSOR is symmetric to TREE-SUCCESSOR and also runs in time O(D).

Value = Set of words it appeared after Applications We store in the tree complex objects, having a comparison key and an additional value: Dictionary Key = a word Value = Synonyms Yellow pages Key = names Value = phone number Word statistics Key = word Value = Set of words it appeared after

Order Statistics The i’ th order statistic of a set of n elements is the i’ th smallest element. Sometimes called rank of an element. The minimum of a set of elements is the first order statistic (i = 1) The maximum is the n’ th order statistic (i = n). A median is the "halfway point" of the set (floor(n/2)).

Order Statistics How can you find the i’ th order statistic in a sorted array ? Time complexity? Find the i’ th order statistic in a BST. Naive Solution: Find minimum, and call successor i times. Worst case: O(n) Can we do better?

Finding Order Statistics Take One Design a BST such that each node holds its rank in the BST How to find the i’th element ? Time: O(h) What happens if we insert a new element? and delete?... 5/4 7/5 3/2 5/3 2/1 8/6

Finding Order Statistics Take Two Design a BST such that each node holds the size of its subtree (rooted in it) When searching for i’th element, each node checks the value of its left child Did we improve the time of insert and delete compared to take one? Can you think of a more simple solution? 5/6 7/2 3/3 5/1 2/1 8/1

Order statistics pseudo code Os-find(root, j) 1. left_size = 0; 2. if has_left(root) {left_size = size(left(root)) } 3. if left_size >= j 4. return Os-find(left(root),j) 5. else if left_size = j-1 6. return value(root) 7. else return Os-find(right(root),j-left_size -1)

More things to do with size Actually, each node holds the size of the left subtree. Given a and b, how can we find the number of elements x such that a ≤ x ≤ b ? Try recursion… 5/4 7/1 3/2 5/1 2/1 8/1