Types of Trees
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
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
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: The time complexity for searching an (a,b)-tree is O(log n).Ternary search tree
A ternary search tree is a type ofSearching 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 nodeIterative
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.rightSearching 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.Gildea, Dan (2004)Minimum
findMinimum(node) if node is ''NULL'' return ''EMPTY_TREE'' min := node while min.left is not ''NULL'' min := min.left return min.keyMaximum
findMaximum(node) if node is ''NULL'' return ''EMPTY_TREE'' max := node while max.right is not ''NULL'' max := max.right return max.keySee also
* Trie * Binary tree * Depth-first searchReferences
{{DEFAULTSORT:Search Tree Search trees Search algorithms