二叉搜索树

Posted 幻觉czw

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了二叉搜索树相关的知识,希望对你有一定的参考价值。

# -*- encoding:utf8 -*-
# 二叉搜索树
class TreeNode(object):
	def __init__(self, key):
		self._key = key
		self._left = None
		self._right = None
		self._parent = None

	def getKey(self):
		return self._key

	def setKey(self, key):
		self._key = key

	key = property(getKey, setKey)

	def getLeft(self):
		return self._left

	def setLeft(self, left):
		self._left = left
		if left:
			left.parent = self

	left = property(getLeft, setLeft)

	def getRight(self):
		return self._right

	def setRight(self, right):
		self._right = right 
		if right:
			right.parent = self

	right = property(getRight, setRight)

	def setParent(self, parent):
		self._parent = parent

	def getParent(self):
		return self._parent

	parent = property(getParent, setParent)

class BinaryTree(object):

	def __init__(self, root):
		if not isinstance(root, TreeNode):
			root = TreeNode(root)
		self.root = root

	def setRoot(self, root):
		self.root = root

	def getRoot(self):
		return self.root

	def _find(self, current_node, last_node, new_node):
		if not current_node:
			return last_node, False
		elif current_node.key == new_node:
			return current_node, True
		else:
			if new_node < current_node.key:
				return self._find(current_node.left, current_node, new_node)
			else:
				return self._find(current_node.right, current_node, new_node)

	def findnode(self, new_node):
		current_node, isExist = self._find(self.root, self.root, new_node)
		if isExist:
			return current_node
		else:
			return None

	def insert(self, new_node):
		current_node, isExist = self._find(self.root, self.root, new_node)
		if isExist:
			#节点已经存在,不需要添加
			return
		else:
			if new_node < current_node.key:
				current_node.left = TreeNode(new_node)
			else:
				current_node.right = TreeNode(new_node)

	def _minnode(self, node):
		current_node = node
		if not current_node:
			return None 
		while current_node.left:
			current_node = current_node.left
		return current_node

	def minnode(self):
		return self._minnode(self.root)

	def _maxnode(self, node):
		current_node = node
		if not current_node:
			return None 
		while current_node.right:
			current_node = current_node.right
		return current_node

	def maxnode(self):
		return self._maxnode(self.root)

	def previous(self, node):
		"""
		寻找前驱
		"""
		current_node, isExist = self._find(self.root, self.root, node)
		if not isExist:
			raise Exception("节点%s不存在" % node)
		else:
			if current_node.left:
				return self._maxnode(current_node.left)
			else:
				if current_node.key == self.minnode().key:
					#最小节点没有前驱
					return
				else:
					previous_node = current_node.parent
					while previous_node:
						if current_node == previous_node.right:
							return previous_node
						else:
							current_node = previous_node
							previous_node = previous_node.parent

	def next(self, node):
		"""
		寻找后继
		"""
		current_node, isExist = self._find(self.root, self.root, node)
		if not isExist:
			raise Exception("节点%s不存在" % node)
		else:
			if current_node.right:
				return self._minnode(current_node.right)
			else:
				if current_node.key == self.maxnode().key:
					#最大节点没有后继
					return
				else:
					previous_node = current_node.parent
					while previous_node:
						if current_node == previous_node.left:
							return previous_node
						else:
							current_node = previous_node
							previous_node = previous_node.parent

	def delete(self, node):
		current_node, isExist = self._find(self.root, self.root, node)
		if not isExist:
			# 结点不存在
			return
		else:
			if current_node.left:
				previous_node = self.previous(current_node.key)
				# print 'key:',previous_node.key
				parent_node = previous_node.parent
				# print 'key:',parent_node.key
				if parent_node.key < previous_node.key:
					parent_node.right = previous_node.left
				else:
					parent_node.left = previous_node.left
				current_node.key = previous_node.key
				# print 'key:',current_node.key
			elif current_node.right:
				next_node = self.next(current_node.key)
				parent_node = next_node.parent
				if parent_node.key > next_node.key:
					parent_node.left = next_node.right
				else:
					parent_node.right = next_node.right
				current_node.key = next_node.key
			else:
				parent_node = current_node.parent
				if parent_node.key > current_node.key:
					parent_node.left = None
				else:
					parent_node.right = None

	def show(self, items, node):
		if not node:
			return
		else:
			self.show(items, node.left)
			items.append(str(node.key))
			self.show(items, node.right)

	def __str__(self):
		alist = []
		self.show(alist, self.root)
		return '\\n'.join(alist)

 

以上是关于二叉搜索树的主要内容,如果未能解决你的问题,请参考以下文章

二叉搜索树的前驱和后继详细推导

剑指offer--二叉搜索树与双向链表

二叉搜索树与双向链表

二叉搜索树与双向链表

LeetCode-树二叉搜索树与双向链表

剑指 Offer 36. 二叉搜索树与双向链表(二叉搜索树,中序遍历,Java)