Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1782329
  • 博文数量: 297
  • 博客积分: 285
  • 博客等级: 二等列兵
  • 技术积分: 3006
  • 用 户 组: 普通用户
  • 注册时间: 2010-03-06 22:04
个人简介

Linuxer, ex IBMer. GNU https://hmchzb19.github.io/

文章分类

全部博文(297)

文章存档

2020年(11)

2019年(15)

2018年(43)

2017年(79)

2016年(79)

2015年(58)

2014年(1)

2013年(8)

2012年(3)

分类: Python/Ruby

2015-09-14 21:57:54


点击(此处)折叠或打开

  1. class BinarySearchTree:
  2.     def __init__(self):
  3.         self.root=None
  4.         self.size=0

  5.     def length(self):
  6.         return self.size

  7.     def __len__(self):
  8.         return self.size

  9.     def __iter__(self):
  10.         return self.root.__iter__()


  11.     def put(self,key,val):
  12.         if self.root:
  13.             self._put(key,val,self.root)
  14.         else:
  15.             self.root=TreeNode(key,val)
  16.         self.size=self.size+1

  17.     def _put(self,key,val,currentNode):
  18.         if key < currentNode.key:
  19.             if currentNode.hasLeftChild():
  20.                 self._put(key,val,currentNode.leftChild)
  21.             else:
  22.                 currentNode.leftChild=TreeNode(key,val,parent=currentNode)
  23.         else:
  24.             if currentNode.hasRightChild():
  25.                 self._put(key,val,current.Node.rigthChild)
  26.             else:
  27.                 currentNode.rightChild=TreeNode(key,val,parent=currentNode)


  28.     def __setitem__(self,k,v):
  29.         slf.put(k,v)

  30.     def get(self,key):
  31.         if self.root:
  32.             res=self._get(key,self.root)
  33.             if res:
  34.                 return res.payload
  35.             else:
  36.                 return None
  37.         else:
  38.             return None

  39.     def _get(self,key,currentNonde):
  40.         if not currentNode:
  41.             return None
  42.         elif currentNode.key==key:
  43.             return currentNode
  44.         elif key < currentNode.key:
  45.             return self._get(key,currentNode.leftChild)
  46.         else:
  47.             return self._get(key,currentNode.rightChild)

  48.     def __getitem__(self,key):
  49.         return self.get(key)

  50.     def __contains__(self,key):
  51.         if self._get(key,self.root):
  52.             return True
  53.         else:
  54.             return False

  55.     def delete(self,key):
  56.         if self.size > 1:
  57.             nodeToRemove=self._get(key,self.root)
  58.             if nodeToRemove:
  59.                 self.remove(nodeToRemove)
  60.                 self.size=self.size-1
  61.             else:
  62.                 raise KeyError('Error,key not in tree')
  63.         elif self.size==1 and self.root.key==key:
  64.             self.root=None
  65.             self.size=self.size-1
  66.         else:
  67.             raise KeyError('Error,key not in tree')

  68.     def __delitem__(self,key):
  69.         self.delete(key)

  70.     def findSuccessor(self):
  71.         succ=None
  72.         if self.hasRightChld():
  73.             succ=self.rightChild.findMind()
  74.         else:
  75.             if self.parent:
  76.                 if self.isLeftChild():
  77.                     succ.self.parent
  78.                 else:
  79.                     self.parent.rightChild=None
  80.                     succ=self.parent.findSuccessor()
  81.                     self.parent.rightChild=self
  82.         return succ

  83.     def findMin(self):
  84.         current=self
  85.         while current.hasLeftChild():
  86.             current=current.leftChild
  87.         return current

  88.     def spliceOut(self):
  89.         if self.isLeaf():
  90.             if self.isLeftChild():
  91.                 self.parent.leftChild=None
  92.             else:
  93.                 self.parent.rightChild=None

  94.         elif self.hasAnyChildren():
  95.             if self.hasLeftChild():
  96.                 if self.isLeftChild():
  97.                     self.parent.leftChild=self.leftChild
  98.                 else:
  99.                     self.parent.rightChild=self.leftChild
  100.                 self.leftChild.parent=self.parent
  101.             else:
  102.                 if self.isLeftChild():
  103.                     self.parent.leftChild=self.rightChild
  104.                 else:
  105.                     self.parent.rigthChild=self.rightChild
  106.                 self.rightChild.parent=self.parent

  107.     def __iter__(self):
  108.         if self:
  109.             if self.hasLeftChild():
  110.                 for elem in self.leftChild:
  111.                     yield elem
  112.             yield self.key
  113.             if self.hasRightChild():
  114.                 for elem in self.rightChild
  115.                     yield elem


  116. class TreeNode:
  117.     def __init__(self,key,val,left=None,right=None,parent=None):
  118.         self.key=key
  119.         self.payload=val
  120.         self.leftChild=left
  121.         self.rightChild=right
  122.         self.parent=parent

  123.     def hasLeftChild(self):
  124.         return self.leftChild

  125.     def hasRightChild(self):
  126.         return self.rightChild

  127.     def isLeftChild(self):
  128.         return self.parent and self.parent.leftChild==self

  129.     def isRightChild(self):
  130.         return self.parent and self.parent.rightChild==self

  131.     def isRoot(self):
  132.         return not self.parent

  133.     def isLeaft(self):
  134.         return not (self.rightChild or self.leftChild)

  135.     def hasAnyChildren(self):
  136.         return self.rightChild or self.leftChild

  137.     def hasBothChildren(self):
  138.         return self.rightChild and self.leftChild

  139.     def replaceNodeData(self,key,value,lc,rc):
  140.         self.key=key
  141.         self.payload=value
  142.         self.leftChild=lc
  143.         self.rightChild=rc
  144.         if self.hasLeftChild():
  145.             self.leftChild.parent=self
  146.         if self.hasRightChild():
  147.             self.rightChild.parent=self

阅读(1106) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~