用于处理d元堆的python函数(具有两个子节点以上的堆)
d-heap的Python项目详细描述
用于处理d元堆(具有两个子节点以上的堆)的python函数。有关此数据结构的详细信息,请访问:https://en.wikipedia.org/wiki/D-ary_heap
此库提供以下堆特定函数。
- heapify
- 将元素列表转换为堆数据结构(minheap/maxheap)
- 添加元素
- 将单个元素/元素列表添加到堆中
- 获取根值
- 返回堆的根值,而不删除元素 最小堆最小值,最大堆值
- 提取根
- 从堆中提取根元素并改革堆
- 搜索值
- 在堆中搜索值并返回索引。 如果同一元素多次出现,则返回第一个出现的索引
- delete_element_at_index
- 删除指定索引处的元素并重新生成堆
例如函数调用,请参阅教程。
Installation
使用pip从pypi安装:
$ pip install d_heap
或使用以下命令从源安装:
$ git clone https://github.com/rameshrvr/d-ary_heap.git $ cd d-ary_heap $ pip install .
Tutorial
- 最小堆(父节点中的数据小于子节点中的数据的堆)
Rameshs-MacBook-Pro:d-ary_heaprameshrv$python3Python3.7.2(default,Dec272018,07:35:06)[Clang10.0.0(clang-1000.11.45.5)]ondarwinType"help","copyright","credits"or"license"formoreinformation.>>>>>>fromd_heapimportMinHeap,MaxHeap>>>>>>array=[4,3,6,8,11,1,5,14,10,7,2,12,9,13,15]>>>>>>min_heap_4_children=MinHeap(4,array)# Convert array to 4 children Heap>>>>>>min_heap_4_children.elements()[1,3,2,8,11,4,5,14,10,7,6,12,9,13,15]>>>>>>min_heap_5_children=MinHeap(5,array)# Convert array to 5 children Heap>>>>>>min_heap_5_children.elements()[1,2,6,8,11,4,5,14,10,7,3,12,9,13,15]>>>>>>min_heap_4_children.add_element(0)# Add single element to Heap>>>>>>min_heap_4_children.elements()[0,3,2,1,11,4,5,14,10,7,6,12,9,13,15,8]>>>>>>min_heap_5_children.add_element([0,24,17,55])# Add list of elements to heap>>>>>>min_heap_5_children.elements()[0,2,1,8,11,4,5,14,10,7,3,12,9,13,15,6,24,17,55]>>>>>>min_heap_4_children.extract_root()# Extract root element from Heap and retrun it. In this case its the minimum element0>>>>>>min_heap_4_children.elements()[1,3,2,8,11,4,5,14,10,7,6,12,9,13,15]>>>>>>min_heap_4_children.get_root_value()# Returns the root value (minimum value) without removing it from Heap1>>>>>>min_heap_4_children.search_value(5)# Returns index of the searched value. -1 if there is no such value in Heap6>>>min_heap_4_children.search_value(7)9>>>min_heap_4_children.search_value(21)-1>>>>>>min_heap_4_children.delete_element_at_index(4)# Remove the element at the specified index>>>>>>min_heap_4_children.elements()[1,3,2,8,15,4,5,14,10,7,6,12,9,13]>>>
- 最大堆(父节点中的数据大于子节点中的数据的堆)
Rameshs-MacBook-Pro:d-ary_heaprameshrv$python3Python3.7.2(default,Dec272018,07:35:06)[Clang10.0.0(clang-1000.11.45.5)]ondarwinType"help","copyright","credits"or"license"formoreinformation.>>>>>>fromd_heapimportMinHeap,MaxHeap>>>>>>array=[4,3,6,8,11,1,5,14,10,7,2,12,9,13,15]>>>>>>max_heap_4_children=MaxHeap(4,array)# Convert array to 4 children Heap>>>>>>max_heap_4_children.elements()[15,14,12,13,11,1,5,3,10,7,2,6,9,4,8]>>>>>>max_heap_5_children=MaxHeap(5,array)# Convert array to 5 children Heap>>>>>>max_heap_5_children.elements()[15,14,13,8,11,1,5,3,10,7,2,12,9,4,6]>>>>>>max_heap_4_children.add_element(21)# Add single element to Heap>>>>>>max_heap_4_children.elements()[21,14,12,15,11,1,5,3,10,7,2,6,9,4,8,13]>>>>>>>>>max_heap_5_children.add_element([21,14,27,35])# Add list of elements to heap>>>>>>max_heap_5_children.elements()[35,14,15,27,11,1,5,3,10,7,2,12,9,4,6,13,8,14,21]>>>>>>max_heap_4_children.extract_root()# Extract root element from Heap and retrun it. In this case its the maximum element21>>>>>>max_heap_4_children.elements()[15,14,12,13,11,1,5,3,10,7,2,6,9,4,8]>>>>>>max_heap_4_children.get_root_value()# Returns the root value (maximum value) without removing it from Heap15>>>>>>max_heap_4_children.search_value(5)# Returns index of the searched value. -1 if there is no such value in Heap6>>>max_heap_4_children.search_value(11)4>>>max_heap_4_children.search_value(21)-1>>>>>>max_heap_4_children.delete_element_at_index(2)# Remove the element at the specified index>>>>>>max_heap_4_children.elements()[15,14,9,13,11,1,5,3,10,7,2,6,8,4]>>>
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/d-ary_heap欢迎错误报告和请求拉取。该项目旨在成为一个安全、受欢迎的合作空间,贡献者应遵守[贡献者契约]<;http://contributor-covenant.org>;行为准则。
License
根据[麻省理工学院许可证]<;https://opensource.org/licenses/MIT>;的条款,该软件包作为开放源码提供。
Code of Conduct
在二进制堆项目的代码库、问题跟踪程序、聊天室和邮件列表中进行交互的每个人都应该遵循[行为准则](https://github.com/rameshrvr/d-ary_heap/blob/master/CODE_OF_CONDUCT.md)。
misc
license: |
|
---|---|
authors: |
|