用二叉链表存储方式,编程实现二叉树的的创建、中序输出、求高度、求结点总数、求叶子数、左右子树交换和查找等算法,并以菜单形式显示。
时间: 2023-07-10 07:18:14 浏览: 91
以下是基于 Python 语言的二叉树实现,包括二叉链表存储方式下的创建、中序输出、求高度、求结点总数、求叶子数、左右子树交换和查找等算法,并以菜单形式显示。
```python
class Node:
def __init__(self, value=None, left_child=None, right_child=None):
self.value = value
self.left_child = left_child
self.right_child = right_child
class BinaryTree:
def __init__(self, root=None):
self.root = root
# 创建二叉树
def create_tree(self):
value = input("请输入节点的值(输入#表示此处无节点):")
if value == "#":
return None
node = Node(value)
node.left_child = self.create_tree()
node.right_child = self.create_tree()
return node
# 中序遍历输出
def in_order_traversal(self, node):
if node is None:
return
self.in_order_traversal(node.left_child)
print(node.value, end=" ")
self.in_order_traversal(node.right_child)
# 求高度
def get_height(self, node):
if node is None:
return 0
left_height = self.get_height(node.left_child)
right_height = self.get_height(node.right_child)
return max(left_height, right_height) + 1
# 求结点总数
def get_node_count(self, node):
if node is None:
return 0
left_node_count = self.get_node_count(node.left_child)
right_node_count = self.get_node_count(node.right_child)
return left_node_count + right_node_count + 1
# 求叶子数
def get_leaf_count(self, node):
if node is None:
return 0
if node.left_child is None and node.right_child is None:
return 1
left_leaf_count = self.get_leaf_count(node.left_child)
right_leaf_count = self.get_leaf_count(node.right_child)
return left_leaf_count + right_leaf_count
# 左右子树交换
def swap_left_right(self, node):
if node is None:
return
node.left_child, node.right_child = node.right_child, node.left_child
self.swap_left_right(node.left_child)
self.swap_left_right(node.right_child)
# 查找
def search(self, node, value):
if node is None:
return None
if node.value == value:
return node
left_result = self.search(node.left_child, value)
if left_result is not None:
return left_result
right_result = self.search(node.right_child, value)
if right_result is not None:
return right_result
return None
if __name__ == "__main__":
tree = BinaryTree()
root = tree.create_tree()
tree.root = root
while True:
print("1.输出中序遍历")
print("2.求高度")
print("3.求结点总数")
print("4.求叶子数")
print("5.左右子树交换")
print("6.查找")
print("7.退出")
choice = int(input("请输入操作编号:"))
if choice == 1:
tree.in_order_traversal(tree.root)
print()
elif choice == 2:
height = tree.get_height(tree.root)
print("树的高度为:", height)
elif choice == 3:
node_count = tree.get_node_count(tree.root)
print("树的结点总数为:", node_count)
elif choice == 4:
leaf_count = tree.get_leaf_count(tree.root)
print("树的叶子数为:", leaf_count)
elif choice == 5:
tree.swap_left_right(tree.root)
print("左右子树交换后的中序遍历为:")
tree.in_order_traversal(tree.root)
print()
elif choice == 6:
value = input("请输入要查找的节点的值:")
node = tree.search(tree.root, value)
if node is None:
print("未找到该节点")
else:
print("找到了节点,其左子节点为:", node.left_child.value if node.left_child else None, ",右子节点为:", node.right_child.value if node.right_child else None)
elif choice == 7:
break
else:
print("操作编号输入错误,请重新输入!")
```
运行这段代码,即可通过菜单选择所需的算法进行操作。
阅读全文
相关推荐
















