1 Star 0 Fork 0

余超/20189220余超 java

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
BinaryTree.py 5.63 KB
一键复制 编辑 原始数据 按行查看 历史
余超 提交于 6年前 . python的代码
class BinaryTree:
def __init__(self):
self.__root = None
self.__size = 0
# Return True if the element is in the tree
def search(self, e):
current = self.__root # Start from the root
while current != None:
if e < current.element:
current = current.left
elif e > current.element:
current = current.right
else: # element matches current.element
return True # Element is found
return False
# Insert element e into the binary search tree
# Return True if the element is inserted successfully
def insert(self, e):
if self.__root == None:
self.__root = self.createNewNode(e) # Create a new root
else:
# Locate the parent node
parent = None
current = self.__root
while current != None:
if e < current.element:
parent = current
current = current.left
elif e > current.element:
parent = current
current = current.right
else:
return False # Duplicate node not inserted
# Create the new node and attach it to the parent node
if e < parent.element:
parent.left = self.createNewNode(e)
else:
parent.right = self.createNewNode(e)
self.__size += 1 # Increase tree size
return True # Element inserted
# Create a new TreeNode for element e
def createNewNode(self, e):
return TreeNode(e)
# Return the size of the tree
def getSize(self):
return self.__size
# Inorder traversal from the root
def inorder(self):
self.inorderHelper(self.__root)
# Inorder traversal from a subtree
def inorderHelper(self, r):
if r != None:
self.inorderHelper(r.left)
print(r.element, end = " ")
self.inorderHelper(r.right)
# Postorder traversal from the root
def postorder(self):
self.postorderHelper(self.__root)
# Postorder traversal from a subtree
def postorderHelper(self, root):
if root != None:
self.postorderHelper(root.left)
self.postorderHelper(root.right)
print(root.element, end = " ")
# Preorder traversal from the root
def preorder(self):
self.preorderHelper(self.__root)
# Preorder traversal from a subtree
def preorderHelper(self, root):
if root != None:
print(root.element, end = " ")
self.preorderHelper(root.left)
self.preorderHelper(root.right)
# Returns a path from the root leading to the specified element
def path(self, e):
list = []
current = self.__root # Start from the root
while current != None:
list.append(current) # Add the node to the list
if e < current.element:
current = current.left
elif e > current.element:
current = current.right
else:
break
return list # Return an array of nodes
# Delete an element from the binary search tree.
# Return True if the element is deleted successfully
# Return False if the element is not in the tree
def delete(self, e):
# Locate the node to be deleted and its parent node
parent = None
current = self.__root
while current != None:
if e < current.element:
parent = current
current = current.left
elif e > current.element:
parent = current
current = current.right
else:
break # Element is in the tree pointed by current
if current == None:
return False # Element is not in the tree
# Case 1: current has no left children
if current.left == None:
# Connect the parent with the right child of the current node
if parent == None:
self.__root = current.right
else:
if e < parent.element:
parent.left = current.right
else:
parent.right = current.right
else:
# Case 2: The current node has a left child
# Locate the rightmost node in the left subtree of
# the current node and also its parent
parentOfRightMost = current
rightMost = current.left
while rightMost.right != None:
parentOfRightMost = rightMost
rightMost = rightMost.right # Keep going to the right
# Replace the element in current by the element in rightMost
current.element = rightMost.element
# Eliminate rightmost node
if parentOfRightMost.right == rightMost:
parentOfRightMost.right = rightMost.left
else:
# Special case: parentOfRightMost == current
parentOfRightMost.left = rightMost.left
self.__size -= 1
return True # Element deleted
# Return true if the tree is empty
def isEmpty(self):
return self.__size == 0
# Remove all elements from the tree
def clear(self):
self.__root == None
self.__size == 0
# Return the root of the tree
def getRoot(self):
return self.__root
class TreeNode:
def __init__(self, e):
self.element = e
self.left = None # Point to the left node, default None
self.right = None # Point to the right node, default None
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Java
1
https://gitee.com/yuchao20189220/over_20189220java.git
git@gitee.com:yuchao20189220/over_20189220java.git
yuchao20189220
over_20189220java
20189220余超 java
master

搜索帮助