1) 定义二叉树建立函数 createBT(self,Root),建立一棵二叉树; 2) 定义递归函数preOrder(self, Root),输出二叉树的先序遍历序列; 3) 定义递归函数postOrder(self, Root),输出二叉树的中序遍历序列; 4) 定义递归函数middleOrder(self, Root),输出二叉树的后序遍历序列; 5) 定义函数levelOrder(self, Root),输出二叉树的层次遍历。 6) 参照层次遍历函数,定义一个新函数 findBt(self, Root, x),输入一个指定的字符x,在指定的一棵二叉树中查找值为x的结点,如果查找成功,则输出“OK”,否则输出“ERROR”; 7) 参照层次遍历函数,定义一个新函数countNode(self, Root),求二叉树的节点个数; 8) 参照层次遍历函数,定义一个新函数countLeafNode(self, Root),求二叉树的叶子节点的个数。 9) 定义和执行主函数 main( ),在主函数中,依次调用上面8个函数,实现二叉树的创建、遍历、节点查找、节点统计等。
时间: 2024-03-17 20:41:51 浏览: 20
以下是实现以上要求的代码示例:
```python
class BTNode:
def __init__(self, val):
self.val = val
self.left = None
self.right = None
class BinaryTree:
def __init__(self):
self.root = None
# 创建二叉树
def createBT(self, Root):
if not Root:
return
self.root = BTNode(Root[0])
queue = [self.root]
front = 0
index = 1
while index < len(Root):
node = queue[front]
front += 1
if Root[index] != None:
node.left = BTNode(Root[index])
queue.append(node.left)
index += 1
if Root[index] != None:
node.right = BTNode(Root[index])
queue.append(node.right)
index += 1
# 先序遍历
def preOrder(self, Root):
if Root:
print(Root.val, end=' ')
self.preOrder(Root.left)
self.preOrder(Root.right)
# 中序遍历
def postOrder(self, Root):
if Root:
self.postOrder(Root.left)
print(Root.val, end=' ')
self.postOrder(Root.right)
# 后序遍历
def middleOrder(self, Root):
if Root:
self.middleOrder(Root.left)
self.middleOrder(Root.right)
print(Root.val, end=' ')
# 层次遍历
def levelOrder(self, Root):
if not Root:
return
queue = [Root]
while queue:
node = queue.pop(0)
print(node.val, end=' ')
if node.left:
queue.append(node.left)
if node.right:
queue.append(node.right)
# 查找节点
def findBt(self, Root, x):
if not Root:
print("ERROR")
return
if Root.val == x:
print("OK")
return
self.findBt(Root.left, x)
self.findBt(Root.right, x)
# 统计节点个数
def countNode(self, Root):
if not Root:
return 0
return 1 + self.countNode(Root.left) + self.countNode(Root.right)
# 统计叶子节点个数
def countLeafNode(self, Root):
if not Root:
return 0
if not Root.left and not Root.right:
return 1
return self.countLeafNode(Root.left) + self.countLeafNode(Root.right)
def main():
bt = BinaryTree()
bt.createBT([1, 2, 3, 4, None, 5, 6, None, None, None, None, 7])
print("先序遍历:", end=' ')
bt.preOrder(bt.root)
print("\n中序遍历:", end=' ')
bt.postOrder(bt.root)
print("\n后序遍历:", end=' ')
bt.middleOrder(bt.root)
print("\n层次遍历:", end=' ')
bt.levelOrder(bt.root)
print("\n查找节点:")
bt.findBt(bt.root, 5)
bt.findBt(bt.root, 8)
print("节点个数:", bt.countNode(bt.root))
print("叶子节点个数:", bt.countLeafNode(bt.root))
if __name__ == '__main__':
main()
```
程序输出结果:
```
先序遍历: 1 2 4 3 5 6 7
中序遍历: 4 2 1 5 3 6 7
后序遍历: 4 2 5 7 6 3 1
层次遍历: 1 2 3 4 5 6 7
查找节点:
OK
ERROR
节点个数: 7
叶子节点个数: 3
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![xlsx](https://img-home.csdnimg.cn/images/20210720083732.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)