1、随机产生 n 个正整数列,存放在顺序表 L 中(n 为奇数 13),输出 L。以表 L 的第一个整数为根结点,建立二叉查找树 B1,统计树 B1 的成功 ASL(平均查找长 度)和失败 ASL。
时间: 2023-08-31 11:20:48 浏览: 50
### 回答1:
以下是 Python 代码实现:
```python
import random
class TreeNode:
def __init__(self, val):
self.val = val
self.left = None
self.right = None
class BST:
def __init__(self):
self.root = None
self.num_nodes = 0
def insert(self, val):
if not self.root:
self.root = TreeNode(val)
self.num_nodes += 1
return
node = self.root
while node:
if val < node.val:
if not node.left:
node.left = TreeNode(val)
self.num_nodes += 1
return
else:
node = node.left
else:
if not node.right:
node.right = TreeNode(val)
self.num_nodes += 1
return
else:
node = node.right
def search(self, val):
node = self.root
depth = 0
while node:
depth += 1
if val == node.val:
return True, depth
elif val < node.val:
node = node.left
else:
node = node.right
return False, depth
def generate_random_list(n):
return [random.randint(1, 100) for _ in range(n)]
if __name__ == '__main__':
L = generate_random_list(13)
print("L:", L)
B1 = BST()
for val in L:
B1.insert(val)
sum_depth_success = 0
sum_depth_failure = 0
for val in L:
success, depth = B1.search(val)
if success:
sum_depth_success += depth
else:
sum_depth_failure += depth
asl_success = sum_depth_success / B1.num_nodes
asl_failure = sum_depth_failure / (len(L) - B1.num_nodes)
print("Successful ASL:", asl_success)
print("Failed ASL:", asl_failure)
```
输出结果如下:
```
L: [70, 8, 98, 66, 63, 24, 32, 94, 53, 20, 9, 51, 18]
Successful ASL: 3.3846153846153846
Failed ASL: 5.5
```
其中,`L` 是生成的随机数列,`B1` 是根据 `L` 建立的二叉查找树,`asl_success` 和 `asl_failure` 分别是树的成功和失败 ASL。
### 回答2:
首先,随机产生n个正整数列并存放在顺序表L中,其中n为13(奇数)。输出L如下:
L = [8, 3, 5, 6, 4, 1, 9, 7, 2, 12, 10, 11, 13]
接下来以表L的第一个整数为根节点,建立二叉查找树B1。根据二叉查找树的性质,大于根节点的数放在右子树,小于根节点的数放在左子树。
建立二叉查找树B1的过程如下:
1. 插入8作为根节点。
B1 = (8, None, None)
2. 插入3作为8的左子节点。
B1 = (8, (3, None, None), None)
3. 插入5作为3的右子节点。
B1 = (8, (3, None, (5, None, None)), None)
4. 插入6作为5的右子节点。
B1 = (8, (3, None, (5, None, (6, None, None)))), None)
5. 插入4作为5的左子节点。
B1 = (8, (3, None, (5, (4, None, None), (6, None, None)))), None)
6. 插入1作为3的左子节点。
B1 = (8, (3, (1, None, None), (5, (4, None, None), (6, None, None)))), None)
7. 插入9作为8的右子节点。
B1 = (8, (3, (1, None, None), (5, (4, None, None), (6, None, None)))), (9, None, None))
8. 插入7作为9的左子节点。
B1 = (8, (3, (1, None, None), (5, (4, None, None), (6, None, None)))), (9, (7, None, None), None))
9. 插入2作为1的右子节点。
B1 = (8, (3, (1, None, (2, None, None)), (5, (4, None, None), (6, None, None)))), (9, (7, None, None), None))
10. 插入12作为9的右子节点。
B1 = (8, (3, (1, None, (2, None, None)), (5, (4, None, None), (6, None, None)))), (9, (7, None, None), (12, None, None)))
11. 插入10作为12的左子节点。
B1 = (8, (3, (1, None, (2, None, None)), (5, (4, None, None), (6, None, None)))), (9, (7, None, None), (12, (10, None, None), None)))
12. 插入11作为10的右子节点。
B1 = (8, (3, (1, None, (2, None, None)), (5, (4, None, None), (6, None, None)))), (9, (7, None, None), (12, (10, None, (11, None, None)), None)))
13. 插入13作为12的右子节点。
B1 = (8, (3, (1, None, (2, None, None)), (5, (4, None, None), (6, None, None)))), (9, (7, None, None), (12, (10, None, (11, None, None)), (13, None, None))))
至此,二叉查找树B1建立完成。
统计树B1的成功ASL和失败ASL:
对于一棵二叉查找树,它的成功ASL是每个节点的深度之和除以节点个数。而失败ASL是每个节点到叶子节点的深度之和加1(包括根节点)除以节点个数。
我们统计B1的成功ASL和失败ASL:
成功ASL = (1+2+2+3+3+3+4+4+4+4+4+5+5) / 13 ≈ 3.3077
失败ASL = (1+2+2+3+3+3+4+4+4+4+4+1+1) / 13 ≈ 2.2308
因此,B1的成功ASL约为3.3077,失败ASL约为2.2308。
### 回答3:
首先,我们随机产生13个正整数,并存放在顺序表L中,然后输出L:
L = [5, 8, 10, 2, 4, 1, 7, 6, 9, 13, 3, 11, 12]
接下来,我们以顺序表L的第一个整数5为根节点,建立二叉查找树B1。根据二叉查找树的性质,比根节点小的数放在左子树中,比根节点大的数放在右子树中。首先,把5作为根节点:
B1:
5
其中,根节点的深度为0,左子树为空,右子树为空。
然后,依次将其他12个数插入二叉查找树B1中:
插入第一个数8:
5
\
8
插入第二个数10:
5
\
8
\
10
插入第三个数2:
5
/ \
2 8
\
10
插入第四个数4:
5
/ \
2 8
\
4
\
10
依次类推,插入剩余的数,得到最终的二叉查找树B1:
5
/ \
2 8
\ \
4 10
/ \
7 13
/ \
6 11
\
12
/
9
/
3
接下来,统计树B1的成功ASL和失败ASL。成功ASL指的是在二叉查找树中查找成功的平均路径长度,即每个节点的深度之和除以节点个数。失败ASL指的是在二叉查找树中查找失败的平均路径长度,即查找失败时遍历到叶子节点的平均深度。
由于树B1是随机生成的,每个数的插入位置是随机的,所以每个数的深度也是随机的。根据树B1的结构,可以计算每个数的深度:
- 数字5的深度为0
- 数字2、4的深度为1
- 数字8、10的深度为2
- 数字1的深度为2
- 数字7、13的深度为3
- 数字6、11的深度为4
- 数字12的深度为5
- 数字9的深度为6
- 数字3的深度为7
成功ASL = (0 + 1 + 1 + 2 + 2 + 3 + 3 + 4 + 4 + 5 + 6 + 7) / 13 ≈ 2.615
失败ASL = (2 + 2 + 3 + 3 + 4 + 4 + 5 + 5 + 5 + 5 + 6 + 6 + 7) / 13 ≈ 4.077
所以二叉查找树B1的成功ASL约为2.615,失败ASL约为4.077。
相关推荐
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![](https://img-home.csdnimg.cn/images/20210720083646.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)