2017-11-13 19:09:54 -02:00
|
|
|
# Binary Search Tree
|
|
|
|
|
|
|
|
|
|
Insert and search for numbers in a binary tree.
|
|
|
|
|
|
|
|
|
|
When we need to represent sorted data, an array does not make a good
|
|
|
|
|
data structure.
|
|
|
|
|
|
|
|
|
|
Say we have the array `[1, 3, 4, 5]`, and we add 2 to it so it becomes
|
|
|
|
|
`[1, 3, 4, 5, 2]` now we must sort the entire array again! We can
|
|
|
|
|
improve on this by realizing that we only need to make space for the new
|
|
|
|
|
item `[1, nil, 3, 4, 5]`, and then adding the item in the space we
|
|
|
|
|
added. But this still requires us to shift many elements down by one.
|
|
|
|
|
|
|
|
|
|
Binary Search Trees, however, can operate on sorted data much more
|
|
|
|
|
efficiently.
|
|
|
|
|
|
|
|
|
|
A binary search tree consists of a series of connected nodes. Each node
|
|
|
|
|
contains a piece of data (e.g. the number 3), a variable named `left`,
|
|
|
|
|
and a variable named `right`. The `left` and `right` variables point at
|
|
|
|
|
`nil`, or other nodes. Since these other nodes in turn have other nodes
|
|
|
|
|
beneath them, we say that the left and right variables are pointing at
|
|
|
|
|
subtrees. All data in the left subtree is less than or equal to the
|
|
|
|
|
current node's data, and all data in the right subtree is greater than
|
|
|
|
|
the current node's data.
|
|
|
|
|
|
|
|
|
|
For example, if we had a node containing the data 4, and we added the
|
|
|
|
|
data 2, our tree would look like this:
|
|
|
|
|
|
|
|
|
|
4
|
|
|
|
|
/
|
|
|
|
|
2
|
|
|
|
|
|
|
|
|
|
If we then added 6, it would look like this:
|
|
|
|
|
|
|
|
|
|
4
|
|
|
|
|
/ \
|
|
|
|
|
2 6
|
|
|
|
|
|
|
|
|
|
If we then added 3, it would look like this
|
|
|
|
|
|
|
|
|
|
4
|
|
|
|
|
/ \
|
|
|
|
|
2 6
|
|
|
|
|
\
|
|
|
|
|
3
|
|
|
|
|
|
|
|
|
|
And if we then added 1, 5, and 7, it would look like this
|
|
|
|
|
|
|
|
|
|
4
|
|
|
|
|
/ \
|
|
|
|
|
/ \
|
|
|
|
|
2 6
|
|
|
|
|
/ \ / \
|
|
|
|
|
1 3 5 7
|
|
|
|
|
|
2017-12-12 18:11:43 +00:00
|
|
|
## Exception messages
|
|
|
|
|
|
|
|
|
|
Sometimes it is necessary to raise an exception. When you do this, you should include a meaningful error message to
|
|
|
|
|
indicate what the source of the error is. This makes your code more readable and helps significantly with debugging. Not
|
|
|
|
|
every exercise will require you to raise an exception, but for those that do, the tests will only pass if you include
|
|
|
|
|
a message.
|
|
|
|
|
|
|
|
|
|
To raise a message with an exception, just write it as an argument to the exception type. For example, instead of
|
2018-03-06 00:45:39 +08:00
|
|
|
`raise Exception`, you should write:
|
2017-12-12 18:11:43 +00:00
|
|
|
|
|
|
|
|
```python
|
|
|
|
|
raise Exception("Meaningful message indicating the source of the error")
|
|
|
|
|
```
|
|
|
|
|
|
2018-02-26 12:03:28 -06:00
|
|
|
## Running the tests
|
|
|
|
|
|
|
|
|
|
To run the tests, run the appropriate command below ([why they are different](https://github.com/pytest-dev/pytest/issues/1629#issue-161422224)):
|
|
|
|
|
|
|
|
|
|
- Python 2.7: `py.test binary_search_tree_test.py`
|
2018-05-26 02:01:55 -07:00
|
|
|
- Python 3.4+: `pytest binary_search_tree_test.py`
|
2018-02-26 12:03:28 -06:00
|
|
|
|
|
|
|
|
Alternatively, you can tell Python to run the pytest module (allowing the same command to be used regardless of Python version):
|
|
|
|
|
`python -m pytest binary_search_tree_test.py`
|
|
|
|
|
|
2018-03-06 00:45:39 +08:00
|
|
|
### Common `pytest` options
|
|
|
|
|
|
|
|
|
|
- `-v` : enable verbose output
|
|
|
|
|
- `-x` : stop running tests on first failure
|
|
|
|
|
- `--ff` : run failures from previous test before running other test cases
|
2018-02-26 12:03:28 -06:00
|
|
|
|
|
|
|
|
For other options, see `python -m pytest -h`
|
|
|
|
|
|
2017-11-13 19:09:54 -02:00
|
|
|
## Submitting Exercises
|
|
|
|
|
|
2018-02-04 11:56:00 -07:00
|
|
|
Note that, when trying to submit an exercise, make sure the solution is in the `$EXERCISM_WORKSPACE/python/binary-search-tree` directory.
|
2017-11-13 19:09:54 -02:00
|
|
|
|
2018-02-04 11:56:00 -07:00
|
|
|
You can find your Exercism workspace by running `exercism debug` and looking for the line that starts with `Workspace`.
|
2017-11-13 19:09:54 -02:00
|
|
|
|
|
|
|
|
For more detailed information about running tests, code style and linting,
|
|
|
|
|
please see the [help page](http://exercism.io/languages/python).
|
|
|
|
|
|
|
|
|
|
## Source
|
|
|
|
|
|
2017-12-12 18:11:43 +00:00
|
|
|
Josh Cheek [https://twitter.com/josh_cheek](https://twitter.com/josh_cheek)
|
2017-11-13 19:09:54 -02:00
|
|
|
|
|
|
|
|
## Submitting Incomplete Solutions
|
2018-03-06 00:45:39 +08:00
|
|
|
|
2017-12-12 18:11:43 +00:00
|
|
|
It's possible to submit an incomplete solution so you can see how others have completed the exercise.
|