diff --git a/README.md b/README.md index 80c88a5..dd21bdf 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ [![repo-size](https://img.shields.io/github/repo-size/mbinary/algorithm.svg)]() [![License](https://img.shields.io/badge/LICENSE-WTFPL-blue.svg)](LICENSE) [![Language](https://img.shields.io/badge/language-python3-orange.svg)]() -[![codebeat badge](https://codebeat.co/badges/4ef725b5-405a-4390-a860-a86deefab3f8)](https://codebeat.co/projects/github-com-mbinary-algorithm-master) +[![codebeat badge](https://codebeat.co/badges/d52dd17d-a437-4dee-a6ec-cb532e8135bd)](https://codebeat.co/projects/github-com-mbinary-algorithm-master) >Notes and codes for learning algorithm and data structures :smiley: diff --git a/dataStructure/binaryHeap1.py b/dataStructure/binaryHeap1.py deleted file mode 100644 index 89a32b1..0000000 --- a/dataStructure/binaryHeap1.py +++ /dev/null @@ -1,83 +0,0 @@ -''' mbinary -######################################################################### -# File : binaryHeap1.py -# Author: mbinary -# Mail: zhuheqin1@gmail.com -# Blog: https://mbinary.xyz -# Github: https://github.com/mbinary -# Created Time: 2019-04-16 09:41 -# Description: -######################################################################### -''' -from functools import partial -class heap: - def __init__(self,lst,reverse = False): - self.data= heapify(lst,reverse) - self.cmp = partial(lambda i,j,r:cmp(self.data[i],self.data[j],r),r= reverse) - def getTop(self): - return self.data[0] - def __getitem__(self,idx): - return self.data[idx] - def __bool__(self): - return self.data != [] - def popTop(self): - ret = self.data[0] - n = len(self.data) - cur = 1 - while cur * 2<=n: - chd = cur-1 - r_idx = cur*2 - l_idx = r_idx-1 - if r_idx==n: - self.data[chd] = self.data[l_idx] - break - j = l_idx if self.cmp(l_idx,r_idx)<0 else r_idx - self.data[chd] = self.data[j] - cur = j+1 - self.data[cur-1] = self.data[-1] - self.data.pop() - return ret - - def addNode(self,val): - self.data.append(val) - self.data = one_heapify(len(self.data)-1) - - -def cmp(n1,n2,reverse=False): - fac = -1 if reverse else 1 - if n1 < n2: return -fac - elif n1 > n2: return fac - return 0 - -def heapify(lst,reverse = False): - for i in range(len(lst)): - lst = one_heapify(lst,i,reverse) - return lst -def one_heapify(lst,cur,reverse = False): - cur +=1 - while cur>1: - chd = cur-1 - prt = cur//2-1 - if cmp(lst[prt],lst[chd],reverse)<0: - break - lst[prt],lst[chd] = lst[chd], lst[prt] - cur = prt+1 - return lst -def heapSort(lst,reverse = False): - lst = lst.copy() - hp = heap(lst,reverse) - ret = [] - while hp: - ret.append(hp.popTop()) - return ret - - -if __name__ == '__main__': - from random import randint - n = randint(10,20) - lst = [randint(0,100) for i in range(n)] - print('random : ', lst) - print('small-heap: ', heapify(lst)) - print('big-heap : ', heapify(lst,True)) - print('ascend : ', heapSort(lst)) - print('descend : ', heapSort(lst,True)) diff --git a/dataStructure/loserTree.py b/dataStructure/loserTree.py deleted file mode 100644 index 3d03a83..0000000 --- a/dataStructure/loserTree.py +++ /dev/null @@ -1,116 +0,0 @@ -''' mbinary -######################################################################### -# File : loserTree.py -# Author: mbinary -# Mail: zhuheqin1@gmail.com -# Blog: https://mbinary.xyz -# Github: https://github.com/mbinary -# Created Time: 2018-05-19 23:06 -# Description: -######################################################################### -''' - -from winnerTree import winnerTree -class loserTree: - '''if i<=lowExt p = (i+offset)//2 - else p = (i+n-1-lowExt)//2 - s is the num of the full subtree node - p is the index of tree - i is the index of players - offset is a num 2^k-1 just bigger than n - lowExt is the double node num of the lowest layer of the tree - ''' - def __init__(self,players,reverse=False): - self.n=len(players) - self.tree = [0]*self.n - players.insert(0,0) - self.players=players - self.reverse=reverse - self.getNum() - self.tree[0] = self.initTree(1) - self.dir=None - def getNum(self): - i=1 - while 2*i< self.n:i=i*2 - if 2*i ==self. n: - self.lowExt=0 - self.s = 2*i-1 - else: - self.lowExt = (self.n-i)*2 - self.s = i-1 - self.offset = 2*i-1 - def treeToArray(self,p): - return 2*p-self.offset if p>self.s else 2*p+self.lowExt-self.n+1 - def arrayToTree(self,i): - return (i+self.offset)//2 if i<=self.lowExt else (i-self.lowExt+ self.n-1)//2 - def win(self,a,b): - return ab - def initTree(self,p): - if p>=self.n: - delta = p%2 #!!! good job notice delta mark the lchild or rchlid - return self.players[self.treeToArray(p//2)+delta] - l = self.initTree(2*p) - r = self.initTree(2*p+1) - if self.win(r,l): - self.tree[p] = l - self.dir = 'r' - return r - else : - self.tree[p] = r - self.dir = 'l' - return l - def getWinIdx(self,idx=1): - while 2*idx