CS210-Data Structures-Module-29-Binary-Heap-II

Total Page:16

File Type:pdf, Size:1020Kb

CS210-Data Structures-Module-29-Binary-Heap-II Data Structures and Algorithms (CS210A) Lecture 29: • Building a Binary heap on 풏 elements in O(풏) time. • Applications of Binary heap : sorting • Binary trees: beyond searching and sorting 1 Recap from the last lecture 2 A complete binary tree How many leaves are there in a Complete Binary tree of size 풏 ? 풏/ퟐ 3 Building a Binary heap Problem: Given 풏 elements {푥0, …, 푥푛−1}, build a binary heap H storing them. Trivial solution: (Building the Binary heap incrementally) CreateHeap(H); For( 풊 = 0 to 풏 − ퟏ ) What is the time Insert(푥,H); complexity of this algorithm? 4 Building a Binary heap incrementally What useful inference can you draw from Top-down this Theorem ? approach The time complexity for inserting a leaf node = ?O (log 풏 ) # leaf nodes = 풏/ퟐ , Theorem: Time complexity of building a binary heap incrementally is O(풏 log 풏). 5 Building a Binary heap incrementally What useful inference can you draw from Top-down this Theorem ? approach The O(풏) time algorithm must take O(1) time for each of the 풏/ퟐ leaves. 6 Building a Binary heap incrementally Top-down approach 7 Think of alternate approach for building a binary heap In any complete 98 binaryDoes treeit suggest, how a manynew nodes approach satisfy to heapbuild propertybinary heap ? ? 14 33 all leaf 37 11 52 32 nodes 41 21 76 85 17 25 88 29 Bottom-up approach 47 75 9 57 23 heap property: “Every ? node stores value smaller than its children” We just need to ensure this property at each node. 8 Think of alternate approach for building a binary heap 98 14 33 37 11 52 32 41 21 76 85 17 25 88 29 Bottom-up approach 47 75 9 57 23 heap property: “Every ? node stores value smaller than its children” We just need to ensure this property at each node. 9 A new approach to build binary heap 1. Just copy the given 풏 elements {푥0, …, 푥푛−1} into an array H. 2. The heap property holds for all the leaf nodes in the corresponding complete binary tree. 3. Leaving all the leaf nodes, process the elements in the decreasing order of their numbering and set the heap property for each of them. 10 A new approach to build binary heap 98 14 33 37 11 52 32 85 17 25 88 41 21 29 76 The first node to be 47 75 9 57 23 processed. H 98 14 33 37 11 52 32 85 17 25 88 41 21 29 76 47 75 9 57 23 11 A new approach to build binary heap 98 14 33 37 11 52 32 85 17 23 88 41 21 29 76 The second node to 47 75 9 57 25 be processed. H 98 14 33 37 11 52 32 85 17 23 88 41 21 29 76 47 75 9 57 25 12 A new approach to build binary heap 98 14 33 37 11 52 32 85 9 23 88 41 21 29 76 The third node to 47 75 17 57 25 be processed. H 98 14 33 37 11 52 32 85 9 23 88 41 21 29 76 47 75 17 57 25 13 A new approach to build binary heap 98 14 33 37 11 52 32 47 9 23 88 41 21 29 76 85 75 17 57 25 H 98 14 33 37 11 52 32 47 9 23 88 41 21 29 76 85 75 17 57 25 14 A new approach to build binary heap 98 v 54 21 9 11 33 29 47 17 23 88 41 52 32 76 85 75 37 57 25 H 98 54 21 9 11 33 29 47 17 23 88 41 52 32 76 85 75 37 57 25 15 A new approach to build binary heap 98 v 9 21 54 11 33 29 47 17 23 88 41 52 32 76 85 75 37 57 25 H 98 54 21 9 11 33 29 47 17 23 88 41 52 32 76 85 75 37 57 25 16 A new approach to build binary heap 98 v 9 21 17 11 33 29 47 54 23 88 41 52 32 76 85 75 37 57 25 H 98 54 21 9 11 33 29 47 17 23 88 41 52 32 76 85 75 37 57 25 17 A new approach to build binary heap 98 v 9 21 17 11 33 29 47 37 23 88 41 52 32 76 85 75 54 57 25 H 98 54 21 9 11 33 29 47 17 23 88 41 52 32 76 85 75 37 57 25 Let v be a node corresponding to index i in H. The process of restoring heap property at i called Heapify(i,H). 18 Heapify(풊,H) Heapify(풊,H) { 풏 size(H) -1 ; While ( ? and ? ) { For node 풊, compare its value with those of its children If it is smaller than any of its children Swap it with smallest child and move down … Else stop ! } } 19 Heapify(풊,H) Heapify(풊,H) { 풏 size(H) -1 ; Flag true; While ( 풊 ≤ ( 풏 −?1 ) / 2 and Flag ?= true ) { 풎풊풏 풊; If( H[ 풊 ]>H[ ?ퟐ 풊 + ퟏ ] ) 풎풊풏 ퟐ풊 + ퟏ; If( ퟐ 풊 + ퟐ ≤ 풏 and ? H[ 풎풊풏 ]>H[ ퟐ 풊 + ퟐ ] ) 풎풊풏 ퟐ풊 + ퟐ; If(풎풊풏 ≠ 풊) { H(풊) ↔ H(풎풊풏); 풊 풎풊풏; } else Flag false; } } 20 Building Binary heap in O(n) time How many nodes of height ℎ can there be 98 in a complete Binary Time to heapify node v ? tree of 풏 nodes ? v 54 21 9 11 33 29 Height( v) 47 17 23 88 41 52 32 76 85 75 37 57 25 H 98 54 21 9 11 33 29 47 17 23 88 41 52 32 76 85 75 37 57 25 Time complexity of algorithm = ? 푶(ℎ) ∙ 푵(ℎ) 21 ℎ No. of nodes of height ℎ A complete binary tree How many nodes of height ℎ can there be Hence the number of nodes 푛 of height ℎ is bounded by in a complete Binary ퟐ풉 tree of 풏 nodes ? Each subtree is also a complete binary tree. A subtree of height ℎ has at least 2ℎ nodes Moreover, no two subtrees of height ℎ in the given tree have any element in common22 Building Binary heap in O(n) time 푛 Lemma: the number of nodes of height ℎ is bounded by . ퟐℎ log 푛 푛 Hence Time complexity to build the heap = 푶(ℎ) 풉=1 ퟐℎ log 푛 ℎ = 푛 푐 =1 ퟐℎ = O(푛) As an exercise (using knowledge from your JEE log 푛 preparation days), show that ℎ is bounded by 2 ℎ=1 ퟐ풉 23 Sorting using a Binary heap 24 Sorting using heap Build heap H on the given 풏 elements; While (H is not empty) { x Extract-min(H); print x; } This is HEAP SORT algorithm Time complexity : O(풏 log 풏) Question: Which is the best sorting algorithm : (Merge sort, Heap sort, Quick sort) ? Answer: Practice programming assignment 25 Binary trees: beyond searching and sorting • Elegant solution for two interesting problem • An important lesson: Lack of proper understanding of a problem is a big hurdle to solve the problem 26 Two interesting problems on sequences 27 What is a sequence ? A sequence S = ≺ 푥0, …, 푥푛−1 ≻ • Can be viewed as a mapping from [0, 푛]. • Order does matter. 28 Problem 1 Multi-increment 29 Problem 1 Given an initial sequence S = ≺ 푥0, …, 푥푛−1 ≻ of numbers, maintain a compact data structure to perform the following operations: • ReportElement(풊): Report the current value of 푥. • Multi-Increment(풊, 풋, ∆): Add ∆ to each 푥푘 for each 풊 ≤ 풌 ≤ 풋 Example: Let the initial sequence be S = ≺ 14, 12, 23, 12, 111, 51, 321, -40 ≻ After Multi-Increment(2,6,10), S becomes ≺ 14, 12, 33, 22, 121, 61, 331, -40 ≻ After Multi-Increment(0,4,25), S becomes ≺ 39, 37, 58, 47, 146, 61, 331, -40 ≻ After Multi-Increment(2,5,31), S becomes ≺ 39, 37, 89, 78, 177, 92, 331, -40 ≻ 30 Problem 1 Given an initial sequence S = ≺ 푥0, …, 푥푛−1 ≻ of numbers, maintain a compact data structure to perform the following operations: • ReportElement(풊): Report the current value of 푥. • Multi-Increment(풊, 풋, ∆): Add ∆ to each 푥푘 for each 풊 ≤ 풌 ≤ 풋 Trivial solution : Store S in an array A[0.. 풏-1] such that A[풊] stores the current value of 푥풊. Multi-Increment(풊, 풋, ∆) { 풊 ≤ 풌 ≤ 풋 풌 풌 For ( ) A[ ] A[ ]+ ∆; O(풋 − 풊) = O(풏) } ReportElement(풊){ return A[풊] } O(1) 31 Problem 1 Given an initial sequence S = ≺ 푥0, …, 푥푛−1 ≻ of numbers, maintain a compact data structure to perform the following operations: • ReportElement(풊): Report the current value of 푥. • Multi-Increment(풊, 풋, ∆): Add ∆ to each 푥푘 for each 풊 ≤ 풌 ≤ 풋 Trivial solution : Store S in an array A[0.. 풏-1] such that A[풊] stores the current value of 푥풊. Question: the source of difficulty in breaking the O (n) barrier for Multi-Increment() ? Answer: we need to explicitly maintain in S. Question: who asked/inspired us to maintain S explicitly. Answer: 1. incomplete understanding of the problem 2. conditioning based on incomplete understanding 32 Towards efficient solution of Problem 1 Assumption: without loss of generality assume 풏 is power of 2. Explore ways to maintain sequence S implicitly such that • Multi-Increment(풊, 풋, ∆) is efficient • Report(풊) is efficient too.
Recommended publications
  • Data Structures and Algorithms Binary Heaps (S&W 2.4)
    Data structures and algorithms DAT038/TDA417, LP2 2019 Lecture 12, 2019-12-02 Binary heaps (S&W 2.4) Some slides by Sedgewick & Wayne Collections A collection is a data type that stores groups of items. stack Push, Pop linked list, resizing array queue Enqueue, Dequeue linked list, resizing array symbol table Put, Get, Delete BST, hash table, trie, TST set Add, ontains, Delete BST, hash table, trie, TST A priority queue is another kind of collection. “ Show me your code and conceal your data structures, and I shall continue to be mystified. Show me your data structures, and I won't usually need your code; it'll be obvious.” — Fred Brooks 2 Priority queues Collections. Can add and remove items. Stack. Add item; remove the item most recently added. Queue. Add item; remove the item least recently added. Min priority queue. Add item; remove the smallest item. Max priority queue. Add item; remove the largest item. return contents contents operation argument value size (unordered) (ordered) insert P 1 P P insert Q 2 P Q P Q insert E 3 P Q E E P Q remove max Q 2 P E E P insert X 3 P E X E P X insert A 4 P E X A A E P X insert M 5 P E X A M A E M P X remove max X 4 P E M A A E M P insert P 5 P E M A P A E M P P insert L 6 P E M A P L A E L M P P insert E 7 P E M A P L E A E E L M P P remove max P 6 E M A P L E A E E L M P A sequence of operations on a priority queue 3 Priority queue API Requirement.
    [Show full text]
  • Priority Queues and Binary Heaps Chapter 6.5
    Priority Queues and Binary Heaps Chapter 6.5 1 Some animals are more equal than others • A queue is a FIFO data structure • the first element in is the first element out • which of course means the last one in is the last one out • But sometimes we want to sort of have a queue but we want to order items according to some characteristic the item has. 107 - Trees 2 Priorities • We call the ordering characteristic the priority. • When we pull something from this “queue” we always get the element with the best priority (sometimes best means lowest). • It is really common in Operating Systems to use priority to schedule when something happens. e.g. • the most important process should run before a process which isn’t so important • data off disk should be retrieved for more important processes first 107 - Trees 3 Priority Queue • A priority queue always produces the element with the best priority when queried. • You can do this in many ways • keep the list sorted • or search the list for the minimum value (if like the textbook - and Unix actually - you take the smallest value to be the best) • You should be able to estimate the Big O values for implementations like this. e.g. O(n) for choosing the minimum value of an unsorted list. • There is a clever data structure which allows all operations on a priority queue to be done in O(log n). 107 - Trees 4 Binary Heap Actually binary min heap • Shape property - a complete binary tree - all levels except the last full.
    [Show full text]
  • CS 270 Algorithms
    CS 270 Algorithms Week 10 Oliver Kullmann Binary heaps Sorting Heapification Building a heap 1 Binary heaps HEAP- SORT Priority 2 Heapification queues QUICK- 3 Building a heap SORT Analysing 4 QUICK- HEAP-SORT SORT 5 Priority queues Tutorial 6 QUICK-SORT 7 Analysing QUICK-SORT 8 Tutorial CS 270 General remarks Algorithms Oliver Kullmann Binary heaps Heapification Building a heap We return to sorting, considering HEAP-SORT and HEAP- QUICK-SORT. SORT Priority queues CLRS Reading from for week 7 QUICK- SORT 1 Chapter 6, Sections 6.1 - 6.5. Analysing 2 QUICK- Chapter 7, Sections 7.1, 7.2. SORT Tutorial CS 270 Discover the properties of binary heaps Algorithms Oliver Running example Kullmann Binary heaps Heapification Building a heap HEAP- SORT Priority queues QUICK- SORT Analysing QUICK- SORT Tutorial CS 270 First property: level-completeness Algorithms Oliver Kullmann Binary heaps In week 7 we have seen binary trees: Heapification Building a 1 We said they should be as “balanced” as possible. heap 2 Perfect are the perfect binary trees. HEAP- SORT 3 Now close to perfect come the level-complete binary Priority trees: queues QUICK- 1 We can partition the nodes of a (binary) tree T into levels, SORT according to their distance from the root. Analysing 2 We have levels 0, 1,..., ht(T ). QUICK- k SORT 3 Level k has from 1 to 2 nodes. Tutorial 4 If all levels k except possibly of level ht(T ) are full (have precisely 2k nodes in them), then we call the tree level-complete. CS 270 Examples Algorithms Oliver The binary tree Kullmann 1 ❚ Binary heaps ❥❥❥❥ ❚❚❚❚ ❥❥❥❥ ❚❚❚❚ ❥❥❥❥ ❚❚❚❚ Heapification 2 ❥ 3 ❖ ❄❄ ❖❖ Building a ⑧⑧ ❄ ⑧⑧ ❖❖ heap ⑧⑧ ❄ ⑧⑧ ❖❖❖ 4 5 6 ❄ 7 ❄ HEAP- ⑧ ❄ ⑧ ❄ SORT ⑧⑧ ❄ ⑧⑧ ❄ Priority 10 13 14 15 queues QUICK- is level-complete (level-sizes are 1, 2, 4, 4), while SORT ❥ 1 ❚❚ Analysing ❥❥❥❥ ❚❚❚❚ QUICK- ❥❥❥ ❚❚❚ SORT ❥❥❥ ❚❚❚❚ 2 ❥❥ 3 ♦♦♦ ❄❄ ⑧ Tutorial ♦♦ ❄❄ ⑧⑧ ♦♦♦ ⑧ 4 ❄ 5 ❄ 6 ❄ ⑧⑧ ❄❄ ⑧ ❄ ⑧ ❄ ⑧⑧ ❄ ⑧⑧ ❄ ⑧⑧ ❄ 8 9 10 11 12 13 is not (level-sizes are 1, 2, 3, 6).
    [Show full text]
  • Kd Trees What's the Goal for This Course? Data St
    Today’s Outline - kd trees CSE 326: Data Structures Too much light often blinds gentlemen of this sort, Seeing the forest for the trees They cannot see the forest for the trees. - Christoph Martin Wieland Hannah Tang and Brian Tjaden Summer Quarter 2002 What’s the goal for this course? Data Structures - what’s in a name? Shakespeare It is not possible for one to teach others, until one can first teach herself - Confucious • Stacks and Queues • Asymptotic analysis • Priority Queues • Sorting – Binary heap, Leftist heap, Skew heap, d - heap – Comparison based sorting, lower- • Trees bound on sorting, radix sorting – Binary search tree, AVL tree, Splay tree, B tree • World Wide Web • Hash Tables – Open and closed hashing, extendible, perfect, • Implement if you had to and universal hashing • Understand trade-offs between • Disjoint Sets various data structures/algorithms • Graphs • Know when to use and when not to – Topological sort, shortest path algorithms, Dijkstra’s algorithm, minimum spanning trees use (Prim’s algorithm and Kruskal’s algorithm) • Real world applications Range Query Range Query Example Y A range query is a search in a dictionary in which the exact key may not be entirely specified. Bellingham Seattle Spokane Range queries are the primary interface Tacoma Olympia with multi-D data structures. Pullman Yakima Walla Walla Remember Assignment #2? Give an algorithm that takes a binary search tree as input along with 2 keys, x and y, with xÃÃy, and ÃÃ ÃÃ prints all keys z in the tree such that x z y. X 1 Range Querying in 1-D
    [Show full text]
  • Readings Findmin Problem Priority Queue
    Readings • Chapter 6 Priority Queues & Binary Heaps › Section 6.1-6.4 CSE 373 Data Structures Winter 2007 Binary Heaps 2 FindMin Problem Priority Queue ADT • Quickly find the smallest (or highest priority) • Priority Queue can efficiently do: item in a set ›FindMin() • Applications: • Returns minimum value but does not delete it › Operating system needs to schedule jobs according to priority instead of FIFO › DeleteMin( ) › Event simulation (bank customers arriving and • Returns minimum value and deletes it departing, ordered according to when the event › Insert (k) happened) • In GT Insert (k,x) where k is the key and x the value. In › Find student with highest grade, employee with all algorithms the important part is the key, a highest salary etc. “comparable” item. We’ll skip the value. › Find “most important” customer waiting in line › size() and isEmpty() Binary Heaps 3 Binary Heaps 4 List implementation of a Priority BST implementation of a Priority Queue Queue • What if we use unsorted lists: • Worst case (degenerate tree) › FindMin and DeleteMin are O(n) › FindMin, DeleteMin and Insert (k) are all O(n) • In fact you have to go through the whole list • Best case (completely balanced BST) › Insert(k) is O(1) › FindMin, DeleteMin and Insert (k) are all O(logn) • What if we used sorted lists • Balanced BSTs › FindMin and DeleteMin are O(1) › FindMin, DeleteMin and Insert (k) are all O(logn) • Be careful if we want both Min and Max (circular array or doubly linked list) › Insert(k) is O(n) Binary Heaps 5 Binary Heaps 6 1 Better than a speeding BST Binary Heaps • A binary heap is a binary tree (NOT a BST) that • Can we do better than Balanced Binary is: Search Trees? › Complete: the tree is completely filled except • Very limited requirements: Insert, possibly the bottom level, which is filled from left to FindMin, DeleteMin.
    [Show full text]
  • Binary Trees, Binary Search Trees
    Binary Trees, Binary Search Trees www.cs.ust.hk/~huamin/ COMP171/bst.ppt Trees • Linear access time of linked lists is prohibitive – Does there exist any simple data structure for which the running time of most operations (search, insert, delete) is O(log N)? Trees • A tree is a collection of nodes – The collection can be empty – (recursive definition) If not empty, a tree consists of a distinguished node r (the root), and zero or more nonempty subtrees T1, T2, ...., Tk, each of whose roots are connected by a directed edge from r Some Terminologies • Child and parent – Every node except the root has one parent – A node can have an arbitrary number of children • Leaves – Nodes with no children • Sibling – nodes with same parent Some Terminologies • Path • Length – number of edges on the path • Depth of a node – length of the unique path from the root to that node – The depth of a tree is equal to the depth of the deepest leaf • Height of a node – length of the longest path from that node to a leaf – all leaves are at height 0 – The height of a tree is equal to the height of the root • Ancestor and descendant – Proper ancestor and proper descendant Example: UNIX Directory Binary Trees • A tree in which no node can have more than two children • The depth of an “average” binary tree is considerably smaller than N, eventhough in the worst case, the depth can be as large as N – 1. Example: Expression Trees • Leaves are operands (constants or variables) • The other nodes (internal nodes) contain operators • Will not be a binary tree if some operators are not binary Tree traversal • Used to print out the data in a tree in a certain order • Pre-order traversal – Print the data at the root – Recursively print out all data in the left subtree – Recursively print out all data in the right subtree Preorder, Postorder and Inorder • Preorder traversal – node, left, right – prefix expression • ++a*bc*+*defg Preorder, Postorder and Inorder • Postorder traversal – left, right, node – postfix expression • abc*+de*f+g*+ • Inorder traversal – left, node, right.
    [Show full text]
  • Nearest Neighbor Searching and Priority Queues
    Nearest neighbor searching and priority queues Nearest Neighbor Search • Given: a set P of n points in Rd • Goal: a data structure, which given a query point q, finds the nearest neighbor p of q in P or the k nearest neighbors p q Variants of nearest neighbor • Near neighbor (range search): find one/all points in P within distance r from q • Spatial join: given two sets P,Q, find all pairs p in P, q in Q, such that p is within distance r from q • Approximate near neighbor: find one/all points p’ in P, whose distance to q is at most (1+ε) times the distance from q to its nearest neighbor Solutions Depends on the value of d: • low d: graphics, GIS, etc. • high d: – similarity search in databases (text, images etc) – finding pairs of similar objects (e.g., copyright violation detection) Nearest neighbor search in documents • How could we represent documents so that we can define a reasonable distance between two documents? • Vector of word frequency occurrences – Probably want to get rid of useless words that occur in all documents – Probably need to worry about synonyms and other details from language – But basically, we get a VERY long vector • And maybe we ignore the frequencies and just idenEfy with a “1” the words that occur in some document. Nearest neighbor search in documents • One reasonable measure of distance between two documents is just a count of words they share – this is just the point wise product of the two vectors when we ignore counts.
    [Show full text]
  • Binary Heaps
    Binary Heaps COL 106 Shweta Agrawal and Amit Kumar Revisiting FindMin • Application: Find the smallest ( or highest priority) item quickly – Operating system needs to schedule jobs according to priority instead of FIFO – Event simulation (bank customers arriving and departing, ordered according to when the event happened) – Find student with highest grade, employee with highest salary etc. 2 Priority Queue ADT • Priority Queue can efficiently do: – FindMin (and DeleteMin) – Insert • What if we use… – Lists: If sorted, what is the run time for Insert and FindMin? Unsorted? – Binary Search Trees: What is the run time for Insert and FindMin? – Hash Tables: What is the run time for Insert and FindMin? 3 Less flexibility More speed • Lists – If sorted: FindMin is O(1) but Insert is O(N) – If not sorted: Insert is O(1) but FindMin is O(N) • Balanced Binary Search Trees (BSTs) – Insert is O(log N) and FindMin is O(log N) • Hash Tables – Insert O(1) but no hope for FindMin • BSTs look good but… – BSTs are efficient for all Finds, not just FindMin – We only need FindMin 4 Better than a speeding BST • We can do better than Balanced Binary Search Trees? – Very limited requirements: Insert, FindMin, DeleteMin. The goals are: – FindMin is O(1) – Insert is O(log N) – DeleteMin is O(log N) 5 Binary Heaps • A binary heap is a binary tree (NOT a BST) that is: – Complete: the tree is completely filled except possibly the bottom level, which is filled from left to right – Satisfies the heap order property • every node is less than or equal to its children
    [Show full text]
  • CMSC132 Summer 2017 Midterm 2
    CMSC132 Summer 2017 Midterm 2 First Name (PRINT): ___________________________________________________ Last Name (PRINT): ___________________________________________________ I pledge on my honor that I have not given or received any unauthorized assistance on this examination. Your signature: _____________________________________________________________ Instructions · This exam is a closed-book and closed-notes exam. · Total point value is 100 points. · The exam is a 80 minutes exam. · Please use a pencil to complete the exam. · WRITE NEATLY. If we cannot understand your answer, we will not grade it (i.e., 0 credit). 1. Multiple Choice / 25 2. Short Answer / 45 3. Programming / 30 Total /100 1 1. [25 pts] Multiple Choice Identify the choice that best completes the statement or answers the question. Circle your answer. 1) [2] The cost of inserting or removing an element to/from a heap is log(N), where N is the total number of elements in the heap. The reason for that is: a) Heaps keep their entries sorted b) Heaps are balanced BST. c) Heaps are balanced binary trees. d) Heaps are a version of Red-Black trees 2) [2] What is the output of fun(2)? int fun(int n){ if (n == 4) return n; else return 2*fun(n+1); } a) 4 b) 8 c) 16 d) Runtime Error 3) [2] 2-3-4 Tree guarantees searching in ______________________time. a) O(log n) b) O (n) c) O (1) d) O (n log n) 4) [2] What is the advantage of an iterative method over a recursive one? a. It makes fewer calls b. It has less overhead c. It is easier to write, since you can use the method within itself.
    [Show full text]
  • CS210-Data Structures-Module-17-RB-Tree-I
    Data Structures and Algorithms (CS210A) Lecture 17: Height balanced BST • Red-black trees 1 Terminologies Full binary tree: A binary tree where every internal node has exactly two children. This node has exactly one child. So the current tree is not a full binary tree. 2 Terminologies Complete binary tree: A full binary tree where every leaf node is at the same level. We shall later extend this definition when we discuss “Binary heap”. 3 Binary Search Tree root 46 v 28 67 5 35 49 2 25 31 41 48 53 Definition: A Binary Tree T storing values is said to be Binary Search Tree if for each node v in T • If left(v) <> NULL, then value(v) > value of every node in subtree(left(v)). • If right(v)<>NULL, then value(v) < value of every node in subtree(right(v)). 4 Binary Search Tree: a slight change This transformation is merely to help us in root the analysis of red-black trees. It does not cause any extra overhead of space. All NULL nodes correspond to a single node 46 in memory. 28 67 5 35 49 2 25 31 41 48 53 Henceforth, for each NULL child link of a node in a BST, we create a NULL node. 1. Each leaf node in a BST will be a NULL node. 2. the BST will always be a full binary tree. 5 A fact we noticed in our previous discussion on BSTs (Lecture 9) Time complexity of Search(T,x) and Insert(T,x) in a Binary Search Tree T = O ??(Height( T)) Height(T): The maximum number of nodes on any path from root to a leaf node.
    [Show full text]
  • Data Structures
    Data Structures Instructor: Sharma Thankachan Lecture 4: Heap 1 Slides modified from Dr. Hon, with permission About this lecture • Introduce Heap – Shape Property and Heap Property – Heap Operations • Heapsort: Use Heap to Sort • Fixing heap property for all nodes • Use Array to represent Heap • Introduce Priority Queue 2 Heap A heap (or binary heap) is a binary tree that satisfies both: (1) Shape Property – All levels, except deepest, are fully filled – Deepest level is filled from left to right (2) Heap Property – Value of a node · Value of its children 3 Satisfying Shape Property Example of a tree with shape property 4 Not Satisfying Shape Property This level (not deepest) is NOT fully filled 5 Not Satisfying Shape Property Deepest level NOT filled from left to right 6 Satisfying Heap Property 7 Not Satisfying Heap Property Help! 8 Min Heap Q. Given a heap, what is so special about the root’s value? A. … always the minimum Because of this, the previous heap is also called a min heap 9 Heap Operations • Find-Min : find the minimum value è Q(1) time • Extract-Min : delete the minimum value è O(log n) time (how??) • Insert : insert a new value into heap è O(log n) time (how??) n = # nodes in the heap 10 How to do Extract-Min? 3 8 4 13 23 12 24 43 38 Heap before Extract-Min 11 Step 1: Restore Shape Property 3 8 4 13 23 12 24 43 38 Copy value of last node to root. Next, remove last node 12 Step 2: Restore Heap Property 38 8 4 13 23 12 24 Next, highlight the root 43 è Only this node may violate heap property If violates, swap highlighted
    [Show full text]
  • Binary Heaps
    Binary Heaps CSE 373 Data Structures Readings • Chapter 8 › Section 8.3 Binary Heaps 2 BST implementation of a Priority Queue • Worst case (degenerate tree) › FindMin, DeleteMin and Insert (k) are all O(n) • Best case (completely balanced BST) › FindMin, DeleteMin and Insert (k) are all O(logn) • Balanced BSTs (next topic after heaps) › FindMin, DeleteMin and Insert (k) are all O(logn) Binary Heaps 3 Better than a speeding BST • We can do better than Balanced Binary Search Trees? › Very limited requirements: Insert, FindMin, DeleteMin. The goals are: › FindMin is O(1) › Insert is O(log N) › DeleteMin is O(log N) Binary Heaps 4 Binary Heaps • A binary heap is a binary tree (NOT a BST) that is: › Complete: the tree is completely filled except possibly the bottom level, which is filled from left to right › Satisfies the heap order property • every node is less than or equal to its children • or every node is greater than or equal to its children • The root node is always the smallest node › or the largest, depending on the heap order Binary Heaps 5 Heap order property • A heap provides limited ordering information • Each path is sorted, but the subtrees are not sorted relative to each other › A binary heap is NOT a binary search tree -1 2 1 0 1 4 6 2 6 0 7 5 8 4 7 These are all valid binary heaps (minimum) Binary Heaps 6 Binary Heap vs Binary Search Tree Binary Heap Binary Search Tree min value 5 94 10 94 10 97 min value 97 24 5 24 Parent is less than both Parent is greater than left left and right children child, less than right child Binary
    [Show full text]