Edit this page Backlinks This page is read only. You can view the source, but not change it. Ask your administrator if you think this is wrong. ====== 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. 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} (lo \le y \le hi) \;\land\; \mathrm{isBSTBetween}(l, lo, y) \;\land\; \mathrm{isBSTBetween}(r, y, hi) \ (\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}\)