(1)请编写一个函数,它的名字为:manageSortBinaryTree,该函数将这个二叉树存储成一个一维数组,我们用list(列表)来表示这个这个二叉树,列表中的下标与二叉树中的结点下标一一对应,即,二叉树中标号为2的结点存储在列表中下标为2的位置。在列表中,没有被使用的位置存储特殊字符。 (2)在该函数中嵌套定义并实现一些对这个排序二叉树进行操作的函数,这些函数如下, 删除某个结点;查找某个结点;插入某个结点;中序遍历二叉树。
时间: 2024-02-06 18:09:38 浏览: 95
下面是实现这些函数的代码,其中二叉树的节点结构体为TreeNode,节点中存储了该节点的值,以及左右子节点在数组中的下标。特殊字符使用None表示。
```python
class TreeNode:
def __init__(self, val, left, right):
self.val = val
self.left = left
self.right = right
def manageSortBinaryTree(root):
# 将二叉树存储成一维数组
tree_list = [None] * 1000
stack = [(root, 1)]
while stack:
node, idx = stack.pop()
tree_list[idx] = node.val
if node.left:
stack.append((node.left, idx * 2))
if node.right:
stack.append((node.right, idx * 2 + 1))
return tree_list
def deleteNode(tree_list, val):
# 删除某个节点
idx = tree_list.index(val)
if not idx:
return False
tree_list[idx] = None
return True
def findNode(tree_list, val):
# 查找某个节点
return val in tree_list
def insertNode(tree_list, val):
# 插入某个节点
idx = 1
while tree_list[idx]:
if val < tree_list[idx]:
idx = idx * 2
else:
idx = idx * 2 + 1
tree_list[idx] = val
def inorderTraversal(tree_list, idx, res):
# 中序遍历二叉树
if idx < len(tree_list) and tree_list[idx]:
inorderTraversal(tree_list, idx * 2, res)
res.append(tree_list[idx])
inorderTraversal(tree_list, idx * 2 + 1, res)
return res
```
使用示例:
```python
# 创建一个排序二叉树
root = TreeNode(4, TreeNode(2, TreeNode(1, None, None), TreeNode(3, None, None)), TreeNode(6, TreeNode(5, None, None), TreeNode(7, None, None)))
# 将二叉树存储成一维数组
tree_list = manageSortBinaryTree(root)
print(tree_list) # [None, 4, 6, 2, 3, 5, 7, 1, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None]
# 删除某个节点
deleteNode(tree_list, 3)
print(tree_list) # [None, 4, 6, 2, None, 5, 7, 1, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None]
# 查找某个节点
print(findNode(tree_list, 5)) # True
print(findNode(tree_list, 8)) # False
# 插入某个节点
insertNode(tree_list, 3)
print(tree_list) # [None, 4, 6, 2, 3, 5, 7, 1, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None]
# 中序遍历二叉树
res = inorderTraversal(tree_list, 1, [])
print(res) # [1, 2, 3, 4, 5, 6, 7]
```
阅读全文