python_二叉查找树 堆排序 优先级队列

匿名 (未验证) 提交于 2019-12-02 22:11:45

Task5

【二叉树】

  • 实现一个二叉查找树,并且支持插入、删除、查找操作
  • 实现查找二叉查找树中某个节点的后继、前驱节点
  • 实现二叉树前、中、后序以及按层遍历

【堆】

  • 实现一个小顶堆、大顶堆、优先级队列
  • 实现堆排序
  • 利用优先级队列合并 K 个有序数组
  • 求一组动态数据集合的最大 Top K

二叉查找树(内部函数已实现BFS与三种DFS算法):

class Node(object):     def __init__(self, value):         self.value = value         self.lchild = None         self.rchild = None   class BinarySearchTree(object):     def __init__(self, value):         self.root = Node(value)      def find(self, value, node, parent, nodetype):         if node is None:             return False, node, parent, nodetype         elif node.value == value:             return True, node, parent, nodetype         elif node.value < value:             return self.find(value, node.rchild, node, 'rchild')         else:             return self.find(value, node.lchild, node, 'lchild')      def insert(self, value):         flag, node, parent, nodetype = self.find(value, self.root, self.root, None)         if nodetype == 'lchild':             parent.lchild = Node(value)         else:             parent.rchild = Node(value)      def preorder(self, node):         if node is None:             return         print(node.value)         self.preorder(node.lchild)         self.preorder(node.rchild)      def inorder(self, node):         if node is None:             return         self.inorder(node.lchild)         print(node.value)         self.inorder(node.rchild)      def postorder(self, node):         if node is None:             return         self.postorder(node.lchild)         self.postorder(node.rchild)         print(node.value)      def bfs(self, node):         if node is None:             return         else:             queue = []             queue.append(node)             while queue:                 cur = queue.pop(0)                 print(cur.value)                 if cur.lchild is not None:                     queue.append(cur.lchild)                 if cur.rchild is not None:                     queue.append(cur.rchild)      def findmin(self, node):         if node.lchild == None:             return node         else:             return self.findmin(node.lchild)      def delvalue(self, value):         flag, node, parent, nodetype = self.find(value, self.root, self.root, None)         if not flag:             return         else:             if node.lchild is None and node.rchild is None:                 if nodetype == 'lchild':                     parent.lchild = None                 else:                     parent.rchild = None                 del node             elif node.lchild is not None and node.rchild is not None:                 minnode = self.findmin(node.rchild)                 n = minnode.value                 self.delvalue(n)                 node.value = n             else:                 if nodetype == 'lchild':                     if node.lchild is None:                         parent.lchild = node.rchild                     else:                         parent.lchild = node.lchild                 else:                     if node.lchild is None:                         parent.rchild = node.rchild                     else:                         parent.rchild = node.lchild                 del node   if __name__ == '__main__':     b = BinarySearchTree(10)     b.insert(5)     b.insert(15)     b.insert(3)     b.insert(8)     b.insert(6)     b.insert(9)     b.insert(16)     b.preorder(b.root)     flag, *rest = b.find(6, b.root, b.root, None)     print(flag)     flag, *rest = b.find(11, b.root, b.root, None)     print(flag)     b.delvalue(5)     flag, *rest = b.find(5, b.root, b.root, None)     print(flag)     b.preorder(b.root) 

大顶堆堆排序(小顶堆类似,不再赘述):

from collections import deque   def swap(L, i, j):     L[i], L[j] = L[j], L[i]     return L   def heap_adjust(L, start, end):     temp = L[start]      i = start     j = 2 * i      while j <= end:         if (j < end) and (L[j] < L[j + 1]):             j += 1         if temp < L[j]:             L[i] = L[j]             i = j             j = 2 * i         else:             break     L[i] = temp   def heap_sort(L):     L_length = len(L) - 1      first_sort_count = L_length // 2     for i in range(first_sort_count):         heap_adjust(L, first_sort_count - i, L_length)      for i in range(L_length - 1):         L = swap_param(L, 1, L_length - i)         heap_adjust(L, 1, L_length - i - 1)      return [L[i] for i in range(1, len(L))]   L = deque([50, 16, 30, 10, 60, 90, 2, 80, 70]) L.appendleft(0) print(heap_sort(L)) 

优先级队列如下:

import heapq   class PriorityQueue:     def __init__(self):         self._queue = []         self._index = 0      def push(self, item, priority):         heapq.heappush(self._queue, (-priority, self._index, item))         self._index += 1      def pop(self):         return heapq.heappop(self._queue)[-1]   class Item:     def __init__(self, name):         self.name = name      def __repr__(self):         return 'Item({!r})'.format(self.name)   if __name__ == "__main__":     q = PriorityQueue()     q.push(Item('python'), 1)     q.push(Item('java'), 5)     q.push(Item('swift'), 4)     q.push(Item('c++'), 1)     for i in range(4):         print(q.pop()) 

K路合并以及寻找TopK找时间再补足,最近有点忙。

文章来源: https://blog.csdn.net/weixin_42698229/article/details/90383316
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!