1、编写用 List 实现的二叉树类 (可参考群中文件,但要编写成类)基本操作包括: 1) 初始化二叉树 2) 判断二叉树是否为空 3)获取根节点4) 获取左子树 5) 获取右子树 6) 设置根节点 7) 设置二叉树左子树 8) 设置二叉树右子树
时间: 2024-05-14 13:15:32 浏览: 12
以下是用 List 实现的二叉树类的代码:
```python
class BinaryTree:
def __init__(self, root_value=None):
self.tree = []
if root_value is not None:
self.tree.append(root_value)
else:
self.tree.append(None)
def is_empty(self):
return len(self.tree) == 1 and self.tree[0] is None
def get_root(self):
if self.is_empty():
raise Exception("Binary tree is empty")
return self.tree[0]
def get_left_child(self, index):
left_child_index = index * 2 + 1
if left_child_index >= len(self.tree) or self.tree[left_child_index] is None:
return None
return left_child_index
def get_right_child(self, index):
right_child_index = index * 2 + 2
if right_child_index >= len(self.tree) or self.tree[right_child_index] is None:
return None
return right_child_index
def set_root(self, value):
if self.is_empty():
self.tree.append(value)
else:
self.tree[0] = value
def set_left_child(self, index, value):
left_child_index = index * 2 + 1
if left_child_index >= len(self.tree):
self.tree.extend([None] * (left_child_index - len(self.tree) + 1))
self.tree[left_child_index] = value
def set_right_child(self, index, value):
right_child_index = index * 2 + 2
if right_child_index >= len(self.tree):
self.tree.extend([None] * (right_child_index - len(self.tree) + 1))
self.tree[right_child_index] = value
```
使用示例:
```python
# 初始化二叉树
tree = BinaryTree(1)
tree.set_left_child(0, 2)
tree.set_right_child(0, 3)
tree.set_left_child(1, 4)
tree.set_right_child(1, 5)
# 判断二叉树是否为空
print("Is binary tree empty?", tree.is_empty())
# 获取根节点
print("Root node:", tree.get_root())
# 获取左子树和右子树
print("Left child of root node:", tree.get_left_child(0))
print("Right child of root node:", tree.get_right_child(0))
# 设置根节点、左子树和右子树
tree.set_root(6)
tree.set_left_child(0, 7)
tree.set_right_child(2, 8)
# 获取修改后的根节点、左子树和右子树
print("Root node after setting:", tree.get_root())
print("Left child of root node after setting:", tree.get_left_child(0))
print("Right child of root node after setting:", tree.get_right_child(0))
```
相关推荐
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)