{"id":15008518,"url":"https://github.com/elandeyan/min_max_heap_dart","last_synced_at":"2026-02-09T11:04:24.987Z","repository":{"id":178376613,"uuid":"661437772","full_name":"ElanDeyan/min_max_heap_dart","owner":"ElanDeyan","description":"A MinMax binary heap implementation as dart's package.","archived":false,"fork":false,"pushed_at":"2023-09-11T10:37:01.000Z","size":34,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-01-18T22:47:21.338Z","etag":null,"topics":["dart-lang","dart-package","dartlang","data-structures"],"latest_commit_sha":null,"homepage":"https://pub.dev/packages/min_max_heap","language":"Dart","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ElanDeyan.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-07-02T20:57:03.000Z","updated_at":"2024-10-21T09:27:58.000Z","dependencies_parsed_at":null,"dependency_job_id":"1c218c01-0576-4424-88b5-6874c121dd00","html_url":"https://github.com/ElanDeyan/min_max_heap_dart","commit_stats":null,"previous_names":["elandeyan/min_max_heap_dart"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ElanDeyan%2Fmin_max_heap_dart","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ElanDeyan%2Fmin_max_heap_dart/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ElanDeyan%2Fmin_max_heap_dart/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ElanDeyan%2Fmin_max_heap_dart/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ElanDeyan","download_url":"https://codeload.github.com/ElanDeyan/min_max_heap_dart/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243190963,"owners_count":20250986,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["dart-lang","dart-package","dartlang","data-structures"],"created_at":"2024-09-24T19:19:12.572Z","updated_at":"2026-02-09T11:04:24.901Z","avatar_url":"https://github.com/ElanDeyan.png","language":"Dart","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003c!-- \nThis README describes the package. If you publish this package to pub.dev,\nthis README's contents appear on the landing page for your package.\n\nFor information about how to write a good package README, see the guide for\n[writing package pages](https://dart.dev/guides/libraries/writing-package-pages). \n\nFor general information about developing packages, see the Dart guide for\n[creating packages](https://dart.dev/guides/libraries/create-library-packages)\nand the Flutter guide for\n[developing packages and plugins](https://flutter.dev/developing-packages). \n--\u003e\n\n# Min-max binary heap data structure\n\nA binary heap, AKA priority queue, with quick access to min and max prioritary elements.\n\n## Getting started\n\n### Concept\n\n#### Binary heap (in general)\n\nA binary heap is a data structure with 2 properties:\n\n* Heap shape: The heap will have all levels, except the last one, fullfilled with max number of elements possible in this level. For example, see the image below:\n\n![A binary min-heap representation, your array mode is: [1,2,3,17,19,36,7,25,100]](https://upload.wikimedia.org/wikipedia/commons/thumb/6/69/Min-heap.png/220px-Min-heap.png)\n\nAs you can see, the first's 3 levels have all number of nodes possible in the respective level (\n\n```dart\npow(base: 2,exponent: level)\n```\n\n):\n\n* Level 0: 1 node\n* Level 1: 2 nodes\n* Level 2: 4 nodes\n* ...\n\n* In a traditional min-heap, all nodes that have descendants should be less than all your respective descendants. The image above explains for yourself.\n\nAn interest thing about binary heaps in general is the fact they can be implemented in a List of values, not necessary with linked list of nodes.\n**This implementation uses 'list/array'.**\n\n#### MinMax heap\n\nIf you try to find the maximum value in a min-heap, you will have O(lg n) in worst case. The same with find minimum value in a max-heap.\nWith MinMax heap, you have O(1) in the worst case to find both min and max value of all heap.\nOperations like insert, remove (min and max) have O(lg n) in worst case. And the build method (when you pass a `List\u003cE\u003e` values in the input parameter) have O(n lg n) in worst case.\nHere an image to illustrate a valid MinMax heap:\n\n![MinMax heap, the array mode is [8,71,41,31,10,11,16,46,51,31,21,13]](https://upload.wikimedia.org/wikipedia/commons/thumb/5/50/Min-max_heap.jpg/300px-Min-max_heap.jpg)\n\nAs you see, the min will always be the root, and the max will be or the direct left child or the direct right child of the root.\n\n## Usage\n\nYou can build a heap from a `List\u003cE\u003e` values in input parameter.\n**This approach is mantained for compatibility reasons.**\n\n```dart\nvoid main() {\n  final myIntHeap = MinMaxHeap\u003cint\u003e(input: [1, 2, 3, 4, 5, 6, 7, 8]);\n}\n```\n\nTo build a heap from any iterable, use the factory constructor `fromIterable`.\n\n```dart\nvoid main() {\n  final myIntHeap = MinMaxHeap.fromIterable(iterable: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);\n}\n```\n\nOr, you can build with successive insertions.\n\n```dart\nvoid main() {\n  const myInput = \u003cint\u003e[1, 2, 3, 4, 5, 6, 7, 8];\n  final myIntHeap = MinMaxHeap\u003cint\u003e();\n  myInput.forEach(myIntHeap.add); \n  // or ```myIntHeap.insert```\n  // same of loop trough myInput and call add method in every cycle.\n}\n```\n\nOther aspect to consider is the use of the criteria parameter.\nIf you want to build your MinMax heap with custom criteria, you need to pass a callback funtion that returns `num` or `int` or `double` types.\nElse will throw an Error.\n\n```dart\nvoid main() {\n  const myInput = \u003cString\u003e['hello', 'my', 'name', 'is', 'Elan', 'and', 'your', 'name', '?'];\n  final myStringHeap = MinMaxHeap\u003cString\u003e.fromIterable(iterable: myInput, criteria: (word) =\u003e word.length);\n}\n```\n\nYou can use sucessive insertions with callbacks too.\n\n```dart\nvoid main() {\n  const myInput = \u003cString\u003e['hello', 'my', 'name', 'is', 'Elan', 'and', 'your', 'name', '?'];\n  final myStringHeap = MinMaxHeap\u003cString\u003e(criteria: (word) =\u003e word.length);\n  for(final element in myInput) {\n    myStringHeap.add(element);\n  }\n}\n```\n\n\u003e **Curiosity**: If you use the same List of elements, but build one heap passing the iterable parameter and build other heap with the same iterable, but with sucessive insertions (add), the results heaps will be different! But both are valid ones. Test and see! To more details, see the test file and search the test \"From a List, is a valid min-max heap?\".\n\n## Additional information\n\nThis package can be improved and tips are welcome! Feel free to create an issue in Github's repository and give a feedback.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Felandeyan%2Fmin_max_heap_dart","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Felandeyan%2Fmin_max_heap_dart","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Felandeyan%2Fmin_max_heap_dart/lists"}