Binary search tree
From Wikipedia, the free encyclopedia
In computer science, a
binary search tree
(
BST
) is a binary tree data structure which has the
following properties:
each node (item in the tree) has a value;
a total order (linear order) is defined on these values;
the left subtree of a node contains only values less than the node's value;
the right subtree of a node contains only values greater than or equal to the node's
value.
The major advantage of binary search trees over other data structures is that the related
sorting algorithms and search algorithms such as in-order traversal can be very efficient.
Binary search trees can choose to allow or disallow duplicate values, depending on the
implementation.
Binary search trees are a fundamental data structure used to construct more abstract data structures such as sets, multisets,
and associative arrays.
Operations
Operations on a binary tree require comparisons between nodes. These comparisons are made with calls to a comparator,
which is a subroutine that computes the total order (linear order) on any two values. This comparator can be explicitly or
implicitly defined, depending on the language in which the BST is implemented.
Searching
Searching a binary tree for a specific value can be a recursive or iterative process. This explanation covers a recursive
method.
We begin by examining the root node. If the tree is null, the value we are searching for does not exist in the tree.
Otherwise, if the value equals the root, the search is successful. If the value is less than the root, search the left subtree.
Similarly, if it is greater than the root, search the right subtree. This process is repeated until the value is found or the
indicated subtree is null. If the searched value is not found before a null subtree is reached, then the item must not be
present in the tree.
Here is the search algorithm in the Python programming language:
A binary search tree of
size 9 and depth 3, with
root 8 and leaves 1, 4, 7
and 13
Contents
1 Operations
1.1 Searching
1.2 Insertion
1.3 Deletion
1.4 Traversal
1.5 Sort
1.6 Example for a Binary Search Tree in Python:
2 Types of binary search trees
2.1 Performance comparisons
2.2 Optimal binary search trees
3 See also
4 References
5 Further reading
6 External links
Wikipedia is sustained by people like you. Please
donate
today.

This
** preview**
has intentionally

**sections.**

*blurred***to view the full version.**

*Sign up*