mirror of
https://github.moeyy.xyz/https://github.com/trekhleb/javascript-algorithms.git
synced 2024-12-25 22:46:20 +08:00
Update BST image.
This commit is contained in:
parent
7236dacb34
commit
b55e79aed2
@ -3,32 +3,34 @@
|
||||
_Read this in other languages:_
|
||||
[_Português_](README.pt-BR.md)
|
||||
|
||||
In computer science, **binary search trees** (BST), sometimes called
|
||||
ordered or sorted binary trees, are a particular type of container:
|
||||
data structures that store "items" (such as numbers, names etc.)
|
||||
in memory. They allow fast lookup, addition and removal of
|
||||
items, and can be used to implement either dynamic sets of
|
||||
items, or lookup tables that allow finding an item by its key
|
||||
In computer science, **binary search trees** (BST), sometimes called
|
||||
ordered or sorted binary trees, are a particular type of container:
|
||||
data structures that store "items" (such as numbers, names etc.)
|
||||
in memory. They allow fast lookup, addition and removal of
|
||||
items, and can be used to implement either dynamic sets of
|
||||
items, or lookup tables that allow finding an item by its key
|
||||
(e.g., finding the phone number of a person by name).
|
||||
|
||||
Binary search trees keep their keys in sorted order, so that lookup
|
||||
and other operations can use the principle of binary search:
|
||||
when looking for a key in a tree (or a place to insert a new key),
|
||||
they traverse the tree from root to leaf, making comparisons to
|
||||
keys stored in the nodes of the tree and deciding, on the basis
|
||||
of the comparison, to continue searching in the left or right
|
||||
subtrees. On average, this means that each comparison allows
|
||||
the operations to skip about half of the tree, so that each
|
||||
lookup, insertion or deletion takes time proportional to the
|
||||
logarithm of the number of items stored in the tree. This is
|
||||
much better than the linear time required to find items by key
|
||||
in an (unsorted) array, but slower than the corresponding
|
||||
Binary search trees keep their keys in sorted order, so that lookup
|
||||
and other operations can use the principle of binary search:
|
||||
when looking for a key in a tree (or a place to insert a new key),
|
||||
they traverse the tree from root to leaf, making comparisons to
|
||||
keys stored in the nodes of the tree and deciding, on the basis
|
||||
of the comparison, to continue searching in the left or right
|
||||
subtrees. On average, this means that each comparison allows
|
||||
the operations to skip about half of the tree, so that each
|
||||
lookup, insertion or deletion takes time proportional to the
|
||||
logarithm of the number of items stored in the tree. This is
|
||||
much better than the linear time required to find items by key
|
||||
in an (unsorted) array, but slower than the corresponding
|
||||
operations on hash tables.
|
||||
|
||||
A binary search tree of size 9 and depth 3, with 8 at the root.
|
||||
The leaves are not drawn.
|
||||
|
||||
![Binary Search Tree](https://upload.wikimedia.org/wikipedia/commons/d/da/Binary_search_tree.svg)
|
||||
![Trie](./images/binary-search-tree.jpg)
|
||||
|
||||
*Made with [okso.app](https://okso.app)*
|
||||
|
||||
## Pseudocode for Basic Operations
|
||||
|
||||
@ -45,7 +47,7 @@ insert(value)
|
||||
end if
|
||||
end insert
|
||||
```
|
||||
|
||||
|
||||
```text
|
||||
insertNode(current, value)
|
||||
Pre: current is the node to start from
|
||||
@ -84,8 +86,8 @@ contains(root, value)
|
||||
end if
|
||||
end contains
|
||||
```
|
||||
|
||||
|
||||
|
||||
|
||||
### Deletion
|
||||
|
||||
```text
|
||||
@ -186,7 +188,7 @@ findNode(root, value)
|
||||
end if
|
||||
end findNode
|
||||
```
|
||||
|
||||
|
||||
### Find Minimum
|
||||
|
||||
```text
|
||||
@ -200,7 +202,7 @@ findMin(root)
|
||||
findMin(root.left)
|
||||
end findMin
|
||||
```
|
||||
|
||||
|
||||
### Find Maximum
|
||||
|
||||
```text
|
||||
@ -214,7 +216,7 @@ findMax(root)
|
||||
findMax(root.right)
|
||||
end findMax
|
||||
```
|
||||
|
||||
|
||||
### Traversal
|
||||
|
||||
#### InOrder Traversal
|
||||
@ -244,7 +246,7 @@ preorder(root)
|
||||
end if
|
||||
end preorder
|
||||
```
|
||||
|
||||
|
||||
#### PostOrder Traversal
|
||||
|
||||
```text
|
||||
@ -258,7 +260,7 @@ postorder(root)
|
||||
end if
|
||||
end postorder
|
||||
```
|
||||
|
||||
|
||||
## Complexities
|
||||
|
||||
### Time Complexity
|
||||
|
@ -26,8 +26,9 @@ Uma pesquisa de árvore binária de tamanho 9 e profundidade 3, com valor 8
|
||||
na raíz.
|
||||
As folhas não foram desenhadas.
|
||||
|
||||
![Trie](./images/binary-search-tree.jpg)
|
||||
|
||||
![Binary Search Tree](https://upload.wikimedia.org/wikipedia/commons/d/da/Binary_search_tree.svg)
|
||||
*Made with [okso.app](https://okso.app)*
|
||||
|
||||
## Pseudocódigo para Operações Básicas
|
||||
|
||||
@ -44,7 +45,7 @@ insert(value)
|
||||
end if
|
||||
end insert
|
||||
```
|
||||
|
||||
|
||||
```text
|
||||
insertNode(current, value)
|
||||
Pre: current is the node to start from
|
||||
@ -83,8 +84,8 @@ contains(root, value)
|
||||
end if
|
||||
end contains
|
||||
```
|
||||
|
||||
|
||||
|
||||
|
||||
### Remoção
|
||||
|
||||
```text
|
||||
@ -185,7 +186,7 @@ findNode(root, value)
|
||||
end if
|
||||
end findNode
|
||||
```
|
||||
|
||||
|
||||
### Encontrar Mínimo
|
||||
|
||||
```text
|
||||
@ -199,7 +200,7 @@ findMin(root)
|
||||
findMin(root.left)
|
||||
end findMin
|
||||
```
|
||||
|
||||
|
||||
### Encontrar Máximo
|
||||
|
||||
```text
|
||||
@ -213,7 +214,7 @@ findMax(root)
|
||||
findMax(root.right)
|
||||
end findMax
|
||||
```
|
||||
|
||||
|
||||
### Traversal
|
||||
|
||||
#### Na Ordem Traversal (InOrder Traversal)
|
||||
@ -243,7 +244,7 @@ preorder(root)
|
||||
end if
|
||||
end preorder
|
||||
```
|
||||
|
||||
|
||||
#### Pós Ordem Traversal (PostOrder Traversal)
|
||||
|
||||
```text
|
||||
@ -257,7 +258,7 @@ postorder(root)
|
||||
end if
|
||||
end postorder
|
||||
```
|
||||
|
||||
|
||||
## Complexidades
|
||||
|
||||
### Complexidade de Tempo
|
||||
|
Binary file not shown.
After Width: | Height: | Size: 146 KiB |
Loading…
Reference in New Issue
Block a user