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