algoritmauzmanı
Sign in
TREN
6.4Binary Search Tree with an Array
Lesson 6.4.1

What is a binary search tree?

In the binary tree in an array lessons the values had no order in the tree; finding a value meant looking at every node. A (BST) adds a single rule. Thanks to that rule a search rules out part of the tree at every step, and the tree keeps its values in sorted order.

What you will learn
  • The BST rule and that it applies to the whole subtree
  • How to spot a tree that looks right at first but breaks the rule
  • Why an inorder traversal gives sorted output
6 min

1The rule

Every node has two conditions: every value in its is smaller than it, and every value in its right subtree is larger. The same rule repeats at every node; the left and right subtrees are binary search trees too.

50307020406080
The example tree. Every lesson in this part uses this tree, built by inserting 50, 30, 70, 20, 40, 60, 80 in that order.

To the left of 50 are 30, 20 and 40: all smaller than 50. To its right are 70, 60 and 80: all larger. 30 has 20 on its left and 40 on its right; 70 has 60 on its left and 80 on its right. The rule holds at every node.

50307020406080
The left subtree of 50: 30, 20 and 40. The rule asks for all three nodes to be smaller than 50, not just 30.

2Checking the children is not enough

A common mistake is to check the rule only between a node and its two children. In the tree below every node looks right next to its own children, but the tree is not a binary search tree.

50307020606580
60 looks fine as the right child of 30. But it sits in the left subtree of 50 and is larger than 50.

There is nothing wrong between 30 and 60: 60 is on the right of 30 and larger than it. But 60 is in the left subtree of 50. Someone searching for 60 goes right at the root because "60 > 50" and never finds it. A single node that breaks the rule makes the search give wrong answers.

When checking whether a tree is a BST, think of an allowed range for every node. The root's range has no limits. Going left, the node's value becomes the upper limit; going right, it becomes the lower limit. The range for 60 was between 30 and 50; 60 is outside it.

3Inorder gives sorted order

An traversal visits the left subtree, then the node, then the right subtree. In a BST the whole left subtree is smaller than the node and the whole right subtree is larger, so the values come out from smallest to largest. The inorder output of the example tree is 20 30 40 50 60 70 80.

This is an easy way to tell whether a tree is a BST: if the inorder output is not strictly increasing, the tree is not a BST. The broken tree's inorder output is 20 30 60 50 65 70 80; 50 comes after 60.

4What the search gains

To find a value you start at the root. If the value is smaller than the node it cannot be in the right subtree, so you go left; if it is larger, you go right. Every comparison rules out a whole subtree. In a balanced tree that means dropping about half of the remaining nodes at every step, and the search takes . In a plain binary tree the value could be anywhere; search looks at every node, .

Number of nodesMost comparisons in a balanced BSTMost comparisons in a plain binary tree
737
1,023101,023
1,048,575201,048,575

5The same value twice

In these lessons a tree never holds the same value twice. If an existing value is inserted again, nothing is inserted and the function says so. Programs that need to count repeats keep a counter in the node, or always put equal values on the same side, for example the right; then the rule reads "everything on the right is larger or equal".

6A BST in an array

In this subtopic the BST is kept with the index layout of the tree in an array: the node at index i has its left child at 2i+1 and its right child at 2i+2. But a BST does not have to be a complete tree; the rule, not the order, decides where a value goes. So empty cells remain in the array, and an empty cell is marked with EMPTY (-1). Since the values are positive, -1 never gets mixed up with a value.

#define CAPACITY 15#define EMPTY -1 struct BST {    int items[CAPACITY];}; void create(struct BST* t) {    for (int i = 0; i < CAPACITY; i++) {        t->items[i] = EMPTY;    }}
5003017022034046058067891011121314
The example tree in the array. The first seven cells are filled, 7 to 14 are EMPTY. 30 (index 1) and its children 20 (2·1+1 = 3) and 40 (2·1+2 = 4) are marked.

The next lessons code search, insertion and the minimum and maximum on this array. The same operations come again in the same order in the BST with linked nodes subtopic.

7Try it

Which inorder output shows for sure that the tree is not a BST?

Show the answer

The answer is B. The inorder output of a BST is strictly increasing. In the second option 9 comes after 12; that tree cannot be a BST.

If 45 is inserted into the example tree (50, 30, 70, 20, 40, 60, 80), whose child does it become?

Show the answer

The answer is A. 45 < 50: left, to 30. 45 > 30: right, to 40. 45 > 40: right. 40 has no right child, so 45 goes there.

Lessons you mark show up in the curriculum. If you are signed in, they are saved to your account too.