Soluții Laboratorul 8
Soluții
1. Definiții și axiome
\( \mathrm{insert} : \mathbb{E} \times \mathrm{BTree} \to \mathrm{BTree} \)
\( \mathrm{insert}(x, \mathrm{Nil}) \overset{(\text{INS1})}{=} \mathrm{Node}(x, \mathrm{{Nil}}, \mathrm{Nil}) \)
\( \mathrm{insert}(x, \mathrm{Node}(y,l,r)) \overset{(\text{INS2})}{=} \mathrm{Node}(y, \mathrm{insert}(x,l), r), x \le y \ \)
\( \mathrm{insert}(x, \mathrm{Node}(y,l,r)) \overset{(\text{INS3})}{=} \mathrm{Node}(y, l, \mathrm{insert}(x,r)), x > y \)
—
\( \mathrm{min} : \mathrm{BTree} \setminus \{\mathrm{Nil}\} \to \mathbb{E} \)
\( \mathrm{min}(\mathrm{Node}(x, \mathrm{Nil}, r)) \overset{(\text{MIN1})}{=} x \)
\( \mathrm{min}(\mathrm{Node}(y, \mathrm{Node}(x,l,r), r')) \overset{(\text{MIN2})}{=} \mathrm{min}(\mathrm{Node}(x,l,r)) \)
—
\( \mathrm{max} : \mathrm{BTree} \setminus \{\mathrm{Nil}\} \to \mathbb{E} \)
\( \mathrm{max}(\mathrm{Node}(x, l, \mathrm{Nil})) \overset{(\text{MAX1})}{=} x \)
\( \mathrm{max}(\mathrm{Node}(y, l', \mathrm{Node}(x,l,r))) \overset{(\text{MAX2})}{=} \mathrm{max}(\mathrm{Node}(x,l,r)) \\ \)
2. Soluție Python
- bst_path.py
import random class Node: def __init__(self, x): self.x = x self.left = None self.right = None def insert(root, x): if root is None: return Node(x) if x <= root.x: root.left = insert(root.left, x) else: root.right = insert(root.right, x) return root def search_path_length(root, x): steps = 0 current = root while current is not None: steps += 1 if x == current.x: return steps elif x < current.x: current = current.left else: current = current.right return steps def average_search_path(n): """Construiește un BST dintr-o permutare aleatoare și măsoară media.""" perm = list(range(1, n + 1)) random.shuffle(perm) root = None for x in perm: root = insert(root, x) lengths = [search_path_length(root, x) for x in perm] return sum(lengths) / n def experiment(trials=2000, n=100): total = 0 for _ in range(trials): total += average_search_path(n) return total / trials for n in [10, 20, 50, 100, 200, 500, 1000]: avg = experiment(trials=1000, n=n) print(f"n = {n} → lungime medie ≈ {avg:.3f}")
3. a) \(\forall x \in \mathbb{E}, \forall t \in \mathrm{BTree} : \mathrm{isBST}(t) \implies \mathrm{isBST}(\mathrm{insert}(x,t))\)
Vrem să demonstrăm \( \mathrm{isBST}(t) \implies \mathrm{isBST}(\mathrm{insert}(x,t)), \forall t \in \mathrm{BTree}, \forall x \in \mathbb{E} \)
—
Caz de bază: \(t = \mathrm{Nil}\)
\( \mathrm{insert}(x, \mathrm{Nil}) \overset{(\text{INS1})}{=} \mathrm{Node}(x, \mathrm{Nil}, \mathrm{Nil}), \forall x \in \mathbb{E} \)
\( \begin{aligned} \mathrm{isBST}(\mathrm{Nil}) &\overset{(\text{BST})}{=} \mathrm{isBSTBetween}(\mathrm{Nil}, -\infty, +\infty) \\ &\overset{(\text{BST1})}{=} \text{true}, \forall x \in \mathbb{E} \end{aligned} \)
\( \begin{aligned} \mathrm{isBST}(\mathrm{Node}(x, \mathrm{Nil}, \mathrm{Nil})) &\overset{(\text{BST})}{=} \mathrm{isBSTBetween}(\mathrm{Node}(x, \mathrm{Nil}, \mathrm{Nil}), -\infty, +\infty) \\ &\overset{(\text{BST2})}{=} (-\infty \le x \le +\infty) \;\land\; \mathrm{isBSTBetween}(\mathrm{Nil}, -\infty, x) \;\land\; \mathrm{isBSTBetween}(\mathrm{Nil}, x, +\infty) \\ &\overset{(\overline{\mathbb{E}})}{=} \text{true} \;\land\; \mathrm{isBSTBetween}(\mathrm{Nil}, -\infty, x) \;\land\; \mathrm{isBSTBetween}(\mathrm{Nil}, x, +\infty) \\ &\overset{(\text{BST1})}{=} \text{true}, \forall x \in \mathbb{E} \end{aligned} \)
Deci cazul de bază este adevărat \(\forall x \in \mathbb{E}\).
—
Pas de inducție: \(t = \mathrm{Node}(y, l, r)\)
Presupunem \(\mathrm{isBST}(t)\) \( \overset{(\text{BST})}{\Leftrightarrow} \mathrm{isBSTBetween}(t, -\infty, +\infty) \overset{(\text{BST2})}{\Leftrightarrow} (-\infty \le y \le +\infty) \;\land\; \mathrm{isBSTBetween}(l, -\infty, y) \;\land\; \mathrm{isBSTBetween}(r, y, +\infty) \ (\text{II}) \)
\( \begin{aligned} \forall lo, y \in \mathbb{E}: &\quad \mathrm{isBSTBetween}(l, lo, y) \overset{(\text{II1})}{\implies} \mathrm{isBSTBetween}(\mathrm{insert}(x,l), lo, y) \\ \forall y, hi \in \mathbb{E}: &\quad \mathrm{isBSTBetween}(r, y, hi) \overset{(\text{II2})}{\implies} \mathrm{isBSTBetween}(\mathrm{insert}(x,r), y, hi) \end{aligned} \)
—
Caz 1: \(x \le y\)
\( \mathrm{insert}(x, \mathrm{Node}(y,l,r)) \overset{(\text{INS2})}{=} \mathrm{Node}(y, \mathrm{insert}(x,l), r) \)
\( \begin{aligned} \mathrm{isBSTBetween}(\mathrm{Node}(y, \mathrm{insert}(x,l), r), lo, hi) &\overset{(\text{BST2})}{=} (lo \le y \le hi) \;\land\; \mathrm{isBSTBetween}(\mathrm{insert}(x,l), lo, y) \;\land\; \mathrm{isBSTBetween}(r, y, hi) \\ &\overset{(\text{II1})}{=} (lo \le y \le hi) \;\land\; \mathrm{isBSTBetween}(l, lo, y) \;\land\; \mathrm{isBSTBetween}(r, y, hi) \\ &\overset{(\text{II})}{=} \text{true} \end{aligned} \)
—
Caz 2: \(x > y\)
\( \mathrm{insert}(x, \mathrm{Node}(y,l,r)) \overset{(\text{INS3})}{=} \mathrm{Node}(y, l, \mathrm{insert}(x,r)) \)
\( \begin{aligned} \mathrm{isBSTBetween}(\mathrm{Node}(y, l, \mathrm{insert}(x,r)), lo, hi) &\overset{(\text{BST2})}{=} (lo \le y \le hi) \;\land\; \mathrm{isBSTBetween}(l, lo, y) \;\land\; \mathrm{isBSTBetween}(\mathrm{insert}(x,r), y, hi) \\ &\overset{(\text{II2})}{=} (lo \le y \le hi) \;\land\; \mathrm{isBSTBetween}(l, lo, y) \;\land\; \mathrm{isBSTBetween}(r, y, hi) \\ &\overset{(\text{II})}{=} \text{true} \end{aligned} \)
—
Deci \(\mathrm{isBST}(t) \implies \mathrm{isBST}(\mathrm{insert}(x,t)), \forall x \in \mathbb{E}, \forall t \in \mathrm{BTree}\)