### The Binary Search Tree

• Binary Search Tree: organizing the nodes in a binary tree

• Definition:

• Binary Search Tree (BST) = a binary tree where:

 The values in the nodes in the left subtree of the node x in the tree has a smaller value than x The values in the nodes in the right subtree of the node x in the tree has a greater value than x

• Example of a binary search tree:

Notice the above property holds for every node in the binary tree:

• Node 3:

• Node 1:

• Node 6:

• Node 9:

• A counter-example

• This is not a binary search tree:

• Why is it called a Binary Search tree

• Fact:

 You can search for a specific value in a binary search tree very easily !!!

Example: search for the node with the value 7 in the following binary search tree:

How to find the node with the value 7:

• Start at the root node:

Since 7 > 3, search in the right subtree

• Continue:

Since 7 > 6, search in the right subtree

• Continue:

Since 7 < 9, search in the left subtree

 Node with value 7 has been found !!!

• What happens if you search for a non-existing value

• Fact:

 When you search for a non-existing value, you will end up in its parent node with a null pointer in the direction of the search value

Example: search for the node with the value 10 in the following binary search tree:

How the search for the value 10 will proceed:

• Start at the root node:

Since 7 > 3, search in the right subtree

• Continue:

Since 7 > 6, search in the right subtree

• Continue:

Since 10 > 9, search in the right subtree

STOP !!!

• The right pointer of node 9 is equal to null !!!

• Notice that node 9 is the parent node of 10 in the binary search tree:

We can add the node 10 as a child node under node 9 !!!

• Some properties of the Binary Search Tree (BST)

• Recursive property

• If T is a Binary Search Tree with root node x, then:

 The left subtree of x is a Binary Search Tree The right subtree of x is a Binary Search Tree

Example:

(This is due to the fact that every subtree must satisfy the Binary Search Tree property !!!)

• How to find the minimum value in a Binary Search Tree:

• The minimum value in a Binary Search Tree can be found by:

• Start at the root node
• Follow the left child in each branch until you reach a node that does not have a left child

 The value at that node is the minimum value in the Binary Subtree

Examples:

• The minimum value in the following BST is:

• The minimum value in the following BST is:

• Notice that because subtrees as also Binary Search Trees, we can find the minimun value in a subtree with the same technique !!!

The minimum value in the right subtree of the root node is:

• How to find the maximum value in a Binary Search Tree:

• The maximum value in a Binary Search Tree can be found by:

• Start at the root node
• Follow the right child in each branch until you reach a node that does not have a right child

 The value at that node is the maximum value in the Binary Subtree

Examples:

• The maximum value in the following BST is:

• The maximum value in the following BST is:

• Notice that because subtrees as also Binary Search Trees, we can find the maximum value in a subtree with the same technique !!!

The maximum value in the left subtree of the root node is: