根据二叉排序树的抽象数据类型的定义,使用二叉链表实现一个二叉排序树。 二叉排序树的基本功能: 1、二叉排序树的建立 2、二叉排序树的查找 3、二叉排序树的插入 4、二叉排序树的删除 5、二叉排序树的销毁 6、其他:自定义操作 编写测试 main()函数测试二叉排序树的正确性。
时间: 2023-08-21 15:38:10 浏览: 101
好的,以下是使用二叉链表实现二叉排序树的代码,包含基本功能和测试 main() 函数。
```python
class BSTNode:
def __init__(self, value):
self.value = value
self.left = None
self.right = None
class BinarySearchTree:
def __init__(self):
self.root = None
def insert(self, value):
if self.root is None:
self.root = BSTNode(value)
else:
self._insert(value, self.root)
def _insert(self, value, current_node):
if value < current_node.value:
if current_node.left is None:
current_node.left = BSTNode(value)
else:
self._insert(value, current_node.left)
elif value > current_node.value:
if current_node.right is None:
current_node.right = BSTNode(value)
else:
self._insert(value, current_node.right)
else:
print("Value already in tree!")
def find(self, value):
if self.root is None:
return False
return self._find(value, self.root)
def _find(self, value, current_node):
if value == current_node.value:
return True
elif value < current_node.value and current_node.left is not None:
return self._find(value, current_node.left)
elif value > current_node.value and current_node.right is not None:
return self._find(value, current_node.right)
return False
def delete(self, value):
if self.root is None:
return False
if self.root.value == value:
if self.root.left is None and self.root.right is None:
self.root = None
elif self.root.left is None:
self.root = self.root.right
elif self.root.right is None:
self.root = self.root.left
else:
self._delete_node_with_two_children(self.root)
else:
self._delete(value, self.root)
def _delete(self, value, current_node):
if value < current_node.value and current_node.left is not None:
if current_node.left.value == value:
if current_node.left.left is None and current_node.left.right is None:
current_node.left = None
elif current_node.left.left is None:
current_node.left = current_node.left.right
elif current_node.left.right is None:
current_node.left = current_node.left.left
else:
self._delete_node_with_two_children(current_node.left)
else:
self._delete(value, current_node.left)
elif value > current_node.value and current_node.right is not None:
if current_node.right.value == value:
if current_node.right.left is None and current_node.right.right is None:
current_node.right = None
elif current_node.right.left is None:
current_node.right = current_node.right.right
elif current_node.right.right is None:
current_node.right = current_node.right.left
else:
self._delete_node_with_two_children(current_node.right)
else:
self._delete(value, current_node.right)
def _delete_node_with_two_children(self, node):
parent = node
successor = node.right
while successor.left is not None:
parent = successor
successor = successor.left
node.value = successor.value
if parent == node:
parent.right = successor.right
else:
parent.left = successor.right
def destroy(self):
self.root = None
def _traverse(self, node):
if node is not None:
self._traverse(node.left)
print(node.value)
self._traverse(node.right)
def traverse(self):
self._traverse(self.root)
# 测试函数
def test_bst():
bst = BinarySearchTree()
# 测试插入
bst.insert(5)
bst.insert(3)
bst.insert(7)
bst.insert(1)
bst.insert(9)
assert bst.find(5) == True
assert bst.find(3) == True
assert bst.find(7) == True
assert bst.find(1) == True
assert bst.find(9) == True
# 测试删除叶子节点
bst.delete(1)
assert bst.find(1) == False
# 测试删除只有一个子节点的节点
bst.delete(3)
assert bst.find(3) == False
assert bst.find(5) == True
assert bst.find(7) == True
assert bst.find(9) == True
# 测试删除有两个子节点的节点
bst.delete(7)
assert bst.find(7) == False
assert bst.find(5) == True
assert bst.find(9) == True
# 测试遍历
bst.traverse()
test_bst()
```
以上代码实现了二叉排序树的基本功能,并包含了测试函数 `test_bst()`。在测试函数中,我们进行了插入、查找、删除和遍历操作,确保代码正确性。
阅读全文