Movatterモバイル変換


[0]ホーム

URL:


Jump to content
WikipediaThe Free Encyclopedia
Search

Search tree

From Wikipedia, the free encyclopedia
Data structure in tree form sorted for fast lookup
Not to be confused withtree search.

Incomputer science, asearch tree is atree 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 theleaves 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 anassociative array. The search tree algorithm uses the key from thekey–value pair to find a location, and then the application stores the entire key–value pair at that particular location.

Types of trees

[edit]
Binary search tree
Binary search tree

Binary search tree

[edit]
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-casetime complexity for searching a binary search tree is theheight of the tree, which can be as small as O(log n) for a tree with n elements.

B-tree

[edit]
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 otherself-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

[edit]
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 leasta children and at mostb children, while the root has at least 2 children and at mostb children.

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

2a(b+1)2{\displaystyle 2\leq a\leq {\frac {(b+1)}{2}}}

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

Ternary search tree

[edit]
Main article:Ternary search tree

A ternary search tree is a type oftree 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 astring to test whether any path contains it.

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

Searching algorithms

[edit]

Searching for a specific key

[edit]

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

[edit]
search-recursive(key, node)if node isNULLreturnEMPTY_TREEif key < node.key        return search-recursive(key, node.left)else if key > node.key        return search-recursive(key, node.right)elsereturn node

Iterative

[edit]
searchIterative(key, node)    currentNode := nodewhile currentNode is notNULLif currentNode.key = keyreturn currentNodeelse if currentNode.key > key            currentNode := currentNode.leftelse            currentNode := currentNode.right

Searching for min and max

[edit]

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

[edit]
findMinimum(node)if node isNULLreturnEMPTY_TREE    min := nodewhile min.left is notNULL        min := min.leftreturn min.key

Maximum

[edit]
findMaximum(node)if node isNULLreturnEMPTY_TREE    max := nodewhile max.right is notNULL        max := max.rightreturn max.key

See also

[edit]

References

[edit]
  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"
Search trees
(dynamic sets/associative arrays)
Heaps
Tries
Spatial data partitioning trees
Other trees
Authority control databases: NationalEdit this at Wikidata
Retrieved from "https://en.wikipedia.org/w/index.php?title=Search_tree&oldid=1193981478"
Categories:
Hidden categories:

[8]ページ先頭

©2009-2025 Movatter.jp