使用二进制堆的python函数
binary-heap的Python项目详细描述
python库,帮助使用列表数据结构形成二进制堆(最小、最大)。 此库提供以下堆特定函数。
- 堆排序
- 使用堆排序算法对给定的元素列表排序
- heapify
- 将元素列表转换为堆数据结构(minheap/maxheap)
- 添加元素
- 将单个元素/元素列表添加到堆中
- 获取根值
- 返回堆的根值,而不删除元素 最小堆最小值,最大堆值
- 提取根
- 从堆中提取根元素并改革堆
- 搜索值
- 在堆中搜索值并返回索引。 如果同一元素多次出现,则返回第一个出现的索引
- delete_element_at_index
- 删除指定索引处的元素并重新生成堆
例如函数调用,请参阅教程。
Installation
使用pip从pypi安装:
$ pip install binary_heap
或简易安装:
$ easy_install binary_heap
或使用以下命令从源安装:
$ git clone https://github.com/rameshrvr/binary_heap.git $ cd binary_heap $ pip install .
Tutorial
- 堆排序(使用堆排序算法对列表排序)
Rameshs-MBP:binary_heapramesh_rv$python3Python3.7.0(v3.7.0:1bf9cc5093,Jun262018,23:26:24)[Clang6.0(clang-600.0.57)]ondarwinType"help","copyright","credits"or"license"formoreinformation.>>>>>>frombinary_heapimportheap_sort>>>>>>data=[4,1,3,2,16,9,10,14,8,7]>>>>>>heap_sort(data)# Returns sorted list in ascending order[1,2,3,4,7,8,9,10,14,16]>>>>>>heap_sort(data,reverse=True)# Returns sorted list in decsnding order[16,14,10,9,8,7,4,3,2,1]>>>
- 最小堆(父节点中的数据小于子节点中的数据的堆)
Rameshs-MBP:binary_heaprameshrv$python3Python3.7.2(default,Dec272018,07:35:06)[Clang10.0.0(clang-1000.11.45.5)]ondarwinType"help","copyright","credits"or"license"formoreinformation.>>>>>>frombinary_heapimportMinHeap>>>>>>min_heap=MinHeap([4,3,6,8,11])# Create an object for Min Heap>>>>>>min_heap.length()# Returns size of the heap5>>>min_heap.elements()[3,4,6,8,11]>>>>>>min_heap.add_element(1)# Add a single element to Heap>>>>>>min_heap.elements()[1,4,3,8,11,6]>>>>>>min_heap.add_element([1,14,7,5])# Add list of elements to Heap>>>>>>min_heap.elements()[1,4,1,7,5,6,3,14,8,11]>>>>>>min_heap.extract_root()# Extract root element from Heap and retrun it. In this case its the minimum element1>>>>>>min_heap.elements()[1,4,3,7,5,6,11,14,8]>>>>>>min_heap.get_root_value()# Returns the root value (minimum value) without removing it from Heap1>>>>>>min_heap.search_value(value=5)# Returns index of the searched value. -1 if there is no such value in Heap4>>>min_heap.search_value(value=7)3>>>min_heap.search_value(value=16)-1>>>>>>min_heap.delete_element_at_index(3)# Remove the element at the specified index>>>>>>min_heap.elements()[1,4,3,8,5,6,11,14]>>>
- 最大堆(父节点中的数据大于子节点中的数据的堆)
Rameshs-MBP:binary_heaprameshrv$python3Python3.7.2(default,Dec272018,07:35:06)[Clang10.0.0(clang-1000.11.45.5)]ondarwinType"help","copyright","credits"or"license"formoreinformation.>>>>>>frombinary_heapimportMaxHeap>>>>>>max_heap=MaxHeap([4,3,6,8,11])# Create an object for Max Heap>>>>>>max_heap.elements()# Returns size of the heap[11,8,6,4,3]>>>>>>max_heap.add_element(13)# Add a single element to Heap>>>>>>max_heap.elements()[13,8,11,4,3,6]>>>>>>max_heap.add_element([1,14,7,5])# Add list of elements to Heap>>>>>>max_heap.elements()[14,13,11,8,5,6,1,4,7,3]>>>>>>max_heap.extract_root()# Extract root element from Heap and retrun it. In this case its the maximum element14>>>>>>max_heap.elements()[13,8,11,7,5,6,1,4,3]>>>>>>max_heap.get_root_value()# Returns the root value (maximum value) without removing it from Heap13>>>>>>max_heap.search_value(value=11)# Returns index of the searched value. -1 if there is no such value in Heap2>>>max_heap.search_value(value=1)6>>>max_heap.search_value(value=14)-1>>>>>>max_heap.delete_element_at_index(3)# Remove the element at the specified index>>>>>>max_heap.elements()[13,8,11,4,5,6,1,3]
Development
签出回购协议后,cd到存储库。然后,运行pip install.在本地安装软件包。您还可以运行python3python(或)python3以获得允许您进行实验的交互式提示。
若要将此软件包安装到本地计算机上,请将cd安装到存储库,然后运行pip install.。要发布新版本,请更新setup.py中的版本号,然后运行python setup.py register,这将为版本创建一个git标记,推送git提交和标记,并将包文件推送到[pypi](https://pypi.org)。
Contributing
github上的https://github.com/rameshrvr/binary_heap欢迎错误报告和请求拉取。该项目旨在成为一个安全、受欢迎的合作空间,贡献者应遵守[贡献者契约](http://contributor-covenant.org)行为准则。
License
根据[gpl-3.0许可证](https://opensource.org/licenses/GPL-3.0)的条款,该包作为开放源码提供。
Code of Conduct
在二进制堆项目的代码库、问题跟踪程序、聊天室和邮件列表中进行交互的每个人都应该遵循[行为准则](https://github.com/rameshrvr/binary_heap/blob/master/CODE_OF_CONDUCT.md)。
misc
license: |
|
---|---|
authors: |
|