#! /usr/local/bin/python# -*- coding:utf-8 -*-class BinaryTree:class Node:def __init__(self,key,id,parent=None,left=None,right=None):self.key=keyself.id=idself.parent=parentself.left=leftself.right=rightdef __init__(self,A=[]):self.count=1 #var for making Node id uniqueself.node=[]# data[0] is dummy node. It shows tree's start# and tree's root is Node.right.self.node.append(self.Node('start',0))for i in A:self.insert(i)def search_node_id(self,k):node_id=self.node[0].right#searching node_idwhile True:if node_id==None:return Noneif k==self.node[node_id].key:return node_idelif k<self.node[node_id].key:node_id=self.node[node_id].leftelse:node_id=self.node[node_id].rightdef search(self,k):node_id=self.search_node_id(k)if node_id==None:return 'no node'else:return self.node[node_id].keydef insert(self,k):# first insertif self.node[0].right==None:self.node.append(self.Node(k,self.count,0))self.node[0].right=self.countself.count+=1else:#searching insert pointnode_id=self.node[0].rightwhile True:if k<=self.node[node_id].key:if self.node[node_id].left==None:self.node.append(self.Node(k,self.count,node_id))self.node[node_id].left=self.countself.count+=1breakelse:node_id=self.node[node_id].leftelse:if self.node[node_id].right==None:self.node.append(self.Node(k,self.count,node_id))self.node[node_id].right=self.countself.count+=1breakelse:node_id=self.node[node_id].rightdef delete(self,k):delete_id=self.search_node_id(k)if delete_id==None:print 'no node'return Noneelse:delete_node=self.node[delete_id]# no childif delete_node.left==None and delete_node.right==None:if delete_node.key<=self.node[delete_node.parent].key:self.node[delete_node.parent].left=Noneelse:self.node[delete_node.parent].right=None#one childelif delete_node.left==None or delete_node.right==None:if delete_node.key<=self.node[delete_node.parent].key:if delete_node.left!=None:self.node[delete_node.parent].left=delete_node.leftelse:self.node[delete_node.parent].left=delete_node.rightelse:if delete_node.left!=None:self.node[delete_node.parent].right=delete_node.leftelse:self.node[delet_node.parent].right=delete_node.right#two childelse:#searching cover nodemin_node=self.node[self.node[delete_node.id].right]while True:if min_node.left==None:breakmin_node=self.node[self.node[min_node.id].left]# changing node pointerif min_node.parent==delete_node.id:min_node.parent=delete_node.parentmin_node.left=delete_node.leftself.node[delete_node.left].parent=min_node.idif min_node.key<=self.node[delete_node.parent].key:self.node[delete_node.parent].left=min_node.idelse:self.node[delete_node.parent].right=min_node.idelse:self.node[min_node.parent].left=min_node.rightif min_node.right!=None:self.node[min_node.right].parent=min_node.parentmin_node.parent=delete_node.parentmin_node.left=delete_node.leftmin_node.right=delete_node.rightself.node[delete_node.left].parent=min_node.idself.node[delete_node.right].parent=min_node.idif min_node.key<=self.node[delete_node.parent].key:self.node[delete_node.parent].left=min_node.idelse:self.node[delete_node.parent].right=min_node.id
deleteのところでポインタの付け替えの条件分岐がめんどくさくてかなり手こずりました。。
正直もう書きたくないです^^; こう考えるとヒープって楽に実装できて,しかもそこそこパフォーマンスも良くていいねってことを実感しました
0 件のコメント:
コメントを投稿