Sha256: bd750dbbb0175631c7915e7a0fe64451ee3c9d0c7c88dc6870ba15122cdf87d9

Contents?: true

Size: 1.98 KB

Versions: 174

Compression:

Stored size: 1.98 KB

Contents

# 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

Refer to the [Exercism CoffeScript page](http://exercism.io/languages/coffeescript) for getting started with CoffeeScript.

In order to run the test, you can run the test file from the exercise directory:
```bash
jasmine-node --coffee .
```

## Source

Josh Cheek [https://twitter.com/josh_cheek](https://twitter.com/josh_cheek)

## Submitting Incomplete Solutions
It's possible to submit an incomplete solution so you can see how others have completed the exercise.

Version data entries

174 entries across 174 versions & 1 rubygems

Version Path
trackler-2.2.1.159 tracks/coffeescript/exercises/binary-search-tree/README.md
trackler-2.2.1.158 tracks/coffeescript/exercises/binary-search-tree/README.md
trackler-2.2.1.157 tracks/coffeescript/exercises/binary-search-tree/README.md
trackler-2.2.1.156 tracks/coffeescript/exercises/binary-search-tree/README.md
trackler-2.2.1.155 tracks/coffeescript/exercises/binary-search-tree/README.md
trackler-2.2.1.154 tracks/coffeescript/exercises/binary-search-tree/README.md
trackler-2.2.1.153 tracks/coffeescript/exercises/binary-search-tree/README.md
trackler-2.2.1.152 tracks/coffeescript/exercises/binary-search-tree/README.md
trackler-2.2.1.151 tracks/coffeescript/exercises/binary-search-tree/README.md
trackler-2.2.1.150 tracks/coffeescript/exercises/binary-search-tree/README.md
trackler-2.2.1.149 tracks/coffeescript/exercises/binary-search-tree/README.md
trackler-2.2.1.148 tracks/coffeescript/exercises/binary-search-tree/README.md
trackler-2.2.1.147 tracks/coffeescript/exercises/binary-search-tree/README.md
trackler-2.2.1.146 tracks/coffeescript/exercises/binary-search-tree/README.md
trackler-2.2.1.145 tracks/coffeescript/exercises/binary-search-tree/README.md
trackler-2.2.1.144 tracks/coffeescript/exercises/binary-search-tree/README.md
trackler-2.2.1.143 tracks/coffeescript/exercises/binary-search-tree/README.md
trackler-2.2.1.142 tracks/coffeescript/exercises/binary-search-tree/README.md
trackler-2.2.1.141 tracks/coffeescript/exercises/binary-search-tree/README.md
trackler-2.2.1.140 tracks/coffeescript/exercises/binary-search-tree/README.md