Differences
This shows you the differences between two versions of the page.
| Both sides previous revision Previous revision Next revision | Previous revision | ||
|
aa:lab:sol:8 [2025/12/01 03:22] mihnea.gheorghe Draft soluții BST |
aa:lab:sol:8 [2025/12/04 12:08] (current) mihnea.gheorghe fix notation consistency |
||
|---|---|---|---|
| Line 63: | Line 63: | ||
| \) | \) | ||
| - | === 2. a) \(\forall x \in \mathbb{E}, \forall t \in \mathrm{BTree} : \mathrm{isBST}(t) \implies \mathrm{isBST}(\mathrm{insert}(x,t))\) === | + | === 2. Soluție Python === |
| + | |||
| + | <file 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}") | ||
| + | </file> | ||
| + | |||
| + | |||
| + | === 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 | Vrem să demonstrăm | ||
| Line 81: | Line 138: | ||
| \begin{aligned} | \begin{aligned} | ||
| \mathrm{isBST}(\mathrm{Nil}) | \mathrm{isBST}(\mathrm{Nil}) | ||
| - | &\overset{(\text{BST})}{=} \mathrm{isBSTBetween}(\mathrm{Nil}, -\infty, \infty) \\ | + | &\overset{(\text{BST})}{=} \mathrm{isBSTBetween}(\mathrm{Nil}, -\infty, +\infty) \\ |
| &\overset{(\text{BST1})}{=} \text{true}, \forall x \in \mathbb{E} | &\overset{(\text{BST1})}{=} \text{true}, \forall x \in \mathbb{E} | ||
| \end{aligned} | \end{aligned} | ||
| Line 89: | Line 146: | ||
| \begin{aligned} | \begin{aligned} | ||
| \mathrm{isBST}(\mathrm{Node}(x, \mathrm{Nil}, \mathrm{Nil})) | \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{BST})}{=} \mathrm{isBSTBetween}(\mathrm{Node}(x, \mathrm{Nil}, \mathrm{Nil}), -\infty, +\infty) \\ |
| - | &\overset{(\text{BST2})}{=} (-\infty \le x \le \infty) | + | &\overset{(\text{BST2})}{=} (-\infty \le x \le +\infty) |
| \;\land\; | \;\land\; | ||
| \mathrm{isBSTBetween}(\mathrm{Nil}, -\infty, x) | \mathrm{isBSTBetween}(\mathrm{Nil}, -\infty, x) | ||
| \;\land\; | \;\land\; | ||
| - | \mathrm{isBSTBetween}(\mathrm{Nil}, x, \infty) \\ | + | \mathrm{isBSTBetween}(\mathrm{Nil}, x, +\infty) \\ |
| &\overset{(\overline{\mathbb{E}})}{=} \text{true} \;\land\; | &\overset{(\overline{\mathbb{E}})}{=} \text{true} \;\land\; | ||
| \mathrm{isBSTBetween}(\mathrm{Nil}, -\infty, x) | \mathrm{isBSTBetween}(\mathrm{Nil}, -\infty, x) | ||
| \;\land\; | \;\land\; | ||
| - | \mathrm{isBSTBetween}(\mathrm{Nil}, x, \infty) \\ | + | \mathrm{isBSTBetween}(\mathrm{Nil}, x, +\infty) \\ |
| &\overset{(\text{BST1})}{=} \text{true}, \forall x \in \mathbb{E} | &\overset{(\text{BST1})}{=} \text{true}, \forall x \in \mathbb{E} | ||
| \end{aligned} | \end{aligned} | ||
| Line 112: | Line 169: | ||
| \( | \( | ||
| \overset{(\text{BST})}{\Leftrightarrow} | \overset{(\text{BST})}{\Leftrightarrow} | ||
| - | \mathrm{isBSTBetween}(t, -\infty, \infty) | + | \mathrm{isBSTBetween}(t, -\infty, +\infty) |
| \overset{(\text{BST2})}{\Leftrightarrow} | \overset{(\text{BST2})}{\Leftrightarrow} | ||
| - | (lo \le y \le hi) \;\land\; | + | (-\infty \le y \le +\infty) \;\land\; |
| - | \mathrm{isBSTBetween}(l, lo, y) \;\land\; | + | \mathrm{isBSTBetween}(l, -\infty, y) \;\land\; |
| - | \mathrm{isBSTBetween}(r, y, hi) \ | + | \mathrm{isBSTBetween}(r, y, +\infty) \ |
| (\text{II}) | (\text{II}) | ||
| \) | \) | ||