1 / 16

Efficient Operations on Binary Search Trees and Sorted Arrays

This document delves into coding practices focusing on minimizing errors and emphasizes testing in isolation. It provides an analysis of operations involving binary search trees (BST) and sorted arrays, comparing the efficiencies of finding, adding, and removing items. Key insights include that finding in a BST takes O(log n) time while adding or removing takes O(log n) as well. In contrast, sorted arrays require O(n) for additions/removals, making BSTs a more efficient choice for dynamic data sets. Additionally, the text explores union operations between two sets and their complexities.

Télécharger la présentation

Efficient Operations on Binary Search Trees and Sorted Arrays

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Thoughts • Most coding errors are/were small ones • Test in isolation • Question what you see – does it make sense

  2. Analysis • 1,000,000 find + add/removes in ~50,000 item BST takes x time • Find = log(n) • Add or remove = log(n) • Find+add or remove = O(logn) 50000 = x

  3. Analysis • Do same in sorted array list? • Find = O(logn) • Add or remove both O(n) • Find + add/remove = O(n) =

  4. Analysis • Do same in sorted array list? • Find = O(logn) • Add or remove both O(n) • Find + add/remove = O(n) =

  5. Analysis • Do same in sorted array list? = ~3200

  6. Union • Set A has n items, set B has m items • Approach 1: • Empty set C • Walk through A, add each item to C • Walk through B, add each item to C

  7. Union • Set A has n items, set B has m items • Approach 1: • Empty set C • Walk through A, add each item to C n steps each log(n) • Walk through B, add each item to C

  8. Union • Set A has n items, set B has m items • Approach 1: • Empty set C • Walk through A, add each item to C n·log(n) • Walk through B, add each item to C

  9. Union • Set A has n items, set B has m items • Approach 1: • Empty set C • Walk through A, add each item to C n·log(n) • Walk through B, add each item to C m steps each log(size(C))

  10. Union • Set A has n items, set B has m items • Approach 1: • Empty set C • Walk through A, add each item to C n·log(n) • Walk through B, add each item to C m steps each log(n + m)

  11. Union • Set A has n items, set B has m items • Approach 1: • Empty set C • Walk through A, add each item to C n·log(n) • Walk through B, add each item to Cm·log(n + m)

  12. Union • Set A has n items, set B has m items • Approach 1: • Empty set C • Walk through A, add each item to C: n·log(n) • Walk through B, add each item to C: m·log(n + m) • O(nlogn) + O(m·log(n+m)) • Dominated by O(m·log(n+m)) unless m is tiny

  13. Union • Set A has n items, set B has m items • Approach 2: • Copy A to C: O(n) • Walk through B, add each item to C

  14. Union • Set A has n items, set B has m items • Approach 2: • Copy A to C: O(n) • Walk through B : m, add each item to C O(log(n+m))

  15. Union • Set A has n items, set B has m items • Approach 2: • Copy A to C: O(n) • Walk through B : m, add each item to CO(log(n+m)) • O(n) + O(m·log(n+m)) • Dominated by O(m·log(n+m)) unless m is tiny

  16. Optimal • Tree A -> Vector A' : O(n) time • Tree B -> Vector B' : O(n) time • Merge A' + B' into combined Vec C' : O(n + m) • Make BST from Vec C': • Recursively build tree using binary search in linear time : O(n + m) Total : O(n + m)

More Related