資料流中的中位數python 資料流中的中位數

2021-10-12 13:06:18 字數 4052 閱讀 3509

python用的自己實現的最大和最小堆的class,getmedian需要加個引數,否則python版會報錯。# -*- coding:utf-8 -*-

# 最小堆

class minheap:

def __init__(self):

self.minheap = 

def __len__(self):

return len(self.minheap)

def insert(self, elem):

l = len(self.minheap) - 1

while l != 0 and self.minheap[int((l - 1) / 2)] > self.minheap[l]:

self.minheap[int((l - 1) / 2)], self.minheap[l] = self.minheap[l], self.minheap[int((l - 1) / 2)]

l = int((l - 1) / 2)

def pophead(self):

self.minheap[0] = self.minheap[-1]

self.minheap = self.minheap[:-1]

i, l = 0, len(self.minheap) - 1

while 1:

if 2 * i + 1 > l:  # 沒有子節點

break

elif 2 * i + 2 > l:  # 只有左節點

if self.minheap[2 * i + 1] 

self.minheap[2 * i + 1], self.minheap[i] = self.minheap[i], self.minheap[2 * i + 1]

i = 2 * i + 1

else:

break

else:  # 兒女雙全

if self.minheap[2 * i + 1]  self.minheap[2 * i + 2]:  # 比兩個孩子都大

if self.minheap[2 * i + 1] 

self.minheap[2 * i + 1], self.minheap[i] = self.minheap[i], self.minheap[2 * i + 1]

i = 2 * i + 1

else:

self.minheap[2 * i + 2], self.minheap[i] = self.minheap[i], self.minheap[2 * i + 2]

i = 2 * i + 2

elif self.minheap[2 * i + 1] 

self.minheap[2 * i + 1], self.minheap[i] = self.minheap[i], self.minheap[2 * i + 1]

i = 2 * i + 1

elif self.minheap[2 * i + 1] > self.minheap[i] > self.minheap[2 * i + 2]:

self.minheap[2 * i + 2], self.minheap[i] = self.minheap[i], self.minheap[2 * i + 2]

i = 2 * i + 2

else:

break

def head(self):

return self.minheap[0]

# 最大堆

class maxheap:

def __init__(self):

self.maxheap = 

def __len__(self):

return len(self.maxheap)

def insert(self, elem):

l = len(self.maxheap) - 1

while l != 0 and self.maxheap[int((l - 1) / 2)] 

self.maxheap[int((l - 1) / 2)], self.maxheap[l] = self.maxheap[l], self.maxheap[int((l - 1) / 2)]

l = int((l - 1) / 2)

def pophead(self):

self.maxheap[0] = self.maxheap[-1]

self.maxheap = self.maxheap[:-1]

i, l = 0, len(self.maxheap) - 1

while 1:

if 2 * i + 1 > l:  # 沒有子節點

break

elif 2 * i + 2 > l:  # 只有左節點

if self.maxheap[2 * i + 1] > self.maxheap[i]:

self.maxheap[2 * i + 1], self.maxheap[i] = self.maxheap[i], self.maxheap[2 * i + 1]

i = 2 * i + 1

else:

break

else:  # 兒女雙全

if self.maxheap[2 * i + 1] > self.maxheap[i] 

if self.maxheap[2 * i + 1] > self.maxheap[2 * i + 2]:  # 左孩子小於右孩子

self.maxheap[2 * i + 1], self.maxheap[i] = self.maxheap[i], self.maxheap[2 * i + 1]

i = 2 * i + 1

else:

self.maxheap[2 * i + 2], self.maxheap[i] = self.maxheap[i], self.maxheap[2 * i + 2]

i = 2 * i + 2

elif self.maxheap[2 * i + 1] > self.maxheap[i] > self.maxheap[2 * i + 2]:

self.maxheap[2 * i + 1], self.maxheap[i] = self.maxheap[i], self.maxheap[2 * i + 1]

i = 2 * i + 1

elif self.maxheap[2 * i + 1] 

self.maxheap[2 * i + 2], self.maxheap[i] = self.maxheap[i], self.maxheap[2 * i + 2]

i = 2 * i + 2

else:

break

def head(self):

return self.maxheap[0]

class solution:

def __init__(self):

self.minh = minheap()

self.maxh = maxheap()

def insert(self, num):

self.maxh.insert(num)

if len(self.maxh) > len(self.minh) + 1:

self.minh.insert(self.maxh.head())

self.maxh.pophead()

if len(self.minh) > 0 and self.maxh.head() > self.minh.head():

self.maxh.insert(self.minh.head())

self.minh.insert(self.maxh.head())

self.maxh.pophead()

self.minh.pophead()

def getmedian(self,n = none):

if len(self.maxh) is len(self.minh):

return float(self.maxh.head() + self.minh.head())/2

elif len(self.maxh) is len(self.minh) + 1:

return self.maxh.head()

資料流中的中位數

資料流中的中位數 如何得到乙個資料流中的中位數?如果從資料流中讀出奇數個數值,那麼中位數就是所有數值排序之後位於中間的數值。如果從資料流中讀出偶數個數值,那麼中位數就是所有數值排序之後中間兩個數的平均值。解題思路 維護乙個大堆和乙個小堆,大堆表示序列前一半數,小堆表示序列後一半數,保持兩個堆的元素個...

資料流中的中位數

如何得到乙個資料流中的中位數?如果從資料流中讀出奇數個數值,那麼中位數就是所有數值排序之後位於中間的數值。如果從資料流中讀出偶數個數值,那麼中位數就是所有數值排序之後中間兩個數的平均值。class solution double getmedian else private vectordata c...

資料流中的中位數

如何得到乙個資料流中的中位數?如果從資料流中讀出奇數個數值,那麼中位數就是所有數值排序之後位於中間的數值。如果從資料流中讀出偶數個數值,那麼中位數就是所有數值排序之後中間兩個數的平均值。簡單題 找中位數 coding utf 8 class solution x def insert self,nu...