Member-only story

Create A Binary Search Tree

randerson112358
4 min readJan 18, 2019

--

How to create a binary search tree from an array

I’m going to discuss how to create a binary search tree from an array. This will be a basic integer array that contains 6 values that are unsorted.

Let’s begin by first establishing some rules for Binary Search Trees (BST):

1. A parent node has, at most, 2 child nodes.

2. The left child node is always less than the parent node.

3. The right child node is always greater than or equal to the parent node.

Unsorted Integer Array

The first value in the array is 7, so the first step in constructing the tree will be to make 7 the root node, as shown here:

With the root node set, all of the remaining values will be children of this node. Referencing our rules from the beginning of this post we know the child nodes will be designated as the right or left child nodes depending on their value. Therefore the first step we’ll take for adding the 2 to the tree will be to compare it to the root node:

If the 2 is less than the 7, it will become the left child node. If the 2 is greater than or equal to 7 it will move to the right. Since we…

--

--

No responses yet