Binary Search Trees

What is a Binary Search Tree?

A binary search tree is a data structure in which each data point, or node, has:

Inserting into a Binary Search Tree

Inserting values into a Binary Search Tree follows a simple algorithm.
We run through each node starting at the root of the tree, and if the value being inserted is less than or equal to the current node's value, we move to the left child of the current node, otherwise we move to the right child of the current node, until we hit a null node, and that will be where we insert.

For example:

To insert the value 20 into this tree, we must follow the steps below:

  1. Compare to root node 50, since 20 < 50, we move to the left child, 30.
  2. Compare to node 30, since 20 < 30, we move to left child, 10.
  3. Compare to node 10, since 20 > 10, we move to right child.
  4. Since right child is null, set 20 to be the right child of 10.

The final tree should look like this:

As you can see, the value 20 has been inserted as the right child of 10.

That's all I have for you here, if you'd like to learn more about binary search trees take a look at the link below.

https://pages.cs.wisc.edu/~cs400/readings/Binary-Search-Trees/

Start location: %s

End location: %s

  1. wuwudww
  2. wdwiadwo