我正在尝试在python中实现二叉树。我创建了一个树类和一个节点类,并创建了许多函数,例如findmin,findmax,insert等。但是我无法获得findnode函数来输出该节点。我的代码如下:
def findnode(self, current, value):
#must pass the root of the tree as current
if current is None:
return None
if current.value == value:
return current
elif value < current.value:
self.findnode(current.leftchild, value)
else:
self.findnode(current.rightchild, value)
如果我将return语句更改为打印语句,例如:
print("The node " + str(current) + " has been found")
运行该函数时,我将获得正确的输出。但是,如果我使用return语句运行函数,我将始终得到None
的输出。这会导致问题,因为我需要在下一个函数中使用节点引用来删除节点。
下面的完整代码:
class TreeNode:
def __init__(self, value, left=None, right=None, parent=None):
self.value = value
self.leftchild = left
self.rightchild = right
self.parent = parent
def __str__(self):
return str(self.value)
class BinaryTree:
def __init__(self):
self.root = None
self.size = 0
def __iter__(self):
return self.root.__iter__()
def insert(self, *args):
for i in args:
new_tree_node = TreeNode(i)
current = self.root
if self.root is None:
self.root = new_tree_node
print("node " + str(i) + " inserted as root")
self.size += 1
else:
self.InsertNode(self.root, i)
print("node " + str(i) + " inserted into tree")
self.size += 1
def InsertNode(self, current, value):
new_tree_node = TreeNode(value)
if value < current.value:
if current.leftchild is None:
current.leftchild = new_tree_node
new_tree_node.parent = current
else:
self.InsertNode(current.leftchild, value)
else:
if current.rightchild is None:
current.rightchild = new_tree_node
new_tree_node.parent = current
else:
self.InsertNode(current.rightchild, value)
def BFT(self, current):
#Breadth First Traversal
#must pass the root of the tree as current
if current is None:
return "the tree is empty"
q = []
while current is not None:
print(str(current.value))
if current.leftchild is not None:
q.append(current.leftchild)
if current.rightchild is not None:
q.append(current.rightchild)
if q:
current = q.pop(0)
else:
current = None
def DFT(self, current):
#must pass the root of the tree as current
#also known as preorder
if current is None:
return "the tree is empty"
else:
print(str(current.value))
self.DFT(current.leftchild)
self.DFT(current.rightchild)
def inorder(self, current):
#must pass the root of the tree as current
#different type of DFT
if current is None:
return "the tree is empty"
else:
self.inorder(current.leftchild)
print(str(current.value))
self.inorder(current.rightchild)
def postorder(self, current):
#must pass the root of the tree as current
#different type of DFT
if current is None:
return "the tree is empty"
else:
self.postorder(current.leftchild)
self.postorder(current.rightchild)
print(str(current.value))
def findmin(self):
current = self.root
while current is not None:
if current.leftchild is None:
return current.value
current = current.leftchild
def findmax(self):
current = self.root
while current is not None:
if current.rightchild is None:
return current.value
current = current.rightchild
def contains(self, current, value):
#must pass the root of the tree as current
if current is None:
print("False")
if current.value == value:
print("True")
elif value < current.value:
self.contains(current.leftchild, value)
else:
self.contains(current.rightchild, value)
def findnode(self, current, value):
#must pass the root of the tree as current
#this function currently will not return anything. printing works but returning doesn't
if current is None:
return None
if current.value == value:
return current
elif value < current.value:
self.findnode(current.leftchild, value)
else:
self.findnode(current.rightchild, value)
def findparent(self, current, value):
#must pass the root of the tree as current
#helper function for deleting
#if you don't have a self.parent descriptor on the node already,
#this function can be used
if value == current.value:
return None
if value < current.value:
if current.leftchild is None:
return None
elif current.leftchild.value == value:
return current
else:
return self.findparent(current.leftchild, value)
else:
if current.rightchild is None:
return None
elif current.rightchild.value == value:
return current
else:
return self.findparent(current.rightchild, value)
def BFScontains(self, current, value):
#must pass the root of the tree as current
if current is None:
print("False")
return
q = []
while current is not None:
if current.value == value:
print("True")
return
if current.leftchild is not None:
q.append(current.leftchild)
if current.rightchild is not None:
q.append(current.rightchild)
if q:
current = q.pop(0)
else:
print("False")
return
def remove(self, value):
node_to_remove = self.findnode(self.root, value)
if node_to_remove is None:
print(str(node_to_remove))
return "node not in tree" #value not in BST
parent = self.findparent(self.root, value)
if self.size == 1:
self.root = None #removing the root node of the tree
elif node_to_remove.leftchild is None and node_to_remove.rightchild is None:
if node_to_remove.value < parent.value:
parent.leftchild = None
else:
parent.rightchild = None
elif node_to_remove.leftchild is None and node_to_remove.rightchild is not None:
if node_to_remove.value < parent.value:
parent.leftchild = node_to_remove.rightchild
else:
parent.rightchild = node_to_remove.rightchild
elif node_to_remove.leftchild is not None and node_to_remove.rightchild is None:
if node_to_remove.value < parent.value:
parent.leftchild = node_to_remove.leftchild
else:
parent.rightchild = node_to_remove.leftchild
else:
largest_value = node_to_remove.leftchild
while largest_value.rightchild:
largest_value = largest_value.right
self.findparent(self.root, largest_value.value).rightchild = None
node_to_remove.value = largest_value.value
self.size -= 1
print("node removed")
btree1 = BinaryTree()
btree2 = BinaryTree()
btree1.insert(35, 17, 22, 45, 234, 15, 32, 50, 44, 70, 500)