Search tree

Short description: Data structure in tree form sorted for fast lookup

In computer science, a search tree is a tree data structure used for locating specific keys from within a set. In order for a tree to function as a search tree, the key for each node must be greater than any keys in subtrees on the left, and less than any keys in subtrees on the right.[1]

The advantage of search trees is their efficient search time given the tree is reasonably balanced, which is to say the leaves at either end are of comparable depths. Various search-tree data structures exist, several of which also allow efficient insertion and deletion of elements, which operations then have to maintain tree balance.

Search trees are often used to implement an associative array. The search tree algorithm uses the key from the key–value pair to find a location, and then the application stores the entire key–value pair at that particular location.

## Contents

* 1 Types of trees
* 1.1 Binary search tree
* 1.2 B-tree
* 1.3 (a,b)-tree
* 1.4 Ternary search tree
* 2 Searching algorithms
* 2.1 Searching for a specific key
* 2.1.1 Recursive
* 2.1.2 Iterative
* 2.2 Searching for min and max
* 2.2.1 Minimum
* 2.2.2 Maximum
* 3 See also
* 4 References

## Types of trees

Binary search tree

### Binary search tree

Main article: Binary search tree

A Binary Search Tree is a node-based data structure where each node contains a key and two subtrees, the left and right. For all nodes, the left subtree's key must be less than the node's key, and the right subtree's key must be greater than the node's key. These subtrees must all qualify as binary search trees.

The worst-case time complexity for searching a binary search tree is the height of the tree, which can be as small as O(log n) for a tree with n elements.

### B-tree

Main article: B-tree

B-trees are generalizations of binary search trees in that they can have a variable number of subtrees at each node. While child-nodes have a pre-defined range, they will not necessarily be filled with data, meaning B-trees can potentially waste some space. The advantage is that B-trees do not need to be re-balanced as frequently as other self-balancing trees.

Due to the variable range of their node length, B-trees are optimized for systems that read large blocks of data, they are also commonly used in databases.

The time complexity for searching a B-tree is O(log n).

### (a,b)-tree

Main article: (a,b)-tree

An (a,b)-tree is a search tree where all of its leaves are the same depth. Each node has at least a children and at most b children, while the root has at least 2 children and at most b children.

a and b can be decided with the following formula:[2]

2≤a≤(b+1)2

The time complexity for searching an (a,b)-tree is O(log n).

### Ternary search tree

Main article: Ternary search tree

A ternary search tree is a type of tree that can have 3 nodes: a low child, an equal child, and a high child. Each node stores a single character and the tree itself is ordered the same way a binary search tree is, with the exception of a possible third node.

Searching a ternary search tree involves passing in a string to test whether any path contains it.

The time complexity for searching a balanced ternary search tree is O(log n).

## Searching algorithms

### Searching for a specific key

Assuming the tree is ordered, we can take a key and attempt to locate it within the tree. The following algorithms are generalized for binary search trees, but the same idea can be applied to trees of other formats.

#### Recursive

search-recursive(key, node)
if node is NULL
return EMPTY_TREE
if key < node.key
return search-recursive(key, node.left)
else if key > node.key
return search-recursive(key, node.right)
else
return node

#### Iterative

searchIterative(key, node)
currentNode := node
while currentNode is not NULL
if currentNode.key = key
return currentNode
else if currentNode.key > key
currentNode := currentNode.left
else
currentNode := currentNode.right

### Searching for min and max

In a sorted tree, the minimum is located at the node farthest left, while the maximum is located at the node farthest right.[3]

#### Minimum

findMinimum(node)
if node is NULL
return EMPTY_TREE
min := node
while min.left is not NULL
min := min.left
return min.key

#### Maximum

findMaximum(node)
if node is NULL
return EMPTY_TREE
max := node
while max.right is not NULL
max := max.right
return max.key

## See also

* Trie
* Binary tree
* Depth-first search

## References

1. ↑ Black, Paul and Pieterse, Vreda (2005). "search tree". Dictionary of Algorithms and Data Structures
2. ↑ Toal, Ray. "(a,b) Trees"
3. ↑ Gildea, Dan (2004). "Binary Search Tree"

* v
* t
* e

Tree data structures

Search trees
(dynamic sets/associative arrays)|

* 2–3
* 2–3–4
* AA
* (a,b)
* AVL
* B
* B+
* B*
* Bx
* (Optimal) Binary search
* Dancing
* HTree
* Interval
* Order statistic
* (Left-leaning) Red-black
* Scapegoat
* Splay
* T
* Treap
* UB
* Weight-balanced

Heaps|

* Binary
* Binomial
* Brodal
* Fibonacci
* Leftist
* Pairing
* Skew
* van Emde Boas
* Weak

Tries|

* Ctrie
* C-trie (compressed ADT)
* Hash
* Radix
* Suffix
* Ternary search
* X-fast
* Y-fast

Spatial data partitioning trees|

* Ball
* BK
* BSP
* Cartesian
* Hilbert R
* k-d (implicit k-d)
* M
* Metric
* MVP
* Octree
* Priority R
* Quad
* R
* R+
* R*
* Segment
* VP
* X

Other trees|

* Cover
* Exponential
* Fenwick
* Finger
* Fractal tree index
* Fusion
* Hash calendar
* iDistance
* K-ary
* Left-child right-sibling
* Link/cut
* Log-structured merge
* Merkle
* PQ
* Range
* SPQR
* Top

0.00

(0 votes)

Original source: https://en.wikipedia.org/wiki/Search tree. Read more

|

Retrieved from "https://handwiki.org/wiki/index.php?title=Search_tree&oldid=229974"
*[v]: View this template
*[t]: Discuss this template
*[e]: Edit this template