python结束进程树_删除二进制搜索树python中的节点

The code below is my implement for my binary search tree, and I want to implement delete method to remove the node. Below is my implementation, but when I perform

bst = BSTRee()

bst.insert(5)

bst.insert(11)

bst.insert(3)

bst.insert(4)

bst.insert(12)

bst.insert(2)

bst.delete(3)

when I call delete method, it did nothing. Can someone help me to fix it. The link below is my code on github. Thank you so much for your help.

https://github.com/hly189/sort/blob/master/tree/BST.py

class BSTreeNode

def ____init__(self, value):

self.value = value

self.left = None

self.right = None

def insert(self,key):

if self.value == key:

print ("the node already exists")

return False

elif self.value > key:

if self.left is not None:

return self.left.insert(key)

else:

self.left = BSTreeNode(key)

return True

else:

if self.right is not None:

return self.right.insert(key)

else:

self.right = BSTreeNode(key)

return False

def delete(self, node, k):

if node == None:

return None

elif node.value == k:

if node.left is None and node.right is None:

return None

elif node.left is None:

return node.right

elif node.right is None:

return node.left

else:

node.value = get_min(node.right)

node.right.delete(node.right,node.value)

elif k < node.value:

node.left.delete(node.left,k)

else:

node.right.delete(node.right,k)

return node

class BSTree:

def __init__(self):

self.root = None

def delete(self,key):

self.root.delete(self.root,key)

def insert(self,data):

if self.root:

self.root.insert(data)

else:

self.root = BSTreeNode(data)

return True

def find_min(self,node):

current_node = node

while current_node.left:

current_node = current_node.left

return current_node

def get_min(node):

current_node = node

while current_node.left:

current_node = current_node.left

return str(current_node.value)

def print_helper(root, indent):

if root is not None:

print_helper(root.right, indent + " ")

print (indent + str(root.value))

print_helper(root.left, indent + " ")

def print_tree(root):

print_helper(root, "")

解决方案def delete(self, key):

""" delete the node with the given key and return the

root node of the tree """

if self.key == key:

# found the node we need to delete

if self.right and self.left:

# get the successor node and its parent

[psucc, succ] = self.right._findMin(self)

# splice out the successor

# (we need the parent to do this)

if psucc.left == succ:

psucc.left = succ.right

else:

psucc.right = succ.right

# reset the left and right children of the successor

succ.left = self.left

succ.right = self.right

return succ

else:

# "easier" case

if self.left:

return self.left # promote the left subtree

else:

return self.right # promote the right subtree

else:

if self.key > key: # key should be in the left subtree

if self.left:

self.left = self.left.delete(key)

# else the key is not in the tree

else: # key should be in the right subtree

if self.right:

self.right = self.right.delete(key)

return self

def _findMin(self, parent):

""" return the minimum node in the current tree and its parent """

# we use an ugly trick: the parent node is passed in as an argument

# so that eventually when the leftmost child is reached, the

# call can return both the parent to the successor and the successor

if self.left:

return self.left._findMin(self)

else:

return [parent, self]

This might help. For complete code and better understanding go to

For code

Binary search Tree in Python

For explanation

Notes on BST in Python

As per my knowledge its working fine.

以下是利用 Python 实现将二叉搜索转换为循环双链表的代码: ```python class TreeNode: def __init__(self, val): self.val = val self.left = None self.right = None class Solution: def __init__(self): self.head = None self.tail = None # 二叉搜索序遍历 def inorderTraversal(self, root): if root: self.inorderTraversal(root.left) self.convertNode(root) self.inorderTraversal(root.right) # 将二叉节点转换为双链表节点 def convertNode(self, node): if not self.head: self.head = node self.tail = node else: self.tail.right = node node.left = self.tail self.tail = node # 输出双链表的所有元素 def printList(self): node = self.head while node: print(node.val, end=' ') node = node.right print() # 以正和反顺序输出双链表的所有元素 def printListReverse(self): node = self.tail while node: print(node.val, end=' ') node = node.left print() ``` 其,我们定义了一个 `TreeNode` 类来表示二叉节点,还定义了一个 `Solution` 类来实现转换过程。在 `Solution` 类,我们使用了一个 `head` 变量来表示双链表的头节点,使用了一个 `tail` 变量来表示双链表的尾节点。在 `inorderTraversal` 方法,我们采用序遍历的方式遍历二叉搜索,并将遍历到的节点转换为双链表节点。在 `convertNode` 方法,我们根据 `head` 和 `tail` 变量的值来更新双链表。在 `printList` 方法,我们输出双链表的所有元素。在 `printListReverse` 方法,我们以正和反顺序输出双链表的所有元素。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值