Differences
This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
fp:lab05 [2022/03/31 08:04] pdmatei |
fp:lab05 [2022/03/31 08:53] (current) pdmatei |
||
---|---|---|---|
Line 31: | Line 31: | ||
In a [[https://en.wikipedia.org/wiki/Binary_search_tree| binary search tree (BST)]], the key of the current node, is always: | In a [[https://en.wikipedia.org/wiki/Binary_search_tree| binary search tree (BST)]], the key of the current node, is always: | ||
- | * **smaller** or equal than **all** keys in the **left** sub-tree. | + | * **smaller** or equal than **all** keys in the **right** sub-tree. |
- | * **larger** or equal than **all** keys in the **right** sub-tree. | + | * **larger** or equal than **all** keys in the **left** sub-tree. |
Consider a binary search tree with keys as integers, encoded as follows: | Consider a binary search tree with keys as integers, encoded as follows: | ||
<code scala> | <code scala> | ||
+ | trait ITree {} | ||
case object Empty extends ITree | case object Empty extends ITree | ||
case class INode(key: Int, left: ITree, right: ITree) extends ITree | case class INode(key: Int, left: ITree, right: ITree) extends ITree | ||
Line 62: | Line 63: | ||
**5.2.6.** Implement a method ''depth'' which returns the maximal depth of a BST. Hint: use the method: ''_.max(_)''. | **5.2.6.** Implement a method ''depth'' which returns the maximal depth of a BST. Hint: use the method: ''_.max(_)''. | ||
- | **5.2.8.** Implement a method ''minimum'' which returns the smallest integer from a BST. (If the tree is empty, we return -1). Hint: use the example above, to guide your implementation. | + | **(!) 5.2.8.** Implement a method ''minimum'' which returns the smallest integer from a BST. (If the tree is empty, we return -1). Hint: use the example above, to guide your implementation. |
**5.2.9.** Implement a similar method ''maximum''. | **5.2.9.** Implement a similar method ''maximum''. | ||
- | **5.2.10.** Implement a method ''successor(k)'' which returns **the smallest** integer from the BST, which is **larger** than ''k''. Use the following examples for your implementation: | + | **(!) 5.2.10.** Implement a method ''successor(k)'' which returns **the smallest** integer from the BST, which is **larger** than ''k''. Use the following examples for your implementation: |
<code> | <code> | ||
5 t.successor(2) = 5 | 5 t.successor(2) = 5 | ||
Line 75: | Line 76: | ||
</code> | </code> | ||
- | **5.2.11.** Implement a method ''remove(k)'' which removes element ''k'' from the BST. | + | ** (!!) 5.2.11.** Implement a method ''remove(k)'' which removes element ''k'' from the BST. |