Skip to content

Commit

Permalink
Update BST image.
Browse files Browse the repository at this point in the history
  • Loading branch information
trekhleb committed Aug 19, 2022
1 parent 7236dac commit b55e79a
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 36 deletions.
56 changes: 29 additions & 27 deletions src/data-structures/tree/binary-search-tree/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -45,7 +47,7 @@ insert(value)
end if
end insert
```

```text
insertNode(current, value)
Pre: current is the node to start from
Expand Down Expand Up @@ -84,8 +86,8 @@ contains(root, value)
end if
end contains
```


### Deletion

```text
Expand Down Expand Up @@ -186,7 +188,7 @@ findNode(root, value)
end if
end findNode
```

### Find Minimum

```text
Expand All @@ -200,7 +202,7 @@ findMin(root)
findMin(root.left)
end findMin
```

### Find Maximum

```text
Expand All @@ -214,7 +216,7 @@ findMax(root)
findMax(root.right)
end findMax
```

### Traversal

#### InOrder Traversal
Expand Down Expand Up @@ -244,7 +246,7 @@ preorder(root)
end if
end preorder
```

#### PostOrder Traversal

```text
Expand All @@ -258,7 +260,7 @@ postorder(root)
end if
end postorder
```

## Complexities

### Time Complexity
Expand Down
19 changes: 10 additions & 9 deletions src/data-structures/tree/binary-search-tree/README.pt-BR.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -44,7 +45,7 @@ insert(value)
end if
end insert
```

```text
insertNode(current, value)
Pre: current is the node to start from
Expand Down Expand Up @@ -83,8 +84,8 @@ contains(root, value)
end if
end contains
```


### Remoção

```text
Expand Down Expand Up @@ -185,7 +186,7 @@ findNode(root, value)
end if
end findNode
```

### Encontrar Mínimo

```text
Expand All @@ -199,7 +200,7 @@ findMin(root)
findMin(root.left)
end findMin
```

### Encontrar Máximo

```text
Expand All @@ -213,7 +214,7 @@ findMax(root)
findMax(root.right)
end findMax
```

### Traversal

#### Na Ordem Traversal (InOrder Traversal)
Expand Down Expand Up @@ -243,7 +244,7 @@ preorder(root)
end if
end preorder
```

#### Pós Ordem Traversal (PostOrder Traversal)

```text
Expand All @@ -257,7 +258,7 @@ postorder(root)
end if
end postorder
```

## Complexidades

### Complexidade de Tempo
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit b55e79a

Please sign in to comment.