{"id":13411598,"url":"https://github.com/daichi-m/go18ds","last_synced_at":"2026-02-13T17:58:42.402Z","repository":{"id":43043892,"uuid":"470296061","full_name":"daichi-m/go18ds","owner":"daichi-m","description":null,"archived":false,"fork":false,"pushed_at":"2023-10-07T08:02:38.000Z","size":507,"stargazers_count":46,"open_issues_count":2,"forks_count":4,"subscribers_count":2,"default_branch":"main","last_synced_at":"2024-07-31T20:48:16.330Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Go","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/daichi-m.png","metadata":{"files":{"readme":"README.md","changelog":null,"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":"2022-03-15T19:03:23.000Z","updated_at":"2024-07-12T12:58:41.000Z","dependencies_parsed_at":"2024-06-19T00:07:28.664Z","dependency_job_id":null,"html_url":"https://github.com/daichi-m/go18ds","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"purl":"pkg:github/daichi-m/go18ds","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/daichi-m%2Fgo18ds","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/daichi-m%2Fgo18ds/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/daichi-m%2Fgo18ds/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/daichi-m%2Fgo18ds/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/daichi-m","download_url":"https://codeload.github.com/daichi-m/go18ds/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/daichi-m%2Fgo18ds/sbom","scorecard":{"id":317007,"data":{"date":"2025-08-11","repo":{"name":"github.com/daichi-m/go18ds","commit":"bc64532cc3920b72d57f1e2e3af83b3f4163b0f0"},"scorecard":{"version":"v5.2.1-40-gf6ed084d","commit":"f6ed084d17c9236477efd66e5b258b9d4cc7b389"},"score":3.8,"checks":[{"name":"Maintained","score":0,"reason":"0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0","details":null,"documentation":{"short":"Determines if the project is \"actively maintained\".","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#maintained"}},{"name":"Code-Review","score":0,"reason":"Found 0/13 approved changesets -- score normalized to 0","details":null,"documentation":{"short":"Determines if the project requires human code review before pull requests (aka merge requests) are merged.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#code-review"}},{"name":"Packaging","score":-1,"reason":"packaging workflow not detected","details":["Warn: no GitHub/GitLab publishing workflow detected."],"documentation":{"short":"Determines if the project is published as a package that others can easily download, install, easily update, and uninstall.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#packaging"}},{"name":"Token-Permissions","score":0,"reason":"detected GitHub workflow tokens with excessive permissions","details":["Warn: no topLevel permission defined: .github/workflows/bench.yml:1","Warn: no topLevel permission defined: .github/workflows/go.yml:1","Warn: no topLevel permission defined: .github/workflows/release.yml:1","Info: no jobLevel write permissions found"],"documentation":{"short":"Determines if the project's workflows follow the principle of least privilege.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#token-permissions"}},{"name":"Dangerous-Workflow","score":10,"reason":"no dangerous workflow patterns detected","details":null,"documentation":{"short":"Determines if the project's GitHub Action workflows avoid dangerous patterns.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#dangerous-workflow"}},{"name":"CII-Best-Practices","score":0,"reason":"no effort to earn an OpenSSF best practices badge detected","details":null,"documentation":{"short":"Determines if the project has an OpenSSF (formerly CII) Best Practices Badge.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#cii-best-practices"}},{"name":"Binary-Artifacts","score":10,"reason":"no binaries found in the repo","details":null,"documentation":{"short":"Determines if the project has generated executable (binary) artifacts in the source repository.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#binary-artifacts"}},{"name":"Security-Policy","score":0,"reason":"security policy file not detected","details":["Warn: no security policy file detected","Warn: no security file to analyze","Warn: no security file to analyze","Warn: no security file to analyze"],"documentation":{"short":"Determines if the project has published a security policy.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#security-policy"}},{"name":"Pinned-Dependencies","score":0,"reason":"dependency not pinned by hash detected -- score normalized to 0","details":["Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/bench.yml:19: update your workflow using https://app.stepsecurity.io/secureworkflow/daichi-m/go18ds/bench.yml/main?enable=pin","Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/bench.yml:22: update your workflow using https://app.stepsecurity.io/secureworkflow/daichi-m/go18ds/bench.yml/main?enable=pin","Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/go.yml:24: update your workflow using https://app.stepsecurity.io/secureworkflow/daichi-m/go18ds/go.yml/main?enable=pin","Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/go.yml:27: update your workflow using https://app.stepsecurity.io/secureworkflow/daichi-m/go18ds/go.yml/main?enable=pin","Warn: third-party GitHubAction not pinned by hash: .github/workflows/go.yml:32: update your workflow using https://app.stepsecurity.io/secureworkflow/daichi-m/go18ds/go.yml/main?enable=pin","Warn: third-party GitHubAction not pinned by hash: .github/workflows/go.yml:57: update your workflow using https://app.stepsecurity.io/secureworkflow/daichi-m/go18ds/go.yml/main?enable=pin","Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/release.yml:44: update your workflow using https://app.stepsecurity.io/secureworkflow/daichi-m/go18ds/release.yml/main?enable=pin","Warn: third-party GitHubAction not pinned by hash: .github/workflows/release.yml:47: update your workflow using https://app.stepsecurity.io/secureworkflow/daichi-m/go18ds/release.yml/main?enable=pin","Warn: third-party GitHubAction not pinned by hash: .github/workflows/release.yml:53: update your workflow using https://app.stepsecurity.io/secureworkflow/daichi-m/go18ds/release.yml/main?enable=pin","Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/release.yml:29: update your workflow using https://app.stepsecurity.io/secureworkflow/daichi-m/go18ds/release.yml/main?enable=pin","Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/release.yml:31: update your workflow using https://app.stepsecurity.io/secureworkflow/daichi-m/go18ds/release.yml/main?enable=pin","Warn: goCommand not pinned by hash: .github/workflows/go.yml:39","Warn: goCommand not pinned by hash: .github/workflows/go.yml:53","Info:   0 out of   7 GitHub-owned GitHubAction dependencies pinned","Info:   0 out of   4 third-party GitHubAction dependencies pinned","Info:   0 out of   2 goCommand dependencies pinned"],"documentation":{"short":"Determines if the project has declared and pinned the dependencies of its build process.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#pinned-dependencies"}},{"name":"License","score":9,"reason":"license file detected","details":["Info: project has a license file: LICENSE:0","Warn: project license file does not contain an FSF or OSI license."],"documentation":{"short":"Determines if the project has defined a license.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#license"}},{"name":"Fuzzing","score":0,"reason":"project is not fuzzed","details":["Warn: no fuzzer integrations found"],"documentation":{"short":"Determines if the project uses fuzzing.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#fuzzing"}},{"name":"Signed-Releases","score":-1,"reason":"no releases found","details":null,"documentation":{"short":"Determines if the project cryptographically signs release artifacts.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#signed-releases"}},{"name":"Branch-Protection","score":-1,"reason":"internal error: error during branchesHandler.setup: internal error: githubv4.Query: Resource not accessible by integration","details":null,"documentation":{"short":"Determines if the default and release branches are protected with GitHub's branch protection settings.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#branch-protection"}},{"name":"SAST","score":0,"reason":"SAST tool is not run on all commits -- score normalized to 0","details":["Warn: 0 commits out of 23 are checked with a SAST tool"],"documentation":{"short":"Determines if the project uses static code analysis.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#sast"}},{"name":"Vulnerabilities","score":10,"reason":"0 existing vulnerabilities detected","details":null,"documentation":{"short":"Determines if the project has open, known unfixed vulnerabilities.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#vulnerabilities"}}]},"last_synced_at":"2025-08-18T00:25:57.707Z","repository_id":43043892,"created_at":"2025-08-18T00:25:57.707Z","updated_at":"2025-08-18T00:25:57.707Z"},"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29414277,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-13T06:24:03.484Z","status":"ssl_error","status_checked_at":"2026-02-13T06:23:12.830Z","response_time":78,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"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":[],"created_at":"2024-07-30T20:01:14.857Z","updated_at":"2026-02-13T17:58:42.368Z","avatar_url":"https://github.com/daichi-m.png","language":"Go","funding_links":[],"categories":["数据结构与算法","Generators","Data Structures and Algorithms","Data Integration Frameworks"],"sub_categories":["杂项数据结构和算法","Miscellaneous Data Structures and Algorithms"],"readme":"\u003c!-- omit in toc --\u003e\n# Go18DS (Go 1.18+ Data Structures)\n\n[![Go Reference](https://pkg.go.dev/badge/github.com/daichi-m/go18ds.svg)](https://pkg.go.dev/github.com/daichi-m/go18ds)\n[![Go Build](https://github.com/daichi-m/go18ds/actions/workflows/go.yml/badge.svg)](https://github.com/daichi-m/go18ds/actions/workflows/go.yml)\n[![Go Report Card](https://goreportcard.com/badge/github.com/daichi-m/go18ds)](https://goreportcard.com/report/github.com/daichi-m/go18ds)\n[![codecov](https://codecov.io/gh/daichi-m/go18ds/branch/main/graph/badge.svg?token=5eYXdBcCzD)](https://codecov.io/gh/daichi-m/go18ds)\n[![License](https://img.shields.io/badge/license-BSD--3--Clause-green)](https://github.com/daichi-m/go18ds/blob/main/LICENSE)\n\n\n\nImplementation of various data structures and algorithms in Go.\n\nThis is based on the [GoDS](https://github.com/emirpasic/gods) library developed by Emir Pasic.\nSince the target is for Go 1.18+, it is inherently generic-aware.\n\n\u003c!-- omit in toc --\u003e\n## Data Structures\n\n- [Containers](#containers)\n  - [Lists](#lists)\n    - [ArrayList](#arraylist)\n    - [SinglyLinkedList](#singlylinkedlist)\n    - [DoublyLinkedList](#doublylinkedlist)\n  - [Sets](#sets)\n    - [HashSet](#hashset)\n    - [TreeSet](#treeset)\n    - [LinkedHashSet](#linkedhashset)\n  - [Stacks](#stacks)\n    - [LinkedListStack](#linkedliststack)\n    - [ArrayStack](#arraystack)\n  - [Maps](#maps)\n    - [HashMap](#hashmap)\n    - [TreeMap](#treemap)\n    - [LinkedHashMap](#linkedhashmap)\n    - [HashBidiMap](#hashbidimap)\n    - [TreeBidiMap](#treebidimap)\n  - [Trees](#trees)\n    - [RedBlackTree](#redblacktree)\n    - [AVLTree](#avltree)\n    - [BTree](#btree)\n    - [BinaryHeap](#binaryheap)\n- [Functions](#functions)\n  - [Comparator](#comparator)\n  - [Iterator](#iterator)\n    - [IteratorWithIndex](#iteratorwithindex)\n    - [IteratorWithKey](#iteratorwithkey)\n    - [ReverseIteratorWithIndex](#reverseiteratorwithindex)\n    - [ReverseIteratorWithKey](#reverseiteratorwithkey)\n  - [Enumerable](#enumerable)\n    - [EnumerableWithIndex](#enumerablewithindex)\n    - [EnumerableWithKey](#enumerablewithkey)\n  - [Serialization](#serialization)\n    - [JSONSerializer](#jsonserializer)\n    - [JSONDeserializer](#jsondeserializer)\n  - [Sort](#sort)\n  - [Container](#container)\n- [Appendix](#appendix)\n  - [Motivation](#motivation)\n  - [Goals](#goals)\n  - [Testing and Benchmarking](#testing-and-benchmarking)\n  - [Contributing](#contributing)\n  - [License](#license)\n\n\n## Containers\n\nAll data structures implement the container interface with the following methods:\n\n```go\ntype Container[T any] interface {\n    Empty() bool\n    Size() int\n    Clear()\n    Values() []T\n}\n```\n\nContainers are either ordered or unordered. All ordered containers provide [stateful iterators](#iterator) and some of them allow [enumerable functions](#enumerable).\n\n| **Data** | **Structure** | **Ordered** | **[Iterator](#iterator)** | **[Enumerable](#enumerable)** | **Referenced by** |\n| :--- | :--- | :---: | :---: | :---: | :---: |\n| [Lists](#lists) |\n|   | [ArrayList](#arraylist) | yes | yes* | yes | index |\n|   | [SinglyLinkedList](#singlylinkedlist) | yes | yes | yes | index |\n|   | [DoublyLinkedList](#doublylinkedlist) | yes | yes* | yes | index |\n| [Sets](#sets) |\n|   | [HashSet](#hashset) | no | no | no | index |\n|   | [TreeSet](#treeset) | yes | yes* | yes | index |\n|   | [LinkedHashSet](#linkedhashset) | yes | yes* | yes | index |\n| [Stacks](#stacks) |\n|   | [LinkedListStack](#linkedliststack) | yes | yes | no | index |\n|   | [ArrayStack](#arraystack) | yes | yes* | no | index |\n| [Maps](#maps) |\n|   | [HashMap](#hashmap) | no | no | no | key |\n|   | [TreeMap](#treemap) | yes | yes* | yes | key |\n|   | [LinkedHashMap](#linkedhashmap) | yes | yes* | yes | key |\n|   | [HashBidiMap](#hashbidimap) | no | no | no | key* |\n|   | [TreeBidiMap](#treebidimap) | yes | yes* | yes | key* |\n| [Trees](#trees) |\n|   | [RedBlackTree](#redblacktree) | yes | yes* | no | key |\n|   | [AVLTree](#avltree) | yes | yes* | no | key |\n|   | [BTree](#btree) | yes | yes* | no | key |\n|   | [BinaryHeap](#binaryheap) | yes | yes* | no | index |\n|   |  |  | \u003csub\u003e\u003csup\u003e*reversible\u003c/sup\u003e\u003c/sub\u003e |  | \u003csub\u003e\u003csup\u003e*bidirectional\u003c/sup\u003e\u003c/sub\u003e |\n\n### Lists\n\nA list is a data structure that stores values and may have repeated values.\n\nImplements [Container](#containers) interface.\n\n```go\ntype List[T comparable] interface {\n    Get(index int) (T, bool)\n    Remove(index int)\n    Add(values ...T)\n    Contains(values ...T) bool\n    Sort(comparator utils.Comparator[T])\n    Swap(index1, index2 int)\n    Insert(index int, values ...T)\n    Set(index int, value T)\n\n    containers.Container[T]\n    // Empty() bool\n    // Size() int\n    // Clear()\n    // Values() []T\n}\n```\n\n#### ArrayList\n\nA [list](#lists) backed by a dynamic array that grows and shrinks implicitly.\n\nImplements [List](#lists), [IteratorWithIndex](#iteratorwithindex), [EnumerableWithIndex](#enumerablewithindex), [JSONSerializer](#jsonserializer) and [JSONDeserializer](#jsondeserializer) interfaces.\n\n```go\npackage main\n\nimport (\n    \"github.com/daichi-m/go18ds/lists/arraylist\"\n    \"github.com/daichi-m/go18ds/utils\"\n)\n\n// ArrayListExample to demonstrate basic usage of ArrayList\nfunc main() {\n    list := arraylist.New[string]()\n    list.Add(\"a\")                         // [\"a\"]\n    list.Add(\"c\", \"b\")                    // [\"a\",\"c\",\"b\"]\n    list.Sort(utils.StringComparator)     // [\"a\",\"b\",\"c\"]\n    _, _ = list.Get(0)                    // \"a\",true\n    _, _ = list.Get(100)                  // nil,false\n    _ = list.Contains(\"a\", \"b\", \"c\")      // true\n    _ = list.Contains(\"a\", \"b\", \"c\", \"d\") // false\n    list.Swap(0, 1)                       // [\"b\",\"a\",c\"]\n    list.Remove(2)                        // [\"b\",\"a\"]\n    list.Remove(1)                        // [\"b\"]\n    list.Remove(0)                        // []\n    list.Remove(0)                        // [] (ignored)\n    _ = list.Empty()                      // true\n    _ = list.Size()                       // 0\n    list.Add(\"a\")                         // [\"a\"]\n    list.Clear()                          // []\n}\n```\n\n#### SinglyLinkedList\n\nA [list](#lists) where each element points to the next element in the list.\n\nImplements [List](#lists), [IteratorWithIndex](#iteratorwithindex), [EnumerableWithIndex](#enumerablewithindex), [JSONSerializer](#jsonserializer) and [JSONDeserializer](#jsondeserializer) interfaces.\n\n```go\npackage main\n\nimport (\n    sll \"github.com/daichi-m/go18ds/lists/singlylinkedlist\"\n    \"github.com/daichi-m/go18ds/utils\"\n)\n\n// SinglyLinkedListExample to demonstrate basic usage of SinglyLinkedList\nfunc main() {\n    list := sll.New[string]()\n    list.Add(\"a\")                         // [\"a\"]\n    list.Append(\"b\")                      // [\"a\",\"b\"] (same as Add())\n    list.Prepend(\"c\")                     // [\"c\",\"a\",\"b\"]\n    list.Sort(utils.StringComparator)     // [\"a\",\"b\",\"c\"]\n    _, _ = list.Get(0)                    // \"a\",true\n    _, _ = list.Get(100)                  // nil,false\n    _ = list.Contains(\"a\", \"b\", \"c\")      // true\n    _ = list.Contains(\"a\", \"b\", \"c\", \"d\") // false\n    list.Remove(2)                        // [\"a\",\"b\"]\n    list.Remove(1)                        // [\"a\"]\n    list.Remove(0)                        // []\n    list.Remove(0)                        // [] (ignored)\n    _ = list.Empty()                      // true\n    _ = list.Size()                       // 0\n    list.Add(\"a\")                         // [\"a\"]\n    list.Clear()                          // []\n}\n```\n\n#### DoublyLinkedList\n\nA [list](#lists) where each element points to the next and previous elements in the list.\n\nImplements [List](#lists), [IteratorWithIndex](#iteratorwithindex), [EnumerableWithIndex](#enumerablewithindex), [JSONSerializer](#jsonserializer) and [JSONDeserializer](#jsondeserializer) interfaces.\n\n```go\npackage main\n\nimport (\n    dll \"github.com/daichi-m/go18ds/lists/doublylinkedlist\"\n    \"github.com/daichi-m/go18ds/utils\"\n)\n\n// DoublyLinkedListExample to demonstrate basic usage of DoublyLinkedList\nfunc main() {\n    list := dll.New[string]()\n    list.Add(\"a\")                         // [\"a\"]\n    list.Append(\"b\")                      // [\"a\",\"b\"] (same as Add())\n    list.Prepend(\"c\")                     // [\"c\",\"a\",\"b\"]\n    list.Sort(utils.StringComparator)     // [\"a\",\"b\",\"c\"]\n    _, _ = list.Get(0)                    // \"a\",true\n    _, _ = list.Get(100)                  // nil,false\n    _ = list.Contains(\"a\", \"b\", \"c\")      // true\n    _ = list.Contains(\"a\", \"b\", \"c\", \"d\") // false\n    list.Remove(2)                        // [\"a\",\"b\"]\n    list.Remove(1)                        // [\"a\"]\n    list.Remove(0)                        // []\n    list.Remove(0)                        // [] (ignored)\n    _ = list.Empty()                      // true\n    _ = list.Size()                       // 0\n    list.Add(\"a\")                         // [\"a\"]\n    list.Clear()                          // []\n}\n```\n\n### Sets\n\nA set is a data structure that can store elements and has no repeated values. It is a computer implementation of the mathematical concept of a finite set. Unlike most other collection types, rather than retrieving a specific element from a set, one typically tests an element for membership in a set. This structure is often used to ensure that no duplicates are present in a container.\n\nImplements [Container](#containers) interface.\n\n```go\ntype Set[T comparable] interface {\n    Add(elements ...T)\n    Remove(elements ...T)\n    Contains(elements ...T) bool\n\n    containers.Container[T]\n    // Empty() bool\n    // Size() int\n    // Clear()\n    // Values() []T\n}\n```\n\n#### HashSet\n\nA [set](#sets) backed by a hash table (actually a Go's map). It makes no guarantees as to the iteration order of the set.\n\nImplements [Set](#sets), [JSONSerializer](#jsonserializer) and [JSONDeserializer](#jsondeserializer) interfaces.\n\n```go\nimport \"github.com/daichi-m/go18ds/sets/hashset\"\n\n// HashSetExample to demonstrate basic usage of HashSet\nfunc main() {\n    set := hashset.New[int]() // empty (keys are of type int)\n    set.Add(1)                // 1\n    set.Add(2, 2, 3, 4, 5)    // 3, 1, 2, 4, 5 (random order, duplicates ignored)\n    set.Remove(4)             // 5, 3, 2, 1 (random order)\n    set.Remove(2, 3)          // 1, 5 (random order)\n    set.Contains(1)           // true\n    set.Contains(1, 5)        // true\n    set.Contains(1, 6)        // false\n    _ = set.Values()          // []int{5,1} (random order)\n    set.Clear()               // empty\n    set.Empty()               // true\n    set.Size()                // 0\n}\n```\n\n#### TreeSet\n\nA [set](#sets) backed by a [red-black tree](#redblacktree) to keep the elements ordered with respect to the [comparator](#comparator).\n\nImplements [Set](#sets), [IteratorWithIndex](#iteratorwithindex), [EnumerableWithIndex](#enumerablewithindex), [JSONSerializer](#jsonserializer) and [JSONDeserializer](#jsondeserializer) interfaces.\n\n```go\npackage main\n\nimport \"github.com/daichi-m/go18ds/sets/treeset\"\n\n// TreeSetExample to demonstrate basic usage of TreeSet\nfunc main() {\n    set := treeset.NewWithIntComparator() // empty\n    set.Add(1)                            // 1\n    set.Add(2, 2, 3, 4, 5)                // 1, 2, 3, 4, 5 (in order, duplicates ignored)\n    set.Remove(4)                         // 1, 2, 3, 5 (in order)\n    set.Remove(2, 3)                      // 1, 5 (in order)\n    set.Contains(1)                       // true\n    set.Contains(1, 5)                    // true\n    set.Contains(1, 6)                    // false\n    _ = set.Values()                      // []int{1,5} (in order)\n    set.Clear()                           // empty\n    set.Empty()                           // true\n    set.Size()                            // 0\n}\n```\n\n#### LinkedHashSet\n\nA [set](#sets) that preserves insertion-order. Data structure is backed by a hash table to store values and [doubly-linked list](#doublylinkedlist) to store insertion ordering.\n\nImplements [Set](#sets), [IteratorWithIndex](#iteratorwithindex), [EnumerableWithIndex](#enumerablewithindex), [JSONSerializer](#jsonserializer) and [JSONDeserializer](#jsondeserializer) interfaces.\n\n```go\npackage main\n\nimport \"github.com/daichi-m/go18ds/sets/linkedhashset\"\n\n// LinkedHashSetExample to demonstrate basic usage of LinkedHashSet\nfunc main() {\n    set := linkedhashset.New[int]() // empty\n    set.Add(5)                      // 5\n    set.Add(4, 4, 3, 2, 1)          // 5, 4, 3, 2, 1 (in insertion-order, duplicates ignored)\n    set.Remove(4)                   // 5, 3, 2, 1 (in insertion-order)\n    set.Remove(2, 3)                // 5, 1 (in insertion-order)\n    set.Contains(1)                 // true\n    set.Contains(1, 5)              // true\n    set.Contains(1, 6)              // false\n    _ = set.Values()                // []int{5, 1} (in insertion-order)\n    set.Clear()                     // empty\n    set.Empty()                     // true\n    set.Size()                      // 0\n}\n```\n\n### Stacks\n\nA stack that represents a last-in-first-out (LIFO) data structure. The usual push and pop operations are provided, as well as a method to peek at the top item on the stack.\n\nImplements [Container](#containers) interface.\n\n```go\ntype Stack[T comparable] interface {\n    Push(value T)\n    Pop() (value T, ok bool)\n    Peek() (value T, ok bool)\n\n    containers.Container[T]\n    // Empty() bool\n    // Size() int\n    // Clear()\n    // Values() []interface{}\n}\n```\n\n#### LinkedListStack\n\nA [stack](#stacks) based on a [linked list](#singlylinkedlist).\n\nImplements [Stack](#stacks), [IteratorWithIndex](#iteratorwithindex), [JSONSerializer](#jsonserializer) and [JSONDeserializer](#jsondeserializer) interfaces.\n\n```go\npackage main\n\nimport lls \"github.com/daichi-m/go18ds/stacks/linkedliststack\"\n\n// LinkedListStackExample to demonstrate basic usage of LinkedListStack\nfunc main() {\n    stack := lls.New[int]() // empty\n    stack.Push(1)           // 1\n    stack.Push(2)           // 1, 2\n    stack.Values()          // 2, 1 (LIFO order)\n    _, _ = stack.Peek()     // 2,true\n    _, _ = stack.Pop()      // 2, true\n    _, _ = stack.Pop()      // 1, true\n    _, _ = stack.Pop()      // nil, false (nothing to pop)\n    stack.Push(1)           // 1\n    stack.Clear()           // empty\n    stack.Empty()           // true\n    stack.Size()            // 0\n}\n```\n\n#### ArrayStack\n\nA [stack](#stacks) based on a [array list](#arraylist).\n\nImplements [Stack](#stacks), [IteratorWithIndex](#iteratorwithindex), [JSONSerializer](#jsonserializer) and [JSONDeserializer](#jsondeserializer) interfaces.\n\n```go\npackage main\n\nimport \"github.com/daichi-m/go18ds/stacks/arraystack\"\n\n// ArrayStackExample to demonstrate basic usage of ArrayStack\nfunc main() {\n    stack := arraystack.New[int]() // empty\n    stack.Push(1)             // 1\n    stack.Push(2)             // 1, 2\n    stack.Values()            // 2, 1 (LIFO order)\n    _, _ = stack.Peek()       // 2,true\n    _, _ = stack.Pop()        // 2, true\n    _, _ = stack.Pop()        // 1, true\n    _, _ = stack.Pop()        // nil, false (nothing to pop)\n    stack.Push(1)             // 1\n    stack.Clear()             // empty\n    stack.Empty()             // true\n    stack.Size()              // 0\n}\n```\n\n### Maps\n\nA Map is a data structure that maps keys to values. A map cannot contain duplicate keys and each key can map to at most one value.\n\nImplements [Container](#containers) interface.\n\n```go\n// Map interface that all maps implement\ntype Map[K comparable, V any] interface {\n    Put(key K, value V)\n    Get(key K) (value V, found bool)\n    Remove(key K)\n    Keys() []K\n\n    containers.Container[V]\n    // Empty() bool\n    // Size() int\n    // Clear()\n    // Values() []interface{}\n}\n```\n\nA BidiMap is an extension to the Map. A bidirectional map (BidiMap), also called a hash bag, is an associative data structure in which the key-value pairs form a one-to-one relation. This relation works in both directions by allow the value to also act as a key to key, e.g. a pair (a,b) thus provides a coupling between 'a' and 'b' so that 'b' can be found when 'a' is used as a key and 'a' can be found when 'b' is used as a key.\n\n```go\n// BidiMap interface that all bidirectional maps implement (extends the Map interface)\ntype BidiMap[K comparable, V any] interface {\n    GetKey(value V) (key K, found bool)\n\n    Map[K, V]\n}\n```\n\n#### HashMap\n\nA [map](#maps) based on hash tables. Keys are unordered.\n\nImplements [Map](#maps), [JSONSerializer](#jsonserializer) and [JSONDeserializer](#jsondeserializer) interfaces.\n\n```go\npackage main\n\nimport \"github.com/daichi-m/go18ds/maps/hashmap\"\n\n// HashMapExample to demonstrate basic usage of HashMap\nfunc main() {\n    m := hashmap.New[int, string]() // empty\n    m.Put(1, \"x\")                   // 1-\u003ex\n    m.Put(2, \"b\")                   // 2-\u003eb, 1-\u003ex  (random order)\n    m.Put(1, \"a\")                   // 2-\u003eb, 1-\u003ea (random order)\n    _, _ = m.Get(2)                 // b, true\n    _, _ = m.Get(3)                 // nil, false\n    _ = m.Values()                  // []interface {}{\"b\", \"a\"} (random order)\n    _ = m.Keys()                    // []interface {}{1, 2} (random order)\n    m.Remove(1)                     // 2-\u003eb\n    m.Clear()                       // empty\n    m.Empty()                       // true\n    m.Size()                        // 0\n}\n```\n\n#### TreeMap\n\nA [map](#maps) based on [red-black tree](#redblacktree). Keys are ordered with respect to the [comparator](#comparator).\n\nImplements [Map](#maps), [IteratorWithKey](#iteratorwithkey), [EnumerableWithKey](#enumerablewithkey), [JSONSerializer](#jsonserializer) and [JSONDeserializer](#jsondeserializer) interfaces.\n\n```go\npackage main\n\nimport \"github.com/emirpasic/gods/maps/treemap\"\n\nfunc main() {\n    m := treemap.NewWithIntComparator() // empty (keys are of type int)\n    m.Put(1, \"x\")                       // 1-\u003ex\n    m.Put(2, \"b\")                       // 1-\u003ex, 2-\u003eb (in order)\n    m.Put(1, \"a\")                       // 1-\u003ea, 2-\u003eb (in order)\n    _, _ = m.Get(2)                     // b, true\n    _, _ = m.Get(3)                     // nil, false\n    _ = m.Values()                      // []interface {}{\"a\", \"b\"} (in order)\n    _ = m.Keys()                        // []interface {}{1, 2} (in order)\n    m.Remove(1)                         // 2-\u003eb\n    m.Clear()                           // empty\n    m.Empty()                           // true\n    m.Size()                            // 0\n\n    // Other:\n    m.Min() // Returns the minimum key and its value from map.\n    m.Max() // Returns the maximum key and its value from map.\n}\n```\n\n#### LinkedHashMap\n\nA [map](#maps) that preserves insertion-order. It is backed by a hash table to store values and [doubly-linked list](doublylinkedlist) to store ordering.\n\nImplements [Map](#maps), [IteratorWithKey](#iteratorwithkey), [EnumerableWithKey](#enumerablewithkey), [JSONSerializer](#jsonserializer) and [JSONDeserializer](#jsondeserializer) interfaces.\n\n```go\npackage main\n\nimport \"github.com/daichi-m/go18ds/maps/linkedhashmap\"\n\n// LinkedHashMapExample to demonstrate basic usage of LinkedHashMapExample\nfunc main() {\n    m := linkedhashmap.New[int, string]() // empty (keys are of type int)\n    m.Put(2, \"b\")                         // 2-\u003eb\n    m.Put(1, \"x\")                         // 2-\u003eb, 1-\u003ex (insertion-order)\n    m.Put(1, \"a\")                         // 2-\u003eb, 1-\u003ea (insertion-order)\n    _, _ = m.Get(2)                       // b, true\n    _, _ = m.Get(3)                       // nil, false\n    _ = m.Values()                        // []interface {}{\"b\", \"a\"} (insertion-order)\n    _ = m.Keys()                          // []interface {}{2, 1} (insertion-order)\n    m.Remove(1)                           // 2-\u003eb\n    m.Clear()                             // empty\n    m.Empty()                             // true\n    m.Size()                              // 0\n}\n```\n\n#### HashBidiMap\n\nA [map](#maps) based on two hashmaps. Keys are unordered.\n\nImplements [BidiMap](#maps), [JSONSerializer](#jsonserializer) and [JSONDeserializer](#jsondeserializer) interfaces.\n\n```go\npackage main\n\nimport \"github.com/daichi-m/go18ds/maps/hashbidimap\"\n\n// HashBidiMapExample to demonstrate basic usage of HashMap\nfunc main() {\n    m := hashbidimap.New[int, string]() // empty\n    m.Put(1, \"x\")                       // 1-\u003ex\n    m.Put(3, \"b\")                       // 1-\u003ex, 3-\u003eb (random order)\n    m.Put(1, \"a\")                       // 1-\u003ea, 3-\u003eb (random order)\n    m.Put(2, \"b\")                       // 1-\u003ea, 2-\u003eb (random order)\n    _, _ = m.GetKey(\"a\")                // 1, true\n    _, _ = m.Get(2)                     // b, true\n    _, _ = m.Get(3)                     // nil, false\n    _ = m.Values()                      // []interface {}{\"a\", \"b\"} (random order)\n    _ = m.Keys()                        // []interface {}{1, 2} (random order)\n    m.Remove(1)                         // 2-\u003eb\n    m.Clear()                           // empty\n    m.Empty()                           // true\n    m.Size()                            // 0\n}\n```\n\n#### TreeBidiMap\n\nA [map](#maps) based on red-black tree. This map guarantees that the map will be in both ascending key and value order.  Other than key and value ordering, the goal with this structure is to avoid duplication of elements (unlike in [HashBidiMap](#hashbidimap)), which can be significant if contained elements are large.\n\nImplements [BidiMap](#maps), [IteratorWithKey](#iteratorwithkey), [EnumerableWithKey](#enumerablewithkey), [JSONSerializer](#jsonserializer) and [JSONDeserializer](#jsondeserializer) interfaces.\n\n```go\npackage main\n\nimport (\n    \"github.com/daichi-m/go18ds/maps/treebidimap\"\n    \"github.com/daichi-m/go18ds/utils\"\n)\n\n// TreeBidiMapExample to demonstrate basic usage of TreeBidiMap\nfunc main() {\n    m := treebidimap.NewWith(utils.NumberComparator[int], utils.StringComparator)\n    m.Put(1, \"x\")        // 1-\u003ex\n    m.Put(3, \"b\")        // 1-\u003ex, 3-\u003eb (ordered)\n    m.Put(1, \"a\")        // 1-\u003ea, 3-\u003eb (ordered)\n    m.Put(2, \"b\")        // 1-\u003ea, 2-\u003eb (ordered)\n    _, _ = m.GetKey(\"a\") // 1, true\n    _, _ = m.Get(2)      // b, true\n    _, _ = m.Get(3)      // nil, false\n    _ = m.Values()       // []interface {}{\"a\", \"b\"} (ordered)\n    _ = m.Keys()         // []interface {}{1, 2} (ordered)\n    m.Remove(1)          // 2-\u003eb\n    m.Clear()            // empty\n    m.Empty()            // true\n    m.Size()             // 0\n}\n```\n\n### Trees\n\nA tree is a widely used data data structure that simulates a hierarchical tree structure, with a root value and subtrees of children, represented as a set of linked nodes; thus no cyclic links.\n\nImplements [Container](#containers) interface.\n\n```go\ntype Tree[T comparable] interface {\n    containers.Container[T]\n    // Empty() bool\n    // Size() int\n    // Clear()\n    // Values() []interface{}\n}\n```\n\n#### RedBlackTree\n\nA red–black [tree](#trees) is a binary search tree with an extra bit of data per node, its color, which can be either red or black. The extra bit of storage ensures an approximately balanced tree by constraining how nodes are colored from any path from the root to the leaf. Thus, it is a data structure which is a type of self-balancing binary search tree.\n\nThe balancing of the tree is not perfect but it is good enough to allow it to guarantee searching in O(log n) time, where n is the total number of elements in the tree. The insertion and deletion operations, along with the tree rearrangement and recoloring, are also performed in O(log n) time. \u003csub\u003e\u003csup\u003e[Wikipedia](http://en.wikipedia.org/wiki/Red%E2%80%93black_tree)\u003c/sup\u003e\u003c/sub\u003e\n\nImplements [Tree](#trees), [ReverseIteratorWithKey](#reverseiteratorwithkey), [JSONSerializer](#jsonserializer) and [JSONDeserializer](#jsondeserializer) interfaces.\n\n\u003cp align=\"center\"\u003e\u003cimg src=\"http://upload.wikimedia.org/wikipedia/commons/thumb/6/66/Red-black_tree_example.svg/500px-Red-black_tree_example.svg.png\" width=\"400px\" height=\"200px\" /\u003e\u003c/p\u003e\n\n```go\npackage main\n\nimport (\n    \"fmt\"\n\n    rbt \"github.com/daichi-m/go18ds/trees/redblacktree\"\n)\n\n// RedBlackTreeExample to demonstrate basic usage of RedBlackTree\nfunc main() {\n    tree := rbt.NewWithIntComparator[string]() // empty(keys are of type int)\n\n    tree.Put(1, \"x\") // 1-\u003ex\n    tree.Put(2, \"b\") // 1-\u003ex, 2-\u003eb (in order)\n    tree.Put(1, \"a\") // 1-\u003ea, 2-\u003eb (in order, replacement)\n    tree.Put(3, \"c\") // 1-\u003ea, 2-\u003eb, 3-\u003ec (in order)\n    tree.Put(4, \"d\") // 1-\u003ea, 2-\u003eb, 3-\u003ec, 4-\u003ed (in order)\n    tree.Put(5, \"e\") // 1-\u003ea, 2-\u003eb, 3-\u003ec, 4-\u003ed, 5-\u003ee (in order)\n    tree.Put(6, \"f\") // 1-\u003ea, 2-\u003eb, 3-\u003ec, 4-\u003ed, 5-\u003ee, 6-\u003ef (in order)\n\n    fmt.Println(tree)\n    //\n    //  RedBlackTree\n    //  │           ┌── 6\n    //  │       ┌── 5\n    //  │   ┌── 4\n    //  │   │   └── 3\n    //  └── 2\n    //       └── 1\n\n    _ = tree.Values() // []interface {}{\"a\", \"b\", \"c\", \"d\", \"e\", \"f\"} (in order)\n    _ = tree.Keys()   // []interface {}{1, 2, 3, 4, 5, 6} (in order)\n\n    tree.Remove(2) // 1-\u003ea, 3-\u003ec, 4-\u003ed, 5-\u003ee, 6-\u003ef (in order)\n    fmt.Println(tree)\n    //\n    //  RedBlackTree\n    //  │       ┌── 6\n    //  │   ┌── 5\n    //  └── 4\n    //      │   ┌── 3\n    //      └── 1\n\n    tree.Clear() // empty\n    tree.Empty() // true\n    tree.Size()  // 0\n}\n```\n\nExtending the red-black tree's functionality  has been demonstrated in the following [example](https://github.com/emirpasic/gods/blob/master/examples/redblacktreeextended/redblacktreeextended.go).\n\n#### AVLTree\n\nAVL [tree](#trees) is a self-balancing binary search tree. In an AVL tree, the heights of the two child subtrees of any node differ by at most one; if at any time they differ by more than one, rebalancing is done to restore this property. Lookup, insertion, and deletion all take O(log n) time in both the average and worst cases, where n is the number of nodes in the tree prior to the operation. Insertions and deletions may require the tree to be rebalanced by one or more tree rotations.\n\nAVL trees are often compared with red–black trees because both support the same set of operations and take O(log n) time for the basic operations. For lookup-intensive applications, AVL trees are faster than red–black trees because they are more strictly balanced. \u003csub\u003e\u003csup\u003e[Wikipedia](https://en.wikipedia.org/wiki/AVL_tree)\u003c/sup\u003e\u003c/sub\u003e\n\nImplements [Tree](#trees), [ReverseIteratorWithKey](#reverseiteratorwithkey), [JSONSerializer](#jsonserializer) and [JSONDeserializer](#jsondeserializer) interfaces.\n\n\u003cp align=\"center\"\u003e\u003cimg src=\"https://upload.wikimedia.org/wikipedia/commons/thumb/a/ad/AVL-tree-wBalance_K.svg/262px-AVL-tree-wBalance_K.svg.png\" width=\"300px\" height=\"180px\" /\u003e\u003cbr/\u003e\u003csub\u003eAVL tree with balance factors (green)\u003c/sub\u003e\u003c/p\u003e\n\n```go\npackage main\n\nimport (\n    \"fmt\"\n\n    avl \"github.com/daichi-m/go18ds/trees/avltree\"\n)\n\n// AVLTreeExample to demonstrate basic usage of AVLTree\nfunc main() {\n    tree := avl.NewWithIntComparator[string]() // empty(keys are of type int)\n\n    tree.Put(1, \"x\") // 1-\u003ex\n    tree.Put(2, \"b\") // 1-\u003ex, 2-\u003eb (in order)\n    tree.Put(1, \"a\") // 1-\u003ea, 2-\u003eb (in order, replacement)\n    tree.Put(3, \"c\") // 1-\u003ea, 2-\u003eb, 3-\u003ec (in order)\n    tree.Put(4, \"d\") // 1-\u003ea, 2-\u003eb, 3-\u003ec, 4-\u003ed (in order)\n    tree.Put(5, \"e\") // 1-\u003ea, 2-\u003eb, 3-\u003ec, 4-\u003ed, 5-\u003ee (in order)\n    tree.Put(6, \"f\") // 1-\u003ea, 2-\u003eb, 3-\u003ec, 4-\u003ed, 5-\u003ee, 6-\u003ef (in order)\n\n    fmt.Println(tree)\n    //\n    //  AVLTree\n    //  │       ┌── 6\n    //  │   ┌── 5\n    //  └── 4\n    //      │   ┌── 3\n    //      └── 2\n    //          └── 1\n\n    _ = tree.Values() // []interface {}{\"a\", \"b\", \"c\", \"d\", \"e\", \"f\"} (in order)\n    _ = tree.Keys()   // []interface {}{1, 2, 3, 4, 5, 6} (in order)\n\n    tree.Remove(2) // 1-\u003ea, 3-\u003ec, 4-\u003ed, 5-\u003ee, 6-\u003ef (in order)\n    fmt.Println(tree)\n    //\n    //  AVLTree\n    //  │       ┌── 6\n    //  │   ┌── 5\n    //  └── 4\n    //      └── 3\n    //          └── 1\n\n    tree.Clear() // empty\n    tree.Empty() // true\n    tree.Size()  // 0\n}\n```\n\n#### BTree\n\nB-tree is a self-balancing tree data structure that keeps data sorted and allows searches, sequential access, insertions, and deletions in logarithmic time. The B-tree is a generalization of a binary search tree in that a node can have more than two children.\n\nAccording to Knuth's definition, a B-tree of order m is a tree which satisfies the following properties:\n\n- Every node has at most m children.\n- Every non-leaf node (except root) has at least ⌈m/2⌉ children.\n- The root has at least two children if it is not a leaf node.\n- A non-leaf node with k children contains k−1 keys.\n- All leaves appear in the same level\n\nEach internal node’s keys act as separation values which divide its subtrees. For example, if an internal node has 3 child nodes (or subtrees) then it must have 2 keys: a1 and a2. All values in the leftmost subtree will be less than a1, all values in the middle subtree will be between a1 and a2, and all values in the rightmost subtree will be greater than a2.\u003csub\u003e\u003csup\u003e[Wikipedia](http://en.wikipedia.org/wiki/Red%E2%80%93black_tree)\u003c/sub\u003e\u003c/sup\u003e\n\nImplements [Tree](#trees), [ReverseIteratorWithKey](#reverseiteratorwithkey), [JSONSerializer](#jsonserializer) and [JSONDeserializer](#jsondeserializer) interfaces.\n\n\u003cp align=\"center\"\u003e\u003cimg src=\"https://upload.wikimedia.org/wikipedia/commons/thumb/6/65/B-tree.svg/831px-B-tree.svg.png\" width=\"400px\" height=\"111px\" /\u003e\u003c/p\u003e\n\n```go\nimport (\n    \"fmt\"\n\n    \"github.com/daichi-m/go18ds/trees/btree\"\n)\n\n// BTreeExample to demonstrate basic usage of BTree\nfunc main() {\n    tree := btree.NewWithIntComparator[string](3) // empty (keys are of type int)\n\n    tree.Put(1, \"x\") // 1-\u003ex\n    tree.Put(2, \"b\") // 1-\u003ex, 2-\u003eb (in order)\n    tree.Put(1, \"a\") // 1-\u003ea, 2-\u003eb (in order, replacement)\n    tree.Put(3, \"c\") // 1-\u003ea, 2-\u003eb, 3-\u003ec (in order)\n    tree.Put(4, \"d\") // 1-\u003ea, 2-\u003eb, 3-\u003ec, 4-\u003ed (in order)\n    tree.Put(5, \"e\") // 1-\u003ea, 2-\u003eb, 3-\u003ec, 4-\u003ed, 5-\u003ee (in order)\n    tree.Put(6, \"f\") // 1-\u003ea, 2-\u003eb, 3-\u003ec, 4-\u003ed, 5-\u003ee, 6-\u003ef (in order)\n    tree.Put(7, \"g\") // 1-\u003ea, 2-\u003eb, 3-\u003ec, 4-\u003ed, 5-\u003ee, 6-\u003ef, 7-\u003eg (in order)\n\n    fmt.Println(tree)\n    // BTree\n    //         1\n    //     2\n    //         3\n    // 4\n    //         5\n    //     6\n    //         7\n\n    _ = tree.Values() // []interface {}{\"a\", \"b\", \"c\", \"d\", \"e\", \"f\", \"g\"} (in order)\n    _ = tree.Keys()   // []interface {}{1, 2, 3, 4, 5, 6, 7} (in order)\n\n    tree.Remove(2) // 1-\u003ea, 3-\u003ec, 4-\u003ed, 5-\u003ee, 6-\u003ef, 7-\u003eg (in order)\n    fmt.Println(tree)\n    // BTree\n    //     1\n    //     3\n    // 4\n    //     5\n    // 6\n    //     7\n\n    tree.Clear() // empty\n    tree.Empty() // true\n    tree.Size()  // 0\n\n    // Other:\n    tree.Height()     // gets the height of the tree\n    tree.Left()       // gets the left-most (min) node\n    tree.LeftKey()    // get the left-most (min) node's key\n    tree.LeftValue()  // get the left-most (min) node's value\n    tree.Right()      // get the right-most (max) node\n    tree.RightKey()   // get the right-most (max) node's key\n    tree.RightValue() // get the right-most (max) node's value\n}\n```\n\n#### BinaryHeap\n\nA binary heap is a [tree](#trees) created using a binary tree. It can be seen as a binary tree with two additional constraints:\n\n- Shape property:\n\n  A binary heap is a complete binary tree; that is, all levels of the tree, except possibly the last one (deepest) are fully filled, and, if the last level of the tree is not complete, the nodes of that level are filled from left to right.\n- Heap property:\n\n  All nodes are either greater than or equal to or less than or equal to each of its children, according to a comparison predicate defined for the heap. \u003csub\u003e\u003csup\u003e[Wikipedia](http://en.wikipedia.org/wiki/Binary_heap)\u003c/sub\u003e\u003c/sup\u003e\n\nImplements [Tree](#trees), [ReverseIteratorWithIndex](#reverseiteratorwithindex), [JSONSerializer](#jsonserializer) and [JSONDeserializer](#jsondeserializer) interfaces.\n\n\u003cp align=\"center\"\u003e\u003cimg src=\"http://upload.wikimedia.org/wikipedia/commons/thumb/3/38/Max-Heap.svg/501px-Max-Heap.svg.png\" width=\"300px\" height=\"200px\" /\u003e\u003c/p\u003e\n\n```go\npackage main\n\nimport (\n    \"github.com/daichi-m/go18ds/trees/binaryheap\"\n    \"github.com/daichi-m/go18ds/utils\"\n)\n\n// BinaryHeapExample to demonstrate basic usage of BinaryHeap\nfunc main() {\n\n    // Min-heap\n    heap := binaryheap.NewWithIntComparator() // empty (min-heap)\n    heap.Push(2)                              // 2\n    heap.Push(3)                              // 2, 3\n    heap.Push(1)                              // 1, 3, 2\n    heap.Values()                             // 1, 3, 2\n    _, _ = heap.Peek()                        // 1,true\n    _, _ = heap.Pop()                         // 1, true\n    _, _ = heap.Pop()                         // 2, true\n    _, _ = heap.Pop()                         // 3, true\n    _, _ = heap.Pop()                         // nil, false (nothing to pop)\n    heap.Push(1)                              // 1\n    heap.Clear()                              // empty\n    heap.Empty()                              // true\n    heap.Size()                               // 0\n\n    // Max-heap\n    inverseIntComparator := func(a, b int) int {\n        return -utils.NumberComparator(a, b)\n    }\n    heap = binaryheap.NewWith(inverseIntComparator) // empty (min-heap)\n    heap.Push(2)                                    // 2\n    heap.Push(3)                                    // 3, 2\n    heap.Push(1)                                    // 3, 2, 1\n    heap.Values()                                   // 3, 2, 1\n}\n```\n\n## Functions\n\nVarious helper functions used throughout the library.\n\n### Comparator\n\nSome data structures (e.g. TreeMap, TreeSet) require a comparator function to automatically keep their elements sorted upon insertion. This comparator is necessary during the initalization.\n\nComparator is defined as:\n\nReturn values (int):\n\n```go\nnegative , if a \u003c b\nzero     , if a == b\npositive , if a \u003e b\n```\n\nComparator signature:\n\n```go\ntype Comparator func[T comparable](a, b T) int\n```\n\nAll common comparators for builtin types are included in the library:\n\n```go\nfunc StringComparator(a, b string) int\n\nfunc NumberComparator[T Number](a, b T) int\n\nfunc TimeComparator(a, b interface{}) int\n```\n\nWriting custom comparators is easy:\n\n```go\npackage main\n\nimport (\n    \"fmt\"\n\n    \"github.com/daichi-m/go18ds/sets/treeset\"\n)\n\n// User model (id and name)\ntype User struct {\n    id   int\n    name string\n}\n\n// Comparator function (sort by IDs)\nfunc byID(c1, c2 User) int {\n\n    switch {\n    case c1.id \u003e c2.id:\n        return 1\n    case c1.id \u003c c2.id:\n        return -1\n    default:\n        return 0\n    }\n}\n\n// CustomComparatorExample to demonstrate basic usage of CustomComparator\nfunc main() {\n    set := treeset.NewWith(byID)\n\n    set.Add(User{2, \"Second\"})\n    set.Add(User{3, \"Third\"})\n    set.Add(User{1, \"First\"})\n    set.Add(User{4, \"Fourth\"})\n\n    fmt.Println(set) // {1 First}, {2 Second}, {3 Third}, {4 Fourth}\n}\n```\n\n### Iterator\n\nAll ordered containers have stateful iterators. Typically an iterator is obtained by _Iterator()_ function of an ordered container. Once obtained, iterator's _Next()_ function moves the iterator to the next element and returns true if there was a next element. If there was an element, then element's can be obtained by iterator's _Value()_ function. Depending on the ordering type, it's position can be obtained by iterator's _Index()_ or _Key()_ functions. Some containers even provide reversible iterators, essentially the same, but provide another extra _Prev()_ function that moves the iterator to the previous element and returns true if there was a previous element.\n\nNote: it is unsafe to remove elements from container while iterating.\n\n#### IteratorWithIndex\n\nAn [iterator](#iterator) whose elements are referenced by an index.\n\nTypical usage:\n```go\nit := list.Iterator()\nfor it.Next() {\n    index, value := it.Index(), it.Value()\n    ...\n}\n```\n\nOther usages:\n```go\nif it.First() {\n    firstIndex, firstValue := it.Index(), it.Value()\n    ...\n}\n```\n\n```go\nfor it.Begin(); it.Next(); {\n    ...\n}\n```\n\n#### IteratorWithKey\n\nAn [iterator](#iterator) whose elements are referenced by a key.\n\nTypical usage:\n```go\nit := tree.Iterator()\nfor it.Next() {\n    key, value := it.Key(), it.Value()\n    ...\n}\n```\n\nOther usages:\n```go\nif it.First() {\n    firstKey, firstValue := it.Key(), it.Value()\n    ...\n}\n```\n\n```go\nfor it.Begin(); it.Next(); {\n    ...\n}\n```\n\n#### ReverseIteratorWithIndex\n\nAn [iterator](#iterator) whose elements are referenced by an index. Provides all functions as [IteratorWithIndex](#iteratorwithindex), but can also be used for reverse iteration.\n\nTypical usage of iteration in reverse:\n```go\nit := list.Iterator()\nfor it.End(); it.Prev(); {\n    index, value := it.Index(), it.Value()\n    ...\n}\n```\n\nOther usages:\n```go\nif it.Last() {\n    lastIndex, lastValue := it.Index(), it.Value()\n    ...\n}\n```\n\n#### ReverseIteratorWithKey\n\nAn [iterator](#iterator) whose elements are referenced by a key. Provides all functions as [IteratorWithKey](#iteratorwithkey), but can also be used for reverse iteration.\n\nTypical usage of iteration in reverse:\n```go\nit := tree.Iterator()\nfor it.End(); it.Prev(); {\n    key, value := it.Key(), it.Value()\n    ...\n}\n```\n\nOther usages:\n```go\nif it.Last() {\n    lastKey, lastValue := it.Key(), it.Value()\n    ...\n}\n```\n\n### Enumerable\n\nEnumerable functions for ordered containers that implement [EnumerableWithIndex](#enumerablewithindex) or [EnumerableWithKey](#enumerablewithkey) interfaces.\n\n#### EnumerableWithIndex\n\n[Enumerable](#enumerable) functions for ordered containers whose values can be fetched by an index.\n\n**Each**\n\nCalls the given function once for each element, passing that element's index and value.\n\n```go\nEach(func(index int, value T))\n```\n\n**Map**\n\nInvokes the given function once for each element and returns a container containing the values returned by the given function.\n\n```go\nMap(func(index int, value T) T) Container\n```\n\n**Select**\n\nReturns a new container containing all elements for which the given function returns a true value.\n\n```go\nSelect(func(index int, value T) bool) Container\n```\n\n**Any**\n\nPasses each element of the container to the given function and returns true if the function ever returns true for any element.\n\n```go\nAny(func(index int, value T) bool) bool\n```\n\n**All**\n\nPasses each element of the container to the given function and returns true if the function returns true for all elements.\n\n```go\nAll(func(index int, value T) bool) bool\n```\n\n**Find**\n\nPasses each element of the container to the given function and returns the first (index,value) for which the function is true or -1,nil otherwise if no element matches the criteria.\n\n```go\nFind(func(index int, value T) bool) (int, T)\n```\n\n**Example:**\n\n```go\npackage main\n\nimport (\n    \"fmt\"\n\n    \"github.com/daichi-m/go18ds/sets/treeset\"\n)\n\nfunc printSet(txt string, set *treeset.Set[int]) {\n    fmt.Print(txt, \"[ \")\n    set.Each(func(index int, value int) {\n        fmt.Print(value, \" \")\n    })\n    fmt.Println(\"]\")\n}\n\n// EnumerableWithIndexExample to demonstrate basic usage of EnumerableWithIndex\nfunc main() {\n    set := treeset.NewWithIntComparator()\n    set.Add(2, 3, 4, 2, 5, 6, 7, 8)\n    printSet(\"Initial\", set) // [ 2 3 4 5 6 7 8 ]\n\n    even := set.Select(func(index int, value int) bool {\n        return value%2 == 0\n    })\n    printSet(\"Even numbers\", even) // [ 2 4 6 8 ]\n\n    foundIndex, foundValue := set.Find(func(index int, value int) bool {\n        return value%2 == 0 \u0026\u0026 value%3 == 0\n    })\n    if foundIndex != -1 {\n        fmt.Println(\"Number divisible by 2 and 3 found is\", foundValue, \"at index\", foundIndex) // value: 6, index: 4\n    }\n\n    square := set.Map(func(index int, value int) int {\n        return value * value\n    })\n    printSet(\"Numbers squared\", square) // [ 4 9 16 25 36 49 64 ]\n\n    bigger := set.Any(func(index int, value int) bool {\n        return value \u003e 5\n    })\n    fmt.Println(\"Set contains a number bigger than 5 is \", bigger) // true\n\n    positive := set.All(func(index int, value int) bool {\n        return value \u003e 0\n    })\n    fmt.Println(\"All numbers are positive is\", positive) // true\n\n    evenNumbersSquared := set.Select(func(index int, value int) bool {\n        return value%2 == 0\n    }).Map(func(index int, value int) int {\n        return value * value\n    })\n    printSet(\"Chaining\", evenNumbersSquared) // [ 4 16 36 64 ]\n}\n```\n\n#### EnumerableWithKey\n\nEnumerable functions for ordered containers whose values whose elements are key/value pairs.\n\n**Each**\n\nCalls the given function once for each element, passing that element's key and value.\n\n```go\nEach(func(key K, value V))\n```\n\n**Map**\n\nInvokes the given function once for each element and returns a container containing the values returned by the given function as key/value pairs.\n\n```go\nMap(func(key K, value V) (K, V)) Container\n```\n\n**Select**\n\nReturns a new container containing all elements for which the given function returns a true value.\n\n```go\nSelect(func(key K, value V) bool) Container\n```\n\n**Any**\n\nPasses each element of the container to the given function and returns true if the function ever returns true for any element.\n\n```go\nAny(func(key K, value V) bool) bool\n```\n\n**All**\n\nPasses each element of the container to the given function and returns true if the function returns true for all elements.\n\n```go\nAll(func(key K, value V) bool) bool\n```\n\n**Find**\n\nPasses each element of the container to the given function and returns the first (key,value) for which the function is true or nil,nil otherwise if no element matches the criteria.\n\n```go\nFind(func(key K, value V) bool) (K, V)\n```\n\n**Example:**\n\n```go\npackage main\n\nimport (\n    \"fmt\"\n\n    \"github.com/daichi-m/go18ds/maps/treemap\"\n)\n\nfunc printMap(txt string, m *treemap.Map[string, int]) {\n    fmt.Print(txt, \" { \")\n    m.Each(func(key string, value int) {\n        fmt.Print(key, \":\", value, \" \")\n    })\n    fmt.Println(\"}\")\n}\n\n// EunumerableWithKeyExample to demonstrate basic usage of EunumerableWithKey\nfunc main() {\n    m := treemap.NewWithStringComparator[int]()\n    m.Put(\"g\", 7)\n    m.Put(\"f\", 6)\n    m.Put(\"e\", 5)\n    m.Put(\"d\", 4)\n    m.Put(\"c\", 3)\n    m.Put(\"b\", 2)\n    m.Put(\"a\", 1)\n    printMap(\"Initial\", m) // { a:1 b:2 c:3 d:4 e:5 f:6 g:7 }\n\n    even := m.Select(func(key string, value int) bool {\n        return value%2 == 0\n    })\n    printMap(\"Elements with even values\", even) // { b:2 d:4 f:6 }\n\n    foundKey, foundValue := m.Find(func(key string, value int) bool {\n        return value%2 == 0 \u0026\u0026 value%3 == 0\n    })\n    if foundKey != \"\" {\n        fmt.Println(\"Element with value divisible by 2 and 3 found is\", foundValue, \"with key\", foundKey) // value: 6, index: 4\n    }\n\n    square := m.Map(func(key string, value int) (string, int) {\n        return key + key, value * value\n    })\n    printMap(\"Elements' values squared and letters duplicated\", square) // { aa:1 bb:4 cc:9 dd:16 ee:25 ff:36 gg:49 }\n\n    bigger := m.Any(func(key string, value int) bool {\n        return value \u003e 5\n    })\n    fmt.Println(\"Map contains element whose value is bigger than 5 is\", bigger) // true\n\n    positive := m.All(func(key string, value int) bool {\n        return value \u003e 0\n    })\n    fmt.Println(\"All map's elements have positive values is\", positive) // true\n\n    evenNumbersSquared := m.Select(func(key string, value int) bool {\n        return value%2 == 0\n    }).Map(func(key string, value int) (string, int) {\n        return key, value * value\n    })\n    printMap(\"Chaining\", evenNumbersSquared) // { b:4 d:16 f:36 }\n}\n```\n\n### Serialization\n\nAll data structures can be serialized (marshalled) and deserialized (unmarshalled). Currently only JSON support is available.\n\n#### JSONSerializer\n\nOutputs the container into its JSON representation.\n\nTypical usage for key-value structures:\n```go\npackage main\n\nimport (\n    \"fmt\"\n    \"github.com/daichi-m/go18ds/maps/hashmap\"\n)\n\nfunc main() {\n    m := hashmap.New[string, string]()\n    m.Put(\"a\", \"1\")\n    m.Put(\"b\", \"2\")\n    m.Put(\"c\", \"3\")\n\n    // Serialization (marshalling)\n    json, err := m.ToJSON()\n    if err != nil {\n        fmt.Println(err)\n    }\n    fmt.Println(string(json)) // {\"a\":\"1\",\"b\":\"2\",\"c\":\"3\"}\n}\n```\n\nTypical usage for value-only structures:\n```go\npackage main\n\nimport (\n    \"fmt\"\n    \"github.com/daichi-m/go18ds/lists/arraylist\"\n)\n\nfunc main() {\n    list := arraylist.New[string]()\n    list.Add(\"a\", \"b\", \"c\")\n\n    // Serialization (marshalling)\n    json, err := list.ToJSON()\n    if err != nil {\n        fmt.Println(err)\n    }\n    fmt.Println(string(json)) // [\"a\",\"b\",\"c\"]\n}\n```\n\n#### JSONDeserializer\n\nPopulates the container with elements from the input JSON representation.\n\nTypical usage for key-value structures:\n```go\npackage main\n\nimport (\n    \"fmt\"\n    \"github.com/daichi-m/go18ds/maps/hashmap\"\n)\n\nfunc main() {\n    hm := hashmap.New[string, string]()\n\n    // Deserialization (unmarshalling)\n    json = []byte(`{\"a\":\"1\",\"b\":\"2\"}`)\n    err = hm.FromJSON(json)\n    if err != nil {\n        fmt.Println(err)\n    }\n    fmt.Println(hm) // HashMap {\"a\":\"1\",\"b\":\"2\"}\n}\n```\n\nTypical usage for value-only structures:\n```go\npackage main\n\nimport (\n    \"fmt\"\n    \"github.com/daichi-m/go18ds/lists/arraylist\"\n)\n\nfunc main() {\n    list := arraylist.New[string]()\n\n    json = []byte(`[\"a\",\"b\"]`)\n    err = list.FromJSON(json)\n    if err != nil {\n        fmt.Println(err)\n    }\n    fmt.Println(list) // ArrayList [\"a\",\"b\"]\n}\n```\n\n### Sort\n\nSort is a general purpose sort function.\n\nLists have an in-place _Sort()_ function and all containers can return their sorted elements via _containers.GetSortedValues()_ function.\n\nInternally these all use the _utils.Sort()_ method:\n\n```go\npackage main\n\nimport \"github.com/daichi-m/go18ds/utils\"\n\n// SortExample to demonstrate basic usage of basic sort\nfunc main() {\n    strings := []string{}                       // []\n    strings = append(strings, \"d\")              // [\"d\"]\n    strings = append(strings, \"a\")              // [\"d\",\"a\"]\n    strings = append(strings, \"b\")              // [\"d\",\"a\",b\"\n    strings = append(strings, \"c\")              // [\"d\",\"a\",b\",\"c\"]\n    utils.Sort(strings, utils.StringComparator) // [\"a\",\"b\",\"c\",\"d\"]\n}\n```\n\n### Container\n\nContainer specific operations:\n\n```go\n// Returns sorted container''s elements with respect to the passed comparator.\n// Does not effect the ordering of elements within the container.\nfunc GetSortedValues(container Container, comparator utils.Comparator) []interface{}\n```\n\nUsage:\n\n```go\npackage main\n\nimport (\n    \"github.com/emirpasic/gods/lists/arraylist\"\n    \"github.com/emirpasic/gods/utils\"\n)\n\nfunc main() {\n    list := arraylist.New[int]()\n    list.Add(2, 1, 3)\n    values := GetSortedValues(container, utils.NumberComparator[int]) // [1, 2, 3]\n}\n```\n\n## Appendix\n\n### Motivation\n\nCollections and data structures found in other languages: Java Collections, C++ Standard Template Library (STL) containers, Qt Containers, Ruby Enumerable etc.\n\n### Goals\n\n**Fast algorithms**:\n\n- Based on decades of knowledge and experiences of other libraries mentioned above.\n\n**Memory efficient algorithms**:\n\n- Avoiding to consume memory by using optimal algorithms and data structures for the given set of problems, e.g. red-black tree in case of TreeMap to avoid keeping redundant sorted array of keys in memory.\n\n**Easy to use library**:\n\n- Well-structured library with minimalistic set of atomic operations from which more complex operations can be crafted.\n\n**Stable library**:\n\n- Only additions are permitted keeping the library backward compatible.\n\n**Solid documentation and examples**:\n\n- Learning by example.\n\n**Production ready**:\n\n- Used in production.\n\n**No dependencies**:\n\n- No external imports.\n\nThere is often a tug of war between speed and memory when crafting algorithms. We choose to optimize for speed in most cases within reasonable limits on memory consumption.\n\nThread safety is not a concern of this project, this should be handled at a higher level.\n\n### Testing and Benchmarking\n\nThis takes a while, so test within sub-packages:\n\n`go test -run=NO_TEST -bench . -benchmem  -benchtime 1s ./...`\n\n[Click here](https://github.com/daichi-m/go18ds/blob/main/benchmark.txt) to see the benchmark results.\n\n### Contributing\n\nBiggest contribution towards this library is to use it and give us feedback for further improvements and additions.\n\nFor direct contributions, _pull request_ into master branch or ask to become a contributor.\n\nCoding style:\n\n```shell\n# Install tooling and set path:\ngo get github.com/golang/lint/golint\ngo get github.com/fzipp/gocyclo\ngo get github.com/kisielk/errcheck\nexport PATH=$PATH:$GOPATH/bin\n\n# Fix errors and warnings:\ngo fmt ./... \u0026\u0026 gofmt -s -w . \u0026\u0026 go vet ./... \u0026\u0026 go get ./... \u0026\u0026 go test ./... \u0026\u0026 golint ./... \u0026\u0026 gocyclo -avg -over 15 . \u0026\u0026 errcheck ./...\n```\n\n### License\n\nThis library is distributed under the BSD-style license found in the [LICENSE](https://github.com/emirpasic/gods/blob/master/LICENSE) file.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdaichi-m%2Fgo18ds","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdaichi-m%2Fgo18ds","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdaichi-m%2Fgo18ds/lists"}