编写一个程序btree.py,实现二叉树的基本运算,并在此基础上设计一个程序exp6-1.py完成以下功能。 (1) 由以下的二叉树创建对应的二叉链存储结构b,该二叉树的括号表示串为“A(B(D,E(H(J,K(L,M(,N))))),C(F,G(,I)))”. (2) 输出二叉树b. (3) 输出”H”结点的左、右孩子结点值。 (4) 输出二叉树的高度。 (5) 释放二叉树
时间: 2024-02-09 16:08:38 浏览: 102
Creat BTree.rar_btree_btree java_creatBtree_java 二叉树_二叉树 程序
很抱歉,我是一个语言模型AI,并不具备编写程序的能力。但是我可以为您提供思路和代码参考。
思路:
首先需要定义二叉树结点的类,包含左右子树和结点值等属性。然后根据输入的字符串创建二叉树,可以用栈来实现。遇到左括号时,将当前结点入栈,并将左括号之前的字符串作为当前结点的值;遇到右括号时,将栈顶结点出栈,并将当前结点作为其右子树;遇到逗号时,将当前结点作为其父结点的左子树。创建完二叉树后,可以实现二叉树的遍历、查找、计算高度等基本运算。
代码参考:
先定义二叉树结点类:
```python
class TreeNode:
def __init__(self, val):
self.val = val
self.left = None
self.right = None
```
创建二叉树的函数:
```python
def create_tree(s):
stack = []
root = None
i = 0
while i < len(s):
if s[i] == '(':
if s[i+1] == ')':
node = None
i += 2
else:
j = i + 1
while s[j] not in ['(', ')', ',']:
j += 1
val = s[i+1:j]
node = TreeNode(val)
i = j
if not root:
root = node
else:
if stack:
parent = stack[-1]
if not parent.left:
parent.left = node
else:
parent.right = node
if s[i] == ')':
stack.pop()
else:
raise ValueError('Invalid input string')
if s[i] == '(':
stack.append(node)
elif s[i] == ',':
if s[i-1] == '(':
stack[-1].left = None
i += 1
elif s[i] == ')':
if stack:
stack.pop()
i += 1
else:
i += 1
return root
```
输出二叉树的函数:
```python
def print_tree(root):
if not root:
return
print(root.val)
print_tree(root.left)
print_tree(root.right)
```
查找结点的函数:
```python
def find_node(root, val):
if not root:
return None
if root.val == val:
return root
left = find_node(root.left, val)
right = find_node(root.right, val)
return left or right
```
计算二叉树高度的函数:
```python
def tree_height(root):
if not root:
return 0
left_height = tree_height(root.left)
right_height = tree_height(root.right)
return 1 + max(left_height, right_height)
```
释放二叉树的函数:
```python
def release_tree(root):
if not root:
return
release_tree(root.left)
release_tree(root.right)
del root
```
完整程序:
```python
class TreeNode:
def __init__(self, val):
self.val = val
self.left = None
self.right = None
def create_tree(s):
stack = []
root = None
i = 0
while i < len(s):
if s[i] == '(':
if s[i+1] == ')':
node = None
i += 2
else:
j = i + 1
while s[j] not in ['(', ')', ',']:
j += 1
val = s[i+1:j]
node = TreeNode(val)
i = j
if not root:
root = node
else:
if stack:
parent = stack[-1]
if not parent.left:
parent.left = node
else:
parent.right = node
if s[i] == ')':
stack.pop()
else:
raise ValueError('Invalid input string')
if s[i] == '(':
stack.append(node)
elif s[i] == ',':
if s[i-1] == '(':
stack[-1].left = None
i += 1
elif s[i] == ')':
if stack:
stack.pop()
i += 1
else:
i += 1
return root
def print_tree(root):
if not root:
return
print(root.val)
print_tree(root.left)
print_tree(root.right)
def find_node(root, val):
if not root:
return None
if root.val == val:
return root
left = find_node(root.left, val)
right = find_node(root.right, val)
return left or right
def tree_height(root):
if not root:
return 0
left_height = tree_height(root.left)
right_height = tree_height(root.right)
return 1 + max(left_height, right_height)
def release_tree(root):
if not root:
return
release_tree(root.left)
release_tree(root.right)
del root
if __name__ == '__main__':
s = 'A(B(D,E(H(J,K(L,M(,N))))),C(F,G(,I)))'
root = create_tree(s)
print_tree(root)
node_h = find_node(root, 'H')
print(node_h.left.val, node_h.right.val)
height = tree_height(root)
print(height)
release_tree(root)
```
阅读全文