Giter Site home page Giter Site logo

algorithms's Introduction

๐™ฐ๐š•๐š๐š˜๐š›๐š’๐š๐š‘๐š–๐šœ & ๐™ณ๐šŠ๐š๐šŠ ๐š‚๐š๐š›๐šž๐šŒ๐š๐šž๐š›๐šŽ๐šœ

๐šƒ๐š‘๐š’๐šœ ๐š›๐šŽ๐š™๐š˜๐šœ๐š’๐š๐š˜๐š›๐šข ๐šŒ๐š˜๐š—๐š๐šŠ๐š’๐š—๐šœ ๐š’๐š–๐š™๐š•๐šŽ๐š–๐šŽ๐š—๐š๐šŠ๐š๐š’๐š˜๐š—๐šœ ๐š˜๐š ๐š–๐šŠ๐š—๐šข ๐š™๐š˜๐š™๐šž๐š•๐šŠ๐š› ๐šŠ๐š•๐š๐š˜๐š›๐š’๐š๐š‘๐š–๐šœ ๐šŠ๐š—๐š ๐š๐šŠ๐š๐šŠ ๐šœ๐š๐š›๐šž๐šŒ๐š๐šž๐š›๐šŽ๐šœ๏ผŽ


Important

Note that this project is meant to be used for learning and researching purposes only. Most optimal implementation for each algorithm or data structure depends on the use case.

๐™ป๐šŠ๐š—๐š๐šž๐šŠ๐š๐šŽ๐šœ ๐šž๐šœ๐šŽ๐š ๐š๐š˜๐š› ๐š’๐š–๐š™๐š•๐šŽ๐š–๐šŽ๐š—๐š๐šŠ๐š๐š’๐š˜๐š—๐šœ:

rust csharp

Sorting

sorting

Big O Notation

In a nutshell, we use Big O to describe the efficiency of algorithms.
It represents an upper bound on the time complexity of an algorithm, indicating how the runtime increases with the size of the input.
For example, O(N) suggests a linear increase in time with the size of the input, while O(1) indicates constant time regardless of input size

Key points:

There are three main mathematical notations used to describe the upper, tight, and lower bounds of algorithm complexity

  • Bษชษข O (O-ษดแดแด›แด€แด›ษชแดษด): It describes the upper bound of the time complexity of an algorithm. (worst-case)
  • Bษชษข Tสœแด‡แด›แด€ (ฮ˜-ษดแดแด›แด€แด›ษชแดษด): Big Theta provides a tight bound on the time complexity. (average-case)
  • Bษชษข Oแดแด‡ษขแด€ (ฮฉ-ษดแดแด›แด€แด›ษชแดษด): Big Omega describes the lower bound of the time complexity of an algorithm. (best-case)

Note

Fแดส€ แดแดส€แด‡ ษชษด๊œฐแดส€แดแด€แด›ษชแดษด แด€ส™แดแดœแด› 'Bษชษข O ษดแดแด›แด€แด›ษชแดษด' - cooervo.github.io

visualgo

Sorting Algorithms:

wแดส€๊œฑแด› แด„แด€๊œฑแด‡ aแด แด‡ส€แด€ษขแด‡ แด„แด€๊œฑแด‡ bแด‡๊œฑแด› แด„แด€๊œฑแด‡ Iแดแด˜สŸแด‡แดแด‡ษดแด›แด€แด›ษชแดษด๊œฑ
Bแดœส™ส™สŸแด‡ ๊œฑแดส€แด› O(n2) ฮ˜(n2) ฮฉ(n) Rust - C#
Sแด‡สŸแด‡แด„แด›ษชแดษด ๊œฑแดส€แด› O(n2) ฮ˜(n2) ฮฉ(n2) Rust - C#
Iษด๊œฑแด‡ส€แด›ษชแดษด ๊œฑแดส€แด› O(n2) ฮ˜(n2) ฮฉ(n) Rust - C#
Sสœแด‡สŸสŸ ๊œฑแดส€แด› O(n(3/2)) ฮ˜(n2) ฮฉ(n log(n)) Rust - C#
Hแด‡แด€แด˜ ๊œฑแดส€แด› O(n log(n)) ฮ˜(n log(n)) ฮฉ(n log(n)) Rust - C#
Mแด‡ส€ษขแด‡ ๊œฑแดส€แด› O(n log(n)) ฮ˜(n log(n)) ฮฉ(n log(n)) Rust - C#
Qแดœษชแด„แด‹๊œฑแดส€แด› O(n log(n)) ฮ˜(n log(n)) ฮฉ(n log(n)) Rust - C#

Primes

primes

What Are Prime Numbers?

๐–จ๐—‡ ๐—๐—๐–พ ๐—๐—ˆ๐—‹๐—…๐–ฝ ๐—ˆ๐–ฟ ๐—†๐–บ๐—๐—๐–พ๐—†๐–บ๐—๐—‚๐–ผ๐—Œ, ๐—‰๐—‹๐—‚๐—†๐–พ ๐—‡๐—Ž๐—†๐–ป๐–พ๐—‹๐—Œ ๐—๐—ˆ๐—…๐–ฝ ๐–บ ๐—Œ๐—‰๐–พ๐–ผ๐—‚๐–บ๐—… ๐—‰๐—…๐–บ๐–ผ๐–พ. ๐–ณ๐—๐–พ๐—’ ๐–บ๐—‹๐–พ ๐—๐—๐–พ ๐–ป๐—Ž๐—‚๐—…๐–ฝ๐—‚๐—‡๐—€ ๐–ป๐—…๐—ˆ๐–ผ๐—„๐—Œ ๐—ˆ๐–ฟ ๐—๐—๐–พ ๐—‡๐–บ๐—๐—Ž๐—‹๐–บ๐—… ๐—‡๐—Ž๐—†๐–ป๐–พ๐—‹๐—Œ, ๐—๐—๐–บ๐—‡๐—„๐—Œ ๐—๐—ˆ ๐–บ ๐—„๐–พ๐—’ ๐—‰๐—‹๐—ˆ๐—‰๐–พ๐—‹๐—๐—’: ๐–ฝ๐—‚๐—๐—‚๐—Œ๐—‚๐–ป๐—‚๐—…๐—‚๐—๐—’.
๐–  ๐—‰๐—‹๐—‚๐—†๐–พ ๐—‡๐—Ž๐—†๐–ป๐–พ๐—‹ ๐—‚๐—Œ ๐–ฝ๐–พ๐–ฟ๐—‚๐—‡๐–พ๐–ฝ ๐–บ๐—Œ ๐–บ๐—‡๐—’ ๐—‡๐–บ๐—๐—Ž๐—‹๐–บ๐—… ๐—‡๐—Ž๐—†๐–ป๐–พ๐—‹ ๐—€๐—‹๐–พ๐–บ๐—๐–พ๐—‹ ๐—๐—๐–บ๐—‡ ๐Ÿฃ ๐—๐—๐–บ๐— ๐—๐–บ๐—Œ ๐—‡๐—ˆ ๐—‰๐—ˆ๐—Œ๐—‚๐—๐—‚๐—๐–พ ๐–ฝ๐—‚๐—๐—‚๐—Œ๐—ˆ๐—‹๐—Œ ๐—ˆ๐—๐—๐–พ๐—‹ ๐—๐—๐–บ๐—‡ ๐Ÿฃ ๐–บ๐—‡๐–ฝ ๐—‚๐—๐—Œ๐–พ๐—…๐–ฟ. ๐–ณ๐—๐—‚๐—Œ ๐—†๐–พ๐–บ๐—‡๐—Œ ๐—๐—๐–บ๐— ๐—‰๐—‹๐—‚๐—†๐–พ ๐—‡๐—Ž๐—†๐–ป๐–พ๐—‹๐—Œ ๐–ผ๐–บ๐—‡๐—‡๐—ˆ๐— ๐–ป๐–พ ๐–ฝ๐—‚๐—๐—‚๐–ฝ๐–พ๐–ฝ ๐–พ๐—๐–พ๐—‡๐—…๐—’ ๐–ป๐—’ ๐–บ๐—‡๐—’ ๐—ˆ๐—๐—๐–พ๐—‹ ๐—‡๐—Ž๐—†๐–ป๐–พ๐—‹๐—Œ ๐–พ๐—‘๐–ผ๐–พ๐—‰๐— ๐–ฟ๐—ˆ๐—‹ ๐Ÿฃ ๐–บ๐—‡๐–ฝ ๐—๐—๐–พ ๐—‡๐—Ž๐—†๐–ป๐–พ๐—‹ ๐—‚๐—๐—Œ๐–พ๐—…๐–ฟ ๐—๐—‚๐—๐—๐—ˆ๐—Ž๐— ๐—…๐–พ๐–บ๐—๐—‚๐—‡๐—€ ๐–บ ๐—‹๐–พ๐—†๐–บ๐—‚๐—‡๐–ฝ๐–พ๐—‹.

๐–ข๐—๐–บ๐—‹๐–บ๐–ผ๐—๐–พ๐—‹๐—‚๐—Œ๐—๐—‚๐–ผ๐—Œ ๐—ˆ๐–ฟ ๐–ฏ๐—‹๐—‚๐—†๐–พ ๐–ญ๐—Ž๐—†๐–ป๐–พ๐—‹๐—Œ:

  • ๐–ด๐—‡๐—‚๐—Š๐—Ž๐–พ๐—‡๐–พ๐—Œ๐—Œ: ๐–ค๐—๐–พ๐—‹๐—’ ๐—‡๐—Ž๐—†๐–ป๐–พ๐—‹ ๐—€๐—‹๐–พ๐–บ๐—๐–พ๐—‹ ๐—๐—๐–บ๐—‡ ๐Ÿฃ ๐—‚๐—Œ ๐–พ๐—‚๐—๐—๐–พ๐—‹ ๐—‰๐—‹๐—‚๐—†๐–พ ๐—ˆ๐—‹ ๐–ผ๐–บ๐—‡ ๐–ป๐–พ ๐–ฟ๐–บ๐–ผ๐—๐—ˆ๐—‹๐–พ๐–ฝ ๐—‚๐—‡๐—๐—ˆ ๐—‰๐—‹๐—‚๐—†๐–พ ๐—‡๐—Ž๐—†๐–ป๐–พ๐—‹๐—Œ, ๐—†๐–บ๐—„๐—‚๐—‡๐—€ ๐—‰๐—‹๐—‚๐—†๐–พ๐—Œ ๐—๐—๐–พ "๐–บ๐—๐—ˆ๐—†๐—Œ" ๐—ˆ๐–ฟ ๐—๐—๐–พ ๐—†๐–บ๐—๐—๐–พ๐—†๐–บ๐—๐—‚๐–ผ๐–บ๐—… ๐—๐—ˆ๐—‹๐—…๐–ฝ.
  • ๐–จ๐—‡๐–ฝ๐—‚๐—๐—‚๐—Œ๐—‚๐–ป๐—‚๐—…๐—‚๐—๐—’: ๐–ณ๐—๐–พ ๐–ฝ๐–พ๐–ฟ๐—‚๐—‡๐—‚๐—‡๐—€ ๐–ผ๐—๐–บ๐—‹๐–บ๐–ผ๐—๐–พ๐—‹๐—‚๐—Œ๐—๐—‚๐–ผ ๐—ˆ๐–ฟ ๐—‰๐—‹๐—‚๐—†๐–พ ๐—‡๐—Ž๐—†๐–ป๐–พ๐—‹๐—Œ ๐—‚๐—Œ ๐—๐—๐–พ๐—‚๐—‹ ๐—‹๐–พ๐—Œ๐—‚๐—Œ๐—๐–บ๐—‡๐–ผ๐–พ ๐—๐—ˆ ๐–ป๐–พ๐—‚๐—‡๐—€ ๐–ฝ๐—‚๐—๐—‚๐–ฝ๐–พ๐–ฝ ๐–พ๐—๐–พ๐—‡๐—…๐—’ ๐–ป๐—’ ๐–บ๐—‡๐—’ ๐—‡๐—Ž๐—†๐–ป๐–พ๐—‹ ๐—ˆ๐—๐—๐–พ๐—‹ ๐—๐—๐–บ๐—‡ ๐Ÿฃ ๐–บ๐—‡๐–ฝ ๐—๐—๐–พ ๐—‡๐—Ž๐—†๐–ป๐–พ๐—‹ ๐—‚๐—๐—Œ๐–พ๐—…๐–ฟ.
  • ๐–ณ๐—๐–พ ๐–ฅ๐—‚๐—‹๐—Œ๐— ๐–ฏ๐—‹๐—‚๐—†๐–พ: ๐–ณ๐—๐–พ ๐—Œ๐—†๐–บ๐—…๐—…๐–พ๐—Œ๐— ๐—‰๐—‹๐—‚๐—†๐–พ ๐—‡๐—Ž๐—†๐–ป๐–พ๐—‹ ๐—‚๐—Œ ๐Ÿค, ๐—๐—๐—‚๐–ผ๐— ๐—‚๐—Œ ๐–บ๐—…๐—Œ๐—ˆ ๐—๐—๐–พ ๐—ˆ๐—‡๐—…๐—’ ๐–พ๐—๐–พ๐—‡ ๐—‰๐—‹๐—‚๐—†๐–พ ๐—‡๐—Ž๐—†๐–ป๐–พ๐—‹. ๐–ค๐—๐–พ๐—‹๐—’ ๐—ˆ๐—๐—๐–พ๐—‹ ๐–พ๐—๐–พ๐—‡ ๐—‡๐—Ž๐—†๐–ป๐–พ๐—‹ ๐–ผ๐–บ๐—‡ ๐–ป๐–พ ๐–ฝ๐—‚๐—๐—‚๐–ฝ๐–พ๐–ฝ ๐–ป๐—’ ๐Ÿค, ๐—†๐–บ๐—„๐—‚๐—‡๐—€ ๐—๐—๐–พ๐—† ๐–ผ๐—ˆ๐—†๐—‰๐—ˆ๐—Œ๐—‚๐—๐–พ ๐—‹๐–บ๐—๐—๐–พ๐—‹ ๐—๐—๐–บ๐—‡ ๐—‰๐—‹๐—‚๐—†๐–พ.

Trial Division

๐–ณ๐—‹๐—‚๐–บ๐—… ๐–ฝ๐—‚๐—๐—‚๐—Œ๐—‚๐—ˆ๐—‡ ๐—‚๐—Œ ๐—ˆ๐—‡๐–พ ๐—ˆ๐–ฟ ๐—๐—๐–พ ๐—Œ๐—‚๐—†๐—‰๐—…๐–พ๐—Œ๐— ๐–บ๐—‡๐–ฝ ๐—†๐—ˆ๐—Œ๐— ๐—Œ๐—๐—‹๐–บ๐—‚๐—€๐—๐—๐–ฟ๐—ˆ๐—‹๐—๐–บ๐—‹๐–ฝ ๐—†๐–พ๐—๐—๐—ˆ๐–ฝ๐—Œ ๐–ฟ๐—ˆ๐—‹ ๐—๐–พ๐—Œ๐—๐—‚๐—‡๐—€ ๐—๐—๐–พ ๐—‰๐—‹๐—‚๐—†๐–บ๐—…๐—‚๐—๐—’ ๐—ˆ๐–ฟ ๐–บ ๐—‡๐—Ž๐—†๐–ป๐–พ๐—‹ ๐—ˆ๐—‹ ๐–ฟ๐—ˆ๐—‹ ๐–ฟ๐—‚๐—‡๐–ฝ๐—‚๐—‡๐—€ ๐—๐—๐–พ ๐—‰๐—‹๐—‚๐—†๐–พ ๐–ฟ๐–บ๐–ผ๐—๐—ˆ๐—‹๐—Œ ๐—ˆ๐–ฟ ๐–บ ๐—‡๐—Ž๐—†๐–ป๐–พ๐—‹.
Here's how it works:

๐–ถ๐–พ ๐—๐—‚๐—…๐—… ๐—๐—‹๐—’ ๐—๐—ˆ ๐–ฟ๐—‚๐—‡๐–ฝ ๐–บ๐—…๐—… ๐—‰๐—‹๐—‚๐—†๐–พ ๐—‡๐—Ž๐—†๐–ป๐–พ๐—‹๐—Œ ๐—Ž๐—‰ ๐—๐—ˆ an integer. <๐–ป๐—‹> ๐–ณ๐—๐–พ ๐—๐—‹๐—‚๐–ผ๐—„ ๐–ฟ๐—ˆ๐—‹ ๐—๐—๐—‚๐—Œ ๐–บ๐—…๐—€๐—ˆ๐—‹๐—‚๐—๐—๐—† ๐—‚๐—Œ, ๐–ฟ๐—ˆ๐—‹ ๐–พ๐–บ๐–ผ๐— ๐—‡๐—Ž๐—†๐–ป๐–พ๐—‹ ๐—๐–พ ๐—๐–บ๐—‡๐—‡๐–บ ๐–ผ๐—๐–พ๐–ผ๐—„ ๐—‚๐–ฟ ๐—‚๐—'๐—Œ ๐–บ ๐—†๐—Ž๐—…๐—๐—‚๐—‰๐—…๐–พ ๐—ˆ๐–ฟ ๐–บ๐—‡๐—’ ๐–ผ๐—Ž๐—‹๐—‹๐–พ๐—‡๐—๐—…๐—’ ๐–ฟ๐—‚๐—‡๐–ฝ ๐—‰๐—‹๐—‚๐—†๐–พ ๐—‡๐—Ž๐—†๐–ป๐–พ๐—‹๐—Œ.

  • ๐–ฅ๐—‚๐—‹๐—Œ๐— ๐—๐–พ ๐—‡๐–พ๐–พ๐–ฝ ๐–บ๐—‡ ๐–พ๐—†๐—‰๐—๐—’ ๐—…๐—‚๐—Œ๐— ๐—๐—๐–พ๐—‹๐–พ ๐—๐–พ ๐—„๐–พ๐–พ๐—‰ ๐—๐—๐–พ ๐—‰๐—‹๐—‚๐—†๐–พ ๐—‡๐—Ž๐—†๐–ป๐–พ๐—‹๐—Œ.
  • ๐–ฒ๐—๐–บ๐—‹๐—๐—‚๐—‡๐—€ ๐—๐—‚๐—๐— ๐Ÿค (๐Ÿฃ ๐—‚๐—Œ ๐—‡๐—ˆ๐— ๐–ผ๐—ˆ๐—‡๐—Œ๐—‚๐–ฝ๐–พ๐—‹๐–พ๐–ฝ ๐—๐—ˆ ๐–ป๐–พ ๐–บ ๐—‰๐—‹๐—‚๐—†๐–พ)
    • ๐–ข๐—๐–พ๐–ผ๐—„ ๐—‚๐–ฟ ๐Ÿค ๐—‚๐—Œ ๐—†๐—Ž๐—…๐—๐—‚๐—‰๐—…๐–พ ๐—ˆ๐–ฟ ๐–บ๐—‡๐—’ ๐—ˆ๐–ฟ ๐—๐—๐–พ ๐–ผ๐—Ž๐—‹๐—‹๐–พ๐—‡๐—๐—…๐—’ ๐–ฟ๐—ˆ๐—Ž๐—‡๐–ฝ ๐—‰๐—‹๐—‚๐—†๐–พ ๐—‡๐—Ž๐—†๐–ป๐–พ๐—‹๐—Œ. ๐–ก๐–พ๐–ผ๐–บ๐—Ž๐—Œ๐–พ ๐—๐–พ ๐—๐–บ๐—๐–พ๐—‡๐— ๐–ฟ๐—‚๐—‡๐–ฝ ๐–บ๐—‡๐—’ ๐–บ๐—‡๐–ฝ ๐—๐—๐–พ ๐—…๐—‚๐—Œ๐— ๐—‚๐—Œ ๐–พ๐—†๐—‰๐—๐—’, ๐—๐–พ ๐–บ๐–ฝ๐–ฝ ๐Ÿค ๐—๐—ˆ ๐—๐—๐–พ ๐—‰๐—‹๐—‚๐—†๐–พ๐—Œ ๐—…๐—‚๐—Œ๐—
  • ๐–ญ๐–พ๐—‘๐— ๐—๐–พ ๐—†๐—ˆ๐—๐–พ ๐—ˆ๐—‡ ๐—๐—ˆ ๐Ÿฅ
    • ๐–ข๐—๐–พ๐–ผ๐—„ ๐—‚๐–ฟ ๐Ÿฅ ๐—‚๐—Œ ๐—†๐—Ž๐—…๐—๐—‚๐—‰๐—…๐–พ ๐—ˆ๐–ฟ ๐–บ๐—‡๐—’ ๐—ˆ๐–ฟ ๐—๐—๐–พ ๐–ผ๐—Ž๐—‹๐—‹๐–พ๐—‡๐—๐—…๐—’ ๐–ฟ๐—ˆ๐—Ž๐—‡๐–ฝ ๐—‰๐—‹๐—‚๐—†๐–พ ๐—‡๐—Ž๐—†๐–ป๐–พ๐—‹๐—Œ. ๐–ณ๐—๐–พ ๐—ˆ๐—‡๐—…๐—’ ๐—ˆ๐—๐—๐–พ๐—‹ ๐—‰๐—‹๐—‚๐—†๐–พ ๐—‚๐—Œ ๐Ÿค ๐–บ๐—‡๐–ฝ ๐Ÿฅ ๐—‚๐—Œ ๐—‡๐—ˆ๐— ๐—†๐—Ž๐—…๐—๐—‚๐—‰๐—…๐–พ ๐—ˆ๐–ฟ ๐Ÿค ๐—Œ๐—ˆ ๐—๐–พ ๐–บ๐–ฝ๐–ฝ ๐Ÿฅ ๐—๐—ˆ ๐—๐—๐–พ ๐—‰๐—‹๐—‚๐—†๐–พ๐—Œ ๐—…๐—‚๐—Œ๐—
  • ๐–ญ๐–พ๐—‘๐— ๐—๐–พ ๐—†๐—ˆ๐—๐–พ ๐—ˆ๐—‡ ๐—๐—ˆ ๐Ÿฆ
    • ๐–ข๐—๐–พ๐–ผ๐—„ ๐—‚๐–ฟ ๐Ÿฆ ๐—‚๐—Œ ๐—†๐—Ž๐—…๐—๐—‚๐—‰๐—…๐–พ ๐—ˆ๐–ฟ ๐–บ๐—‡๐—’ ๐—ˆ๐–ฟ ๐—๐—๐–พ ๐–ผ๐—Ž๐—‹๐—‹๐–พ๐—‡๐—๐—…๐—’ ๐–ฟ๐—ˆ๐—Ž๐—‡๐–ฝ ๐—‰๐—‹๐—‚๐—†๐–พ ๐—‡๐—Ž๐—†๐–ป๐–พ๐—‹๐—Œ. ๐–ถ๐–พ ๐–ฟ๐—ˆ๐—Ž๐—‡๐–ฝ ๐—๐—๐–บ๐— ๐Ÿค ๐—‚๐—Œ ๐—†๐—Ž๐—…๐—๐—‚๐—‰๐—…๐–พ ๐—ˆ๐–ฟ ๐Ÿฆ ๐—Œ๐—ˆ ๐—๐–พ ๐—Œ๐—„๐—‚๐—‰ ๐—๐—๐—‚๐—Œ ๐—‡๐—Ž๐—†๐–ป๐–พ๐—‹
  • ๐–ณ๐—๐—‚๐—Œ ๐—‰๐—‹๐—ˆ๐–ผ๐–พ๐—Œ๐—Œ ๐—‚๐—Œ ๐—‹๐–พ๐—‰๐–พ๐–บ๐—๐–พ๐–ฝ ๐—Ž๐—‡๐—๐—‚๐—… ๐—๐–พ ๐—‹๐–พ๐–บ๐–ผ๐— ๐—๐—๐–พ ๐—Œ๐–พ๐— ๐—…๐—‚๐—†๐—‚๐—. (๐—‚๐—‡ ๐—ˆ๐—๐—‹ ๐–ผ๐–บ๐—Œ๐–พ: ๐Ÿฅ๐Ÿข)
About the ๐—Œ๐—Š๐—Ž๐–บ๐—‹๐–พ ๐—‹๐—ˆ๐—ˆ๐—:

Important

๐– ๐–ผ๐—๐—Ž๐–บ๐—…๐—…๐—’ ๐—๐–พ ๐–ฝ๐—ˆ๐—‡'๐— ๐—๐–บ๐—๐–พ ๐—๐—ˆ ๐–ผ๐—๐–พ๐–ผ๐—„ ๐—๐—๐–พ ๐—‡๐—Ž๐—†๐–ป๐–พ๐—‹ ๐—๐—‚๐—๐— ๐–พ๐—๐–พ๐—‹๐—’ ๐—‰๐—‹๐—‚๐—†๐–พ ๐—๐—๐–บ๐— ๐—๐–พ ๐–ฟ๐—ˆ๐—Ž๐—‡๐–ฝ. ๐–ฎ๐—‡๐—…๐—’ ๐—Ž๐—‰ ๐—๐—ˆ ๐—๐—๐–พ ๐—Œ๐—Š๐—Ž๐–บ๐—‹๐–พ ๐—‹๐—ˆ๐—ˆ๐— ๐—ˆ๐–ฟ ๐—๐—๐–พ ๐–ผ๐—Ž๐—‹๐—‹๐–พ๐—‡๐— ๐—‡๐—Ž๐—†๐–ป๐–พ๐—‹
๐–ถ๐—๐—’?
๐–ซ๐–พ๐— ๐–ผ๐—ˆ๐—‡๐—Œ๐—‚๐–ฝ๐–พ๐—‹ ๐–ฟ๐—ˆ๐—‹ ๐–พ๐—‘๐–บ๐—†๐—‰๐—…๐–พ ๐—๐—๐–พ ๐—‡๐—Ž๐—†๐–ป๐–พ๐—‹ ๐Ÿค๐Ÿฆ
๐–ณ๐—๐–พ ๐—Œ๐—Š๐—Ž๐–บ๐—‹๐–พ ๐—‹๐—ˆ๐—ˆ๐— ๐—ˆ๐–ฟ ๐Ÿค๐Ÿฆ ๐—‚๐—Œ ๐–ป๐–พ๐—๐—๐–พ๐–พ๐—‡ ๐Ÿฆ ๐–บ๐—‡๐–ฝ ๐Ÿง
๐–ถ๐—๐–บ๐— ๐–บ๐—‹๐–พ ๐—๐—๐–พ ๐–ฟ๐–บ๐–ผ๐—๐—ˆ๐—‹๐—Œ ๐—ˆ๐–ฟ ๐Ÿค๐Ÿฆ:

  • ๐Ÿฃ - ๐Ÿฃ๐—‘๐Ÿค๐Ÿฆ
  • ๐Ÿค - ๐Ÿค๐—‘๐Ÿฃ๐Ÿค
  • ๐Ÿฅ - ๐Ÿฅ๐—‘๐Ÿช
  • ๐Ÿฆ - ๐Ÿฆ๐—‘๐Ÿจ
  • ๐Ÿจ - ๐Ÿจ๐—‘๐Ÿฆ
  • ๐Ÿช - ๐Ÿช๐—‘๐Ÿฅ
  • ๐Ÿฃ๐Ÿค - ๐Ÿฃ๐Ÿค๐—‘๐Ÿค
  • ๐Ÿค๐Ÿฆ - ๐Ÿค๐Ÿฆ๐—‘๐Ÿฃ

๐–จ๐— ๐—‚๐—Œ ๐—‡๐—ˆ๐—๐—‚๐–ผ๐–พ๐–บ๐–ป๐—…๐–พ ๐—๐—๐–บ๐— ๐—๐—๐–พ๐—‡ ๐—๐–พ ๐—‹๐–พ๐–บ๐–ผ๐— ๐—๐—ˆ ๐—๐—๐–พ ๐—Œ๐—Š๐—Ž๐–บ๐—‹๐–พ ๐—‹๐—ˆ๐—ˆ๐— (๐Ÿฆ), ๐–บ๐–ฟ๐—๐–พ๐—‹ ๐—๐—a๐— ๐–บ๐—…๐—… ๐–ป๐–พ๐–ผ๐—ˆ๐—†๐–พ ๐—†๐—‚๐—‹๐—‹๐—ˆ๐—‹๐–พ๐–ฝ ๐–บ๐—‡๐–ฝ ๐–บ๐—…๐—… ๐—‹๐–พ๐—†๐–บ๐—‚๐—‡๐—‚๐—‡๐—€ ๐–ฟ๐–บ๐–ผ๐—๐—ˆ๐—‹๐—Œ ๐–บ๐—‹๐–พ ๐—๐–พ๐–ผ๐—๐—‡๐—‚๐–ผ๐–บ๐—…๐—…๐—’ ๐–บ๐—…๐—‹๐–พ๐–บ๐–ฝ๐—’ ๐—‚๐–ฝ๐–พ๐—‡๐—๐—‚๐–ฟ๐—‚๐–พ๐–ฝ.

sqrt
๐™ธ๐š–๐š™๐š•๐šŽ๐š–๐šŽ๐š—๐š๐šŠ๐š๐š’๐š˜๐š—๐šœ:
rust
csharp

Sieve of Eratosthenes

๐–ณ๐—๐–พ ๐–ฒ๐—‚๐–พ๐—๐–พ ๐—ˆ๐–ฟ ๐–ค๐—‹๐–บ๐—๐—ˆ๐—Œ๐—๐—๐–พ๐—‡๐–พ๐—Œ ๐—‚๐—Œ ๐–บ๐—‡ ๐–บ๐—‡๐–ผ๐—‚๐–พ๐—‡๐— ๐–บ๐—…๐—€๐—ˆ๐—‹๐—‚๐—๐—๐—† ๐—Ž๐—Œ๐–พ๐–ฝ ๐—๐—ˆ ๐–ฟ๐—‚๐—‡๐–ฝ ๐–บ๐—…๐—… ๐—‰๐—‹๐—‚๐—†๐–พ ๐—‡๐—Ž๐—†๐–ป๐–พ๐—‹๐—Œ ๐—Ž๐—‰ ๐—๐—ˆ ๐–บ ๐—Œ๐—‰๐–พ๐–ผ๐—‚๐–ฟ๐—‚๐–พ๐–ฝ ๐—‚๐—‡๐—๐–พ๐—€๐–พ๐—‹. ๐–จ๐— ๐—‚๐—Œ ๐–บ๐—‡ ๐–พ๐–ฟ๐–ฟ๐—‚๐–ผ๐—‚๐–พ๐—‡๐— ๐–บ๐—‡๐–ฝ ๐—Œ๐—๐—‹๐–บ๐—‚๐—€๐—๐—๐–ฟ๐—ˆ๐—‹๐—๐–บ๐—‹๐–ฝ ๐—†๐–พ๐—๐—๐—ˆ๐–ฝ, ๐–พ๐—Œ๐—‰๐–พ๐–ผ๐—‚๐–บ๐—…๐—…๐—’ ๐—Ž๐—Œ๐–พ๐–ฟ๐—Ž๐—… ๐–ฟ๐—ˆ๐—‹ ๐–ฝ๐–พ๐–บ๐—…๐—‚๐—‡๐—€ ๐—๐—‚๐—๐— ๐—…๐–บ๐—‹๐—€๐–พ ๐—Š๐—Ž๐–บ๐—‡๐—๐—‚๐—๐—‚๐–พ๐—Œ ๐—ˆ๐–ฟ ๐—‡๐—Ž๐—†๐–ป๐–พ๐—‹๐—Œ.
Here's how it works:

๐–ถ๐–พ ๐—๐—‚๐—…๐—… ๐—๐—‹๐—’ ๐—๐—ˆ ๐–ฟ๐—‚๐—‡๐–ฝ ๐–บ๐—…๐—… ๐—‰๐—‹๐—‚๐—†๐–พ ๐—‡๐—Ž๐—†๐–ป๐–พ๐—‹๐—Œ ๐—Ž๐—‰ ๐—๐—ˆ ๐–บ๐—‡ ๐—‚๐—‡๐—๐–พ๐—€๐–พ๐—‹ ๐–ญ.
๐–ณ๐—๐–พ ๐–บ๐—…๐—€๐—ˆ๐—‹๐—‚๐—๐—๐—† ๐—๐—ˆ๐—‹๐—„๐—Œ ๐–ป๐—’ ๐—‚๐—๐–พ๐—‹๐–บ๐—๐—‚๐—๐–พ๐—…๐—’ ๐—†๐–บ๐—‹๐—„๐—‚๐—‡๐—€ ๐—๐—๐–พ ๐—†๐—Ž๐—…๐—๐—‚๐—‰๐—…๐–พ๐—Œ ๐—ˆ๐–ฟ ๐–พ๐–บ๐–ผ๐— ๐—‰๐—‹๐—‚๐—†๐–พ ๐—‡๐—Ž๐—†๐–ป๐–พ๐—‹ ๐—Œ๐—๐–บ๐—‹๐—๐—‚๐—‡๐—€ ๐–ฟ๐—‹๐—ˆ๐—† ๐—๐—๐–พ ๐–ฟ๐—‚๐—‹๐—Œ๐— ๐—‰๐—‹๐—‚๐—†๐–พ ๐—‡๐—Ž๐—†๐–ป๐–พ๐—‹, ๐Ÿค.

  • ๐–ณ๐—ˆ ๐—Œ๐—๐–บ๐—‹๐—, ๐—๐—๐—‚๐—Œ ๐–บ๐—…๐—€๐—ˆ๐—‹๐—‚๐—๐—๐—† ๐–ผ๐—‹๐–พ๐–บ๐—๐–พ๐—Œ ๐–ป๐—ˆ๐—ˆ๐—…๐–พ๐–บ๐—‡ ๐–บ๐—‹๐—‹๐–บ๐—’ ๐—๐—๐–บ๐— ๐—‚๐—Œ ๐—๐—๐–พ ๐—Œ๐—‚๐—“๐–พ ๐—ˆ๐–ฟ ๐—†๐–บ๐—‘ ๐—‡๐—Ž๐—†๐–ป๐–พ๐—‹ ๐—๐—๐–บ๐— ๐—๐–พ ๐–บ๐—‹๐–พ ๐–ผ๐—๐–พ๐–ผ๐—„๐—‚๐—‡๐—€. ๐–ฒ๐—ˆ ๐—‚๐–ฟ ๐—๐–พ ๐–ผ๐—๐–พ๐–ผ๐—„ ๐–บ๐—…๐—… ๐—‰๐—‹๐—‚๐—†๐–พ๐—Œ ๐—Ž๐—‰ ๐—๐—ˆ ๐Ÿฅ๐Ÿข, ๐—๐—๐–พ ๐–บ๐—‹๐—‹๐–บ๐—’ ๐—๐–บ๐—Œ ๐—…๐–พ๐—‡๐—€๐—๐— ๐Ÿฅ๐Ÿข ๐–บ๐—‡๐–ฝ ๐—‚๐—‡๐—‚๐—๐—‚๐–บ๐—…๐—…๐—’ ๐–บ๐—…๐—… ๐—๐–บ๐—…๐—Ž๐–พ๐—Œ ๐–บ๐—‹๐–พ ๐—๐—‹๐—Ž๐–พ
  • ๐–ถ๐–พ ๐—Œ๐–พ๐— ๐Ÿข ๐–บ๐—‡๐–ฝ ๐Ÿฃ ๐—๐—ˆ ๐–ฟ๐–บ๐—…๐—Œ๐–พ (๐—‡๐—ˆ๐— ๐—‰๐—‹๐—‚๐—†๐–พ๐—Œ)
  • ๐–ญ๐–พ๐—‘๐— ๐—๐–พ ๐—Œ๐—๐–บ๐—‹๐— ๐—‚๐—๐–พ๐—‹๐–บ๐—๐—‚๐—‡๐—€ ๐—ˆ๐—๐–พ๐—‹ ๐–บ๐—…๐—… ๐—‚๐—‡๐–ฝ๐–พ๐—‘๐–พ๐—Œ ๐—ˆ๐–ฟ ๐—๐—๐–พ ๐–บ๐—‹๐—‹๐–บ๐—’ ๐—Ž๐—‰ ๐—๐—ˆ ๐—Œ๐—Š๐—Ž๐–บ๐—‹๐–พ ๐—‹๐—ˆ๐—ˆ๐— ๐—ˆ๐–ฟ ๐–ญ, ๐—Œ๐—๐–บ๐—‹๐—๐—‚๐—‡๐—€ ๐–ฟ๐—‹๐—ˆ๐—† ๐Ÿค
  • ๐–จ๐–ฟ ๐—๐—๐–พ ๐–ผ๐—Ž๐—‹๐—‹๐–พ๐—‡๐— ๐–ผ๐–พ๐—…๐—… ๐—‚๐—Œ ๐—๐—‹๐—Ž๐–พ, ๐—๐–พ ๐–ฟ๐—ˆ๐—Ž๐—‡๐–ฝ ๐–บ ๐—‰๐—‹๐—‚๐—†๐–พ
    • ๐—๐–พ ๐—†๐–บ๐—‹๐—„ ๐–บ๐—…๐—… ๐—‡๐—Ž๐—†๐–ป๐–พ๐—‹๐—Œ(๐–ผ๐–พ๐—…๐—…๐—Œ ๐–ป๐—’ ๐—‚๐—‡๐–ฝ๐–พ๐—‘) ๐—๐—๐–บ๐— ๐–บ๐—‹๐–พ ๐—†๐—Ž๐—…๐—๐—‚๐—‰๐—…๐–พ ๐—ˆ๐–ฟ ๐—๐—๐–พ ๐–ผ๐—Ž๐—‹๐—‹๐–พ๐—‡๐— ๐—๐–บ๐—…๐—Ž๐–พ ๐—๐—ˆ ๐–ฟ๐–บ๐—…๐—Œ๐–พ
  • ๐–ณ๐—๐–พ๐—‡ ๐—†๐—ˆ๐—๐–พ ๐—๐—ˆ ๐—๐—๐–พ ๐—‡๐–พ๐—‘๐— ๐—‚๐—‡๐–ฝ๐–พ๐—‘

๐–ฅ๐—ˆ๐—‹ ๐–พ๐—‘๐–บ๐—†๐—‰๐—…๐–พ:
๐–จ๐–ฟ ๐—๐–พ ๐—๐–บ๐—‡๐— ๐—๐—ˆ ๐–ฟ๐—‚๐—‡๐–ฝ ๐—๐—๐–พ ๐—‰๐—‹๐—‚๐—†๐–พ ๐—‡๐—Ž๐—†๐–ป๐–พ๐—‹๐—Œ ๐—Ž๐—‰ ๐—๐—ˆ ๐Ÿฃ๐Ÿข:

  • ๐–ฒ๐—๐–บ๐—‹๐— ๐—๐—‚๐—๐— ๐Ÿค. ๐–ฌ๐–บ๐—‹๐—„ ๐–บ๐—…๐—… ๐—†๐—Ž๐—…๐—๐—‚๐—‰๐—…๐–พ๐—Œ ๐—ˆ๐–ฟ ๐Ÿค ๐—๐—ˆ ๐–ฟ๐–บ๐—…๐—Œ๐–พ (๐Ÿฆ, ๐Ÿจ, ๐Ÿช, ๐Ÿฃ๐Ÿข), ๐—…๐–พ๐–บ๐—๐—‚๐—‡๐—€ ๐—‚๐—‡๐–ฝ๐–พ๐—‘๐–พ๐—Œ ๐Ÿค, ๐Ÿฅ, ๐Ÿง, ๐Ÿฉ, ๐Ÿซ ๐–บ๐—Œ ๐—๐—‹๐—Ž๐–พ.
  • ๐–ญ๐–พ๐—‘๐— ๐—‚๐—‡๐–ฝ๐–พ๐—‘ ๐—‚๐—Œ ๐Ÿฅ. ๐–ณ๐—๐–พ ๐–ผ๐–พ๐—…๐—… ๐—๐–บ๐—…๐—Ž๐–พ ๐—‚๐—Œ ๐—๐—‹๐—Ž๐–พ, ๐Ÿฅ ๐—‚๐—Œ ๐–บ ๐—‰๐—‹๐—‚๐—†๐–พ. ๐–ฌ๐–บ๐—‹๐—„ ๐–บ๐—…๐—… ๐—†๐—Ž๐—…๐—๐—‚๐—‰๐—…๐–พ๐—Œ ๐—ˆ๐–ฟ ๐Ÿฅ (๐Ÿซ), ๐—…๐–พ๐–บ๐—๐—‚๐—‡๐—€ ๐Ÿค, ๐Ÿฅ, ๐Ÿง, ๐Ÿฉ ๐–บ๐—Œ ๐—๐—‹๐—Ž๐–พ.
  • ๐–ญ๐–พ๐—‘๐— ๐—‚๐—‡๐–ฝ๐–พ๐—‘ ๐—‚๐—Œ ๐Ÿฆ. ๐–ณ๐—๐–พ ๐–ผ๐–พ๐—…๐—… ๐—๐–บ๐—…๐—Ž๐–พ ๐—ˆ๐–ฟ ๐Ÿฆ ๐—‚๐—Œ ๐–ฟ๐–บ๐—…๐—Œ๐–พ. ๐–ฌ๐—ˆ๐—๐–พ ๐—ˆ๐—‡.
  • ๐–ญ๐–พ๐—‘๐— ๐—‚๐—‡๐–ฝ๐–พ๐—‘ ๐—‚๐—Œ ๐Ÿง, ๐—๐—๐—‚๐–ผ๐— ๐—‚๐—Œ ๐—€๐—‹๐–พ๐–บ๐—๐–พ๐—‹ ๐—๐—๐–บ๐—‡ ๐—Œ๐—Š๐—‹๐—_๐Ÿฃ๐Ÿข. ๐–ณ๐—๐–พ ๐–บ๐—…๐—€๐—ˆ๐—‹๐—‚๐—๐—๐—† ๐—Œ๐—๐—ˆ๐—‰๐—Œ.
  • ๐–ณ๐—๐–พ ๐—‰๐—‹๐—‚๐—†๐–พ๐—Œ ๐—Ž๐—‰ ๐—๐—ˆ ๐Ÿฃ๐Ÿข ๐–บ๐—‹๐–พ: ๐Ÿค, ๐Ÿฅ, ๐Ÿง, ๐Ÿฉ.

Important

๐–ถ๐—๐—’ ๐—๐–พ ๐–ผ๐—๐–พ๐–ผ๐—„ ๐—ˆ๐—‡๐—…๐—’ ๐—๐—ˆ ๐—Œ๐—Š๐—Ž๐–บ๐—‹๐–พ ๐—‹๐—ˆ๐—ˆ๐— ๐—ˆ๐–ฟ ๐—๐—๐–พ ๐–ผ๐—Ž๐—‹๐—‹๐–พ๐—‡๐— ๐—‡๐—Ž๐—†๐–ป๐–พ๐—‹?
Learn here!

๐™ธ๐š–๐š™๐š•๐šŽ๐š–๐šŽ๐š—๐š๐šŠ๐š๐š’๐š˜๐š—๐šœ:
rust
csharp

Dijkstra Primes

๐–ฃ๐—‚๐—ƒ๐—„๐—Œ๐—๐—‹๐–บ'๐—Œ ๐–ฏ๐—‹๐—‚๐—†๐–พ๐—Œ ๐—‚๐—Œ ๐—ˆ๐—‡๐–พ ๐—ˆ๐–ฟ ๐—๐—๐–พ ๐—†๐—ˆ๐—Œ๐— ๐—Ž๐—‡๐—„๐—‡๐—ˆ๐—๐—‡ ๐–บ๐—…๐—€๐—ˆ๐—‹๐—‚๐—๐—๐—†๐—Œ. ๐–ก๐—Ž๐—‚๐—…๐–ฝ๐—‚๐—‡๐—€ ๐—ˆ๐–ฟ ๐—๐—๐–พ ๐–ฟ๐—ˆ๐—Ž๐—‡๐–ฝ๐–บ๐—๐—‚๐—ˆ๐—‡ ๐—ˆ๐–ฟ ๐—๐—๐–พ ๐—๐—‹๐–บ๐–ฝ๐—‚๐—๐—‚๐—ˆ๐—‡๐–บ๐—… ๐—‰๐—‹๐—‚๐—†๐–พ ๐–ฟ๐—‚๐—‡๐–ฝ๐—‚๐—‡๐—€ ๐–บ๐—…๐—€๐—ˆ๐—‹๐—‚๐—๐—๐—†๐—Œ, ๐–ฃ๐—‚๐—ƒ๐—„๐—Œ๐—๐—‹๐–บ ๐–ฝ๐–พ๐–ผ๐—‚๐–ฝ๐–พ๐–ฝ ๐—๐—ˆ ๐—๐–พ๐—‡๐—๐—Ž๐—‹๐–พ ๐–ป๐–พ๐—’๐—ˆ๐—‡๐–ฝ ๐—๐—๐–พ๐—Œ๐–พ ๐–ผ๐—ˆ๐—‡๐—๐–พ๐—‡๐—๐—‚๐—ˆ๐—‡๐–บ๐—… ๐—‰๐–บ๐—๐—๐—Œ. Here's how it works:

๐–ฃ๐—‚๐—ƒ๐—„๐—Œ๐—๐—‹๐–บ'๐—Œ ๐–บ๐—‰๐—‰๐—‹๐—ˆ๐–บ๐–ผ๐— ๐—‚๐—‡๐–ผ๐—…๐—Ž๐–ฝ๐–พ ๐—๐—๐—ˆ ๐—Œ๐—๐—‹๐—Ž๐–ผ๐—๐—Ž๐—‹๐–พ๐—Œ. ๐–  ๐—‰๐—ˆ๐—ˆ๐—… ๐–บ๐—‡๐–ฝ ๐–บ ๐—…๐—‚๐—Œ๐— ๐—ˆ๐–ฟ ๐—‰๐—‹๐—‚๐—†๐–พ๐—Œ.

  • ๐–ถ๐–พ ๐—Œ๐—„๐—‚๐—‰ ๐Ÿฃ ๐–บ๐—‡๐–ฝ ๐—€๐—ˆ ๐—๐—ˆ ๐Ÿค. ๐–ถ๐–พ ๐–บ๐—Œ๐—Œ๐—Ž๐—†๐–พ ๐—๐—๐–บ๐— ๐Ÿค ๐—‚๐—Œ ๐—‰๐—‹๐—‚๐—†๐–พ ๐–บ๐—‡๐–ฝ ๐–บ๐–ฝ๐–ฝ ๐—‚๐— ๐—๐—ˆ ๐—๐—๐–พ ๐—…๐—‚๐—Œ๐— ๐—ˆ๐–ฟ ๐—‰๐—‹๐—‚๐—†๐–พ๐—Œ.
    ๐–ญ๐—ˆ๐—, ๐—ˆ๐—‡๐–ผ๐–พ ๐—๐–พ ๐–บ๐–ฝ๐–ฝ๐–พ๐–ฝ ๐–บ ๐—‰๐—‹๐—‚๐—†๐–พ ๐—‡๐—Ž๐—†๐–ป๐–พ๐—‹ ๐—๐—ˆ ๐—๐—๐–พ ๐—…๐—‚๐—Œ๐— ๐—๐–พ ๐–บ๐—…๐—Œ๐—ˆ ๐—๐–พ๐–พ๐–ฝ ๐—๐—ˆ ๐–บ๐–ฝ๐–ฝ ๐—๐—๐–พ ๐—Œ๐–บ๐—†๐–พ ๐—‡๐—Ž๐—†๐–ป๐–พ๐—‹ ๐—๐—ˆ ๐—๐—๐–พ ๐—‰๐—ˆ๐—ˆ๐—… ๐–บ๐—…๐—ˆ๐—‡๐—€ ๐—๐—‚๐—๐— ๐—๐—๐–พ ๐—Œ๐—Š๐—Ž๐–บ๐—‹๐–พ ๐—ˆ๐–ฟ ๐—๐—๐–พ ๐—‡๐—Ž๐—†๐–ป๐–พ๐—‹ ๐–บ๐—Œ ๐–บ ๐—๐—Ž๐—‰๐—…๐–พ.

    primes

    ๐–จ๐—‡ ๐—๐—๐–พ ๐—‰๐—ˆ๐—ˆ๐—…, ๐—๐—๐–พ ๐–ฟ๐—‚๐—‹๐—Œ๐— ๐—‹๐—ˆ๐— ๐—‚๐—Œ ๐—๐—๐–พ ๐—‰๐—‹๐—‚๐—†๐–พ ๐–บ๐—‡๐–ฝ ๐—๐—๐–พ ๐—Œ๐–พ๐–ผ๐—ˆ๐—‡๐–ฝ ๐—‹๐—ˆ๐— ๐—๐—๐–พ ๐—†๐—Ž๐—…๐—๐—‚๐—‰๐—…๐–พ ๐–บ๐—Œ๐—Œ๐—ˆ๐–ผ๐—‚๐–บ๐—๐–พ๐–ฝ ๐—๐—‚๐—๐— ๐—๐—๐–บ๐— ๐—‰๐—‹๐—‚๐—†๐–พ.
    ๐–ฒ๐—ˆ ๐—‚๐—‡ ๐—๐—๐—‚๐—Œ ๐–ผ๐–บ๐—Œ๐–พ, ๐—‚๐—‡ ๐—๐—๐–พ ๐—‰๐—ˆ๐—ˆ๐—… ๐—๐–พ ๐—๐–บ๐—๐–พ 2 ๐–บ๐—‡๐–ฝ ๐—†๐—Ž๐—…๐—๐—‚๐—‰๐—…๐–พ: ๐Ÿฆ.

  • ๐–ญ๐–พ๐—‘๐— ๐—๐–พ ๐—€๐—ˆ ๐—๐—ˆ ๐Ÿฅ ๐–บ๐—‡๐–ฝ ๐—๐—๐–บ๐— ๐—๐–พ ๐—๐–บ๐—‡๐— ๐—๐—ˆ ๐–ฟ๐—‚๐—‡๐–ฝ ๐—‚๐—Œ ๐—๐—๐–พ ๐—Œ๐—†๐–บ๐—…๐—…๐–พ๐—Œ๐— ๐—†๐—Ž๐—…๐—๐—‚๐—‰๐—…๐–พ ๐—ˆ๐–ฟ ๐—๐—๐–พ ๐—‰๐—ˆ๐—ˆ๐—…. ๐–จ๐—‡ ๐—๐—๐–พ ๐–ผ๐—Ž๐—‹๐—‹๐–พ๐—‡๐— ๐—‰๐—ˆ๐—ˆ๐—… ๐—๐—๐—‚๐—Œ ๐—‚๐—Œ ๐Ÿฆ.
    ๐–จ๐–ฟ ๐—๐—๐–พ ๐—‡๐—Ž๐—†๐–ป๐–พ๐—‹ ๐—๐—๐–บ๐— ๐—๐–พ ๐–บ๐—‹๐–พ ๐–ผ๐—Ž๐—‹๐—‹๐–พ๐—‡๐—๐—…๐—’ ๐—ˆ๐—‡ ๐—‚๐—Œ ๐—Œ๐—†๐–บ๐—…๐—…๐–พ๐—‹ ๐—๐—๐–บ๐—‡ ๐—๐—๐–พ ๐—Œ๐—†๐–บ๐—…๐—…๐–พ๐—Œ๐— ๐—‰๐—ˆ๐—ˆ๐—… ๐—†๐—Ž๐—…๐—๐—‚๐—‰๐—…๐–พ (๐—‚๐—‡ ๐—๐—๐—‚๐—Œ ๐–ผ๐–บ๐—Œ๐–พ ๐—‚๐— ๐—‚๐—Œ -> ๐Ÿฅ < ๐Ÿฆ), ๐—๐—๐–พ๐—‡ ๐—๐–พ ๐–บ๐–ฝ๐–ฝ ๐—๐—๐—‚๐—Œ ๐—‡๐—Ž๐—†๐–ป๐–พ๐—‹ ๐–บ๐—Œ ๐–บ ๐—‰๐—‹๐—‚๐—†๐–พ ๐–บ๐—‡๐–ฝ ๐–บ๐—€๐–บ๐—‚๐—‡ ๐–บ๐–ฝ๐–ฝ ๐—๐—๐–พ ๐—Œ๐–บ๐—†๐–พ ๐—‡๐—Ž๐—†๐–ป๐–พ๐—‹ ๐—๐—ˆ ๐—๐—๐–พ ๐—‰๐—ˆ๐—ˆ๐—… ๐–บ๐—…๐—ˆ๐—‡๐—€ ๐—๐—‚๐—๐— ๐—๐—๐–พ ๐—Œ๐—Š๐—Ž๐–บ๐—‹๐–พ ๐—ˆ๐–ฟ ๐—๐—๐–พ ๐—‡๐—Ž๐—†๐–ป๐–พ๐—‹ ๐–บ๐—Œ ๐–บ ๐—๐—Ž๐—‰๐—…๐–พ.

    primes

    ๐–ญ๐–พ๐—‘๐— ๐—๐–พ ๐—€๐—ˆ ๐—๐—ˆ ๐Ÿฆ. ๐– ๐—€๐–บ๐—‚๐—‡ ๐—๐–พ ๐–ฟ๐—‚๐—‡๐–ฝ ๐—๐—๐–พ ๐—Œ๐—†๐–บ๐—…๐—…๐–พ๐—Œ๐— ๐—†๐—Ž๐—…๐—๐—‚๐—‰๐—…๐–พ ๐—ˆ๐–ฟ ๐—๐—๐–พ ๐—‰๐—ˆ๐—ˆ๐—… ๐—๐—๐—‚๐–ผ๐— ๐—‚๐—Œ ๐Ÿฆ and ๐—๐–พ ๐–ผ๐—๐–พ๐–ผ๐—„ ๐—‚๐–ฟ ๐—๐—๐–พ ๐—‡๐—Ž๐—†๐–ป๐–พ๐—‹ ๐—๐—๐–บ๐— ๐—๐–พ ๐–บ๐—‹๐–พ ๐–ผ๐—Ž๐—‹๐—‹๐–พ๐—‡๐—๐—…๐—’ ๐—ˆ๐—‡ ๐—‚๐—Œ ๐—…๐–พ๐—Œ๐—Œ ๐—๐—๐–บ๐—‡ ๐—๐—๐–พ ๐—†๐—Ž๐—…๐—๐—‚๐—‰๐—…๐–พ.
    ๐–จ๐—‡ ๐—๐—๐—‚๐—Œ ๐–ผ๐–บ๐—Œ๐–พ ๐—๐—๐–พ๐—’ ๐–บ๐—‹๐–พ ๐–พ๐—Š๐—Ž๐–บ๐—… (๐Ÿฆ == ๐Ÿฆ) ๐—๐—๐—‚๐–ผ๐— ๐—†๐–พ๐–บ๐—‡๐—Œ ๐—๐—๐–บ๐— ๐—๐—๐–พ ๐–ผ๐—Ž๐—‹๐—‹๐–พ๐—‡๐— ๐—‡๐—Ž๐—†๐–ป๐–พ๐—‹ ๐—‚๐—Œ ๐—†๐—Ž๐—…๐—๐—‚๐—‰๐—…๐–พ ๐—ˆ๐–ฟ ๐–บ ๐—‰๐—‹๐—‚๐—†๐–พ.
    ๐–ฒ๐—ˆ ๐—‚๐—‡๐—Œ๐—๐–พ๐–บ๐–ฝ ๐—๐–พ ๐—‡๐–พ๐–พ๐–ฝ ๐—๐—ˆ ๐—‚๐—‡๐–ผ๐—‹๐–พ๐—†๐–พ๐—‡๐— ๐—ˆ๐—Ž๐—‹ ๐—Œ๐—†๐–บ๐—…๐—…๐–พ๐—Œ๐— ๐—†๐—Ž๐—…๐—๐—‚๐—‰๐—…๐–พ ๐–ป๐—’ ๐—๐—๐–พ ๐—‰๐—‹๐—‚๐—†๐–พ ๐—‡๐—Ž๐—†๐–ป๐–พ๐—‹ ๐—‚๐—Œ ๐–บ๐—Œ๐—Œ๐—ˆ๐–ผ๐—‚๐–บ๐—๐–พ๐–ฝ ๐—๐—‚๐—๐— - ๐Ÿค/๐Ÿฆ ๐–ป๐–พ๐–ผ๐—ˆ๐—†๐–พ๐—Œ ๐Ÿค/๐Ÿจ.

    primes
  • ๐–ญ๐–พ๐—‘๐— ๐—๐–พ ๐—€๐—ˆ ๐—๐—ˆ ๐Ÿง ๐–บ๐—‡๐–ฝ ๐–บ๐—€๐–บ๐—‚๐—‡ ๐–ฟ๐—‚๐—‡๐–ฝ ๐—‚๐—Œ ๐—๐—๐–พ ๐—Œ๐—†๐–บ๐—…๐—…๐–พ๐—Œ๐— ๐—†๐—Ž๐—…๐—๐—‚๐—‰๐—…๐–พ ๐—ˆ๐–ฟ ๐—๐—๐–พ ๐—‰๐—ˆ๐—ˆ๐—…. ๐–จ๐—‡ ๐—๐—๐–พ ๐–ผ๐—Ž๐—‹๐—‹๐–พ๐—‡๐— ๐—‰๐—ˆ๐—ˆ๐—… ๐—๐—๐—‚๐—Œ ๐—‚๐—Œ ๐Ÿจ.
    ๐Ÿง ๐—‚๐—Œ ๐—…๐–พ๐—Œ๐—Œ ๐—๐—๐–บ๐—‡ ๐Ÿจ ๐—Œ๐—ˆ ๐—๐–พ ๐–บ๐–ฝ๐–ฝ๐–พ๐–ฝ ๐—‚๐— ๐–บ๐—Œ ๐–บ ๐—‰๐—‹๐—‚๐—†๐–พ ๐—๐—ˆ ๐—๐—๐–พ ๐—‰๐—‹๐—‚๐—†๐–พ๐—Œ ๐—…๐—‚๐—Œ๐— ๐–บ๐—…๐—ˆ๐—‡๐—€ ๐—๐—‚๐—๐— ๐—๐—๐–พ ๐—Œ๐—Š๐—Ž๐–บ๐—‹๐–พ ๐—ˆ๐–ฟ ๐—๐—๐–พ ๐—‡๐—Ž๐—†๐–ป๐–พ๐—‹ ๐–บ๐—Œ ๐–บ ๐—๐—Ž๐—‰๐—…๐–พ (๐Ÿง/๐Ÿค๐Ÿง).

    primes
  • ๐– ๐—‡ ๐—‚๐—‡๐—๐–พ๐—‹๐–พ๐—Œ๐—๐—‚๐—‡๐—€ ๐–ผ๐–บ๐—Œ๐–พ ๐—ˆ๐–ผ๐–ผ๐—Ž๐—‹๐—Œ ๐—๐—๐–พ๐—‡ ๐—๐–พ ๐—€๐–พ๐— ๐—๐—ˆ ๐Ÿฃ๐Ÿค.
    ๐–ฅ๐—ˆ๐—…๐—…๐—ˆ๐—๐—‚๐—‡๐—€ ๐—๐—๐—‚๐—Œ ๐—‰๐–บ๐—๐—๐–พ๐—‹๐—‡, ๐–พ๐—๐–พ๐—‹๐—’ ๐—๐—‚๐—†๐–พ ๐—๐—๐–พ๐—‡ ๐—๐—๐–พ ๐—†๐—Ž๐—…๐—๐—‚๐—‰๐—…๐–พ๐—Œ ๐—ˆ๐–ฟ ๐Ÿค ๐–บ๐—‡๐–ฝ ๐Ÿฅ ๐–บ๐—‹๐–พ ๐—‚๐—‡๐–ผ๐—‹๐–พ๐—†๐–พ๐—‡๐—๐–พ๐–ฝ, ๐–บ๐–ฟ๐—๐–พ๐—‹ ๐Ÿฃ๐Ÿฃ, ๐–ป๐—ˆ๐—๐— ๐Ÿค ๐–บ๐—‡๐–ฝ ๐Ÿฅ ๐–บ๐—‹๐–พ ๐—๐—‚๐—๐— ๐—†๐—Ž๐—…๐—๐—‚๐—‰๐—…๐–พ ๐Ÿฃ๐Ÿค:

    primes

    ๐–จ๐—‡ ๐—๐—๐—‚๐—Œ ๐–ผ๐–บ๐—Œ๐–พ, we ๐—‚๐—‡๐–ผ๐—‹๐–พ๐—†๐–พ๐—‡๐— ๐—ˆ๐—Ž๐—‹ ๐—Œ๐—†๐–บ๐—…๐—…๐–พ๐—Œ๐— ๐—†๐—Ž๐—…๐—๐—‚๐—‰๐—…๐–พ ๐–ป๐—’ ๐—๐—๐–พ both ๐—‰๐—‹๐—‚๐—†๐–พ ๐—‡๐—Ž๐—†๐–ป๐–พ๐—‹s (๐Ÿค/12 ๐–ป๐–พ๐–ผ๐—ˆ๐—†๐–พ๐—Œ ๐Ÿค/14 and 3/12 ๐–ป๐–พ๐–ผ๐—ˆ๐—†๐–พ๐—Œ 3/15)

    primes
  • ๐–ณ๐—๐—‚๐—Œ ๐—‰๐–บ๐—๐—๐–พ๐—‹๐—‡ ๐—๐—‚๐—…๐—… ๐—ƒ๐—Ž๐—Œ๐— ๐–ผ๐—ˆ๐—‡๐—๐—‚๐—‡๐—Ž๐–พ ๐—ˆ๐—‡๐—๐–บ๐—‹๐–ฝ.


Comparing the Dijkstra's Primes algorithm to Trial Division and Sieve of Eratosthenes

๐–ถ๐—๐—‚๐—…๐–พ ๐–ณ๐—‹๐—‚๐–บ๐—… ๐–ฃ๐—‚๐—๐—‚๐—Œ๐—‚๐—ˆ๐—‡ ๐—‚๐—Œ ๐—๐–พ๐—‹๐—’ ๐—Œ๐—‰๐–บ๐–ผ๐–พ ๐–พ๐–ฟ๐–ฟ๐—‚๐–ผ๐—‚๐–พ๐—‡๐—, ๐—‚๐—'๐—Œ ๐—‡๐—ˆ๐— ๐—๐–พ๐—‹๐—’ ๐—๐—‚๐—†๐–พ ๐–พ๐–ฟ๐–ฟ๐—‚๐–ผ๐—‚๐–พ๐—‡๐—. ๐– ๐–ผ๐—๐—Ž๐–บ๐—…๐—…๐—’ ๐—‚๐—'๐—Œ ๐—๐—๐–พ ๐—Œ๐—…๐—ˆ๐—๐–พ๐—Œ๐— ๐—ˆ๐–ฟ ๐—๐—๐–พ ๐—๐—๐—‹๐–พ๐–พ.
๐–ฒ๐—‚๐–พ๐—๐–พ ๐—ˆ๐–ฟ ๐–ค๐—‹๐–บ๐—๐—ˆ๐—Œ๐—๐—๐–พ๐—‡๐–พ๐—Œ ๐—‚๐—Œ ๐—๐—๐–พ ๐—ˆ๐—‰๐—‰๐—ˆ๐—Œ๐—‚๐—๐–พ. ๐–จ๐—'๐—Œ ๐—๐–พ๐—‹๐—’ ๐—๐—‚๐—†๐–พ ๐–พ๐–ฟ๐–ฟ๐—‚๐–ผ๐—‚๐–พ๐—‡๐—, ๐–ป๐—Ž๐— ๐—๐–พ๐—‹๐—’ ๐—Œ๐—‰๐–บ๐–ผ๐–พ ๐—‚๐—‡๐–พ๐–ฟ๐–ฟ๐—‚๐–ผ๐—‚๐–พ๐—‡๐—.
๐–ฃ๐—‚๐—ƒ๐—„๐—Œ๐—๐—‹๐–บ'๐—Œ ๐–ฏ๐—‹๐—‚๐—†๐–พ๐—Œ ๐—‚๐—Œ ๐—Œ๐—ˆ๐—†๐–พ๐—๐—๐–พ๐—‹๐–พ ๐—‚๐—‡ ๐—๐—๐–พ ๐—†๐—‚๐–ฝ๐–ฝ๐—…๐–พ ๐–ป๐–พ๐—๐—๐–พ๐–พ๐—‡ ๐—๐—๐–พ ๐—๐—๐—ˆ.
๐–จ๐—‡ ๐–ฃ๐—‚๐—ƒ๐—„๐—Œ๐—๐—‹๐–บ ๐–บ๐—…๐—€๐—ˆ๐—‹๐—‚๐—๐—๐—† ๐—๐–พ ๐—ƒ๐—Ž๐—Œ๐— ๐—Ž๐—Œ๐—‚๐—‡๐—€ ๐–บ ๐—Œ๐—‚๐—†๐—‰๐—…๐–พ ๐–บ๐–ฝ๐–ฝ๐—‚๐—๐—‚๐—ˆ๐—‡ ๐—๐—ˆ ๐—„๐–พ๐–พ๐—‰ ๐—๐—‹๐–บ๐–ผ๐—„ ๐—ˆ๐–ฟ ๐—๐—๐–พ ๐—†๐—Ž๐—…๐—๐—‚๐—‰๐—…๐–พ๐—Œ ๐—ˆ๐–ฟ ๐—‰๐—‹๐—‚๐—†๐–พ ๐—‡๐—Ž๐—†๐–ป๐–พ๐—‹๐—Œ, ๐—‡๐—ˆ๐— ๐–ฝ๐—‚๐—๐—‚๐—Œ๐—‚๐—ˆ๐—‡ ๐—…๐—‚๐—„๐–พ ๐—๐–พ ๐–ฝ๐—‚๐–ฝ ๐—‚๐—‡ ๐–ณ๐—‹๐—‚๐–บ๐—… ๐–ฃ๐—‚๐—๐—‚๐—Œ๐—‚๐—ˆ๐—‡.
๐– ๐—…๐—Œ๐—ˆ ๐—๐–พ ๐—Ž๐—Œ๐–พ ๐—Œ๐—†๐–บ๐—…๐—…๐–พ๐—‹ ๐–ฝ๐–บ๐—๐–บ ๐—Œ๐—๐—‹๐—Ž๐–ผ๐—๐—Ž๐—‹๐–พ ๐—๐—ˆ ๐—„๐–พ๐–พ๐—‰ ๐—๐—‹๐–บ๐–ผ๐—„ ๐—ˆ๐–ฟ ๐—๐—๐–พ ๐—†๐—Ž๐—…๐—๐—‚๐—‰๐—…๐–พ๐—Œ ๐–ผ๐—ˆ๐—†๐—‰๐–บ๐—‹๐–พ ๐—๐—๐–บ๐— ๐—๐–พ ๐—Ž๐—Œ๐–พ๐–ฝ ๐—‚๐—‡ ๐—๐—๐–พ ๐–ฒ๐—‚๐–พ๐—๐–พ ๐—ˆ๐–ฟ ๐–ค๐—‹๐–บ๐—๐—ˆ๐—Œ๐—๐—๐–พ๐—‡๐–พ๐—Œ. ๐–จ๐—‡๐—Œ๐—๐–พ๐–บ๐–ฝ ๐—ˆ๐–ฟ ๐—„๐–พ๐–พ๐—‰๐—‚๐—‡๐—€ ๐—๐—‹๐–บ๐–ผ๐—„ ๐—ˆ๐–ฟ ๐–บ๐—…๐—… ๐—๐—๐–พ ๐—†๐—Ž๐—…๐—๐—‚๐—‰๐—…๐–พ๐—Œ ๐–บ๐— ๐—ˆ๐—‡๐–ผ๐–พ, ๐–ฃ๐—‚๐—ƒ๐—„๐—Œ๐—๐—‹๐–บ'๐—Œ ๐—†๐–พ๐—๐—๐—ˆ๐–ฝ ๐—ˆ๐—‡๐—…๐—’ ๐—„๐–พ๐–พ๐—‰๐—Œ ๐—๐—‹๐–บ๐–ผ๐—„ ๐—ˆ๐–ฟ ๐—๐—๐–พ ๐—†๐—Ž๐—…๐—๐—‚๐—‰๐—…๐–พ๐—Œ ๐—‚๐— ๐—‡๐–พ๐–พ๐–ฝ๐—Œ.

primes


๐™ธ๐š–๐š™๐š•๐šŽ๐š–๐šŽ๐š—๐š๐šŠ๐š๐š’๐š˜๐š—๐šœ:
rust
csharp

Data Structures

Tree Theory

๐–  ๐—๐—‹๐–พ๐–พ ๐—‚๐—Œ ๐–บ ๐–ฝ๐–บ๐—๐–บ ๐—Œ๐—๐—‹๐—Ž๐–ผ๐—๐—Ž๐—‹๐–พ ๐–ผ๐—ˆ๐—†๐—‰๐—ˆ๐—Œ๐–พ๐–ฝ ๐—ˆ๐–ฟ ๐—‡๐—ˆ๐–ฝ๐–พ๐—Œ.

  • ๐–ค๐–บ๐–ผ๐— ๐—๐—‹๐–พ๐–พ ๐—๐–บ๐—Œ ๐–บ ๐—‹๐—ˆ๐—ˆ๐— ๐—‡๐—ˆ๐–ฝ๐–พ.
  • ๐–ณ๐—๐–พ ๐—‹๐—ˆ๐—ˆ๐— ๐—‡๐—ˆ๐–ฝ๐–พ ๐—๐–บ๐—Œ ๐—“๐–พ๐—‹๐—ˆ ๐—ˆ๐—‹ ๐—†๐—ˆ๐—‹๐–พ ๐–ผ๐—๐—‚๐—…๐–ฝ ๐—‡๐—ˆ๐–ฝ๐–พ๐—Œ.
  • ๐–ค๐–บ๐–ผ๐— ๐–ผ๐—๐—‚๐—…๐–ฝ ๐—‡๐—ˆ๐–ฝ๐–พ ๐—๐–บ๐—Œ ๐—“๐–พ๐—‹๐—ˆ ๐—ˆ๐—‹ ๐—†๐—ˆ๐—‹๐–พ ๐–ผ๐—๐—‚๐—…๐–ฝ ๐—‡๐—ˆ๐–ฝ๐–พ๐—Œ, ๐–บ๐—‡๐–ฝ ๐—Œ๐—ˆ ๐—ˆ๐—‡.

๐–ณ๐—๐–พ ๐—๐—‹๐–พ๐–พ ๐–ผ๐–บ๐—‡๐—‡๐—ˆ๐— ๐–ผ๐—ˆ๐—‡๐—๐–บ๐—‚๐—‡ ๐–ผ๐—’๐–ผ๐—…๐–พ๐—Œ. ๐–ณ๐—๐–พ ๐—‡๐—ˆ๐–ฝ๐–พ๐—Œ ๐—†๐–บ๐—’ ๐—ˆ๐—‹ ๐—†๐–บ๐—’ ๐—‡๐—ˆ๐— ๐–ป๐–พ ๐—‚๐—‡ ๐–บ ๐—‰๐–บ๐—‹๐—๐—‚๐–ผ๐—Ž๐—…๐–บ๐—‹ ๐—ˆ๐—‹๐–ฝ๐–พ๐—‹ ๐–บ๐—‡๐–ฝ ๐—๐—๐–พ๐—’ ๐–ผ๐—ˆ๐—Ž๐—…๐–ฝ ๐—๐–บ๐—๐–พ ๐–บ๐—‡๐—’ ๐–ฝ๐–บ๐—๐–บ ๐—๐—’๐—‰๐–พ ๐–บ๐—Œ ๐—๐–บ๐—…๐—Ž๐–พ๐—Œ.

Binary Tree vs. Binary Search Tree

๐–  ๐–ป๐—‚๐—‡๐–บ๐—‹๐—’ ๐—Œ๐–พ๐–บ๐—‹๐–ผ๐— ๐—๐—‹๐–พ๐–พ ๐—‚๐—Œ ๐–บ ๐–ป๐—‚๐—‡๐–บ๐—‹๐—’ ๐—๐—‹๐–พ๐–พ ๐—‚๐—‡ ๐—๐—๐—‚๐–ผ๐— ๐–พ๐—๐–พ๐—‹๐—’ ๐—‡๐—ˆ๐–ฝ๐–พ ๐–ฟ๐—‚๐—๐—Œ ๐–บ ๐—Œ๐—‰๐–พ๐–ผ๐—‚๐–ฟ๐—‚๐–ผ ๐—ˆ๐—‹๐–ฝ๐–พ๐—‹๐—‚๐—‡๐—€ ๐—‰๐—‹๐—ˆ๐—‰๐–พ๐—‹๐—๐—’. ๐–ณ๐—๐—‚๐—Œ ๐—†๐—Ž๐—Œ๐— ๐–ป๐–พ ๐—๐—‹๐—Ž๐–พ ๐–ฟ๐—ˆ๐—‹ ๐–พ๐–บ๐–ผ๐— ๐—‡๐—ˆ๐–ฝ๐–พ ๐—‡.

๐–ก๐—‚๐—‡๐–บ๐—‹๐—’ ๐–ฒ๐–พ๐–บ๐—‹๐–ผ๐— ๐–ฏ๐—‹๐—ˆ๐—‰๐–พ๐—‹๐—๐—’:

  • ๐–ฅ๐—ˆ๐—‹ ๐–พ๐—๐–พ๐—‹๐—’ ๐—‡๐—ˆ๐–ฝ๐–พ, ๐–บ๐—…๐—… ๐–พ๐—…๐–พ๐—†๐–พ๐—‡๐—๐—Œ ๐—‚๐—‡ ๐—๐—๐–พ ๐—…๐–พ๐–ฟ๐— ๐—Œ๐—Ž๐–ป๐—๐—‹๐–พ๐–พ ๐–บ๐—‹๐–พ ๐—…๐–พ๐—Œ๐—Œ ๐—๐—๐–บ๐—‡ ๐—๐—๐–พ ๐—‡๐—ˆ๐–ฝ๐–พ'๐—Œ ๐—๐–บ๐—…๐—Ž๐–พ, ๐–บ๐—‡๐–ฝ ๐–บ๐—…๐—… ๐–พ๐—…๐–พ๐—†๐–พ๐—‡๐—๐—Œ ๐—‚๐—‡ ๐—๐—๐–พ ๐—‹๐—‚๐—€๐—๐— ๐—Œ๐—Ž๐–ป๐—๐—‹๐–พ๐–พ ๐–บ๐—‹๐–พ ๐—€๐—‹๐–พ๐–บ๐—๐–พ๐—‹ ๐—๐—๐–บ๐—‡ ๐—๐—๐–พ ๐—‡๐—ˆ๐–ฝ๐–พ'๐—Œ ๐—๐–บ๐—…๐—Ž๐–พ.

๐–ณ๐—๐—‚๐—Œ ๐—‚๐—‡๐–พ๐—Š๐—Ž๐–บ๐—…๐—‚๐—๐—’ ๐—†๐—Ž๐—Œ๐— ๐–ป๐–พ ๐—๐—‹๐—Ž๐–พ ๐–ฟ๐—ˆ๐—‹ ๐–บ๐—…๐—… ๐—ˆ๐–ฟ ๐–บ ๐—‡๐—ˆ๐–ฝ๐–พ'๐—Œ ๐–ฝ๐–พ๐—Œ๐–ผ๐–พ๐—‡๐–ฝ๐–พ๐—‡๐—๐—Œ, ๐—‡๐—ˆ๐— ๐—ƒ๐—Ž๐—Œ๐— ๐—‚๐—๐—Œ ๐—‚๐—†๐—†๐–พ๐–ฝ๐—‚๐–บ๐—๐–พ ๐–ผ๐—๐—‚๐—…๐–ฝ๐—‹๐–พ๐—‡. ๐–ณ๐—๐–พ ๐–ฟ๐—ˆ๐—…๐—…๐—ˆ๐—๐—‚๐—‡๐—€ ๐—๐—‹๐–พ๐–พ ๐—ˆ๐—‡ ๐—๐—๐–พ ๐—…๐–พ๐–ฟ๐— ๐–ป๐–พ๐—…๐—ˆ๐— ๐—‚๐—Œ ๐–บ ๐–ป๐—‚๐—‡๐–บ๐—‹๐—’ ๐—Œ๐–พ๐–บ๐—‹๐–ผ๐— ๐—๐—‹๐–พ๐–พ. ๐–ณ๐—๐–พ ๐—๐—‹๐–พ๐–พ ๐—ˆ๐—‡ ๐—๐—๐–พ ๐—‹๐—‚๐—€๐—๐— ๐—‚๐—Œ ๐—‡๐—ˆ๐—, ๐—Œ๐—‚๐—‡๐–ผ๐–พ ๐Ÿฃ๐Ÿค ๐—‚๐—Œ ๐—๐—ˆ ๐—๐—๐–พ ๐—…๐–พ๐–ฟ๐— ๐—ˆ๐–ฟ ๐Ÿช.

heap heap
๐–ก๐—‚๐—‡๐–บ๐—‹๐—’ ๐—Œ๐–พ๐–บ๐—‹๐–ผ๐— ๐—๐—‹๐–พ๐–พ ๐–ญ๐—ˆ๐— ๐–บ ๐–ป๐—‚๐—‡๐–บ๐—‹๐—’ ๐—Œ๐–พ๐–บ๐—‹๐–ผ๐— ๐—๐—‹๐–พ๐–พ

Balanced vs. Unbalanced

๐–ฎ๐—‡๐–พ ๐—๐–บ๐—’ ๐—๐—ˆ ๐—๐—๐—‚๐—‡๐—„ ๐–บ๐–ป๐—ˆ๐—Ž๐— ๐—‚๐— ๐—‚๐—Œ ๐—๐—๐–บ๐— ๐–บ ๐–ป๐–บ๐—…๐–บ๐—‡๐–ผ๐–พ๐–ฝ ๐—๐—‹๐–พ๐–พ ๐—‹๐–พ๐–บ๐—…๐—…๐—’ ๐—†๐–พ๐–บ๐—‡๐—Œ ๐—Œ๐—ˆ๐—†๐–พ๐—๐—๐—‚๐—‡๐—€ ๐—†๐—ˆ๐—‹๐–พ ๐—…๐—‚๐—„๐–พ "๐—‡๐—ˆ๐— ๐—๐–พ๐—‹๐—‹๐—‚๐–ป๐—…๐—’ ๐—‚๐—†๐–ป๐–บ๐—…๐–บ๐—‡๐–ผ๐–พ๐–ฝ". ๐–จ๐—'๐—Œ ๐–ป๐–บ๐—…๐–บ๐—‡๐–ผ๐–พ๐–ฝ ๐–พ๐—‡๐—ˆ๐—Ž๐—€๐— ๐—๐—ˆ ๐–พ๐—‡๐—Œ๐—Ž๐—‹๐–พ ฮ˜(<i>n</i> log(<i>n</i>)) ๐–ฟ๐—ˆ๐—‹ ๐—‚๐—‡๐—Œ๐–พ๐—‹๐— ๐–บ๐—‡๐–ฝ ๐–ฟ๐—‚๐—‡๐–ฝ, ๐–ป๐—Ž๐— ๐—‚๐—'๐—Œ ๐—‡๐—ˆ๐— ๐—‡๐–พ๐–ผ๐–พ๐—Œ๐—Œ๐–บ๐—‹๐—‚๐—…๐—’ ๐–บ๐—Œ ๐–ป๐–บ๐—…๐–บ๐—‡๐–ผ๐–พ๐–ฝ ๐–บ๐—Œ ๐—‚๐— ๐–ผ๐—ˆ๐—Ž๐—…๐–ฝ ๐–ป๐–พ.

๐–ณ๐—๐—ˆ ๐–ผ๐—ˆ๐—†๐—†๐—ˆ๐—‡ ๐—๐—’๐—‰๐–พ๐—Œ ๐—ˆ๐–ฟ ๐–ป๐–บ๐—…๐–บ๐—‡๐–ผ๐–พ๐–ฝ ๐—๐—‹๐–พ๐–พ๐—Œ ๐–บ๐—‹๐–พ ๐–ฑ๐–พ๐–ฝ-๐–ป๐—…๐–บ๐–ผ๐—„ ๐—๐—‹๐–พ๐–พ ๐–บ๐—‡๐–ฝ ๐– ๐–ต๐–ซ ๐—๐—‹๐–พ๐–พ.

๐–ณ๐—๐–พ ๐—€๐—ˆ๐–บ๐—… ๐—‚๐—Œ ๐—๐—ˆ ๐—„๐–พ๐–พ๐—‰ ๐—๐—๐–พ ๐—๐—‹๐–พ๐–พ ๐–บ๐—Œ ๐–ฟ๐—…๐–บ๐— ๐–บ๐—Œ ๐—‰๐—ˆ๐—Œ๐—Œ๐—‚๐–ป๐—…๐–พ ๐–ป๐—’ ๐–พ๐—๐–พ๐—‡๐—…๐—’ ๐–ฝ๐—‚๐—Œ๐—๐—‹๐—‚๐–ป๐—Ž๐—๐—‚๐—‡๐—€ ๐—‡๐—ˆ๐–ฝ๐–พ๐—Œ ๐—ˆ๐—‡ ๐–ป๐—ˆ๐—๐— ๐—Œ๐—‚๐–ฝ๐–พ๐—Œ, ๐—‰๐—‹๐–พ๐—๐–พ๐—‡๐—๐—‚๐—‡๐—€ ๐—‰๐–พ๐—‹๐–ฟ๐—ˆ๐—‹๐—†๐–บ๐—‡๐–ผ๐–พ ๐–ฝ๐–พ๐—€๐—‹๐–บ๐–ฝ๐–บ๐—๐—‚๐—ˆ๐—‡

  • ๐–ฑ๐–พ๐–ฝ-๐–ป๐—…๐–บ๐–ผ๐—„ ๐—๐—‹๐–พ๐–พ๐—Œ ๐–บ๐—‹๐–พ ๐–ป๐–บ๐—…๐–บ๐—‡๐–ผ๐–พ๐–ฝ ๐–ป๐—‚๐—‡๐–บ๐—‹๐—’ ๐—Œ๐–พ๐–บ๐—‹๐–ผ๐— ๐—๐—‹๐–พ๐–พ๐—Œ ๐—๐—๐–บ๐— ๐—Ž๐—Œ๐–พ๐—Œ ๐–ผ๐—ˆ๐—…๐—ˆ๐—‹๐—Œ (๐—‹๐–พ๐–ฝ ๐–บ๐—‡๐–ฝ ๐–ป๐—…๐–บ๐–ผ๐—„) ๐—๐—ˆ ๐—๐–พ๐—…๐—‰ ๐—„๐–พ๐–พ๐—‰ ๐—๐—๐–พ ๐—๐—‹๐–พ๐–พ ๐–ป๐–บ๐—…๐–บ๐—‡๐–ผ๐–พ๐–ฝ.
    • ๐–ค๐–บ๐–ผ๐— ๐—‡๐—ˆ๐–ฝ๐–พ ๐—‚๐—Œ ๐–พ๐—‚๐—๐—๐–พ๐—‹ ๐—‹๐–พ๐–ฝ ๐—ˆ๐—‹ ๐–ป๐—…๐–บ๐–ผ๐—„.
    • ๐–ณ๐—๐–พ ๐—‹๐—ˆ๐—ˆ๐— ๐—‚๐—Œ ๐–บ๐—…๐—๐–บ๐—’๐—Œ ๐–ป๐—…๐–บ๐–ผ๐—„.
    • ๐–ฑ๐–พ๐–ฝ ๐—‡๐—ˆ๐–ฝ๐–พ๐—Œ ๐–ผ๐–บ๐—‡๐—‡๐—ˆ๐— ๐—๐–บ๐—๐–พ ๐—‹๐–พ๐–ฝ ๐–ผ๐—๐—‚๐—…๐–ฝ๐—‹๐–พ๐—‡.
    • ๐–ค๐—๐–พ๐—‹๐—’ ๐—‰๐–บ๐—๐— ๐–ฟ๐—‹๐—ˆ๐—† ๐–บ ๐—‡๐—ˆ๐–ฝ๐–พ ๐—๐—ˆ ๐—‚๐—๐—Œ ๐—‡๐—Ž๐—…๐—… ๐–ฝ๐–พ๐—Œ๐–ผ๐–พ๐—‡๐–ฝ๐–บ๐—‡๐—๐—Œ ๐–ผ๐—ˆ๐—‡๐—๐–บ๐—‚๐—‡๐—Œ ๐—๐—๐–พ ๐—Œ๐–บ๐—†๐–พ ๐—‡๐—Ž๐—†๐–ป๐–พ๐—‹ ๐—ˆ๐–ฟ ๐–ป๐—…๐–บ๐–ผ๐—„ ๐—‡๐—ˆ๐–ฝ๐–พ๐—Œ.
  • ๐– ๐–ต๐–ซ ๐–ณ๐—‹๐–พ๐–พ๐—Œ ๐–บ๐—‹๐–พ ๐–ป๐–บ๐—…๐–บ๐—‡๐–ผ๐–พ๐–ฝ ๐–ป๐—‚๐—‡๐–บ๐—‹๐—’ ๐—Œ๐–พ๐–บ๐—‹๐–ผ๐— ๐—๐—‹๐–พ๐–พ๐—Œ ๐—๐—๐–บ๐— ๐—†๐–บ๐—‚๐—‡๐—๐–บ๐—‚๐—‡ ๐–ป๐–บ๐—…๐–บ๐—‡๐–ผ๐–พ ๐–ป๐—’ ๐—Œ๐—๐—ˆ๐—‹๐—‚๐—‡๐—€ ๐—๐—๐–พ ๐—๐–พ๐—‚๐—€๐—๐— ๐—ˆ๐–ฟ ๐—๐—๐–พ ๐—Œ๐—Ž๐–ป๐—๐—‹๐–พ๐–พ ๐–บ๐— ๐–พ๐–บ๐–ผ๐— ๐—‡๐—ˆ๐–ฝ๐–พ ๐–บ๐—‡๐–ฝ ๐–พ๐—‡๐—Œ๐—Ž๐—‹๐—‚๐—‡๐—€ ๐—๐—๐–พ ๐—๐–พ๐—‚๐—€๐—๐— ๐–ฝ๐—‚๐–ฟ๐–ฟ๐–พ๐—‹๐–พ๐—‡๐–ผ๐–พ (๐–ป๐–บ๐—…๐–บ๐—‡๐–ผ๐–พ ๐–ฟ๐–บ๐–ผ๐—๐—ˆ๐—‹) ๐–ป๐–พ๐—๐—๐–พ๐–พ๐—‡ ๐—๐—๐–พ ๐—…๐–พ๐–ฟ๐— ๐–บ๐—‡๐–ฝ ๐—‹๐—‚๐—€๐—๐— ๐—Œ๐—Ž๐–ป๐—๐—‹๐–พ๐–พ๐—Œ ๐—‚๐—Œ ๐—‡๐—ˆ ๐—†๐—ˆ๐—‹๐–พ ๐—๐—๐–บ๐—‡ ๐—ˆ๐—‡๐–พ. <๐–ป๐—‹> ๐–จ๐–ฟ ๐—๐—๐—‚๐—Œ ๐–ป๐–บ๐—…๐–บ๐—‡๐–ผ๐–พ ๐–ฟ๐–บ๐–ผ๐—๐—ˆ๐—‹ ๐—‚๐—Œ ๐—๐—‚๐—ˆ๐—…๐–บ๐—๐–พ๐–ฝ ๐–บ๐–ฟ๐—๐–พ๐—‹ ๐–บ๐—‡ ๐—‚๐—‡๐—Œ๐–พ๐—‹๐—๐—‚๐—ˆ๐—‡ ๐—ˆ๐—‹ ๐–ฝ๐–พ๐—…๐–พ๐—๐—‚๐—ˆ๐—‡, ๐—‹๐—ˆ๐—๐–บ๐—๐—‚๐—ˆ๐—‡๐—Œ ๐–บ๐—‹๐–พ ๐—‰๐–พ๐—‹๐–ฟ๐—ˆ๐—‹๐—†๐–พ๐–ฝ ๐—๐—ˆ ๐—‹๐–พ๐—Œ๐—๐—ˆ๐—‹๐–พ ๐–ป๐–บ๐—…๐–บ๐—‡๐–ผ๐–พ.
    • ๐–ก๐–บ๐—…๐–บ๐—‡๐–ผ๐–พ ๐–ฟ๐–บ๐–ผ๐—๐—ˆ๐—‹ ๐—‚๐—Œ ๐—๐—๐–พ ๐–ฝ๐—‚๐–ฟ๐–ฟ๐–พ๐—‹๐–พ๐—‡๐–ผ๐–พ ๐—‚๐—‡ ๐—๐–พ๐—‚๐—€๐—๐— ๐–ป๐–พ๐—๐—๐–พ๐–พ๐—‡ ๐—๐—๐–พ ๐—…๐–พ๐–ฟ๐— ๐–บ๐—‡๐–ฝ ๐—‹๐—‚๐—€๐—๐— ๐—Œ๐—‚๐–ฝ๐–พ๐—Œ ๐—ˆ๐–ฟ ๐–บ ๐—‡๐—ˆ๐–ฝ๐–พ.
      • ๐–จ๐–ฟ ๐—๐—๐–พ ๐—…๐–พ๐–ฟ๐— ๐—Œ๐—‚๐–ฝ๐–พ ๐—‚๐—Œ ๐—๐–บ๐—…๐—…๐–พ๐—‹, ๐—๐—๐–พ ๐–ป๐–บ๐—…๐–บ๐—‡๐–ผ๐–พ ๐–ฟ๐–บ๐–ผ๐—๐—ˆ๐—‹ ๐—‚๐—Œ +๐Ÿฃ
      • ๐–จ๐–ฟ ๐—๐—๐–พ ๐—‹๐—‚๐—€๐—๐— ๐—Œ๐—‚๐–ฝ๐–พ ๐—‚๐—Œ ๐—๐–บ๐—…๐—…๐–พ๐—‹, ๐—๐—๐–พ ๐–ป๐–บ๐—…๐–บ๐—‡๐–ผ๐–พ ๐–ฟ๐–บ๐–ผ๐—๐—ˆ๐—‹ ๐—‚๐—Œ -๐Ÿฃ
      • ๐–จ๐–ฟ ๐–ป๐—ˆ๐—๐— ๐—Œ๐—‚๐–ฝ๐–พ๐—Œ ๐–บ๐—‹๐–พ ๐–พ๐—Š๐—Ž๐–บ๐—…, ๐—๐—๐–พ ๐–ป๐–บ๐—…๐–บ๐—‡๐–ผ๐–พ ๐–ฟ๐–บ๐–ผ๐—๐—ˆ๐—‹ ๐—‚๐—Œ ๐Ÿข

Complete Binary Trees

๐–  ๐–ผ๐—ˆ๐—†๐—‰๐—…๐–พ๐—๐–พ ๐–ป๐—‚๐—‡๐–บ๐—‹๐—’ ๐—๐—‹๐–พ๐–พ ๐—‚๐—Œ ๐–บ ๐–ป๐—‚๐—‡๐–บ๐—‹๐—’ ๐—๐—‹๐–พ๐–พ ๐—‚๐—‡ ๐—๐—๐—‚๐–ผ๐— ๐–พ๐—๐–พ๐—‹๐—’ ๐—…๐–พ๐—๐–พ๐—… ๐—ˆ๐–ฟ ๐—๐—๐–พ ๐—๐—‹๐–พ๐–พ ๐—‚๐—Œ ๐–ฟ๐—Ž๐—…๐—…๐—’ ๐–ฟ๐—‚๐—…๐—…๐–พ๐–ฝ, ๐–พ๐—‘๐–ผ๐–พ๐—‰๐— ๐–ฟ๐—ˆ๐—‹ ๐—‰๐–พ๐—‹๐—๐–บ๐—‰๐—Œ ๐—๐—๐–พ ๐—…๐–บ๐—Œ๐— ๐—…๐–พ๐—๐–พ๐—…. <๐–ป๐—‹> ๐–ณ๐—ˆ ๐—๐—๐–พ ๐–พ๐—‘๐—๐–พ๐—‡๐— ๐—๐—๐–บ๐— ๐—๐—๐–พ ๐—…๐–บ๐—Œ๐— ๐—…๐–พ๐—๐–พ๐—… ๐—‚๐—Œ ๐–ฟ๐—‚๐—…๐—…๐–พ๐–ฝ, ๐—‚๐— ๐—‚๐—Œ ๐–ฟ๐—‚๐—…๐—…๐–พ๐–ฝ ๐—…๐–พ๐–ฟ๐— ๐—๐—ˆ ๐—‹๐—‚๐—€๐—๐—.

heap heap
๐–ข๐—ˆ๐—†๐—‰๐—…๐–พ๐—๐–พ ๐–ป๐—‚๐—‡๐–บ๐—‹๐—’ ๐—๐—‹๐–พ๐–พ ๐–ญ๐—ˆ๐— ๐–บ ๐–ผ๐—ˆ๐—†๐—‰๐—…๐–พ๐—๐–พ ๐–ป๐—‚๐—‡๐–บ๐—‹๐—’ ๐—๐—‹๐–พ๐–พ

Full Binary Trees

๐–  ๐–ฟ๐—Ž๐—…๐—… ๐–ป๐—‚๐—‡๐–บ๐—‹๐—’ ๐—๐—‹๐–พ๐–พ ๐—‚๐—Œ ๐–บ ๐–ป๐—‚๐—‡๐–บ๐—‹๐—’ ๐—๐—‹๐–พ๐–พ ๐—‚๐—‡ ๐—๐—๐—‚๐–ผ๐— ๐–พ๐—๐–พ๐—‹๐—’ ๐—‡๐—ˆ๐–ฝ๐–พ ๐—๐–บ๐—Œ ๐–พ๐—‚๐—๐—๐–พ๐—‹ ๐—“๐–พ๐—‹๐—ˆ ๐—ˆ๐—‹ ๐—๐—๐—ˆ ๐–ผ๐—๐—‚๐—…๐–ฝ๐—‹๐–พ๐—‡. ๐–ณ๐—๐–บ๐— ๐—‚๐—Œ, ๐—‡๐—ˆ ๐—‡๐—ˆ๐–ฝ๐–พ๐—Œ ๐—๐–บ๐—๐–พ ๐—ˆ๐—‡๐—…๐—’ ๐—ˆ๐—‡๐–พ ๐–ผ๐—๐—‚๐—…๐–ฝ.

heap heap
๐–ฅ๐—Ž๐—…๐—… ๐–ป๐—‚๐—‡๐–บ๐—‹๐—’ ๐—๐—‹๐–พ๐–พ ๐–ญ๐—ˆ๐— ๐–บ ๐–ฟ๐—Ž๐—…๐—… ๐–ป๐—‚๐—‡๐–บ๐—‹๐—’ ๐—๐—‹๐–พ๐–พ

Binary Tree Traversal

๐–ณ๐—๐–พ๐—‹๐–พ ๐–บ๐—‹๐–พ ๐—๐—๐—‹๐–พ๐–พ ๐—†๐–บ๐—‚๐—‡ ๐—๐—‹๐–บ๐—๐–พ๐—‹๐—Œ๐–บ๐—… ๐—†๐–พ๐—๐—๐—ˆ๐–ฝ๐—Œ:

  • ๐–จ๐—‡-๐—ˆ๐—‹๐–ฝ๐–พ๐—‹
  • ๐–ฏ๐—‹๐–พ-๐—ˆ๐—‹๐–ฝ๐–พ๐—‹
  • ๐–ฏ๐—ˆ๐—Œ๐—-๐—ˆ๐—‹๐–ฝ๐–พ๐—‹

๐–จ๐—‡-๐–ฎ๐—‹๐–ฝ๐–พ๐—‹: ๐–จ๐—‡-๐—ˆ๐—‹๐–ฝ๐–พ๐—‹ ๐—๐—‹๐–บ๐—๐–พ๐—‹๐—Œ๐–บ๐—… ๐—†๐–พ๐–บ๐—‡๐—Œ ๐—๐—ˆ "๐—๐—‚๐—Œ๐—‚๐—" (๐—ˆ๐–ฟ๐—๐–พ๐—‡, ๐—‰๐—‹๐—‚๐—‡๐—) ๐—๐—๐–พ ๐—…๐–พ๐–ฟ๐— ๐–ป๐—‹๐–บ๐—‡๐–ผ๐—, ๐—๐—๐–พ๐—‡ ๐—๐—๐–พ ๐–ผ๐—Ž๐—‹๐—‹๐–พ๐—‡๐— ๐—‡๐—ˆ๐–ฝ๐–พ, ๐–บ๐—‡๐–ฝ ๐–ฟ๐—‚๐—‡๐–บ๐—…๐—…๐—’, ๐—๐—๐–พ ๐—‹๐—‚๐—€๐—๐— ๐–ป๐—‹๐–บ๐—‡๐–ผ๐—.

fn in_order_traversal(node: TreeNode) {
    if let Some(value) = node.value {
        in_order_traversal(node.left);
        print(value);
        in_order_traversal(node.right);
    }
}

๐–ฏ๐—‹๐–พ-๐–ฎ๐—‹๐–ฝ๐–พ๐—‹: ๐–ฏ๐—‹๐–พ-๐—ˆ๐—‹๐–ฝ๐–พ๐—‹ ๐—๐—‹๐–บ๐—๐–พ๐—‹๐—Œ๐–บ๐—… ๐—๐—‚๐—Œ๐—‚๐—๐—Œ ๐—๐—๐–พ ๐–ผ๐—Ž๐—‹๐—‹๐–พ๐—‡๐— ๐—‡๐—ˆ๐–ฝ๐–พ ๐–ป๐–พ๐–ฟ๐—ˆ๐—‹๐–พ ๐—‚๐—๐—Œ ๐–ผ๐—๐—‚๐—…๐–ฝ ๐—‡๐—ˆ๐–ฝ๐–พ๐—Œ.

fn pre_order_traversal(node: TreeNode) {
    if let Some(value) = node.value {
        print(value);
        pre_order_traversal(node.left);
        pre_order_traversal(node.right);
    }
}

๐–ฏ๐—ˆ๐—Œ๐—-๐–ฎ๐—‹๐–ฝ๐–พ๐—‹: ๐–ฏ๐—ˆ๐—Œ๐—-๐—ˆ๐—‹๐–ฝ๐–พ๐—‹ ๐—๐—‹๐–บ๐—๐–พ๐—‹๐—Œ๐–บ๐—… ๐—๐—‚๐—Œ๐—‚๐—๐—Œ ๐—๐—๐–พ ๐–ผ๐—Ž๐—‹๐—‹๐–พ๐—‡๐— ๐—‡๐—ˆ๐–ฝ๐–พ ๐–บ๐–ฟ๐—๐–พ๐—‹ ๐—‚๐—๐—Œ ๐–ผ๐—๐—‚๐—…๐–ฝ ๐—‡๐—ˆ๐–ฝ๐–พ๐—Œ.

fn post_order_traversal(node: TreeNode) {
    if let Some(value) = node.value {
        post_order_traversal(node.left);
        post_order_traversal(node.right);
        print(value);
    }
}

๐–ฒ๐—ˆ ๐–ฟ๐—ˆ๐—‹ ๐—๐—๐–พ ๐–ฟ๐—ˆ๐—…๐—…๐—ˆ๐—๐—‚๐—‡๐—€ ๐—๐—‹๐–พ๐–พ:

heap

๐–ณ๐—๐–พ ๐—‹๐–พ๐—Œ๐—Ž๐—…๐— ๐—๐—‚๐—…๐—… be:

  • ๐–จ๐—‡-๐—ˆ๐—‹๐–ฝ๐–พ๐—‹ - [๐Ÿฃ, ๐Ÿง, ๐Ÿฉ, ๐Ÿฃ๐Ÿข, ๐Ÿค๐Ÿข]
  • ๐–ฏ๐—‹๐–พ-๐—ˆ๐—‹๐–ฝ๐–พ๐—‹ - [๐Ÿฃ๐Ÿข, ๐Ÿง, ๐Ÿฃ, ๐Ÿฉ, ๐Ÿค๐Ÿข]
  • ๐–ฏ๐—ˆ๐—Œ๐—-๐—ˆ๐—‹๐–ฝ๐–พ๐—‹ - [๐Ÿฃ, ๐Ÿฉ, ๐Ÿง, ๐Ÿค๐Ÿข, ๐Ÿฃ๐Ÿข]

Balanced vs. Unbalanced Trees

  • Balanced Trees
    ๐–  ๐–ป๐–บ๐—…๐–บ๐—‡๐–ผ๐–พ๐–ฝ ๐—๐—‹๐–พ๐–พ ๐—‚๐—Œ ๐–บ ๐—๐—’๐—‰๐–พ ๐—ˆ๐–ฟ ๐–ป๐—‚๐—‡๐–บ๐—‹๐—’ ๐—๐—‹๐–พ๐–พ ๐—๐—๐–พ๐—‹๐–พ ๐—๐—๐–พ ๐–ฝ๐—‚๐–ฟ๐–ฟ๐–พ๐—‹๐–พ๐—‡๐–ผ๐–พ ๐—‚๐—‡ ๐—๐–พ๐—‚๐—€๐—๐— ๐–ป๐–พ๐—๐—๐–พ๐–พ๐—‡ ๐—๐—๐–พ ๐—…๐–พ๐–ฟ๐— ๐–บ๐—‡๐–ฝ ๐—‹๐—‚๐—€๐—๐— ๐—Œ๐—Ž๐–ป๐—๐—‹๐–พ๐–พ๐—Œ ๐—ˆ๐–ฟ ๐–บ๐—‡๐—’ ๐—‡๐—ˆ๐–ฝ๐–พ ๐—‚๐—Œ ๐—†๐—‚๐—‡๐—‚๐—†๐–บ๐—… โ€” ๐—Ž๐—Œ๐—Ž๐–บ๐—…๐—…๐—’ ๐—‡๐—ˆ ๐—†๐—ˆ๐—‹๐–พ ๐—๐—๐–บ๐—‡ ๐—ˆ๐—‡๐–พ. ๐–ณ๐—๐—‚๐—Œ ๐–ป๐–บ๐—…๐–บ๐—‡๐–ผ๐–พ ๐–พ๐—‡๐—Œ๐—Ž๐—‹๐–พ๐—Œ ๐—๐—๐–บ๐— ๐—๐—๐–พ ๐—๐—‹๐–พ๐–พ ๐—‹๐–พ๐—†๐–บ๐—‚๐—‡๐—Œ ๐—‹๐–พ๐—…๐–บ๐—๐—‚๐—๐–พ๐—…๐—’ ๐—…๐—ˆ๐— ๐—‚๐—‡ ๐—๐–พ๐—‚๐—€๐—๐—, ๐—†๐–บ๐—„๐—‚๐—‡๐—€ ๐—ˆ๐—‰๐–พ๐—‹๐–บ๐—๐—‚๐—ˆ๐—‡๐—Œ ๐—…๐—‚๐—„๐–พ ๐—Œ๐–พ๐–บ๐—‹๐–ผ๐—, ๐—‚๐—‡๐—Œ๐–พ๐—‹๐—, ๐–บ๐—‡๐–ฝ ๐–ฝ๐–พ๐—…๐–พ๐—๐–พ ๐—†๐—ˆ๐—‹๐–พ ๐–พ๐–ฟ๐–ฟ๐—‚๐–ผ๐—‚๐–พ๐—‡๐—, ๐—๐—’๐—‰๐—‚๐–ผ๐–บ๐—…๐—…๐—’ ๐—‚๐—‡ ๐—…๐—ˆ๐—€๐–บ๐—‹๐—‚๐—๐—๐—†๐—‚๐–ผ ๐—๐—‚๐—†๐–พ ๐–ผ๐—ˆ๐—†๐—‰๐—…๐–พ๐—‘๐—‚๐—๐—’ (๐–ฎ(๐—…๐—ˆ๐—€ ๐—‡)), ๐—๐—๐–พ๐—‹๐–พ ๐—‡ ๐—‚๐—Œ ๐—๐—๐–พ ๐—‡๐—Ž๐—†๐–ป๐–พ๐—‹ ๐—ˆ๐–ฟ ๐—‡๐—ˆ๐–ฝ๐–พ๐—Œ ๐—‚๐—‡ ๐—๐—๐–พ ๐—๐—‹๐–พ๐–พ. ๐–ค๐—‘๐–บ๐—†๐—‰๐—…๐–พ๐—Œ ๐—ˆ๐–ฟ ๐–ป๐–บ๐—…๐–บ๐—‡๐–ผ๐–พ๐–ฝ ๐—๐—‹๐–พ๐–พ๐—Œ ๐—‚๐—‡๐–ผ๐—…๐—Ž๐–ฝ๐–พ ๐– ๐–ต๐–ซ ๐—๐—‹๐–พ๐–พ๐—Œ ๐–บ๐—‡๐–ฝ ๐–ฑ๐–พ๐–ฝ-๐–ก๐—…๐–บ๐–ผ๐—„ ๐—๐—‹๐–พ๐–พ๐—Œ, ๐—๐—๐—‚๐–ผ๐— ๐—‚๐—†๐—‰๐—…๐–พ๐—†๐–พ๐—‡๐— ๐—Œ๐–พ๐—…๐–ฟ-๐–ป๐–บ๐—…๐–บ๐—‡๐–ผ๐—‚๐—‡๐—€ ๐—†๐–พ๐–ผ๐—๐–บ๐—‡๐—‚๐—Œ๐—†๐—Œ ๐—๐—ˆ ๐—†๐–บ๐—‚๐—‡๐—๐–บ๐—‚๐—‡ ๐—๐—๐—‚๐—Œ ๐—‰๐—‹๐—ˆ๐—‰๐–พ๐—‹๐—๐—’ ๐–บ๐–ฟ๐—๐–พ๐—‹ ๐—ˆ๐—‰๐–พ๐—‹๐–บ๐—๐—‚๐—ˆ๐—‡๐—Œ ๐—๐—๐–บ๐— ๐—†๐—ˆ๐–ฝ๐—‚๐–ฟ๐—’ ๐—๐—๐–พ ๐—๐—‹๐–พ๐–พ.
  • Unbalanced Trees
    ๐– ๐—‡ ๐—Ž๐—‡๐–ป๐–บ๐—…๐–บ๐—‡๐–ผ๐–พ๐–ฝ ๐—๐—‹๐–พ๐–พ, ๐—ˆ๐—‡ ๐—๐—๐–พ ๐—ˆ๐—๐—๐–พ๐—‹ ๐—๐–บ๐—‡๐–ฝ, ๐–ฝ๐—ˆ๐–พ๐—Œ ๐—‡๐—ˆ๐— ๐—๐–บ๐—๐–พ ๐—‹๐–พ๐—Œ๐—๐—‹๐—‚๐–ผ๐—๐—‚๐—ˆ๐—‡๐—Œ ๐—ˆ๐—‡ ๐—๐—๐–พ ๐—๐–พ๐—‚๐—€๐—๐— ๐–ฝ๐—‚๐–ฟ๐–ฟ๐–พ๐—‹๐–พ๐—‡๐–ผ๐–พ๐—Œ ๐—ˆ๐–ฟ ๐—‚๐—๐—Œ ๐—Œ๐—Ž๐–ป๐—๐—‹๐–พ๐–พ๐—Œ. ๐–จ๐—‡ ๐–บ๐—‡ ๐–พ๐—‘๐—๐—‹๐–พ๐—†๐–พ ๐–ผ๐–บ๐—Œ๐–พ, ๐—๐—๐—‚๐—Œ ๐–ผ๐–บ๐—‡ ๐—…๐–พ๐–บ๐–ฝ ๐—๐—ˆ ๐–บ ๐—Œ๐—‚๐—๐—Ž๐–บ๐—๐—‚๐—ˆ๐—‡ ๐—๐—๐–พ๐—‹๐–พ ๐—๐—๐–พ ๐—๐—‹๐–พ๐–พ ๐–ป๐–พ๐–ผ๐—ˆ๐—†๐–พ๐—Œ ๐–บ "๐—…๐—‚๐—‡๐–พ๐–บ๐—‹ ๐–ผ๐—๐–บ๐—‚๐—‡" ๐—ˆ๐–ฟ ๐—‡๐—ˆ๐–ฝ๐–พ๐—Œ, ๐—‹๐–พ๐—Œ๐–พ๐—†๐–ป๐—…๐—‚๐—‡๐—€ ๐–บ ๐—…๐—‚๐—‡๐—„๐–พ๐–ฝ ๐—…๐—‚๐—Œ๐—, ๐–พ๐—Œ๐—‰๐–พ๐–ผ๐—‚๐–บ๐—…๐—…๐—’ ๐—‚๐–ฟ ๐—‡๐—ˆ๐–ฝ๐–พ๐—Œ ๐–บ๐—‹๐–พ ๐—‚๐—‡๐—Œ๐–พ๐—‹๐—๐–พ๐–ฝ ๐—‚๐—‡ ๐–บ ๐—Œ๐—ˆ๐—‹๐—๐–พ๐–ฝ ๐—ˆ๐—‹๐–ฝ๐–พ๐—‹. ๐–จ๐—‡ ๐—Œ๐—Ž๐–ผ๐— ๐–ผ๐–บ๐—Œ๐–พ๐—Œ, ๐—๐—๐–พ ๐—ˆ๐—‰๐–พ๐—‹๐–บ๐—๐—‚๐—ˆ๐—‡๐—Œ ๐—…๐—‚๐—„๐–พ ๐—Œ๐–พ๐–บ๐—‹๐–ผ๐—, ๐—‚๐—‡๐—Œ๐–พ๐—‹๐—, ๐–บ๐—‡๐–ฝ ๐–ฝ๐–พ๐—…๐–พ๐—๐–พ ๐–ผ๐–บ๐—‡ ๐–ฝ๐–พ๐—€๐—‹๐–บ๐–ฝ๐–พ ๐—๐—ˆ ๐—…๐—‚๐—‡๐–พ๐–บ๐—‹ ๐—๐—‚๐—†๐–พ ๐–ผ๐—ˆ๐—†๐—‰๐—…๐–พ๐—‘๐—‚๐—๐—’ (๐–ฎ(๐—‡)), ๐—†๐–บ๐—„๐—‚๐—‡๐—€ ๐—๐—๐–พ๐—† ๐—‚๐—‡๐–พ๐–ฟ๐–ฟ๐—‚๐–ผ๐—‚๐–พ๐—‡๐— ๐–บ๐—Œ ๐—๐—๐–พ ๐—Œ๐—‚๐—“๐–พ ๐—ˆ๐–ฟ ๐—๐—๐–พ ๐—๐—‹๐–พ๐–พ ๐—€๐—‹๐—ˆ๐—๐—Œ. ๐–ด๐—‡๐–ป๐–บ๐—…๐–บ๐—‡๐–ผ๐–พ๐–ฝ ๐—๐—‹๐–พ๐–พ๐—Œ ๐–ฝ๐—ˆ ๐—‡๐—ˆ๐— ๐–บ๐—Ž๐—๐—ˆ๐—†๐–บ๐—๐—‚๐–ผ๐–บ๐—…๐—…๐—’ ๐–บ๐–ฝ๐—ƒ๐—Ž๐—Œ๐— ๐—ˆ๐—‹ ๐—‹๐–พ๐–ป๐–บ๐—…๐–บ๐—‡๐–ผ๐–พ ๐—๐—๐–พ๐—†๐—Œ๐–พ๐—…๐—๐–พ๐—Œ, ๐—๐—๐—‚๐–ผ๐— ๐–ผ๐–บ๐—‡ ๐—…๐–พ๐–บ๐–ฝ ๐—๐—ˆ ๐—Œ๐—Ž๐–ป๐—ˆ๐—‰๐—๐—‚๐—†๐–บ๐—… ๐—‰๐–พ๐—‹๐–ฟ๐—ˆ๐—‹๐—†๐–บ๐—‡๐–ผ๐–พ ๐–ฟ๐—ˆ๐—‹ ๐–ผ๐–พ๐—‹๐—๐–บ๐—‚๐—‡ ๐—Œ๐–พ๐—Š๐—Ž๐–พ๐—‡๐–ผ๐–พ๐—Œ ๐—ˆ๐–ฟ ๐—ˆ๐—‰๐–พ๐—‹๐–บ๐—๐—‚๐—ˆ๐—‡๐—Œ.

๐–ช๐–พ๐–พ๐—‰๐—‚๐—‡๐—€ ๐–บ ๐—๐—‹๐–พ๐–พ ๐–ป๐–บ๐—…๐–บ๐—‡๐–ผ๐–พ๐–ฝ ๐—‚๐—‡๐—๐—ˆ๐—…๐—๐–พ๐—Œ ๐–บ๐–ฝ๐—ƒ๐—Ž๐—Œ๐—๐—‚๐—‡๐—€ ๐—๐—๐–พ ๐—Œ๐—๐—‹๐—Ž๐–ผ๐—๐—Ž๐—‹๐–พ ๐—Ž๐—Œ๐—‚๐—‡๐—€ rotations.

Rotations

  • Left-Left Rotation:

    • ๐–ญ๐—ˆ๐–ฝ๐–พ ๐–ธ ๐–ป๐–พ๐–ผ๐—ˆ๐—†๐–พ ๐—๐—๐–พ ๐—‡๐–พ๐— ๐—‹๐—ˆ๐—ˆ๐— ๐—ˆ๐–ฟ ๐—๐—๐–พ ๐—Œ๐—Ž๐–ป๐—๐—‹๐–พ๐–พ ๐—๐—๐–บ๐— ๐—ˆ๐—‹๐—‚๐—€๐—‚๐—‡๐–บ๐—…๐—…๐—’ ๐—๐–บ๐–ฝ ๐–น ๐–บ๐—Œ ๐—๐—๐–พ ๐—‹๐—ˆ๐—ˆ๐—
    • ๐–ณ๐—๐–พ ๐—‹๐—‚๐—€๐—๐— ๐—Œ๐—Ž๐–ป๐—๐—‹๐–พ๐–พ ๐—ˆ๐–ฟ ๐–ธ (๐—๐—๐—‚๐–ผ๐— ๐—‚๐—Œ ๐–ณ๐Ÿฅ) ๐–ป๐–พ๐–ผ๐—ˆ๐—†๐–พ ๐—๐—๐–พ ๐—…๐–พ๐–ฟ๐— ๐—Œ๐—Ž๐–ป๐—๐—‹๐–พ๐–พ ๐—ˆ๐–ฟ ๐–น
    • ๐–ญ๐—ˆ๐–ฝ๐–พ ๐–น ๐–ป๐–พ๐–ผ๐—ˆ๐—†๐–พ ๐—๐—๐–พ ๐—‹๐—‚๐—€๐—๐— ๐–ผ๐—๐—‚๐—…๐–ฝ ๐—ˆ๐–ฟ ๐—๐—๐–พ ๐—‡๐–พ๐— ๐—‹๐—ˆ๐—ˆ๐— ๐–ธ
    rotations
    ๐–ณ๐—๐—‚๐—Œ ๐—‹๐—ˆ๐—๐–บ๐—๐—‚๐—ˆ๐—‡ ๐—‚๐—Œ ๐—‰๐–พ๐—‹๐–ฟ๐—ˆ๐—‹๐—†๐–พ๐–ฝ ๐—๐—๐–พ๐—‡ ๐—๐—๐–พ ๐—…๐–พ๐–ฟ๐— ๐–ผ๐—๐—‚๐—…๐–ฝ ๐—ˆ๐–ฟ ๐—‡๐—ˆ๐–ฝ๐–พ ๐–ธ ๐—‚๐—Œ ๐—…๐–พ๐–ฟ๐—-๐—๐–พ๐–บ๐—๐—’, ๐–บ๐—‡๐–ฝ ๐–บ ๐—‹๐—ˆ๐—๐–บ๐—๐—‚๐—ˆ๐—‡ ๐—‚๐—Œ ๐—‡๐–พ๐–พ๐–ฝ๐–พ๐–ฝ ๐—๐—ˆ ๐—‹๐–พ๐—Œ๐—๐—ˆ๐—‹๐–พ ๐—๐—๐–พ ๐–ป๐–บ๐—…๐–บ๐—‡๐–ผ๐–พ.
  • Left-Right Rotation:

    • ๐–ฅ๐—‚๐—‹๐—Œ๐— ๐–ฑ๐—ˆ๐—๐–บ๐—๐—‚๐—ˆ๐—‡
      • ๐–ท ๐–ป๐–พ๐–ผ๐—ˆ๐—†๐–พ๐—Œ ๐—๐—๐–พ ๐—‡๐–พ๐— ๐—‹๐—ˆ๐—ˆ๐— ๐—ˆ๐–ฟ ๐—๐—๐–พ ๐—Œ๐—Ž๐–ป๐—๐—‹๐–พ๐–พ ๐—๐—๐–บ๐— ๐—ˆ๐—‹๐—‚๐—€๐—‚๐—‡๐–บ๐—…๐—…๐—’ ๐—๐–บ๐–ฝ ๐–ธ ๐–บ๐—Œ ๐—๐—๐–พ ๐—‹๐—ˆ๐—ˆ๐—
      • ๐–ธ ๐–ป๐–พ๐–ผ๐—ˆ๐—†๐–พ๐—Œ ๐—๐—๐–พ ๐—…๐–พ๐–ฟ๐— ๐–ผ๐—๐—‚๐—…๐–ฝ ๐—ˆ๐–ฟ ๐–ท
      • ๐–ณ๐—๐–พ ๐—…๐–พ๐–ฟ๐— ๐—Œ๐—Ž๐–ป๐—๐—‹๐–พ๐–พ ๐—ˆ๐–ฟ ๐–ท (๐–ณ๐Ÿค) ๐–ป๐–พ๐–ผ๐—ˆ๐—†๐–พ๐—Œ ๐—๐—๐–พ ๐—‹๐—‚๐—€๐—๐— ๐–ผ๐—๐—‚๐—…๐–ฝ ๐—ˆ๐–ฟ ๐–ธ
    • ๐–ฒ๐–พ๐–ผ๐—ˆ๐—‡๐–ฝ ๐–ฑ๐—ˆ๐—๐–บ๐—๐—‚๐—ˆ๐—‡
      • ๐–ท ๐–ป๐–พ๐–ผ๐—ˆ๐—†๐–พ๐—Œ ๐—๐—๐–พ ๐—‡๐–พ๐— ๐—‹๐—ˆ๐—ˆ๐— ๐—ˆ๐–ฟ ๐—๐—๐–พ ๐—Œ๐—Ž๐–ป๐—๐—‹๐–พ๐–พ ๐—๐—๐–บ๐— ๐—ˆ๐—‹๐—‚๐—€๐—‚๐—‡๐–บ๐—…๐—…๐—’ ๐—๐–บ๐–ฝ ๐–น ๐–บ๐—Œ ๐—๐—๐–พ ๐—‹๐—ˆ๐—ˆ๐—
      • ๐–น ๐–ป๐–พ๐–ผ๐—ˆ๐—†๐–พ๐—Œ ๐—๐—๐–พ ๐—‹๐—‚๐—€๐—๐— ๐–ผ๐—๐—‚๐—…๐–ฝ ๐—ˆ๐–ฟ ๐–ท
      • ๐–ณ๐—๐–พ ๐—‹๐—‚๐—€๐—๐— ๐—Œ๐—Ž๐–ป๐—๐—‹๐–พ๐–พ ๐—ˆ๐–ฟ ๐–ท (๐–ณ๐Ÿฅ) ๐–ป๐–พ๐–ผ๐—ˆ๐—†๐–พ๐—Œ ๐—๐—๐–พ ๐—…๐–พ๐–ฟ๐— ๐–ผ๐—๐—‚๐—…๐–ฝ ๐—ˆ๐–ฟ ๐–น
    rotations ๐–ณ๐—๐—‚๐—Œ ๐—‚๐—Œ ๐–บ ๐–ผ๐—ˆ๐—†๐–ป๐—‚๐—‡๐–บ๐—๐—‚๐—ˆ๐—‡ ๐—ˆ๐–ฟ ๐–บ ๐—…๐–พ๐–ฟ๐— ๐—‹๐—ˆ๐—๐–บ๐—๐—‚๐—ˆ๐—‡ ๐–ฟ๐—ˆ๐—…๐—…๐—ˆ๐—๐–พ๐–ฝ ๐–ป๐—’ ๐–บ ๐—‹๐—‚๐—€๐—๐— ๐—‹๐—ˆ๐—๐–บ๐—๐—‚๐—ˆ๐—‡, ๐—Ž๐—Œ๐–พ๐–ฝ ๐—๐—๐–พ๐—‡ ๐—๐—๐–พ ๐—…๐–พ๐–ฟ๐— ๐–ผ๐—๐—‚๐—…๐–ฝ ๐—ˆ๐–ฟ ๐—‡๐—ˆ๐–ฝ๐–พ ๐–น ๐—‚๐—Œ ๐—‹๐—‚๐—€๐—๐—-๐—๐–พ๐–บ๐—๐—’.
  • Right-Right Rotation:

    rotations
    ๐–ณ๐—๐—‚๐—Œ ๐—‹๐—ˆ๐—๐–บ๐—๐—‚๐—ˆ๐—‡ ๐—‚๐—Œ ๐—‰๐–พ๐—‹๐–ฟ๐—ˆ๐—‹๐—†๐–พ๐–ฝ ๐—๐—๐–พ๐—‡ ๐—๐—๐–พ ๐—‹๐—‚๐—€๐—๐— ๐–ผ๐—๐—‚๐—…๐–ฝ ๐—ˆ๐–ฟ ๐—‡๐—ˆ๐–ฝ๐–พ ๐–ธ ๐—‚๐—Œ ๐—‹๐—‚๐—€๐—๐—-๐—๐–พ๐–บ๐—๐—’, ๐–บ๐—‡๐–ฝ ๐–บ ๐—‹๐—ˆ๐—๐–บ๐—๐—‚๐—ˆ๐—‡ ๐—‚๐—Œ ๐—‡๐–พ๐–พ๐–ฝ๐–พ๐–ฝ ๐—๐—ˆ ๐—‹๐–พ๐—Œ๐—๐—ˆ๐—‹๐–พ ๐—๐—๐–พ ๐–ป๐–บ๐—…๐–บ๐—‡๐–ผ๐–พ.
  • Right-Left Rotation:

    rotations
    ๐–ณ๐—๐—‚๐—Œ ๐—‚๐—Œ ๐–บ ๐–ผ๐—ˆ๐—†๐–ป๐—‚๐—‡๐–บ๐—๐—‚๐—ˆ๐—‡ ๐—ˆ๐–ฟ ๐–บ ๐—‹๐—‚๐—€๐—๐— ๐—‹๐—ˆ๐—๐–บ๐—๐—‚๐—ˆ๐—‡ ๐–ฟ๐—ˆ๐—…๐—…๐—ˆ๐—๐–พ๐–ฝ ๐–ป๐—’ ๐–บ ๐—…๐–พ๐–ฟ๐— ๐—‹๐—ˆ๐—๐–บ๐—๐—‚๐—ˆ๐—‡, ๐—Ž๐—Œ๐–พ๐–ฝ ๐—๐—๐–พ๐—‡ ๐—๐—๐–พ ๐—‹๐—‚๐—€๐—๐— ๐–ผ๐—๐—‚๐—…๐–ฝ ๐—ˆ๐–ฟ ๐—‡๐—ˆ๐–ฝ๐–พ `๐–น` ๐—‚๐—Œ ๐—…๐–พ๐–ฟ๐—-๐—๐–พ๐–บ๐—๐—’.

Heap

heap

Note

๐–  ๐—๐–พ๐–บ๐—‰ ๐—‚๐—Œ ๐–บ ๐—Œ๐—‰๐–พ๐–ผ๐—‚๐–บ๐—… ๐–ณ๐—‹๐–พ๐–พ-๐–ป๐–บ๐—Œ๐–พ๐–ฝ ๐–ฝ๐–บ๐—๐–บ ๐—Œ๐—๐—‹๐—Ž๐–ผ๐—๐—Ž๐—‹๐–พ ๐—‚๐—‡ ๐—๐—๐—‚๐–ผ๐— ๐—๐—๐–พ ๐—๐—‹๐–พ๐–พ ๐—‚๐—Œ ๐–บ ๐–ผ๐—ˆ๐—†๐—‰๐—…๐–พ๐—๐–พ ๐–ก๐—‚๐—‡๐–บ๐—‹๐—’ ๐–ณ๐—‹๐–พ๐–พ ๐—‚๐—‡ ๐—๐—๐—‚๐–ผ๐— ๐–พ๐–บ๐–ผ๐— ๐—…๐–พ๐—๐–พ๐—… ๐—๐–บ๐—Œ ๐–บ๐—…๐—… ๐—ˆ๐–ฟ ๐—‚๐—๐—Œ ๐—‡๐—ˆ๐–ฝ๐–พ๐—Œ.
๐–ณ๐—๐–พ ๐–พ๐—‘๐–ผ๐–พ๐—‰๐—๐—‚๐—ˆ๐—‡ ๐—๐—ˆ ๐—๐—๐—‚๐—Œ ๐—‚๐—Œ ๐—๐—๐–พ ๐–ป๐—ˆ๐—๐—๐—ˆ๐—† ๐—…๐–พ๐—๐–พ๐—… ๐—ˆ๐–ฟ ๐—๐—๐–พ ๐—๐—‹๐–พ๐–พ, ๐—๐—๐—‚๐–ผ๐— ๐—๐–พ ๐–ฟ๐—‚๐—…๐—… ๐—‚๐—‡ ๐–ฟ๐—‹๐—ˆ๐—† ๐—…๐–พ๐–ฟ๐— ๐—๐—ˆ ๐—‹๐—‚๐—€๐—๐—.

Heap type

  • ๐–ฌ๐—‚๐—‡ ๐–ง๐–พ๐–บ๐—‰ - ๐—‚๐–ฟ ๐–พ๐–บ๐–ผ๐— ๐—‰๐–บ๐—‹๐–พ๐—‡๐— ๐—‡๐—ˆ๐–ฝ๐–พ ๐—‚๐—Œ ๐—…๐–พ๐—Œ๐—Œ ๐—๐—๐–บ๐—‡ ๐—ˆ๐—‹ ๐–พ๐—Š๐—Ž๐–บ๐—… ๐—๐—ˆ ๐—‚๐—๐—Œ ๐–ผ๐—๐—‚๐—…๐–ฝ ๐—‡๐—ˆ๐–ฝ๐–พ (๐—‹๐—ˆ๐—ˆ๐— ๐—‚๐—Œ ๐–บ๐—…๐—๐–บ๐—’๐—Œ ๐—๐—๐–พ ๐—Œ๐—†๐–บ๐—…๐—…๐–พ๐—Œ๐— ๐—‚๐—๐–พ๐—†)
  • ๐–ฌ๐–บ๐—‘ ๐–ง๐–พ๐–บ๐—‰ - ๐—‚๐–ฟ ๐–พ๐–บ๐–ผ๐— ๐—‰๐–บ๐—‹๐–พ๐—‡๐— ๐—‡๐—ˆ๐–ฝ๐–พ ๐—‚๐—Œ ๐—€๐—‹๐–พ๐–บ๐—๐–พ๐—‹ ๐—๐—๐–บ๐—‡ ๐—ˆ๐—‹ ๐–พ๐—Š๐—Ž๐–บ๐—… ๐—๐—ˆ ๐—‚๐—๐—Œ ๐–ผ๐—๐—‚๐—…๐–ฝ ๐—‡๐—ˆ๐–ฝ๐–พ (๐—‹๐—ˆ๐—ˆ๐— ๐—‚๐—Œ ๐–บ๐—…๐—๐–บ๐—’๐—Œ ๐—๐—๐–พ ๐—…๐–บ๐—‹๐—€๐–พ๐—Œ๐— ๐—‚๐—๐–พ๐—†)

Representation

๐–ฃ๐–พ๐—Œ๐—‰๐—‚๐—๐–พ ๐–ป๐–พ๐—‚๐—‡๐—€ ๐–ผ๐—ˆ๐—‡๐–ผ๐–พ๐—‰๐—๐—Ž๐–บ๐—…๐—…๐—’ ๐–บ ๐—๐—‹๐–พ๐–พ, ๐–บ ๐—๐–พ๐–บ๐—‰ ๐—‚๐—Œ ๐—‚๐—†๐—‰๐—…๐–พ๐—†๐–พ๐—‡๐—๐–พ๐–ฝ ๐—Ž๐—Œ๐—‚๐—‡๐—€ ๐–บ๐—‡ ๐–บ๐—‹๐—‹๐–บ๐—’. ๐–ณ๐—๐—‚๐—Œ ๐—‚๐—Œ ๐–พ๐–ฟ๐–ฟ๐—‚๐–ผ๐—‚๐–พ๐—‡๐— ๐—‚๐—‡ ๐—๐–พ๐—‹๐—†๐—Œ ๐—ˆ๐–ฟ ๐–ป๐—ˆ๐—๐— ๐—Œ๐—‰๐–บ๐–ผ๐–พ ๐–บ๐—‡๐–ฝ ๐—๐—‚๐—†๐–พ.
๐–ด๐—Œ๐—‚๐—‡๐—€ ๐–บ๐—‡ ๐–บ๐—‹๐—‹๐–บ๐—’ ๐–พ๐—…๐—‚๐—†๐—‚๐—‡๐–บ๐—๐–พ๐—Œ ๐—๐—๐–พ ๐—‡๐–พ๐–พ๐–ฝ ๐–ฟ๐—ˆ๐—‹ ๐—‰๐—ˆ๐—‚๐—‡๐—๐–พ๐—‹๐—Œ ๐—‹๐–พ๐—Š๐—Ž๐—‚๐—‹๐–พ๐–ฝ ๐—‚๐—‡ ๐–บ ๐—๐—‹๐–พ๐–พ ๐—‹๐–พ๐—‰๐—‹๐–พ๐—Œ๐–พ๐—‡๐—๐–บ๐—๐—‚๐—ˆ๐—‡, ๐—๐—๐—Ž๐—Œ ๐—Œ๐–บ๐—๐—‚๐—‡๐—€ ๐—Œ๐—‰๐–บ๐–ผ๐–พ.

  • S๐—ˆ ๐—๐–พ๐–บ๐—‰ ๐—๐—๐–บ๐— ๐—…๐—ˆ๐—ˆ๐—„๐—Œ ๐—…๐—‚๐—„๐–พ ๐—๐—๐—‚๐—Œ:
heap
  • I๐—Œ ๐—‹๐–พ๐—‰๐—‹๐–พ๐—Œ๐–พ๐—‡๐—๐–พ๐–ฝ ๐—…๐—‚๐—„๐–พ ๐—๐—๐—‚๐—Œ:
heap

๐–ง๐–พ๐–บ๐—‰ ๐—‹๐—ˆ๐—ˆ๐— ๐—‚๐—Œ ๐—‚๐—‡๐–ฝ๐–พ๐—‘=๐Ÿข - ๐–ฟ๐—‚๐—‹๐—Œ๐— ๐—‚๐—๐–พ๐—† ๐—ˆ๐–ฟ ๐—๐—๐–พ ๐–บ๐—‹๐—‹๐–บ๐—’. ๐–ฃ๐–พ๐—‰๐–พ๐—‡๐–ฝ๐—‚๐—‡๐—€ ๐—ˆ๐—‡ ๐—๐—๐–พ ๐—‚๐—†๐—‰๐—…๐–พ๐—†๐–พ๐—‡๐—๐–บ๐—๐—‚๐—ˆ๐—‡ ๐–ฌ๐—‚๐—‡/๐–ฌ๐–บ๐—‘, ๐—‹๐—ˆ๐—ˆ๐— ๐—‚๐—Œ ๐–บ๐—…๐—๐–บ๐—’๐—Œ ๐—๐—๐–พ ๐—Œ๐—†๐–บ๐—…๐—…๐–พ๐—Œ๐—/๐—…๐–บ๐—‹๐—€๐–พ๐—Œ๐— ๐—‚๐—๐–พ๐—† ๐—ˆ๐–ฟ ๐—๐—๐–พ ๐–บ๐—‹๐—‹๐–บ๐—’

๐–ถ๐—๐–พ๐—‡ ๐—Ž๐—Œ๐—‚๐—‡๐—€ ๐–บ๐—‡ ๐–บ๐—‹๐—‹๐–บ๐—’ ๐—๐—ˆ ๐—‹๐–พ๐—‰๐—‹๐–พ๐—Œ๐–พ๐—‡๐— ๐–บ ๐–ป๐—‚๐—‡๐–บ๐—‹๐—’ ๐—๐–พ๐–บ๐—‰, ๐—๐–พ ๐–บ๐—‹๐–พ ๐–พ๐—Œ๐—Œ๐–พ๐—‡๐—๐—‚๐–บ๐—…๐—…๐—’ "๐–ฟ๐—…๐–บ๐—๐—๐–พ๐—‡๐—‚๐—‡๐—€" ๐—๐—๐–พ ๐—๐—‹๐–พ๐–พ ๐—Œ๐—๐—‹๐—Ž๐–ผ๐—๐—Ž๐—‹๐–พ ๐—‚๐—‡๐—๐—ˆ ๐–บ ๐—…๐—‚๐—Œ๐—, ๐—๐—๐—‚๐—…๐–พ ๐—Œ๐—๐—‚๐—…๐—… ๐—†๐–บ๐—‚๐—‡๐—๐–บ๐—‚๐—‡๐—‚๐—‡๐—€ ๐—๐—๐–พ ๐—‰๐–บ๐—‹๐–พ๐—‡๐—-๐–ผ๐—๐—‚๐—…๐–ฝ ๐—‹๐–พ๐—…๐–บ๐—๐—‚๐—ˆ๐—‡๐—Œ๐—๐—‚๐—‰๐—Œ.

๐–ณ๐—๐–พ ๐–บ๐—‹๐—‹๐–บ๐—’ ๐—‚๐—Œ ๐—Œ๐—๐—‹๐—Ž๐–ผ๐—๐—Ž๐—‹๐–พ๐–ฝ ๐—Œ๐—Ž๐–ผ๐— ๐—๐—๐–บ๐— ๐–ฟ๐—ˆ๐—‹ ๐–บ๐—‡๐—’ ๐—€๐—‚๐—๐–พ๐—‡ ๐—‚๐—‡๐–ฝ๐–พ๐—‘ i (๐—Œ๐—๐–บ๐—‹๐—๐—‚๐—‡๐—€ ๐–บ๐— ๐Ÿข), ๐—๐—๐–พ ๐—…๐–พ๐–ฟ๐— ๐–ผ๐—๐—‚๐—…๐–ฝ ๐—‚๐—Œ ๐–บ๐— ๐Ÿค*๐—‚ + ๐Ÿฃ ๐–บ๐—‡๐–ฝ ๐—๐—๐–พ ๐—‹๐—‚๐—€๐—๐— ๐–ผ๐—๐—‚๐—…๐–ฝ ๐—‚๐—Œ ๐–บ๐— ๐Ÿค*๐—‚ + 2.
๐–ข๐—ˆ๐—‡๐—๐–พ๐—‹๐—Œ๐–พ๐—…๐—’, ๐—๐—๐–พ ๐—‰๐–บ๐—‹๐–พ๐—‡๐— ๐—ˆ๐–ฟ ๐–บ๐—‡๐—’ ๐—‡๐—ˆ๐—‡-๐—‹๐—ˆ๐—ˆ๐— ๐—‡๐—ˆ๐–ฝ๐–พ ๐–บ๐— ๐—‚๐—‡๐–ฝ๐–พ๐—‘ i ๐—‚๐—Œ ๐–บ๐— (๐—‚-๐Ÿฃ)/๐Ÿค.

  • ๐– ๐–ฝ๐–ฝ๐—‚๐—‡๐—€ ๐–บ๐—‡ ๐–จ๐—๐–พ๐—† (๐—‰๐—Ž๐—Œ๐—):
    • ๐–ถ๐—๐–พ๐—‡ ๐—๐–พ ๐–บ๐–ฝ๐–ฝ ๐–บ ๐—‡๐–พ๐— ๐—‚๐—๐–พ๐—† ๐—๐—ˆ ๐–บ ๐—๐–พ๐–บ๐—‰, ๐—๐–พ ๐–บ๐—…๐—๐–บ๐—’๐—Œ ๐–บ๐–ฝ๐–ฝ ๐—‚๐— ๐–บ๐— ๐—๐—๐–พ ๐—๐–พ๐—‹๐—’ ๐–พ๐—‡๐–ฝ ๐—ˆ๐–ฟ ๐—๐—๐–พ ๐–บ๐—‹๐—‹๐–บ๐—’. ๐–ณ๐—๐—‚๐—Œ ๐–พ๐—‡๐—Œ๐—Ž๐—‹๐–พ๐—Œ ๐—๐—๐–บ๐— ๐—๐—๐–พ ๐—๐—‹๐–พ๐–พ ๐—‹๐–พ๐—†๐–บ๐—‚๐—‡๐—Œ ๐–ผ๐—ˆ๐—†๐—‰๐—…๐–พ๐—๐–พ ๐–บ๐—‡๐–ฝ ๐–พ๐—๐–พ๐—‹๐—’ ๐—…๐–พ๐—๐–พ๐—… ๐—ˆ๐–ฟ ๐—๐—๐–พ ๐—๐—‹๐–พ๐–พ ๐—‚๐—Œ ๐–ฟ๐—‚๐—…๐—…๐–พ๐–ฝ ๐–ฟ๐—‹๐—ˆ๐—† ๐—…๐–พ๐–ฟ๐— ๐—๐—ˆ ๐—‹๐—‚๐—€๐—๐— ๐—๐—‚๐—๐—๐—ˆ๐—Ž๐— ๐—€๐–บ๐—‰๐—Œ.
    • ๐– ๐–ฟ๐—๐–พ๐—‹ ๐–บ๐–ฝ๐–ฝ๐—‚๐—‡๐—€ ๐—๐—๐–พ ๐—‡๐–พ๐— ๐—‚๐—๐–พ๐—†, ๐—๐–พ "๐—๐–พ๐–บ๐—‰๐—‚๐–ฟ๐—’" ๐—‚๐— ๐–ป๐—’ ๐–ผ๐—ˆ๐—†๐—‰๐–บ๐—‹๐—‚๐—‡๐—€ ๐—‚๐— ๐—๐—‚๐—๐— ๐—‚๐—๐—Œ ๐—‰๐–บ๐—‹๐–พ๐—‡๐— ๐—‡๐—ˆ๐–ฝ๐–พ ๐–บ๐—‡๐–ฝ ๐—Œ๐—๐–บ๐—‰๐—‰๐—‚๐—‡๐—€ ๐—‚๐–ฟ ๐—‡๐–พ๐–ผ๐–พ๐—Œ๐—Œ๐–บ๐—‹๐—’ (๐—‚๐—‡ ๐–บ ๐—†๐–บ๐—‘-๐—๐–พ๐–บ๐—‰, ๐—‚๐–ฟ ๐—‚๐—'๐—Œ ๐—…๐–บ๐—‹๐—€๐–พ๐—‹ ๐—๐—๐–บ๐—‡ ๐—๐—๐–พ ๐—‰๐–บ๐—‹๐–พ๐—‡๐—). ๐–ณ๐—๐—‚๐—Œ ๐—‰๐—‹๐—ˆ๐–ผ๐–พ๐—Œ๐—Œ ๐—‚๐—Œ ๐—‹๐–พ๐—‰๐–พ๐–บ๐—๐–พ๐–ฝ ๐—Ž๐—‡๐—๐—‚๐—… ๐—๐—๐–พ ๐—‡๐–พ๐— ๐—‚๐—๐–พ๐—† ๐—‚๐—Œ ๐—‚๐—‡ ๐—๐—๐–พ ๐–ผ๐—ˆ๐—‹๐—‹๐–พ๐–ผ๐— ๐—‰๐—ˆ๐—Œ๐—‚๐—๐—‚๐—ˆ๐—‡ ๐—๐—๐–พ๐—‹๐–พ ๐—‚๐—'๐—Œ ๐—‡๐—ˆ ๐—…๐—ˆ๐—‡๐—€๐–พ๐—‹ ๐—€๐—‹๐–พ๐–บ๐—๐–พ๐—‹ ๐—๐—๐–บ๐—‡ ๐—‚๐—๐—Œ ๐—‰๐–บ๐—‹๐–พ๐—‡๐—.
  • ๐–ฑ๐–พ๐—†๐—ˆ๐—๐—‚๐—‡๐—€ ๐–บ๐—‡ ๐–จ๐—๐–พ๐—† (๐—‰๐—ˆ๐—‰):
    • ๐–ถ๐—๐–พ๐—‡ ๐—‹๐–พ๐—†๐—ˆ๐—๐–พ๐—‚๐—‡๐—€ ๐–บ๐—‡ ๐—‚๐—๐–พ๐—†, ๐—Ž๐—Œ๐—Ž๐–บ๐—…๐—…๐—’ ๐—๐–พ ๐—‹๐–พ๐—†๐—ˆ๐—๐–พ ๐—๐—๐–พ ๐—‹๐—ˆ๐—ˆ๐—, ๐–ป๐–พ๐–ผ๐–บ๐—Ž๐—Œ๐–พ ๐—๐—๐–บ๐—'๐—Œ ๐—๐—๐–พ ๐—†๐–บ๐—‘๐—‚๐—†๐—Ž๐—† ๐—‚๐—๐–พ๐—† ๐—‚๐—‡ ๐–บ ๐—†๐–บ๐—‘-๐—๐–พ๐–บ๐—‰.
    • ๐–ณ๐—ˆ ๐—†๐–บ๐—‚๐—‡๐—๐–บ๐—‚๐—‡ ๐—๐—๐–พ ๐–ผ๐—ˆ๐—†๐—‰๐—…๐–พ๐—๐–พ ๐—๐—‹๐–พ๐–พ ๐—‰๐—‹๐—ˆ๐—‰๐–พ๐—‹๐—๐—’, ๐—๐–พ ๐—๐–บ๐—„๐–พ ๐—๐—๐–พ ๐—…๐–บ๐—Œ๐— ๐—‚๐—๐–พ๐—† ๐—‚๐—‡ ๐—๐—๐–พ ๐–บ๐—‹๐—‹๐–บ๐—’ ๐–บ๐—‡๐–ฝ ๐—†๐—ˆ๐—๐–พ ๐—‚๐— ๐—๐—ˆ ๐—๐—๐–พ ๐—‹๐—ˆ๐—ˆ๐— ๐—‰๐—ˆ๐—Œ๐—‚๐—๐—‚๐—ˆ๐—‡. ๐–ฅ๐—‹๐—ˆ๐—† ๐—๐—๐–พ๐—‹๐–พ, ๐—๐–พ "๐—๐–พ๐–บ๐—‰๐—‚๐–ฟ๐—’" ๐—๐—๐–พ ๐—๐—‹๐–พ๐–พ ๐–ป๐—’ ๐—‹๐–พ๐—‰๐–พ๐–บ๐—๐–พ๐–ฝ๐—…๐—’ ๐—Œ๐—๐–บ๐—‰๐—‰๐—‚๐—‡๐—€ ๐—๐—๐—‚๐—Œ ๐—‡๐–พ๐— ๐—‹๐—ˆ๐—ˆ๐— ๐—๐—‚๐—๐— ๐—‚๐—๐—Œ ๐—…๐–บ๐—‹๐—€๐–พ๐—Œ๐— ๐–ผ๐—๐—‚๐—…๐–ฝ (๐—‚๐–ฟ ๐—‚๐—'๐—Œ ๐—Œ๐—†๐–บ๐—…๐—…๐–พ๐—‹ ๐—๐—๐–บ๐—‡ ๐—‚๐—๐—Œ ๐–ผ๐—๐—‚๐—…๐–ฝ๐—‹๐–พ๐—‡) ๐—Ž๐—‡๐—๐—‚๐—… ๐—๐—๐–พ ๐—๐–พ๐–บ๐—‰ ๐—‰๐—‹๐—ˆ๐—‰๐–พ๐—‹๐—๐—’ ๐—‚๐—Œ ๐—‹๐–พ๐—Œ๐—๐—ˆ๐—‹๐–พ๐–ฝ.

F๐—ˆr๐—†๐—Ž๐—…๐–บ๐—Œ ๐–ฟ๐—ˆ๐—‹ ๐–ฟ๐—‚๐—‡๐–ฝ๐—‚๐—‡๐—€ parent and child nodes ๐–ฟ๐—‹๐—ˆ๐—† ๐—๐–พ๐–บ๐—‰ ๐—๐—๐—‚๐–ผ๐— ๐—‚๐—Œ ๐—‹๐–พ๐—‰๐—‹๐–พ๐—Œ๐–พ๐—‡๐—๐–พ๐–ฝ ๐–บ๐—Œ ๐–บ๐—‡ ๐–บ๐—‹๐—‹๐–บ๐—’:

  • ๐–ฅ๐—‚๐—‡๐–ฝ ๐—‰๐–บ๐—‹๐–พ๐—‡๐— ๐—‡๐—ˆ๐–ฝ๐–พ - ๐—‰๐–บ๐—‹๐–พ๐—‡๐—(๐—‚๐—‡๐–ฝ๐–พ๐—‘) = (๐—‚๐—‡๐–ฝ๐–พ๐—‘ - ๐Ÿฃ) / ๐Ÿค
  • ๐–ฅ๐—‚๐—‡๐–ฝ ๐—…๐–พ๐–ฟ๐— ๐–ผ๐—๐—‚๐—…๐–ฝ ๐—‡๐—ˆ๐–ฝ๐–พ - ๐—…๐–พ๐–ฟ๐—(๐—‚๐—‡๐–ฝ๐–พ๐—‘) = ๐Ÿค * ๐—‚๐—‡๐–ฝ๐–พ๐—‘ + ๐Ÿฃ
  • ๐–ฅ๐—‚๐—‡๐–ฝ ๐—‹๐—‚๐—€๐—๐— ๐–ผ๐—๐—‚๐—…๐–ฝ ๐—‡๐—ˆ๐–ฝ๐–พ - ๐—‹๐—‚๐—€๐—๐—(๐—‚๐—‡๐–ฝ๐–พ๐—‘) = ๐Ÿค * ๐—‚๐—‡๐–ฝ๐–พ๐—‘ + ๐Ÿค

๐–ฅ๐—ˆ๐—‹ ๐–พ๐—‘๐–บ๐—†๐—‰๐—…๐–พ:

  • ๐–ฏ๐–บ๐—‹๐–พ๐—‡๐— ๐—‡๐—ˆ๐–ฝ๐–พ ๐—ˆ๐–ฟ ๐Ÿฉ ๐—‚๐—Œ - ๐—‰๐–บ๐—‹๐–พ๐—‡๐—(๐Ÿฅ) = (๐Ÿฅ-๐Ÿฃ) / ๐Ÿค = ๐Ÿฃ ๐—Œ๐—ˆ, ๐—‰๐–บ๐—‹๐–พ๐—‡๐—๐–ญ๐—ˆ๐–ฝ๐–พ๐–จ๐—‡๐–ฝ๐–พ๐—‘ = ๐Ÿฃ (๐Ÿฅ ๐—‚๐—Œ ๐—‚๐—‡๐–ฝ๐–พ๐—‘ ๐—ˆ๐–ฟ ๐—‚๐—๐–พ๐—† ๐Ÿฉ)
  • ๐–ซ๐–พ๐–ฟ๐— ๐–ผ๐—๐—‚๐—…๐–ฝ ๐—‡๐—ˆ๐–ฝ๐–พ ๐—ˆ๐–ฟ ๐Ÿช ๐—‚๐—Œ, ๐—…๐–พ๐–ฟ๐—(๐Ÿฃ) = (๐Ÿค ๐—‘ ๐Ÿฃ) + ๐Ÿฃ = ๐Ÿฅ ๐—Œ๐—ˆ, ๐—…๐–พ๐–ฟ๐—๐–ข๐—๐—‚๐—…๐–ฝ๐–ญ๐—ˆ๐–ฝ๐–พ๐–จ๐—‡๐–ฝ๐–พ๐—‘ = ๐Ÿฅ (๐—‚๐—๐–พ๐—†: ๐Ÿฉ)
  • ๐–ฑ๐—‚๐—€๐—๐— ๐–ผ๐—๐—‚๐—…๐–ฝ ๐—‡๐—ˆ๐–ฝ๐–พ ๐—ˆ๐–ฟ ๐Ÿช ๐—‚๐—Œ, ๐—‹๐—‚๐—€๐—๐—(๐Ÿฃ) = (๐Ÿค ๐—‘ ๐Ÿฃ) + ๐Ÿค = ๐Ÿฆ ๐—Œ๐—ˆ, ๐—‹๐—‚๐—€๐—๐—๐–ข๐—๐—‚๐—…๐–ฝ๐–ญ๐—ˆ๐–ฝ๐–พ๐–จ๐—‡๐–ฝ๐–พ๐—‘ = ๐Ÿฆ (๐—‚๐—๐–พ๐—†: ๐Ÿฃ)

Operations

  • I๐—‡๐—Œ๐–พ๐—‹๐—: ฮ˜(๐—…๐—ˆ๐—€ ๐—‡)
  • D๐–พ๐—…๐–พ๐—๐–พ: ฮ˜(๐—…๐—ˆ๐—€ ๐—‡)
  • F๐—‚๐—‡๐–ฝ ๐—†๐—‚๐—‡/๐—†๐–บ๐—‘: ๐–ฎ(1) (๐—๐—๐–พ ๐—†๐–บ๐—‘๐—‚๐—†๐—Ž๐—†/๐—†๐—‚๐—‡๐—‚๐—†๐—Ž๐—† ๐—‚๐—๐–พ๐—† ๐—‚๐—Œ ๐–บ๐—…๐—๐–บ๐—’๐—Œ ๐–บ๐— ๐—๐—๐–พ ๐—‹๐—ˆ๐—ˆ๐—, ๐—†๐–บ๐—„๐—‚๐—‡๐—€ ๐—๐—๐—‚๐—Œ ๐—ˆ๐—‰๐–พ๐—‹๐–บ๐—๐—‚๐—ˆ๐—‡ ๐—๐—‚๐—๐— O(1) ๐—๐—‚๐—†๐–พ ๐–ผ๐—ˆ๐—†๐—‰๐—…๐–พ๐—‘๐—‚๐—๐—’)
๐™ธ๐š–๐š™๐š•๐šŽ๐š–๐šŽ๐š—๐š๐šŠ๐š๐š’๐š˜๐š—๐šœ:
rust
csharp

Binary Search Tree

bst

Note

๐–  ๐–ก๐—‚๐—‡๐–บ๐—‹๐—’ ๐–ฒ๐–พ๐–บ๐—‹๐–ผ๐— ๐–ณ๐—‹๐–พ๐–พ ๐—‚๐—Œ ๐–บ ๐—‡๐—ˆ๐–ฝ๐–พ-๐–ป๐–บ๐—Œ๐–พ๐–ฝ ๐–ฝ๐–บ๐—๐–บ ๐—Œ๐—๐—‹๐—Ž๐–ผ๐—๐—Ž๐—‹๐–พ ๐—๐—๐–พ๐—‹๐–พ ๐–พ๐–บ๐–ผ๐— ๐—‡๐—ˆ๐–ฝ๐–พ ๐–ผ๐—ˆ๐—‡๐—๐–บ๐—‚๐—‡๐—Œ ๐–บ ๐—„๐–พ๐—’ ๐–บ๐—‡๐–ฝ ๐—๐—๐—ˆ ๐—Œ๐—Ž๐–ป๐—๐—‹๐–พ๐–พ๐—Œ, ๐—…๐–พ๐–ฟ๐— ๐–บ๐—‡๐–ฝ ๐—‹๐—‚๐—€๐—๐—.
๐–ณ๐—๐–พ ๐—…๐–พ๐–ฟ๐— ๐—Œ๐—Ž๐–ป๐—๐—‹๐–พ๐–พ ๐—ˆ๐–ฟ ๐–บ ๐—‡๐—ˆ๐–ฝ๐–พ ๐–ผ๐—ˆ๐—‡๐—๐–บ๐—‚๐—‡๐—Œ ๐—ˆ๐—‡๐—…๐—’ ๐—‡๐—ˆ๐–ฝ๐–พ๐—Œ ๐—๐—‚๐—๐— ๐—„๐–พ๐—’๐—Œ ๐—…๐–พ๐—Œ๐—Œ๐–พ๐—‹ ๐—๐—๐–บ๐—‡ ๐—๐—๐–พ ๐—‡๐—ˆ๐–ฝ๐–พโ€™๐—Œ ๐—„๐–พ๐—’. ๐–ณ๐—๐–พ ๐—‹๐—‚๐—€๐—๐— ๐—Œ๐—Ž๐–ป๐—๐—‹๐–พ๐–พ ๐—ˆ๐–ฟ ๐–บ ๐—‡๐—ˆ๐–ฝ๐–พ ๐–ผ๐—ˆ๐—‡๐—๐–บ๐—‚๐—‡๐—Œ ๐—ˆ๐—‡๐—…๐—’ ๐—‡๐—ˆ๐–ฝ๐–พ๐—Œ ๐—๐—‚๐—๐— ๐—„๐–พ๐—’๐—Œ ๐—€๐—‹๐–พ๐–บ๐—๐–พ๐—‹ ๐—๐—๐–บ๐—‡ ๐—๐—๐–พ ๐—‡๐—ˆ๐–ฝ๐–พโ€™๐—Œ ๐—„๐–พ๐—’.

๐–ซ๐–พ๐— ๏ผด = ๐–ป๐—‚๐—‡๐–บ๐—‹๐—’ ๐—Œ๐–พ๐–บ๐—‹๐–ผ๐— ๐—๐—‹๐–พ๐–พ
๐–ซ๐–พ๐— ๏ผฎ = ๐—‡๐—ˆ๐–ฝ๐–พ
๐–ฅ๐—ˆ๐—‹ ๐–พ๐—๐–พ๐—‹๐—’ ๐—‡๐—ˆ๐–ฝ๐–พ ๏ผฎ ๐—‚๐—‡ ๏ผด, ๐—‚๐–ฟ ๏ผฎ.๐—…๐–พ๐–ฟ๐— ๐–บ๐—‡๐–ฝ ๏ผฎ.๐—‹๐—‚๐—€๐—๐— ๐–พ๐—‘๐—‚๐—Œ๐—, ๐—๐—๐–พ๐—‡:

  • ๐– ๐—…๐—… ๐—๐–บ๐—…๐—Ž๐–พ๐—Œ ๐—‚๐—‡ ๐—๐—๐–พ ๐—Œ๐—Ž๐–ป๐—๐—‹๐–พ๐–พ ๐—‹๐—ˆ๐—ˆ๐—๐–พ๐–ฝ ๐–บ๐— ๏ผฎ.๐—…๐–พ๐–ฟ๐— ๐–บ๐—‹๐–พ ๐—…๐–พ๐—Œ๐—Œ ๐—๐—๐–บ๐—‡ ๏ผฎ.๐—๐–บ๐—…๐—Ž๐–พ.
  • ๐– ๐—…๐—… ๐—๐–บ๐—…๐—Ž๐–พ๐—Œ ๐—‚๐—‡ ๐—๐—๐–พ ๐—Œ๐—Ž๐–ป๐—๐—‹๐–พ๐–พ ๐—‹๐—ˆ๐—ˆ๐—๐–พ๐–ฝ ๐–บ๐— ๏ผฎ.๐—‹๐—‚๐—€๐—๐— ๐–บ๐—‹๐–พ ๐—€๐—‹๐–พ๐–บ๐—๐–พ๐—‹ ๐—๐—๐–บ๐—‡ ๏ผฎ.๐—๐–บ๐—…๐—Ž๐–พ.

binary search tree

Operations

  • ๐–ฒ๐–พ๐–บ๐—‹๐–ผ๐—: ๐–ก๐–พ๐–ผ๐–บ๐—Ž๐—Œ๐–พ ๐—ˆ๐–ฟ ๐—‚๐—๐—Œ ๐—ˆ๐—‹๐–ฝ๐–พ๐—‹๐–พ๐–ฝ ๐—‡๐–บ๐—๐—Ž๐—‹๐–พ, ๐—Œ๐–พ๐–บ๐—‹๐–ผ๐—๐—‚๐—‡๐—€ ๐–ฟ๐—ˆ๐—‹ ๐–บ๐—‡ ๐–พ๐—…๐–พ๐—†๐–พ๐—‡๐— ๐—‚๐—‡ ๐–บ ๐–ก๐–ฒ๐–ณ ๐—‚๐—Œ ๐—๐—’๐—‰๐—‚๐–ผ๐–บ๐—…๐—…๐—’ ฮ˜(๐—…๐—ˆ๐—€ ๐—‡) ๐—ˆ๐—‹ ๐–ฎ(๐—‡) ๐—๐—๐–พ๐—‡ ๐—๐—๐–พ ๐—๐—‹๐–พ๐–พ ๐—‹๐–พ๐—Œ๐–พ๐—†๐–ป๐—…๐–พ๐—Œ ๐–บ ๐—…๐—‚๐—‡๐—„๐–พ๐–ฝ ๐—…๐—‚๐—Œ๐—
  • ๐–จ๐—‡๐—Œ๐–พ๐—‹๐—: ๐–ณ๐—’๐—‰๐—‚๐–ผ๐–บ๐—…๐—…๐—’ ฮ˜(๐—…๐—ˆ๐—€ ๐—‡) ๐—๐—๐–พ๐—‡ ๐—๐—๐–พ ๐—๐—‹๐–พ๐–พ ๐—‹๐–พ๐—†๐–บ๐—‚๐—‡๐—Œ ๐—‹๐–พ๐–บ๐—Œ๐—ˆ๐—‡๐–บ๐–ป๐—…๐—’ ๐–ป๐–บ๐—…๐–บ๐—‡๐–ผ๐–พ๐–ฝ ๐—ˆ๐—‹ ๐–ฎ(๐—‡) ๐—‚๐–ฟ ๐—๐—๐–พ ๐—๐—‹๐–พ๐–พ ๐—‚๐—Œ ๐—๐—‚๐—€๐—๐—…๐—’ ๐—Ž๐—‡๐–ป๐–บ๐—…๐–บ๐—‡๐–ผ๐–พ๐–ฝ
  • ๐–ฃ๐–พ๐—…๐–พ๐—๐–พ: ๐–ฒ๐—‚๐—†๐—‚๐—…๐–บ๐—‹ ๐—๐—ˆ ๐—Œ๐–พ๐–บ๐—‹๐–ผ๐— ๐–บ๐—‡๐–ฝ ๐—‚๐—‡๐—Œ๐–พ๐—‹๐— - ฮ˜(๐—…๐—ˆ๐—€ ๐—‡) ๐—ˆ๐—‹ ๐–ฎ(๐—‡) ๐–ฟ๐—ˆ๐—‹ ๐—๐—‚๐—€๐—๐—…๐—’ ๐—Ž๐—‡๐–ป๐–บ๐—…๐–บ๐—‡๐–ผ๐–พ๐–ฝ ๐—๐—‹๐–พ๐–พ
๐™ธ๐š–๐š™๐š•๐šŽ๐š–๐šŽ๐š—๐š๐šŠ๐š๐š’๐š˜๐š—๐šœ:
rust
csharp

Red-Black Tree

Note

๐–  ๐–ฑ๐–พ๐–ฝ-๐–ก๐—…๐–บ๐–ผ๐—„ ๐–ณ๐—‹๐–พ๐–พ ๐—‚๐—Œ ๐–บ ๐—„๐—‚๐—‡๐–ฝ ๐—ˆ๐–ฟ ๐—Œ๐–พ๐—…๐–ฟ-๐–ป๐–บ๐—…๐–บ๐—‡๐–ผ๐—‚๐—‡๐—€ ๐–ป๐—‚๐—‡๐–บ๐—‹๐—’ ๐—Œ๐–พ๐–บ๐—‹๐–ผ๐— ๐—๐—‹๐–พ๐–พ ๐—๐—๐–พ๐—‹๐–พ ๐–พ๐–บ๐–ผ๐— ๐—‡๐—ˆ๐–ฝ๐–พ ๐—๐–บ๐—Œ ๐–บ๐—‡ ๐–พ๐—‘๐—๐—‹๐–บ ๐–ป๐—‚๐— ๐–ฟ๐—ˆ๐—‹ ๐–ฝ๐–พ๐—‡๐—ˆ๐—๐—‚๐—‡๐—€ ๐—๐—๐–พ ๐–ผ๐—ˆ๐—…๐—ˆ๐—‹ ๐—ˆ๐–ฟ ๐—๐—๐–พ ๐—‡๐—ˆ๐–ฝ๐–พ, ๐–พ๐—‚๐—๐—๐–พ๐—‹ ๐—‹๐–พ๐–ฝ ๐—ˆ๐—‹ ๐–ป๐—…๐–บ๐–ผ๐—„.

๐–  ๐–ฑ๐–พ๐–ฝ-๐–ก๐—…๐–บ๐–ผ๐—„ ๐–ณ๐—‹๐–พ๐–พ ๐—Œ๐–บ๐—๐—‚๐—Œ๐–ฟ๐—‚๐–พ๐—Œ ๐—๐—๐–พ ๐–ฟ๐—ˆ๐—…๐—…๐—ˆ๐—๐—‚๐—‡๐—€ ๐—‰๐—‹๐—ˆ๐—‰๐–พ๐—‹๐—๐—‚๐–พ๐—Œ:

  • ๐–ค๐–บ๐–ผ๐— ๐—‡๐—ˆ๐–ฝ๐–พ ๐—‚๐—Œ ๐–พ๐—‚๐—๐—๐–พ๐—‹ ๐—‹๐–พ๐–ฝ ๐—ˆ๐—‹ ๐–ป๐—…๐–บ๐–ผ๐—„.
  • ๐–ณ๐—๐–พ ๐—‹๐—ˆ๐—ˆ๐— ๐—‚๐—Œ ๐–บ๐—…๐—๐–บ๐—’๐—Œ ๐–ป๐—…๐–บ๐–ผ๐—„.
  • ๐–ค๐—๐–พ๐—‹๐—’ ๐—‡๐–พ๐— ๐—‡๐—ˆ๐–ฝ๐–พ ๐—‚๐—Œ ๐–บ๐–ฝ๐–ฝ ๐–บ๐—Œ ๐—‹๐–พ๐–ฝ.
  • ๐–ฑ๐–พ๐–ฝ ๐—‡๐—ˆ๐–ฝ๐–พ๐—Œ ๐–ผ๐–บ๐—‡๐—‡๐—ˆ๐— ๐—๐–บ๐—๐–พ ๐—‹๐–พ๐–ฝ ๐–ผ๐—๐—‚๐—…๐–ฝ๐—‹๐–พ๐—‡ (๐—‡๐—ˆ ๐—๐—๐—ˆ ๐—‹๐–พ๐–ฝ ๐—‡๐—ˆ๐–ฝ๐–พ๐—Œ ๐–ผ๐–บ๐—‡ ๐–ป๐–พ ๐–บ๐–ฝ๐—ƒ๐–บ๐–ผ๐–พ๐—‡๐—).
  • ๐–ค๐—๐–พ๐—‹๐—’ ๐—‰๐–บ๐—๐— ๐–ฟ๐—‹๐—ˆ๐—† ๐–บ ๐—‡๐—ˆ๐–ฝ๐–พ ๐—๐—ˆ ๐–บ๐—‡๐—’ ๐—ˆ๐–ฟ ๐—‚๐—๐—Œ ๐–ฝ๐–พ๐—Œ๐–ผ๐–พ๐—‡๐–ฝ๐–บ๐—‡๐— ๐–ญ๐–ด๐–ซ๐–ซ ๐—‡๐—ˆ๐–ฝ๐–พ๐—Œ ๐—€๐—ˆ๐–พ๐—Œ ๐—๐—๐—‹๐—ˆ๐—Ž๐—€๐— ๐—๐—๐–พ ๐—Œ๐–บ๐—†๐–พ ๐—‡๐—Ž๐—†๐–ป๐–พ๐—‹ ๐—ˆ๐–ฟ ๐–ป๐—…๐–บ๐–ผ๐—„ ๐—‡๐—ˆ๐–ฝ๐–พ๐—Œ.

๐– ๐–ฟ๐—๐–พ๐—‹ ๐—‚๐—‡๐—Œ๐–พ๐—‹๐—๐—‚๐—ˆ๐—‡ ๐–บ๐—‡๐–ฝ ๐–ฝ๐–พ๐—…๐–พ๐—๐—‚๐—ˆ๐—‡, ๐—๐—๐–พ ๐—‹๐–พ๐–ฝ-๐–ป๐—…๐–บ๐–ผ๐—„ ๐—‹๐—Ž๐—…๐–พ๐—Œ ๐–บ๐—‹๐–พ ๐—‹๐–พ๐—๐—‚๐–พ๐—๐–พ๐–ฝ. ๐–จ๐–ฟ ๐—๐—๐–พ๐—’ ๐—๐–บ๐—๐–พ ๐–ป๐–พ๐–พ๐—‡ ๐—๐—‚๐—ˆ๐—…๐–บ๐—๐–พ๐–ฝ, ๐—๐—๐–พ๐—’ ๐—†๐—Ž๐—Œ๐— ๐–ป๐–พ ๐—‹๐–พ๐—Œ๐—๐—ˆ๐—‹๐–พ๐–ฝ. ๐–ณ๐—๐–บ๐— ๐—๐–บ๐—‰๐—‰๐–พ๐—‡๐—Œ ๐–ป๐—’ ๐—‹๐–พ๐–ผ๐—ˆ๐—…๐—ˆ๐—‹๐—‚๐—‡๐—€ ๐—‡๐—ˆ๐–ฝ๐–พ๐—Œ ๐–บ๐—‡๐–ฝ ๐–ป๐—’ ๐—‹๐—ˆ๐—๐–บ๐—๐—‚๐—ˆ๐—‡๐—Œ.

Red-Black Tree Adjustments

๐–จ๐—‡ ๐–บ ๐–ฑ๐–พ๐–ฝ-๐–ก๐—…๐–บ๐–ผ๐—„ ๐–ณ๐—‹๐–พ๐–พ, ๐–บ๐–ฟ๐—๐–พ๐—‹ ๐—‚๐—‡๐—Œ๐–พ๐—‹๐—๐—‚๐—ˆ๐—‡ ๐—ˆ๐—‹ ๐–ฝ๐–พ๐—…๐–พ๐—๐—‚๐—ˆ๐—‡, ๐—๐—๐–พ ๐—๐—‹๐–พ๐–พ ๐—†๐—‚๐—€๐—๐— ๐—๐—‚๐—ˆ๐—…๐–บ๐—๐–พ ๐—๐—๐–พ ๐—‹๐–พ๐–ฝ-๐–ป๐—…๐–บ๐–ผ๐—„ ๐—‰๐—‹๐—ˆ๐—‰๐–พ๐—‹๐—๐—‚๐–พ๐—Œ. ๐–ณ๐—ˆ ๐–ฟ๐—‚๐—‘ ๐—๐—๐–พ๐—Œ๐–พ ๐—๐—‚๐—ˆ๐—…๐–บ๐—๐—‚๐—ˆ๐—‡๐—Œ, ๐—๐—๐—ˆ ๐—†๐–บ๐—‚๐—‡ ๐—ˆ๐—‰๐–พ๐—‹๐–บ๐—๐—‚๐—ˆ๐—‡๐—Œ ๐–บ๐—‹๐–พ ๐—Ž๐—Œ๐–พ๐–ฝ: ๐—‹๐—ˆ๐—๐–บ๐—๐—‚๐—ˆ๐—‡ ๐–บ๐—‡๐–ฝ ๐—‹๐–พ๐–ผ๐—ˆ๐—…๐—ˆ๐—‹๐—‚๐—‡๐—€. ๐–ณ๐—๐–พ ๐–ผ๐—๐—ˆ๐—‚๐–ผ๐–พ ๐–ป๐–พ๐—๐—๐–พ๐–พ๐—‡ ๐—๐—๐–พ๐—Œ๐–พ ๐—ˆ๐—‰๐–พ๐—‹๐–บ๐—๐—‚๐—ˆ๐—‡๐—Œ ๐–ฝ๐–พ๐—‰๐–พ๐—‡๐–ฝ๐—Œ ๐—ˆ๐—‡ ๐—๐—๐–พ ๐—Œ๐—‰๐–พ๐–ผ๐—‚๐–ฟ๐—‚๐–ผ ๐—Œ๐–ผ๐–พ๐—‡๐–บ๐—‹๐—‚๐—ˆ ๐–พ๐—‡๐–ผ๐—ˆ๐—Ž๐—‡๐—๐–พ๐—‹๐–พ๐–ฝ.

๐–จ๐—‡๐—Œ๐–พ๐—‹๐—๐—‚๐—ˆ๐—‡ ๐–ข๐–บ๐—Œ๐–พ๐—Œ

  • ๐–ณ๐—๐–พ ๐—๐—‹๐–พ๐–พ ๐—‚๐—Œ ๐–พ๐—†๐—‰๐—๐—’.

    • ๐–ณ๐—๐–พ ๐—‡๐–พ๐—๐—…๐—’ ๐—‚๐—‡๐—Œ๐–พ๐—‹๐—๐–พ๐–ฝ ๐—‡๐—ˆ๐–ฝ๐–พ ๐—‚๐—Œ ๐—†๐–บ๐–ฝ๐–พ ๐—๐—๐–พ ๐—‹๐—ˆ๐—ˆ๐— ๐–บ๐—‡๐–ฝ ๐–ผ๐—ˆ๐—…๐—ˆ๐—‹๐–พ๐–ฝ ๐–ป๐—…๐–บ๐–ผ๐—„.
  • ๐–ณ๐—๐–พ ๐—‰๐–บ๐—‹๐–พ๐—‡๐— ๐—ˆ๐–ฟ ๐—๐—๐–พ ๐—‡๐–พ๐—๐—…๐—’ ๐—‚๐—‡๐—Œ๐–พ๐—‹๐—๐–พ๐–ฝ ๐—‡๐—ˆ๐–ฝ๐–พ ๐—‚๐—Œ ๐–ป๐—…๐–บ๐–ผ๐—„.

    • ๐–ญ๐—ˆ ๐—๐—‚๐—ˆ๐—…๐–บ๐—๐—‚๐—ˆ๐—‡๐—Œ ๐–บ๐—‹๐–พ ๐—‚๐—‡๐—๐—‹๐—ˆ๐–ฝ๐—Ž๐–ผ๐–พ๐–ฝ. ๐–ญ๐—ˆ ๐—‹๐—ˆ๐—๐–บ๐—๐—‚๐—ˆ๐—‡ ๐—ˆ๐—‹ ๐—‹๐–พ๐–ผ๐—ˆ๐—…๐—ˆ๐—‹๐—‚๐—‡๐—€ ๐—‚๐—Œ ๐—‡๐–พ๐–พ๐–ฝ๐–พ๐–ฝ.
      rb
  • ๐–ณ๐—๐–พ ๐—‰๐–บ๐—‹๐–พ๐—‡๐— ๐–บ๐—‡๐–ฝ ๐—Ž๐—‡๐–ผ๐—…๐–พ ๐–บ๐—‹๐–พ ๐—‹๐–พ๐–ฝ (๐–ฏ๐–บ๐—‹๐–พ๐—‡๐—-๐—Ž๐—‡๐–ผ๐—…๐–พ ๐—‹๐–พ๐–ฝ ๐–ผ๐–บ๐—Œ๐–พ).

    • ๐– ๐–ผ๐—๐—‚๐—ˆ๐—‡: ๐–ฑ๐–พ๐–ผ๐—ˆ๐—…๐—ˆ๐—‹ ๐—๐—๐–พ ๐—‰๐–บ๐—‹๐–พ๐—‡๐— ๐–บ๐—‡๐–ฝ ๐—Ž๐—‡๐–ผ๐—…๐–พ ๐–ป๐—…๐–บ๐–ผ๐—„, ๐–บ๐—‡๐–ฝ ๐—๐—๐–พ ๐—€๐—‹๐–บ๐—‡๐–ฝ๐—‰๐–บ๐—‹๐–พ๐—‡๐— ๐—‹๐–พ๐–ฝ.
    • ๐–ญ๐–พ๐—‘๐— ๐–ฒ๐—๐–พ๐—‰: ๐–จ๐–ฟ ๐—๐—๐–พ ๐—€๐—‹๐–บ๐—‡๐–ฝ๐—‰๐–บ๐—‹๐–พ๐—‡๐— ๐—‚๐—Œ ๐—๐—๐–พ ๐—‹๐—ˆ๐—ˆ๐—, ๐—‚๐— ๐—‚๐—Œ ๐—‹๐–พ๐–ผ๐—ˆ๐—…๐—ˆ๐—‹๐–พ๐–ฝ ๐–ป๐—…๐–บ๐–ผ๐—„. ๐–ฎ๐—๐—๐–พ๐—‹๐—๐—‚๐—Œ๐–พ, ๐–ผ๐—๐–พ๐–ผ๐—„ ๐–ฟ๐—ˆ๐—‹ ๐–ฟ๐—Ž๐—‹๐—๐—๐–พ๐—‹ ๐—๐—‚๐—ˆ๐—…๐–บ๐—๐—‚๐—ˆ๐—‡๐—Œ ๐—Ž๐—‰ ๐—๐—๐–พ ๐—๐—‹๐–พ๐–พ.
      rb rb
      Before After
  • ๐–ณ๐—๐–พ ๐—‰๐–บ๐—‹๐–พ๐—‡๐— ๐—‚๐—Œ ๐—‹๐–พ๐–ฝ ๐–ป๐—Ž๐— ๐—๐—๐–พ ๐—Ž๐—‡๐–ผ๐—…๐–พ ๐—‚๐—Œ ๐–ป๐—…๐–บ๐–ผ๐—„, ๐–ฟ๐—ˆ๐—‹๐—†๐—‚๐—‡๐—€ ๐–บ ๐—๐—‹๐—‚๐–บ๐—‡๐—€๐—…๐–พ (๐–ฏ๐–บ๐—‹๐–พ๐—‡๐— ๐—‹๐–พ๐–ฝ, ๐—Ž๐—‡๐–ผ๐—…๐–พ ๐–ป๐—…๐–บ๐–ผ๐—„, ๐—๐—‹๐—‚๐–บ๐—‡๐—€๐—…๐–พ ๐–ฟ๐—ˆ๐—‹๐—†๐–บ๐—๐—‚๐—ˆ๐—‡).

    • ๐– ๐–ผ๐—๐—‚๐—ˆ๐—‡: ๐–ฏ๐–พ๐—‹๐–ฟ๐—ˆ๐—‹๐—† ๐–บ ๐—‹๐—ˆ๐—๐–บ๐—๐—‚๐—ˆ๐—‡ ๐—ˆ๐—‡ ๐—๐—๐–พ ๐—‰๐–บ๐—‹๐–พ๐—‡๐— ๐—๐—ˆ ๐—๐—‹๐–บ๐—‡๐—Œ๐–ฟ๐—ˆ๐—‹๐—† ๐—๐—๐–พ ๐–ผ๐–บ๐—Œ๐–พ ๐—‚๐—‡๐—๐—ˆ ๐–บ ๐—…๐—‚๐—‡๐–พ ๐–ฟ๐—ˆ๐—‹๐—†๐–บ๐—๐—‚๐—ˆ๐—‡ (๐–ข๐–บ๐—Œ๐–พ ๐Ÿง).
    • ๐–ฑ๐—ˆ๐—๐–บ๐—๐—‚๐—ˆ๐—‡: ๐–ซ๐–พ๐–ฟ๐— ๐—‹๐—ˆ๐—๐–บ๐—๐—‚๐—ˆ๐—‡ ๐—‚๐–ฟ ๐—๐—๐–พ ๐—๐—‹๐—‚๐–บ๐—‡๐—€๐—…๐–พ ๐—‚๐—Œ ๐—‹๐—‚๐—€๐—๐—-๐—…๐–พ๐–บ๐—‡๐—‚๐—‡๐—€, ๐—‹๐—‚๐—€๐—๐— ๐—‹๐—ˆ๐—๐–บ๐—๐—‚๐—ˆ๐—‡ ๐—‚๐–ฟ ๐—…๐–พ๐–ฟ๐—-๐—…๐–พ๐–บ๐—‡๐—‚๐—‡๐—€.
      • ๐–จ๐—‡ ๐—๐—๐—‚๐—Œ ๐–ผ๐–บ๐—Œ๐–พ, ๐—๐–พ ๐–ฟ๐—‚๐—‹๐—Œ๐— ๐—‹๐—ˆ๐—๐–บ๐—๐–พ ๐–บ๐— ๐—๐—๐–พ ๐—‰๐–บ๐—‹๐–พ๐—‡๐— ๐—‡๐—ˆ๐–ฝ๐–พ ๐—‚๐—‡ ๐—๐—๐–พ ๐—ˆ๐—‰๐—‰๐—ˆ๐—Œ๐—‚๐—๐–พ ๐–ฝ๐—‚๐—‹๐–พ๐–ผ๐—๐—‚๐—ˆ๐—‡ ๐—ˆ๐–ฟ ๐—๐—๐–พ ๐—‚๐—‡๐—Œ๐–พ๐—‹๐—๐–พ๐–ฝ ๐—‡๐—ˆ๐–ฝ๐–พ.
        ๐–ถ๐—๐–บ๐— ๐–ฝ๐—ˆ๐–พ๐—Œ ๐—๐—๐–บ๐— ๐—†๐–พ๐–บ๐—‡?
        ๐–จ๐–ฟ ๐—๐—๐–พ ๐—‚๐—‡๐—Œ๐–พ๐—‹๐—๐–พ๐–ฝ ๐—‡๐—ˆ๐–ฝ๐–พ ๐—‚๐—Œ ๐—๐—๐–พ ๐—…๐–พ๐–ฟ๐— ๐–ผ๐—๐—‚๐—…๐–ฝ ๐—ˆ๐–ฟ ๐—‚๐—๐—Œ ๐—‰๐–บ๐—‹๐–พ๐—‡๐— ๐—‡๐—ˆ๐–ฝ๐–พ, ๐—๐–พ ๐—‹๐—ˆ๐—๐–บ๐—๐–พ ๐—๐—ˆ ๐—๐—๐–พ ๐—‹๐—‚๐—€๐—๐— ๐–บ๐— ๐—๐—๐–พ ๐—‰๐–บ๐—‹๐–พ๐—‡๐— ๐—‡๐—ˆ๐–ฝ๐–พ. ๐–จ๐–ฟ ๐—๐—๐–พ ๐—‚๐—‡๐—Œ๐–พ๐—‹๐—๐–พ๐–ฝ ๐—‡๐—ˆ๐–ฝ๐–พ ๐—‚๐—Œ ๐—๐—๐–พ ๐—‹๐—‚๐—€๐—๐— ๐–ผ๐—๐—‚๐—…๐–ฝ, ๐—๐–พ ๐—‹๐—ˆ๐—๐–บ๐—๐–พ ๐—๐—ˆ ๐—๐—๐–พ ๐—…๐–พ๐–ฟ๐—.
        ๐–จ๐—‡ ๐—๐—๐–พ ๐–พ๐—‘๐–บ๐—†๐—‰๐—…๐–พ, ๐—๐—๐–พ ๐—‚๐—‡๐—Œ๐–พ๐—‹๐—๐–พ๐–ฝ ๐—‡๐—ˆ๐–ฝ๐–พ (๐Ÿฃ๐Ÿฉ) ๐—‚๐—Œ ๐–บ ๐—…๐–พ๐–ฟ๐— ๐–ผ๐—๐—‚๐—…๐–ฝ, ๐—Œ๐—ˆ ๐—๐–พ ๐—‹๐—ˆ๐—๐–บ๐—๐–พ ๐—๐—ˆ ๐—๐—๐–พ ๐—‹๐—‚๐—€๐—๐— ๐–บ๐— ๐—๐—๐–พ ๐—‰๐–บ๐—‹๐–พ๐—‡๐— ๐—‡๐—ˆ๐–ฝ๐–พ (๐Ÿค๐Ÿข ๐—‚๐—‡ ๐—๐—๐–พ ๐–พ๐—‘๐–บ๐—†๐—‰๐—…๐–พ)

      • ๐–ฒ๐–พ๐–ผ๐—ˆ๐—‡๐–ฝ, ๐—๐–พ ๐—‹๐—ˆ๐—๐–บ๐—๐–พ ๐–บ๐— ๐—๐—๐–พ ๐—€๐—‹๐–บ๐—‡๐–ฝ๐—‰๐–บ๐—‹๐–พ๐—‡๐— ๐—‡๐—ˆ๐–ฝ๐–พ ๐—‚๐—‡ ๐—๐—๐–พ ๐—ˆ๐—‰๐—‰๐—ˆ๐—Œ๐—‚๐—๐–พ ๐–ฝ๐—‚๐—‹๐–พ๐–ผ๐—๐—‚๐—ˆ๐—‡ ๐—๐—ˆ ๐—๐—๐–พ ๐—‰๐—‹๐–พ๐—๐—‚๐—ˆ๐—Ž๐—Œ ๐—‹๐—ˆ๐—๐–บ๐—๐—‚๐—ˆ๐—‡. ๐–จ๐—‡ ๐—๐—๐–พ ๐–พ๐—‘๐–บ๐—†๐—‰๐—…๐–พ, ๐—๐–พ ๐—‹๐—ˆ๐—๐–บ๐—๐–พ ๐—…๐–พ๐–ฟ๐— ๐–บ๐—‹๐—ˆ๐—Ž๐—‡๐–ฝ ๐—๐—๐–พ ๐Ÿฃ๐Ÿง

      • ๐–ถ๐–พ ๐–ผ๐—ˆ๐—…๐—ˆ๐—‹ ๐—๐—๐–พ ๐—‡๐—ˆ๐–ฝ๐–พ ๐—๐–พ ๐—ƒ๐—Ž๐—Œ๐— ๐—‚๐—‡๐—Œ๐–พ๐—‹๐—๐–พ๐–ฝ (๐Ÿฃ๐Ÿฉ ๐—‚๐—‡ ๐—๐—๐–พ ๐–พ๐—‘๐–บ๐—†๐—‰๐—…๐–พ) ๐–ป๐—…๐–บ๐–ผ๐—„ ๐–บ๐—‡๐–ฝ ๐—๐—๐–พ ๐—ˆ๐—‹๐—‚๐—€๐—‚๐—‡๐–บ๐—… ๐—€๐—‹๐–บ๐—‡๐–ฝ๐—‰๐–บ๐—‹๐–พ๐—‡๐— (๐Ÿฃ๐Ÿง ๐—‚๐—‡ ๐—๐—๐–พ ๐–พ๐—‘๐–บ๐—†๐—‰๐—…๐–พ) ๐—‹๐–พ๐–ฝ

        rb rb rb
  • ๐–ณ๐—๐–พ ๐—‰๐–บ๐—‹๐–พ๐—‡๐— ๐—‚๐—Œ ๐—‹๐–พ๐–ฝ, ๐—๐—๐–พ ๐—Ž๐—‡๐–ผ๐—…๐–พ ๐—‚๐—Œ ๐–ป๐—…๐–บ๐–ผ๐—„, ๐–บ๐—‡๐–ฝ ๐—๐—๐–พ ๐—‡๐—ˆ๐–ฝ๐–พ๐—Œ ๐–ฟ๐—ˆ๐—‹๐—† ๐–บ ๐—Œ๐—๐—‹๐–บ๐—‚๐—€๐—๐— ๐—…๐—‚๐—‡๐–พ (๐–ฏ๐–บ๐—‹๐–พ๐—‡๐— ๐—‹๐–พ๐–ฝ, ๐—Ž๐—‡๐–ผ๐—…๐–พ ๐–ป๐—…๐–บ๐–ผ๐—„, ๐—…๐—‚๐—‡๐–พ ๐–ฟ๐—ˆ๐—‹๐—†๐–บ๐—๐—‚๐—ˆ๐—‡).

    • ๐– ๐–ผ๐—๐—‚๐—ˆ๐—‡: ๐–ฑ๐—ˆ๐—๐–บ๐—๐–พ ๐—๐—๐–พ ๐—€๐—‹๐–บ๐—‡๐–ฝ๐—‰๐–บ๐—‹๐–พ๐—‡๐—, ๐—๐—๐–พ๐—‡ ๐—Œ๐—๐–บ๐—‰ ๐—๐—๐–พ ๐–ผ๐—ˆ๐—…๐—ˆ๐—‹๐—Œ ๐—ˆ๐–ฟ ๐—๐—๐–พ ๐—€๐—‹๐–บ๐—‡๐–ฝ๐—‰๐–บ๐—‹๐–พ๐—‡๐— ๐–บ๐—‡๐–ฝ ๐—‰๐–บ๐—‹๐–พ๐—‡๐—.
    • ๐–ฑ๐—ˆ๐—๐–บ๐—๐—‚๐—ˆ๐—‡: ๐–ฑ๐—‚๐—€๐—๐— ๐—‹๐—ˆ๐—๐–บ๐—๐—‚๐—ˆ๐—‡ ๐—‚๐–ฟ ๐—๐—๐–พ ๐—…๐—‚๐—‡๐–พ ๐—‚๐—Œ ๐—…๐–พ๐–ฟ๐—-๐—…๐–พ๐–บ๐—‡๐—‚๐—‡๐—€, ๐—…๐–พ๐–ฟ๐— ๐—‹๐—ˆ๐—๐–บ๐—๐—‚๐—ˆ๐—‡ ๐—‚๐–ฟ ๐—‹๐—‚๐—€๐—๐—-๐—…๐–พ๐–บ๐—‡๐—‚๐—‡๐—€.
      • "L๐—‚๐—‡๐–พ ๐–ฟ๐—ˆ๐—‹๐—†๐–บ๐—๐—‚๐—ˆ๐—‡" ๐—†๐–พ๐–บ๐—‡๐—Œ ๐—๐—๐–บ๐— ๐—๐—๐–พ ๐—‰๐–บ๐—๐— ๐–ฟ๐—‹๐—ˆ๐—† ๐—€๐—‹๐–บ๐—‡๐–ฝ๐—‰๐–บ๐—‹๐–พ๐—‡๐— ๐—๐—ˆ ๐—‚๐—‡๐—Œ๐–พ๐—‹๐—๐–พ๐–ฝ ๐—‡๐—ˆ๐–ฝ๐–พ ๐–ฟ๐—ˆ๐—‹๐—†๐—Œ ๐–บ ๐—…๐—‚๐—‡๐–พ, ๐—Œ๐—Ž๐–ผ๐— ๐–บ๐—Œ ๐—๐—๐–พ ๐Ÿฃ๐Ÿง, ๐Ÿค๐Ÿข, ๐–บ๐—‡๐–ฝ ๐Ÿค๐Ÿง ๐—‚๐—‡ ๐—๐—๐–พ ๐–พ๐—‘๐–บ๐—†๐—‰๐—…๐–พ.
        ๐–จ๐—‡ ๐—๐—๐—‚๐—Œ ๐–ผ๐–บ๐—Œ๐–พ, ๐—๐–พ ๐—‹๐—ˆ๐—๐–บ๐—๐–พ ๐–บ๐— ๐—๐—๐–พ ๐—€๐—‹๐–บ๐—‡๐–ฝ๐—‰๐–บ๐—‹๐–พ๐—‡๐— (๐Ÿฃ๐Ÿง) ๐—‚๐—‡ ๐—๐—๐–พ ๐—ˆ๐—‰๐—‰๐—ˆ๐—Œ๐—‚๐—๐–พ ๐–ฝ๐—‚๐—‹๐–พ๐–ผ๐—๐—‚๐—ˆ๐—‡ ๐—ˆ๐–ฟ ๐—๐—๐–พ ๐—‰๐–บ๐—‹๐–พ๐—‡๐— ๐–บ๐—‡๐–ฝ ๐—‚๐—‡๐—Œ๐–พ๐—‹๐—๐–พ๐–ฝ ๐—‡๐—ˆ๐–ฝ๐–พ (๐–บ๐–ฟ๐—๐–พ๐—‹ ๐–บ๐—…๐—…, ๐–ป๐—ˆ๐—๐— ๐—€๐—ˆ ๐—‚๐—‡ ๐—๐—๐–พ ๐—Œ๐–บ๐—†๐–พ ๐–ฝ๐—‚๐—‹๐–พ๐–ผ๐—๐—‚๐—ˆ๐—‡ ๐—‚๐—‡ ๐—๐—๐—‚๐—Œ ๐–ผ๐–บ๐—Œ๐–พ).
        ๐–จ๐—‡ ๐—๐—๐–พ ๐–พ๐—‘๐–บ๐—†๐—‰๐—…๐–พ, ๐—๐—๐–พ ๐—‰๐–บ๐—‹๐–พ๐—‡๐— ๐–บ๐—‡๐–ฝ ๐—‚๐—‡๐—Œ๐–พ๐—‹๐—๐–พ๐–ฝ ๐—‡๐—ˆ๐–ฝ๐–พ๐—Œ ๐–บ๐—‹๐–พ ๐–ป๐—ˆ๐—๐— ๐—‹๐—‚๐—€๐—๐— ๐–ผ๐—๐—‚๐—…๐–ฝ๐—‹๐–พ๐—‡, ๐—Œ๐—ˆ ๐—๐–พ ๐—‹๐—ˆ๐—๐–บ๐—๐–พ ๐—…๐–พ๐–ฟ๐— ๐–บ๐— ๐—๐—๐–พ ๐—€๐—‹๐–บ๐—‡๐–ฝ๐—‰๐–บ๐—‹๐–พ๐—‡๐—. ๐–ณ๐—๐–พ๐—‡ ๐—๐–พ ๐—‹๐–พ๐–ผ๐—ˆ๐—…๐—ˆ๐—‹ ๐—๐—๐–พ ๐–ฟ๐—ˆ๐—‹๐—†๐–พ๐—‹ ๐—‰๐–บ๐—‹๐–พ๐—‡๐— (๐Ÿค๐Ÿข ๐—‚๐—‡ ๐—๐—๐–พ ๐–พ๐—‘๐–บ๐—†๐—‰๐—…๐–พ) ๐–ป๐—…๐–บ๐–ผ๐—„ ๐–บ๐—‡๐–ฝ ๐—๐—๐–พ ๐–ฟ๐—ˆ๐—‹๐—†๐–พ๐—‹ ๐—€๐—‹๐–บ๐—‡๐–ฝ๐—‰๐–บ๐—‹๐–พ๐—‡๐— (๐Ÿฃ๐Ÿง) ๐—‹๐–พ๐–ฝ.
        rb rb
๐™ธ๐š–๐š™๐š•๐šŽ๐š–๐šŽ๐š—๐š๐šŠ๐š๐š’๐š˜๐š—๐šœ:
rust
csharp

AVL Tree

Note

๐– ๐–ต๐–ซ ๐—๐—‹๐–พ๐–พ ๐—‚๐—Œ ๐–บ ๐—Œ๐–พ๐—…๐–ฟ-๐–ป๐–บ๐—…๐–บ๐—‡๐–ผ๐—‚๐—‡๐—€ ๐–ก๐—‚๐—‡๐–บ๐—‹๐—’ ๐–ฒ๐–พ๐–บ๐—‹๐–ผ๐— ๐–ณ๐—‹๐–พ๐–พ (๐–ก๐–ฒ๐–ณ) ๐—๐—๐–พ๐—‹๐–พ ๐—๐—๐–พ ๐—๐–พ๐—‚๐—€๐—๐—๐—Œ ๐—ˆ๐–ฟ ๐—๐—๐–พ ๐—๐—๐—ˆ ๐–ผ๐—๐—‚๐—…๐–ฝ ๐—Œ๐—Ž๐–ป๐—๐—‹๐–พ๐–พ๐—Œ ๐—ˆ๐–ฟ ๐–บ๐—‡๐—’ ๐—‡๐—ˆ๐–ฝ๐–พ ๐–ฝ๐—‚๐–ฟ๐–ฟ๐–พ๐—‹ ๐–ป๐—’ ๐–บ๐— ๐—†๐—ˆ๐—Œ๐— ๐—ˆ๐—‡๐–พ. ๐–ณ๐—๐—‚๐—Œ ๐–พ๐—‡๐—Œ๐—Ž๐—‹๐–พ๐—Œ ๐—๐—๐–บ๐— ๐—๐—๐–พ ๐—๐—‹๐–พ๐–พ ๐—‹๐–พ๐—†๐–บ๐—‚๐—‡๐—Œ ๐–ป๐–บ๐—…๐–บ๐—‡๐–ผ๐–พ๐–ฝ ๐–บ๐—‡๐–ฝ ๐—†๐–บ๐—‚๐—‡๐—๐–บ๐—‚๐—‡๐—Œ ๐–พ๐–ฟ๐–ฟ๐—‚๐–ผ๐—‚๐–พ๐—‡๐— ๐—Œ๐–พ๐–บ๐—‹๐–ผ๐—, ๐—‚๐—‡๐—Œ๐–พ๐—‹๐—๐—‚๐—ˆ๐—‡, ๐–บ๐—‡๐–ฝ ๐–ฝ๐–พ๐—…๐–พ๐—๐—‚๐—ˆ๐—‡ ๐—ˆ๐—‰๐–พ๐—‹๐–บ๐—๐—‚๐—ˆ๐—‡๐—Œ.

AVL Tree Properties:

  • ๐–ณ๐—๐–พ ๐—๐–พ๐—‚๐—€๐—๐— ๐—ˆ๐–ฟ ๐—๐—๐–พ ๐—๐—‹๐–พ๐–พ ๐—‚๐—Œ ๐–บ๐— ๐—†๐—ˆ๐—Œ๐— <๐—‚>๐—‡ ๐—…๐—ˆ๐—€(<๐—‚>๐—‡), ๐—๐—๐–พ๐—‹๐–พ ๐—‡ ๐—‚๐—Œ ๐—๐—๐–พ ๐—‡๐—Ž๐—†๐–ป๐–พ๐—‹ ๐—ˆ๐–ฟ ๐—‡๐—ˆ๐–ฝ๐–พ๐—Œ ๐—‚๐—‡ ๐—๐—๐–พ ๐—๐—‹๐–พ๐–พ.
  • ๐–ฅ๐—ˆ๐—‹ ๐–พ๐—๐–พ๐—‹๐—’ ๐—‡๐—ˆ๐–ฝ๐–พ ๐—‚๐—‡ ๐—๐—๐–พ ๐—๐—‹๐–พ๐–พ, ๐—๐—๐–พ ๐—๐–พ๐—‚๐—€๐—๐—๐—Œ ๐—ˆ๐–ฟ ๐—๐—๐–พ ๐—…๐–พ๐–ฟ๐— ๐–บ๐—‡๐–ฝ ๐—‹๐—‚๐—€๐—๐— ๐—Œ๐—Ž๐–ป๐—๐—‹๐–พ๐–พ๐—Œ ๐–ฝ๐—‚๐–ฟ๐–ฟ๐–พ๐—‹ ๐–ป๐—’ ๐–บ๐— ๐—†๐—ˆ๐—Œ๐— ๐—ˆ๐—‡๐–พ.
  • ๐–ณ๐—๐–พ ๐—…๐–พ๐–ฟ๐— ๐–บ๐—‡๐–ฝ ๐—‹๐—‚๐—€๐—๐— ๐—Œ๐—Ž๐–ป๐—๐—‹๐–พ๐–พ๐—Œ ๐—ˆ๐–ฟ ๐–พ๐—๐–พ๐—‹๐—’ ๐—‡๐—ˆ๐–ฝ๐–พ ๐–บ๐—‹๐–พ ๐—๐—๐–พ๐—†๐—Œ๐–พ๐—…๐—๐–พ๐—Œ ๐– ๐–ต๐–ซ ๐—๐—‹๐–พ๐–พ๐—Œ.

๐– ๐—‡ ๐– ๐–ต๐–ซ ๐—๐—‹๐–พ๐–พ ๐—Œ๐—๐—ˆ๐—‹๐–พ๐—Œ ๐—‚๐—‡ ๐–พ๐–บ๐–ผ๐— ๐—‡๐—ˆ๐–ฝ๐–พ ๐—๐—๐–พ ๐—๐–พ๐—‚๐—€๐—๐— ๐—ˆ๐–ฟ ๐—๐—๐–พ ๐—Œ๐—Ž๐–ป๐—๐—‹๐–พ๐–พ๐—Œ ๐—‹๐—ˆ๐—ˆ๐—๐–พ๐–ฝ ๐–บ๐— ๐—๐—๐—‚๐—Œ ๐—‡๐—ˆ๐–ฝ๐–พ. ๐–ณ๐—๐–พ๐—‡, ๐–ฟ๐—ˆ๐—‹ ๐–บ๐—‡๐—’ ๐—‡๐—ˆ๐–ฝ๐–พ, ๐—๐–พ ๐–ผ๐–บ๐—‡ ๐–ผ๐—๐–พ๐–ผ๐—„ ๐—‚๐–ฟ ๐—‚๐— ๐—‚๐—Œ ๐—๐–พ๐—‚๐—€๐—๐— ๐–ป๐–บ๐—…๐–บ๐—‡๐–ผ๐–พ๐–ฝ: ๐—๐—๐–บ๐— ๐—๐—๐–พ ๐—๐–พ๐—‚๐—€๐—๐— ๐—ˆ๐–ฟ ๐—๐—๐–พ ๐—…๐–พ๐–ฟ๐— ๐—Œ๐—Ž๐–ป๐—๐—‹๐–พ๐–พ ๐–บ๐—‡๐–ฝ ๐—๐—๐–พ ๐—๐–พ๐—‚๐—€๐—๐— ๐—ˆ๐–ฟ ๐—๐—๐–พ ๐—‹๐—‚๐—€๐—๐— ๐—Œ๐—Ž๐–ป๐—๐—‹๐–พ๐–พ ๐–ฝ๐—‚๐–ฟ๐–ฟ๐–พ๐—‹ ๐–ป๐—’ ๐—‡๐—ˆ ๐—†๐—ˆ๐—‹๐–พ ๐—๐—๐–บ๐—‡ ๐—ˆ๐—‡๐–พ.

avl bf

Balance Factor

๐–ก๐–บ๐—…๐–บ๐—‡๐–ผ๐–พ ๐–ฟ๐–บ๐–ผ๐—๐—ˆ๐—‹ ๐—ˆ๐–ฟ ๐–บ ๐—‡๐—ˆ๐–ฝ๐–พ ๐—‚๐—‡ ๐–บ๐—‡ ๐– ๐–ต๐–ซ ๐—๐—‹๐–พ๐–พ ๐—‚๐—Œ ๐—๐—๐–พ ๐–ฝ๐—‚๐–ฟ๐–ฟ๐–พ๐—‹๐–พ๐—‡๐–ผ๐–พ ๐–ป๐–พ๐—๐—๐–พ๐–พ๐—‡ ๐—๐—๐–พ ๐—๐–พ๐—‚๐—€๐—๐— ๐—ˆ๐–ฟ ๐—๐—๐–พ ๐—…๐–พ๐–ฟ๐— ๐—Œ๐—Ž๐–ป๐—๐—‹๐–พ๐–พ ๐–บ๐—‡๐–ฝ ๐—๐—๐–บ๐— ๐—ˆ๐–ฟ ๐—๐—๐–พ ๐—‹๐—‚๐—€๐—๐— ๐—Œ๐—Ž๐–ป๐—๐—‹๐–พ๐–พ ๐—ˆ๐–ฟ ๐—๐—๐–บ๐— ๐—‡๐—ˆ๐–ฝ๐–พ.
๐–ก๐–บ๐—…๐–บ๐—‡๐–ผ๐–พ ๐–ฅ๐–บ๐–ผ๐—๐—ˆ๐—‹ = (๐–ง๐–พ๐—‚๐—€๐—๐— ๐—ˆ๐–ฟ ๐–ซ๐–พ๐–ฟ๐— ๐–ฒ๐—Ž๐–ป๐—๐—‹๐–พ๐–พ - ๐–ง๐–พ๐—‚๐—€๐—๐— ๐—ˆ๐–ฟ ๐–ฑ๐—‚๐—€๐—๐— ๐–ฒ๐—Ž๐–ป๐—๐—‹๐–พ๐–พ) ๐—ˆ๐—‹ (๐–ง๐–พ๐—‚๐—€๐—๐— ๐—ˆ๐–ฟ ๐–ฑ๐—‚๐—€๐—๐— ๐–ฒ๐—Ž๐–ป๐—๐—‹๐–พ๐–พ - ๐–ง๐–พ๐—‚๐—€๐—๐— ๐—ˆ๐–ฟ ๐–ซ๐–พ๐–ฟ๐— ๐–ฒ๐—Ž๐–ป๐—๐—‹๐–พ๐–พ).
๐–ณ๐—๐–พ ๐—Œ๐–พ๐—…๐–ฟ ๐–ป๐–บ๐—…๐–บ๐—‡๐–ผ๐—‚๐—‡๐—€ ๐—‰๐—‹๐—ˆ๐—‰๐–พ๐—‹๐—๐—’ ๐—ˆ๐–ฟ ๐–บ๐—‡ ๐–บ๐—๐—… ๐—๐—‹๐–พ๐–พ ๐—‚๐—Œ ๐—†๐–บ๐—‚๐—‡๐—๐–บ๐—‚๐—‡๐–พ๐–ฝ ๐–ป๐—’ ๐—๐—๐–พ ๐–ป๐–บ๐—…๐–บ๐—‡๐–ผ๐–พ ๐–ฟ๐–บ๐–ผ๐—๐—ˆ๐—‹. ๐–ณ๐—๐–พ ๐—๐–บ๐—…๐—Ž๐–พ ๐—ˆ๐–ฟ ๐–ป๐–บ๐—…๐–บ๐—‡๐–ผ๐–พ ๐–ฟ๐–บ๐–ผ๐—๐—ˆ๐—‹ ๐—Œ๐—๐—ˆ๐—Ž๐—…๐–ฝ ๐–บ๐—…๐—๐–บ๐—’๐—Œ ๐–ป๐–พ -๐Ÿฃ, ๐Ÿข ๐—ˆ๐—‹ +๐Ÿฃ.

  • ๐–ฅ๐—ˆ๐—‹ ๐–บ ๐—€๐—‚๐—๐–พ๐—‡ ๐—‡๐—ˆ๐–ฝ๐–พ
  • ๐–จ๐–ฟ ๐—…๐–พ๐–ฟ๐— ๐—Œ๐—Ž๐–ป๐—๐—‹๐–พ๐–พ ๐—‚๐—Œ ๐—๐—‚๐—€๐—๐–พ๐—‹: ๐Ÿฃ (๐—‰๐—ˆ๐—Œ๐—‚๐—๐—‚๐—๐–พ ๐—‚๐—‡๐—๐–พ๐—€๐–พ๐—‹)
  • ๐–จ๐–ฟ ๐—‹๐—‚๐—€๐—๐— ๐—Œ๐—Ž๐–ป๐—๐—‹๐–พ๐–พ ๐—‚๐—Œ ๐—๐—‚๐—€๐—๐–พ๐—‹: -๐Ÿฃ (๐—‡๐–พ๐—€๐–บ๐—๐—‚๐—๐–พ ๐—‚๐—‡๐—๐–พ๐—€๐–พ๐—‹)

Insertion and Height Update

  • ๐–ถ๐—๐–พ๐—‡ ๐–บ ๐—‡๐—ˆ๐–ฝ๐–พ ๐—‚๐—Œ ๐—‚๐—‡๐—Œ๐–พ๐—‹๐—๐–พ๐–ฝ ๐—‚๐—‡๐—๐—ˆ ๐–บ๐—‡ ๐– ๐–ต๐–ซ ๐—๐—‹๐–พ๐–พ, ๐—๐—๐–พ ๐—‚๐—‡๐—Œ๐–พ๐—‹๐—๐—‚๐—ˆ๐—‡ ๐—‰๐—‹๐—ˆ๐–ผ๐–พ๐—Œ๐—Œ ๐–ฟ๐—ˆ๐—…๐—…๐—ˆ๐—๐—Œ ๐—๐—๐–พ ๐–ป๐–บ๐—Œ๐—‚๐–ผ ๐—‚๐—‡๐—Œ๐–พ๐—‹๐—๐—‚๐—ˆ๐—‡ ๐—‰๐—‹๐—ˆ๐–ผ๐–พ๐—Œ๐—Œ ๐—ˆ๐–ฟ ๐–บ ๐–ป๐—‚๐—‡๐–บ๐—‹๐—’ ๐—Œ๐–พ๐–บ๐—‹๐–ผ๐— ๐—๐—‹๐–พ๐–พ:
    • ๐–จ๐—‡๐—Œ๐–พ๐—‹๐— ๐—๐—๐–พ ๐–ญ๐—ˆ๐–ฝ๐–พ: ๐–ฒ๐—๐–บ๐—‹๐—๐—‚๐—‡๐—€ ๐–ฟ๐—‹๐—ˆ๐—† ๐—๐—๐–พ ๐—‹๐—ˆ๐—ˆ๐—, ๐—๐—‹๐–บ๐—๐–พ๐—‹๐—Œ๐–พ ๐—๐—๐–พ ๐—๐—‹๐–พ๐–พ ๐—๐—ˆ ๐–ฟ๐—‚๐—‡๐–ฝ ๐—๐—๐–พ ๐–ผ๐—ˆ๐—‹๐—‹๐–พ๐–ผ๐— ๐—‰๐—ˆ๐—Œ๐—‚๐—๐—‚๐—ˆ๐—‡ ๐–ฟ๐—ˆ๐—‹ ๐—๐—๐–พ ๐—‡๐–พ๐— ๐—‡๐—ˆ๐–ฝ๐–พ, ๐—†๐–บ๐—‚๐—‡๐—๐–บ๐—‚๐—‡๐—‚๐—‡๐—€ ๐—๐—๐–พ ๐–ป๐—‚๐—‡๐–บ๐—‹๐—’ ๐—Œ๐–พ๐–บ๐—‹๐–ผ๐— ๐—๐—‹๐–พ๐–พ ๐—‰๐—‹๐—ˆ๐—‰๐–พ๐—‹๐—๐—’.
    • ๐–ด๐—‰๐–ฝ๐–บ๐—๐–พ ๐–ง๐–พ๐—‚๐—€๐—๐—๐—Œ: ๐– ๐–ฟ๐—๐–พ๐—‹ ๐—‚๐—‡๐—Œ๐–พ๐—‹๐—๐—‚๐—ˆ๐—‡, ๐–ป๐–บ๐–ผ๐—„๐—๐—‹๐–บ๐–ผ๐—„ ๐—Ž๐—‰ ๐—๐—๐–พ ๐—๐—‹๐–พ๐–พ ๐–ฟ๐—‹๐—ˆ๐—† ๐—๐—๐–พ ๐—‚๐—‡๐—Œ๐–พ๐—‹๐—๐–พ๐–ฝ ๐—‡๐—ˆ๐–ฝ๐–พ ๐—๐—ˆ ๐—๐—๐–พ ๐—‹๐—ˆ๐—ˆ๐—, ๐—Ž๐—‰๐–ฝ๐–บ๐—๐—‚๐—‡๐—€ ๐—๐—๐–พ ๐—๐–พ๐—‚๐—€๐—๐—๐—Œ ๐—ˆ๐–ฟ ๐–พ๐–บ๐–ผ๐— ๐—‡๐—ˆ๐–ฝ๐–พ ๐–พ๐—‡๐–ผ๐—ˆ๐—Ž๐—‡๐—๐–พ๐—‹๐–พ๐–ฝ.
    • ๐–ข๐—๐–พ๐–ผ๐—„ ๐–ก๐–บ๐—…๐–บ๐—‡๐–ผ๐–พ ๐–ฅ๐–บ๐–ผ๐—๐—ˆ๐—‹๐—Œ
    • ๐–ถ๐—๐—‚๐—…๐–พ ๐—Ž๐—‰๐–ฝ๐–บ๐—๐—‚๐—‡๐—€ ๐—๐–พ๐—‚๐—€๐—๐—๐—Œ, ๐–ผ๐–บ๐—…๐–ผ๐—Ž๐—…๐–บ๐—๐–พ ๐—๐—๐–พ ๐–ป๐–บ๐—…๐–บ๐—‡๐–ผ๐–พ ๐–ฟ๐–บ๐–ผ๐—๐—ˆ๐—‹ ๐–ฟ๐—ˆ๐—‹ ๐–พ๐–บ๐–ผ๐— ๐—‡๐—ˆ๐–ฝ๐–พ. ๐–จ๐–ฟ ๐–บ ๐—‡๐—ˆ๐–ฝ๐–พ'๐—Œ ๐–ป๐–บ๐—…๐–บ๐—‡๐–ผ๐–พ ๐–ฟ๐–บ๐–ผ๐—๐—ˆ๐—‹ ๐–ป๐–พ๐–ผ๐—ˆ๐—†๐–พ๐—Œ ๐Ÿค ๐—ˆ๐—‹ -๐Ÿค, ๐—‰๐–พ๐—‹๐–ฟ๐—ˆ๐—‹๐—† ๐—๐—๐–พ ๐–บ๐—‰๐—‰๐—‹๐—ˆ๐—‰๐—‹๐—‚๐–บ๐—๐–พ ๐—‹๐—ˆ๐—๐–บ๐—๐—‚๐—ˆ๐—‡(๐—Œ) ๐—๐—ˆ ๐—‹๐–พ๐–ป๐–บ๐—…๐–บ๐—‡๐–ผ๐–พ ๐—๐—๐–พ ๐—๐—‹๐–พ๐–พ.

Handling Imbalance (Balance Factor of 2 or -2)

  • ๐–ถ๐—๐–พ๐—‡ ๐—๐—๐–พ ๐–ป๐–บ๐—…๐–บ๐—‡๐–ผ๐–พ ๐–ฟ๐–บ๐–ผ๐—๐—ˆ๐—‹ ๐—ˆ๐–ฟ ๐–บ ๐—‡๐—ˆ๐–ฝ๐–พ ๐–ป๐–พ๐–ผ๐—ˆ๐—†๐–พ๐—Œ ๐Ÿค ๐—ˆ๐—‹ -๐Ÿค, ๐—‚๐— ๐—‚๐—‡๐–ฝ๐—‚๐–ผ๐–บ๐—๐–พ๐—Œ ๐—๐—๐–บ๐— ๐—๐—๐–พ ๐—๐—‹๐–พ๐–พ ๐—๐–บ๐—Œ ๐–ป๐–พ๐–ผ๐—ˆ๐—†๐–พ ๐—‚๐—†๐–ป๐–บ๐—…๐–บ๐—‡๐–ผ๐–พ๐–ฝ ๐–บ๐— ๐—๐—๐–บ๐— ๐—‡๐—ˆ๐–ฝ๐–พ, ๐–บ๐—‡๐–ฝ ๐—‹๐—ˆ๐—๐–บ๐—๐—‚๐—ˆ๐—‡๐—Œ ๐–บ๐—‹๐–พ ๐—‹๐–พ๐—Š๐—Ž๐—‚๐—‹๐–พ๐–ฝ ๐—๐—ˆ ๐—‹๐–พ๐—Œ๐—๐—ˆ๐—‹๐–พ ๐—๐—๐–พ ๐–ป๐–บ๐—…๐–บ๐—‡๐–ผ๐–พ. ๐–ณ๐—๐–พ๐—‹๐–พ ๐–บ๐—‹๐–พ ๐–ฟ๐—ˆ๐—Ž๐—‹ ๐—‰๐—ˆ๐—Œ๐—Œ๐—‚๐–ป๐—…๐–พ ๐–ผ๐–บ๐—Œ๐–พ๐—Œ ๐—ˆ๐–ฟ ๐—‚๐—†๐–ป๐–บ๐—…๐–บ๐—‡๐–ผ๐–พ:
    • ๐–ซ๐–พ๐–ฟ๐—-๐–ซ๐–พ๐–ฟ๐— (๐–ซ๐–ซ): ๐–จ๐—†๐–ป๐–บ๐—…๐–บ๐—‡๐–ผ๐–พ ๐–ผ๐–บ๐—Ž๐—Œ๐–พ๐–ฝ ๐–ป๐—’ ๐–บ๐–ฝ๐–ฝ๐—‚๐—‡๐—€ ๐–บ ๐—‡๐—ˆ๐–ฝ๐–พ ๐—๐—ˆ ๐—๐—๐–พ ๐—…๐–พ๐–ฟ๐— ๐—Œ๐—Ž๐–ป๐—๐—‹๐–พ๐–พ ๐—ˆ๐–ฟ ๐—๐—๐–พ ๐—…๐–พ๐–ฟ๐— ๐–ผ๐—๐—‚๐—…๐–ฝ. ๐–ฒ๐—ˆ๐—…๐—Ž๐—๐—‚๐—ˆ๐—‡: ๐–ฒ๐—‚๐—‡๐—€๐—…๐–พ ๐—‹๐—‚๐—€๐—๐— ๐—‹๐—ˆ๐—๐–บ๐—๐—‚๐—ˆ๐—‡.
    • ๐–ฑ๐—‚๐—€๐—๐—-๐–ฑ๐—‚๐—€๐—๐— (๐–ฑ๐–ฑ): ๐–จ๐—†๐–ป๐–บ๐—…๐–บ๐—‡๐–ผ๐–พ ๐–ผ๐–บ๐—Ž๐—Œ๐–พ๐–ฝ ๐–ป๐—’ ๐–บ๐–ฝ๐–ฝ๐—‚๐—‡๐—€ ๐–บ ๐—‡๐—ˆ๐–ฝ๐–พ ๐—๐—ˆ ๐—๐—๐–พ ๐—‹๐—‚๐—€๐—๐— ๐—Œ๐—Ž๐–ป๐—๐—‹๐–พ๐–พ ๐—ˆ๐–ฟ ๐—๐—๐–พ ๐—‹๐—‚๐—€๐—๐— ๐–ผ๐—๐—‚๐—…๐–ฝ. ๐–ฒ๐—ˆ๐—…๐—Ž๐—๐—‚๐—ˆ๐—‡: ๐–ฒ๐—‚๐—‡๐—€๐—…๐–พ ๐—…๐–พ๐–ฟ๐— ๐—‹๐—ˆ๐—๐–บ๐—๐—‚๐—ˆ๐—‡.
    • ๐–ซ๐–พ๐–ฟ๐—-๐–ฑ๐—‚๐—€๐—๐— (๐–ซ๐–ฑ): ๐–จ๐—†๐–ป๐–บ๐—…๐–บ๐—‡๐–ผ๐–พ ๐–ผ๐–บ๐—Ž๐—Œ๐–พ๐–ฝ ๐–ป๐—’ ๐–บ๐–ฝ๐–ฝ๐—‚๐—‡๐—€ ๐–บ ๐—‡๐—ˆ๐–ฝ๐–พ ๐—๐—ˆ ๐—๐—๐–พ ๐—‹๐—‚๐—€๐—๐— ๐—Œ๐—Ž๐–ป๐—๐—‹๐–พ๐–พ ๐—ˆ๐–ฟ ๐—๐—๐–พ ๐—…๐–พ๐–ฟ๐— ๐–ผ๐—๐—‚๐—…๐–ฝ. ๐–ฒ๐—ˆ๐—…๐—Ž๐—๐—‚๐—ˆ๐—‡: ๐–ฃ๐—ˆ๐—Ž๐–ป๐—…๐–พ ๐—‹๐—ˆ๐—๐–บ๐—๐—‚๐—ˆ๐—‡, ๐—…๐–พ๐–ฟ๐— ๐—‹๐—ˆ๐—๐–บ๐—๐—‚๐—ˆ๐—‡ ๐—ˆ๐—‡ ๐—๐—๐–พ ๐—…๐–พ๐–ฟ๐— ๐–ผ๐—๐—‚๐—…๐–ฝ ๐–ฟ๐—ˆ๐—…๐—…๐—ˆ๐—๐–พ๐–ฝ ๐–ป๐—’ ๐–บ ๐—‹๐—‚๐—€๐—๐— ๐—‹๐—ˆ๐—๐–บ๐—๐—‚๐—ˆ๐—‡ ๐—ˆ๐—‡ ๐—๐—๐–พ ๐—‹๐—ˆ๐—ˆ๐— ๐—ˆ๐–ฟ ๐—๐—๐–พ ๐—‚๐—†๐–ป๐–บ๐—…๐–บ๐—‡๐–ผ๐–พ๐–ฝ ๐—Œ๐—Ž๐–ป๐—๐—‹๐–พ๐–พ.
    • ๐–ฑ๐—‚๐—€๐—๐—-๐–ซ๐–พ๐–ฟ๐— (๐–ฑ๐–ซ): ๐–จ๐—†๐–ป๐–บ๐—…๐–บ๐—‡๐–ผ๐–พ ๐–ผ๐–บ๐—Ž๐—Œ๐–พ๐–ฝ ๐–ป๐—’ ๐–บ๐–ฝ๐–ฝ๐—‚๐—‡๐—€ ๐–บ ๐—‡๐—ˆ๐–ฝ๐–พ ๐—๐—ˆ ๐—๐—๐–พ ๐—…๐–พ๐–ฟ๐— ๐—Œ๐—Ž๐–ป๐—๐—‹๐–พ๐–พ ๐—ˆ๐–ฟ ๐—๐—๐–พ ๐—‹๐—‚๐—€๐—๐— ๐–ผ๐—๐—‚๐—…๐–ฝ. ๐–ฒ๐—ˆ๐—…๐—Ž๐—๐—‚๐—ˆ๐—‡: ๐–ฃ๐—ˆ๐—Ž๐–ป๐—…๐–พ ๐—‹๐—ˆ๐—๐–บ๐—๐—‚๐—ˆ๐—‡, ๐—‹๐—‚๐—€๐—๐— ๐—‹๐—ˆ๐—๐–บ๐—๐—‚๐—ˆ๐—‡ ๐—ˆ๐—‡ ๐—๐—๐–พ ๐—‹๐—‚๐—€๐—๐— ๐–ผ๐—๐—‚๐—…๐–ฝ ๐–ฟ๐—ˆ๐—…๐—…๐—ˆ๐—๐–พ๐–ฝ ๐–ป๐—’ ๐–บ ๐—…๐–พ๐–ฟ๐— ๐—‹๐—ˆ๐—๐–บ๐—๐—‚๐—ˆ๐—‡ ๐—ˆ๐—‡ ๐—๐—๐–พ ๐—‹๐—ˆ๐—ˆ๐— ๐—ˆ๐–ฟ ๐—๐—๐–พ ๐—‚๐—†๐–ป๐–บ๐—…๐–บ๐—‡๐–ผ๐–พ๐–ฝ ๐—Œ๐—Ž๐–ป๐—๐—‹๐–พ๐–พ.

Rotations

  • ๐–ฑ๐—ˆ๐—๐–บ๐—๐—‚๐—ˆ๐—‡๐—Œ ๐–บ๐—‹๐–พ ๐—๐—๐–พ ๐—ˆ๐—‰๐–พ๐—‹๐–บ๐—๐—‚๐—ˆ๐—‡๐—Œ ๐—๐—๐–บ๐— ๐—‹๐–พ๐—Œ๐—๐—‹๐—Ž๐–ผ๐—๐—Ž๐—‹๐–พ ๐—๐—๐–พ ๐—๐—‹๐–พ๐–พ ๐—๐—ˆ ๐—‹๐–พ๐—Œ๐—๐—ˆ๐—‹๐–พ ๐—‚๐—๐—Œ ๐–ป๐–บ๐—…๐–บ๐—‡๐–ผ๐–พ ๐—๐—๐—‚๐—…๐–พ ๐—‰๐—‹๐–พ๐—Œ๐–พ๐—‹๐—๐—‚๐—‡๐—€ ๐—๐—๐–พ ๐–ป๐—‚๐—‡๐–บ๐—‹๐—’ ๐—Œ๐–พ๐–บ๐—‹๐–ผ๐— ๐—๐—‹๐–พ๐–พ ๐—‰๐—‹๐—ˆ๐—‰๐–พ๐—‹๐—๐—’.
    • ๐–ฒ๐—‚๐—‡๐—€๐—…๐–พ ๐–ฑ๐—‚๐—€๐—๐— ๐–ฑ๐—ˆ๐—๐–บ๐—๐—‚๐—ˆ๐—‡ (๐–ซ๐–ซ ๐–ฑ๐—ˆ๐—๐–บ๐—๐—‚๐—ˆ๐—‡): ๐–ณ๐—๐—‚๐—Œ ๐—‹๐—ˆ๐—๐–บ๐—๐—‚๐—ˆ๐—‡ ๐—‚๐—Œ ๐–บ๐—‰๐—‰๐—…๐—‚๐–พ๐–ฝ ๐—๐—๐–พ๐—‡ ๐–บ ๐—…๐–พ๐–ฟ๐—-๐—…๐–พ๐–ฟ๐— ๐–ผ๐–บ๐—Œ๐–พ ๐—ˆ๐–ผ๐–ผ๐—Ž๐—‹๐—Œ. ๐–ณ๐—๐–พ ๐—Œ๐—Ž๐–ป๐—๐—‹๐–พ๐–พ'๐—Œ ๐—‹๐—ˆ๐—ˆ๐— ๐—‚๐—Œ ๐—‹๐—ˆ๐—๐–บ๐—๐–พ๐–ฝ ๐—๐—ˆ ๐—๐—๐–พ ๐—‹๐—‚๐—€๐—๐—, ๐—†๐–บ๐—„๐—‚๐—‡๐—€ ๐—๐—๐–พ ๐—…๐–พ๐–ฟ๐— ๐–ผ๐—๐—‚๐—…๐–ฝ ๐—๐—๐–พ ๐—‡๐–พ๐— ๐—‹๐—ˆ๐—ˆ๐— ๐—ˆ๐–ฟ ๐—๐—๐–พ ๐—Œ๐—Ž๐–ป๐—๐—‹๐–พ๐–พ.
    • ๐–ฒ๐—‚๐—‡๐—€๐—…๐–พ ๐–ซ๐–พ๐–ฟ๐— ๐–ฑ๐—ˆ๐—๐–บ๐—๐—‚๐—ˆ๐—‡ (๐–ฑ๐–ฑ ๐–ฑ๐—ˆ๐—๐–บ๐—๐—‚๐—ˆ๐—‡): ๐–ณ๐—๐—‚๐—Œ ๐—‹๐—ˆ๐—๐–บ๐—๐—‚๐—ˆ๐—‡ ๐—‚๐—Œ ๐–บ๐—‰๐—‰๐—…๐—‚๐–พ๐–ฝ ๐—๐—๐–พ๐—‡ ๐–บ ๐—‹๐—‚๐—€๐—๐—-๐—‹๐—‚๐—€๐—๐— ๐–ผ๐–บ๐—Œ๐–พ ๐—ˆ๐–ผ๐–ผ๐—Ž๐—‹๐—Œ. ๐–ณ๐—๐–พ ๐—Œ๐—Ž๐–ป๐—๐—‹๐–พ๐–พ'๐—Œ ๐—‹๐—ˆ๐—ˆ๐— ๐—‚๐—Œ ๐—‹๐—ˆ๐—๐–บ๐—๐–พ๐–ฝ ๐—๐—ˆ ๐—๐—๐–พ ๐—…๐–พ๐–ฟ๐—, ๐—†๐–บ๐—„๐—‚๐—‡๐—€ ๐—๐—๐–พ ๐—‹๐—‚๐—€๐—๐— ๐–ผ๐—๐—‚๐—…๐–ฝ ๐—๐—๐–พ ๐—‡๐–พ๐— ๐—‹๐—ˆ๐—ˆ๐— ๐—ˆ๐–ฟ ๐—๐—๐–พ ๐—Œ๐—Ž๐–ป๐—๐—‹๐–พ๐–พ.
    • ๐–ฃ๐—ˆ๐—Ž๐–ป๐—…๐–พ ๐–ฑ๐—ˆ๐—๐–บ๐—๐—‚๐—ˆ๐—‡ (๐–ซ๐–ฑ ๐–ฑ๐—ˆ๐—๐–บ๐—๐—‚๐—ˆ๐—‡ ๐–บ๐—‡๐–ฝ ๐–ฑ๐–ซ ๐–ฑ๐—ˆ๐—๐–บ๐—๐—‚๐—ˆ๐—‡): ๐–ฃ๐—ˆ๐—Ž๐–ป๐—…๐–พ ๐—‹๐—ˆ๐—๐–บ๐—๐—‚๐—ˆ๐—‡๐—Œ ๐–บ๐—‹๐–พ ๐–บ๐—‰๐—‰๐—…๐—‚๐–พ๐–ฝ ๐—‚๐—‡ ๐—๐—๐–พ ๐–ผ๐–บ๐—Œ๐–พ ๐—ˆ๐–ฟ ๐–บ๐—‡ ๐–ซ๐–ฑ ๐—ˆ๐—‹ ๐–ฑ๐–ซ ๐—‚๐—†๐–ป๐–บ๐—…๐–บ๐—‡๐–ผ๐–พ. ๐–จ๐— ๐–ผ๐—ˆ๐—‡๐—Œ๐—‚๐—Œ๐—๐—Œ ๐—ˆ๐–ฟ ๐—๐—๐—ˆ ๐—Œ๐—‚๐—‡๐—€๐—…๐–พ ๐—‹๐—ˆ๐—๐–บ๐—๐—‚๐—ˆ๐—‡๐—Œ: ๐–ฟ๐—‚๐—‹๐—Œ๐— ๐—‹๐—ˆ๐—๐–บ๐—๐—‚๐—ˆ๐—‡ ๐—ˆ๐—‡ ๐—๐—๐–พ ๐–ผ๐—๐—‚๐—…๐–ฝ (๐—…๐–พ๐–ฟ๐— ๐—ˆ๐—‡ ๐—๐—๐–พ ๐—…๐–พ๐–ฟ๐— ๐–ผ๐—๐—‚๐—…๐–ฝ ๐–ฟ๐—ˆ๐—‹ ๐–ซ๐–ฑ, ๐—‹๐—‚๐—€๐—๐— ๐—ˆ๐—‡ ๐—๐—๐–พ ๐—‹๐—‚๐—€๐—๐— ๐–ผ๐—๐—‚๐—…๐–ฝ ๐–ฟ๐—ˆ๐—‹ ๐–ฑ๐–ซ) ๐–บ๐—‡๐–ฝ ๐—๐—๐–พ๐—‡ ๐–บ ๐—Œ๐–พ๐–ผ๐—ˆ๐—‡๐–ฝ ๐—‹๐—ˆ๐—๐–บ๐—๐—‚๐—ˆ๐—‡ ๐—ˆ๐—‡ ๐—๐—๐–พ ๐—‹๐—ˆ๐—ˆ๐— (๐—‹๐—‚๐—€๐—๐— ๐–ฟ๐—ˆ๐—‹ ๐–ซ๐–ฑ, ๐—…๐–พ๐–ฟ๐— ๐–ฟ๐—ˆ๐—‹ ๐–ฑ๐–ซ).

Note

More on AVL Trees and how they work - happycoders.eu

๐™ธ๐š–๐š™๐š•๐šŽ๐š–๐šŽ๐š—๐š๐šŠ๐š๐š’๐š˜๐š—๐šœ:
rust
csharp

LinkedList

list

Note

๐–ซ๐—‚๐—‡๐—„๐–พ๐–ฝ๐–ซ๐—‚๐—Œ๐—๐—Œ ๐–บ๐—‹๐–พ ๐–บ ๐—Œ๐–พ๐—Š๐—Ž๐–พ๐—‡๐–ผ๐–พ ๐—ˆ๐–ฟ ๐—‡๐—ˆ๐–ฝ๐–พ๐—Œ, ๐–พ๐–บ๐–ผ๐— ๐–ผ๐—ˆ๐—‡๐—๐–บ๐—‚๐—‡๐—‚๐—‡๐—€ ๐–ฝ๐–บ๐—๐–บ ๐–บ๐—‡๐–ฝ ๐–บ ๐—‹๐–พ๐–ฟ๐–พ๐—‹๐–พ๐—‡๐–ผ๐–พ (๐—…๐—‚๐—‡๐—„) ๐—๐—ˆ ๐—๐—๐–พ ๐—‡๐–พ๐—‘๐— ๐—‡๐—ˆ๐–ฝ๐–พ. ๐–ค๐–บ๐–ผ๐— ๐—‡๐—ˆ๐–ฝ๐–พ ๐—๐—’๐—‰๐—‚๐–ผ๐–บ๐—…๐—…๐—’ ๐–ผ๐—ˆ๐—‡๐—๐–บ๐—‚๐—‡๐—Œ ๐—๐—๐—ˆ ๐—‰๐–บ๐—‹๐—๐—Œ:

  • Data: ๐–ณ๐—๐–พ ๐–บ๐–ผ๐—๐—Ž๐–บ๐—… ๐—๐–บ๐—…๐—Ž๐–พ ๐—ˆ๐—‹ ๐—‚๐—‡๐–ฟ๐—ˆ๐—‹๐—†๐–บ๐—๐—‚๐—ˆ๐—‡ ๐—๐—๐–บ๐— ๐—๐—๐–พ ๐—‡๐—ˆ๐–ฝ๐–พ ๐—‹๐–พ๐—‰๐—‹๐–พ๐—Œ๐–พ๐—‡๐—๐—Œ.
  • Pointer (or Link): ๐–  ๐—‹๐–พ๐–ฟ๐–พ๐—‹๐–พ๐—‡๐–ผ๐–พ ๐—๐—ˆ ๐—๐—๐–พ ๐—‡๐–พ๐—‘๐— ๐—‡๐—ˆ๐–ฝ๐–พ ๐—‚๐—‡ ๐—๐—๐–พ ๐—Œ๐–พ๐—Š๐—Ž๐–พ๐—‡๐–ผ๐–พ. ๐–จ๐—‡ ๐–บ ๐—Œ๐—‚๐—‡๐—€๐—…๐—’ ๐—…๐—‚๐—‡๐—„๐–พ๐–ฝ ๐—…๐—‚๐—Œ๐—, ๐–พ๐–บ๐–ผ๐— ๐—‡๐—ˆ๐–ฝ๐–พ ๐—‰๐—ˆ๐—‚๐—‡๐—๐—Œ ๐—๐—ˆ ๐—‚๐—๐—Œ ๐—Œ๐—Ž๐–ผ๐–ผ๐–พ๐—Œ๐—Œ๐—ˆ๐—‹, ๐—๐—๐—‚๐—…๐–พ ๐—‚๐—‡ ๐–บ ๐–ฝ๐—ˆ๐—Ž๐–ป๐—…๐—’ ๐—…๐—‚๐—‡๐—„๐–พ๐–ฝ ๐—…๐—‚๐—Œ๐—, ๐–พ๐–บ๐–ผ๐— ๐—‡๐—ˆ๐–ฝ๐–พ ๐—๐–บ๐—Œ ๐—‰๐—ˆ๐—‚๐—‡๐—๐–พ๐—‹๐—Œ ๐—๐—ˆ ๐–ป๐—ˆ๐—๐— ๐—‚๐—๐—Œ ๐—‰๐—‹๐–พ๐—๐—‚๐—ˆ๐—Ž๐—Œ ๐–บ๐—‡๐–ฝ ๐—‡๐–พ๐—‘๐— ๐—‡๐—ˆ๐–ฝ๐–พ.

๐–ณ๐—๐–พ ๐—Œ๐—๐—‹๐—Ž๐–ผ๐—๐—Ž๐—‹๐–พ ๐—Œ๐—๐–บ๐—‹๐—๐—Œ ๐—๐—‚๐—๐— ๐—๐—๐–พ ๐—๐–พ๐–บ๐–ฝ, ๐—๐—๐—‚๐–ผ๐— ๐—‰๐—ˆ๐—‚๐—‡๐—๐—Œ ๐—๐—ˆ ๐—๐—๐–พ ๐–ฟ๐—‚๐—‹๐—Œ๐— ๐—‡๐—ˆ๐–ฝ๐–พ, ๐–ผ๐—ˆ๐—‡๐—๐—‚๐—‡๐—Ž๐–พ๐—Œ ๐–บ๐—Œ ๐–พ๐–บ๐–ผ๐— ๐—Œ๐—Ž๐–ป๐—Œ๐–พ๐—Š๐—Ž๐–พ๐—‡๐— ๐—‡๐—ˆ๐–ฝ๐–พ ๐—‰๐—ˆ๐—‚๐—‡๐—๐—Œ ๐—๐—ˆ ๐—๐—๐–พ ๐—‡๐–พ๐—‘๐—, ๐–บ๐—‡๐–ฝ ๐–ผ๐—ˆ๐—‡๐–ผ๐—…๐—Ž๐–ฝ๐–พ๐—Œ ๐—๐—‚๐—๐— ๐—๐—๐–พ ๐—๐–บ๐—‚๐—…, ๐—๐—๐—‚๐–ผ๐— ๐—‰๐—ˆ๐—‚๐—‡๐—๐—Œ ๐—๐—ˆ ๐—‡๐—…๐—…, ๐—‚๐—‡๐–ฝ๐—‚๐–ผ๐–บ๐—๐—‚๐—‡๐—€ ๐—๐—๐–พ ๐–พ๐—‡๐–ฝ ๐—ˆ๐–ฟ ๐—๐—๐–พ ๐—…๐—‚๐—‡๐—„๐–พ๐–ฝ ๐—…๐—‚๐—Œ๐—.
๐–ค๐–บ๐–ผ๐— ๐—…๐—‚๐—Œ๐— ๐—‡๐—ˆ๐–ฝ๐–พ ๐—‚๐—‡ ๐—๐—๐—‚๐—Œ ๐—Œ๐—๐—‹๐—Ž๐–ผ๐—๐—Ž๐—‹๐–พ ๐—๐–บ๐—Œ ๐–บ ๐—๐–บ๐—…๐—Ž๐–พ ๐—‰๐—‹๐—ˆ๐—‰๐–พ๐—‹๐—๐—’ ๐–ฟ๐—ˆ๐—‹ ๐—Œ๐—๐—ˆ๐—‹๐—‚๐—‡๐—€ ๐–ฝ๐–บ๐—๐–บ, ๐–บ๐—‡๐–ฝ ๐–บ ๐—‡๐–พ๐—‘๐— ๐—‰๐—‹๐—ˆ๐—‰๐–พ๐—‹๐—๐—’ ๐—๐—๐–บ๐— ๐—‰๐—ˆ๐—‚๐—‡๐—๐—Œ ๐—๐—ˆ ๐—๐—๐–พ ๐—‡๐–พ๐—‘๐— ๐—‡๐—ˆ๐–ฝ๐–พ ๐—‚๐—‡ ๐—๐—๐–พ ๐—…๐—‚๐—Œ๐—.

sll
  • Rust:

       pub struct Node<T> {
           pub data: T,
           pub next: Option<NonNull<Node<T>>>
       }
    
       pub struct LinkedList<T> {
           pub head: Option<NonNull<Node<T>>>,
           pub tail: Option<NonNull<Node<T>>>,
           pub length: usize,
       }
  • C#:

       public class LinkedList<T>
       {
          private class Node
          {
              public T data;
              public Node next;
          }
    
          public Node head;
          public Node tail;
          public int length;
       }

Types of LinkedLists

  • Singly Linked List: ๐–ค๐–บ๐–ผ๐— ๐—‡๐—ˆ๐–ฝ๐–พ ๐—๐–บ๐—Œ ๐—ˆ๐—‡๐—…๐—’ ๐—ˆ๐—‡๐–พ ๐—‰๐—ˆ๐—‚๐—‡๐—๐–พ๐—‹ ๐—๐—ˆ ๐—๐—๐–พ ๐—‡๐–พ๐—‘๐— ๐—‡๐—ˆ๐–ฝ๐–พ
  • Doubly Linked List: ๐–ค๐–บ๐–ผ๐— ๐—‡๐—ˆ๐–ฝ๐–พ ๐—๐–บ๐—Œ ๐—๐—๐—ˆ ๐—‰๐—ˆ๐—‚๐—‡๐—๐–พ๐—‹๐—Œ, ๐—ˆ๐—‡๐–พ ๐—๐—ˆ ๐—๐—๐–พ ๐—‡๐–พ๐—‘๐— ๐—‡๐—ˆ๐–ฝ๐–พ ๐–บ๐—‡๐–ฝ ๐—ˆ๐—‡๐–พ ๐—๐—ˆ ๐—๐—๐–พ ๐—‰๐—‹๐–พ๐—๐—‚๐—ˆ๐—Ž๐—Œ ๐—‡๐—ˆ๐–ฝ๐–พ
  • Circular Linked List: ๐–ณ๐—๐–พ ๐—…๐–บ๐—Œ๐— ๐—‡๐—ˆ๐–ฝ๐–พ ๐—‰๐—ˆ๐—‚๐—‡๐—๐—Œ ๐–ป๐–บ๐–ผ๐—„ ๐—๐—ˆ ๐—๐—๐–พ ๐–ฟ๐—‚๐—‹๐—Œ๐— ๐—‡๐—ˆ๐–ฝ๐–พ, ๐–ฟ๐—ˆ๐—‹๐—†๐—‚๐—‡๐—€ ๐–บ ๐–ผ๐—‚๐—‹๐–ผ๐—…๐–พ (๐—‚๐— ๐–ผ๐–บ๐—‡ ๐–ป๐–พ ๐—Œ๐—‚๐—‡๐—€๐—…๐—’ ๐—ˆ๐—‹ ๐–ฝ๐—ˆ๐—Ž๐–ป๐—…๐—’)

Singly Linked Lists vs. Doubly Linked Lists

  • ๐–ฒ๐—‚๐—‡๐—€๐—…๐—’ ๐–ซ๐—‚๐—‡๐—„๐–พ๐–ฝ ๐–ซ๐—‚๐—Œ๐—๐—Œ:
    ๐–จ๐—‡ ๐—๐—๐–พ๐—Œ๐–พ ๐—…๐—‚๐—Œ๐—๐—Œ, ๐–พ๐–บ๐–ผ๐— ๐—‡๐—ˆ๐–ฝ๐–พ ๐—‰๐—ˆ๐—‚๐—‡๐—๐—Œ ๐—ˆ๐—‡๐—…๐—’ ๐—๐—ˆ ๐—๐—๐–พ ๐—‡๐–พ๐—‘๐— ๐—‡๐—ˆ๐–ฝ๐–พ. ๐–ณ๐—๐—‚๐—Œ ๐—Œ๐—๐—‹๐—Ž๐–ผ๐—๐—Ž๐—‹๐–พ ๐—‚๐—Œ ๐—Œ๐—‚๐—†๐—‰๐—…๐–พ ๐–บ๐—‡๐–ฝ ๐—†๐–พ๐—†๐—ˆ๐—‹๐—’-๐–พ๐–ฟ๐–ฟ๐—‚๐–ผ๐—‚๐–พ๐—‡๐—, ๐–ป๐—Ž๐— ๐—๐—‹๐–บ๐—๐–พ๐—‹๐—Œ๐–บ๐—… ๐–ผ๐–บ๐—‡ ๐—ˆ๐—‡๐—…๐—’ ๐—ˆ๐–ผ๐–ผ๐—Ž๐—‹ ๐—‚๐—‡ ๐—ˆ๐—‡๐–พ ๐–ฝ๐—‚๐—‹๐–พ๐–ผ๐—๐—‚๐—ˆ๐—‡.
  • ๐–ฃ๐—ˆ๐—Ž๐–ป๐—…๐—’ ๐–ซ๐—‚๐—‡๐—„๐–พ๐–ฝ ๐–ซ๐—‚๐—Œ๐—๐—Œ:
    ๐–ณ๐—๐–พ๐—Œ๐–พ ๐—…๐—‚๐—Œ๐—๐—Œ ๐–บ๐—…๐—…๐—ˆ๐— ๐—‡๐—ˆ๐–ฝ๐–พ๐—Œ ๐—๐—ˆ ๐—‰๐—ˆ๐—‚๐—‡๐— ๐–ป๐—ˆ๐—๐— ๐—๐—ˆ ๐—๐—๐–พ ๐—‡๐–พ๐—‘๐— ๐–บ๐—‡๐–ฝ ๐—๐—๐–พ ๐—‰๐—‹๐–พ๐—๐—‚๐—ˆ๐—Ž๐—Œ ๐—‡๐—ˆ๐–ฝ๐–พ๐—Œ, ๐–พ๐—‡๐–บ๐–ป๐—…๐—‚๐—‡๐—€ ๐–ป๐—‚๐–ฝ๐—‚๐—‹๐–พ๐–ผ๐—๐—‚๐—ˆ๐—‡๐–บ๐—… ๐—๐—‹๐–บ๐—๐–พ๐—‹๐—Œ๐–บ๐—…. ๐–ณ๐—๐—‚๐—Œ ๐—‚๐—‡๐–ผ๐—‹๐–พ๐–บ๐—Œ๐–พ๐–ฝ ๐–ฟ๐—…๐–พ๐—‘๐—‚๐–ป๐—‚๐—…๐—‚๐—๐—’ ๐–ผ๐—ˆ๐—†๐–พ๐—Œ ๐–บ๐— ๐—๐—๐–พ ๐–ผ๐—ˆ๐—Œ๐— ๐—ˆ๐–ฟ ๐–บ๐–ฝ๐–ฝ๐—‚๐—๐—‚๐—ˆ๐—‡๐–บ๐—… ๐—†๐–พ๐—†๐—ˆ๐—‹๐—’ ๐—Ž๐—Œ๐–บ๐—€๐–พ ๐–ฝ๐—Ž๐–พ ๐—๐—ˆ ๐—๐—๐–พ ๐—Œ๐–พ๐–ผ๐—ˆ๐—‡๐–ฝ ๐—‰๐—ˆ๐—‚๐—‡๐—๐–พ๐—‹.
dll
  • Rust:

       pub struct Node<T> {
           pub data: T,
           pub next: Option<NonNull<Node<T>>>,
           pub prev: Option<NonNull<Node<T>>>
       }
    
       pub struct LinkedList<T> {
           pub head: Option<NonNull<Node<T>>>,
           pub tail: Option<NonNull<Node<T>>>,
           pub length: usize,
       }
  • C#:

       public class LinkedList<T>
       {
          private class Node
          {
              public T data;
              public Node next;
              public Node prev;
          }
    
          public Node head;
          public Node tail;
          public int length;
       }

๐– ๐—Œ ๐—๐–พ ๐–ผ๐–บ๐—‡ ๐—Œ๐–พ๐–พ ๐—๐—๐–พ ๐—ˆ๐—‡๐—…๐—’ ๐–ฝ๐—‚๐–ฟ๐–ฟ๐–พ๐—‹๐–พ๐—‡๐–ผ๐–พ ๐—‚๐—Œ ๐—๐—๐–พ ๐–บ๐–ฝ๐–ฝ๐—‚๐—๐—‚๐—ˆ๐—‡๐–บ๐—… ๐—‰๐—‹๐–พ๐—๐—‚๐—ˆ๐—Ž๐—Œ ๐—‰๐—ˆ๐—‚๐—‡๐—๐–พ๐—‹ (๐—‰๐—‹๐–พ๐—) ๐—ˆ๐—‡ ๐—๐—๐–พ ๐–ฝ๐—ˆ๐—Ž๐–ป๐—…๐—’ ๐—…๐—‚๐—‡๐—„๐–พ๐–ฝ ๐—…๐—‚๐—Œ๐—.

Operations and Complexity

Operation Location Complexity
Insertion At the beginning (head) O(1)
At the end (tail) O(1)
In the middle O(n)
Deletion At the beginning (head) O(1)
Singly - At the end (tail) O(n)
Doubly - At the end (tail) O(1)
In the middle O(n)
Traversal/Searching - O(n)
๐™ธ๐š–๐š™๐š•๐šŽ๐š–๐šŽ๐š—๐š๐šŠ๐š๐š’๐š˜๐š—๐šœ:
rust
csharp

Disjoint-set

djs

Note

๐–ฃ๐—‚๐—Œ๐—ƒ๐—ˆ๐—‚๐—‡๐—-๐—Œ๐–พ๐— ๐–ฃ๐–บ๐—๐–บ ๐–ฒ๐—๐—‹๐—Ž๐–ผ๐—๐—Ž๐—‹๐–พ ๐–บ๐—…๐—Œ๐—ˆ ๐—„๐—‡๐—ˆ๐—๐—‡ ๐–บ๐—Œ ๐–บ ๐—Ž๐—‡๐—‚๐—ˆ๐—‡-๐–ฟ๐—‚๐—‡๐–ฝ, ๐—„๐–พ๐–พ๐—‰๐—Œ ๐—๐—‹๐–บ๐–ผ๐—„ ๐—ˆ๐–ฟ ๐–บ ๐—Œ๐–พ๐— ๐—ˆ๐–ฟ ๐–พ๐—…๐–พ๐—†๐–พ๐—‡๐—๐—Œ ๐—‰๐–บ๐—‹๐—๐—‚๐—๐—‚๐—ˆ๐—‡๐–พ๐–ฝ ๐—‚๐—‡๐—๐—ˆ ๐—Œ๐–พ๐—๐–พ๐—‹๐–บ๐—… ๐—‡๐—ˆ๐—‡-๐—ˆ๐—๐–พ๐—‹๐—…๐–บ๐—‰๐—‰๐—‚๐—‡๐—€ ๐—Œ๐—Ž๐–ป๐—Œ๐–พ๐—๐—Œ.

Characteristics

  • ๐–ค๐–ฟ๐–ฟ๐—‚๐–ผ๐—‚๐–พ๐—‡๐— ๐–ฟ๐—ˆ๐—‹ ๐—๐–บ๐—‡๐–ฝ๐—…๐—‚๐—‡๐—€ ๐–พ๐—Š๐—Ž๐—‚๐—๐–บ๐—…๐–พ๐—‡๐–ผ๐–พ ๐—‹๐–พ๐—…๐–บ๐—๐—‚๐—ˆ๐—‡๐—Œ ๐–บ๐—‡๐–ฝ ๐–ผ๐—ˆ๐—‡๐—‡๐–พ๐–ผ๐—๐–พ๐–ฝ ๐–ผ๐—ˆ๐—†๐—‰๐—ˆ๐—‡๐–พ๐—‡๐—๐—Œ ๐—‚๐—‡ ๐–บ ๐—‡๐–พ๐—๐—๐—ˆ๐—‹๐—„.
  • ๐–ข๐—ˆ๐—†๐—†๐—ˆ๐—‡๐—…๐—’ ๐—Ž๐—Œ๐–พ๐–ฝ ๐—‚๐—‡ ๐–บ๐—…๐—€๐—ˆ๐—‹๐—‚๐—๐—๐—†๐—Œ ๐—๐—๐–บ๐— ๐—‹๐–พ๐—Š๐—Ž๐—‚๐—‹๐–พ ๐–ฟ๐—‹๐–พ๐—Š๐—Ž๐–พ๐—‡๐— ๐–ผ๐—๐–พ๐–ผ๐—„๐—Œ ๐—ˆ๐–ฟ ๐—๐—๐–พ๐—๐—๐–พ๐—‹ ๐—๐—๐—ˆ ๐–พ๐—…๐–พ๐—†๐–พ๐—‡๐—๐—Œ ๐–บ๐—‹๐–พ ๐—‚๐—‡ ๐—๐—๐–พ ๐—Œ๐–บ๐—†๐–พ ๐—Œ๐—Ž๐–ป๐—Œ๐–พ๐—.

Operations

  • Find: ๐–ฃ๐–พ๐—๐–พ๐—‹๐—†๐—‚๐—‡๐–พ๐—Œ ๐—๐—๐—‚๐–ผ๐— ๐—Œ๐—Ž๐–ป๐—Œ๐–พ๐— ๐–บ ๐—‰๐–บ๐—‹๐—๐—‚๐–ผ๐—Ž๐—…๐–บ๐—‹ ๐–พ๐—…๐–พ๐—†๐–พ๐—‡๐— ๐—‚๐—Œ ๐—‚๐—‡. ๐–ณ๐—๐—‚๐—Œ ๐–ผ๐–บ๐—‡ ๐–ป๐–พ ๐—Ž๐—Œ๐–พ๐–ฝ ๐–ฟ๐—ˆ๐—‹ ๐–ฝ๐–พ๐—๐–พ๐—‹๐—†๐—‚๐—‡๐—‚๐—‡๐—€ ๐—‚๐–ฟ ๐—๐—๐—ˆ ๐–พ๐—…๐–พ๐—†๐–พ๐—‡๐—๐—Œ ๐–บ๐—‹๐–พ ๐—‚๐—‡ ๐—๐—๐–พ ๐—Œ๐–บ๐—†๐–พ ๐—Œ๐—Ž๐–ป๐—Œ๐–พ๐—
  • Union: ๐–ฉ๐—ˆ๐—‚๐—‡๐—Œ ๐—๐—๐—ˆ ๐—Œ๐—Ž๐–ป๐—Œ๐–พ๐—๐—Œ ๐—‚๐—‡๐—๐—ˆ ๐–บ ๐—Œ๐—‚๐—‡๐—€๐—…๐–พ ๐—Œ๐—Ž๐–ป๐—Œ๐–พ๐—

Efficiency

  • ๐–ถ๐—‚๐—๐— ๐—ˆ๐—‰๐—๐—‚๐—†๐—‚๐—“๐–บ๐—๐—‚๐—ˆ๐—‡๐—Œ ๐—…๐—‚๐—„๐–พ ๐—Ž๐—‡๐—‚๐—ˆ๐—‡ ๐–ป๐—’ ๐—‹๐–บ๐—‡๐—„ ๐–บ๐—‡๐–ฝ ๐—‰๐–บ๐—๐— ๐–ผ๐—ˆ๐—†๐—‰๐—‹๐–พ๐—Œ๐—Œ๐—‚๐—ˆ๐—‡, ๐—๐—๐–พ ๐—๐—‚๐—†๐–พ ๐–ผ๐—ˆ๐—†๐—‰๐—…๐–พ๐—‘๐—‚๐—๐—’ ๐—ˆ๐–ฟ ๐–ป๐—ˆ๐—๐— ๐–ฅ๐—‚๐—‡๐–ฝ ๐–บ๐—‡๐–ฝ ๐–ด๐—‡๐—‚๐—ˆ๐—‡ ๐—ˆ๐—‰๐–พ๐—‹๐–บ๐—๐—‚๐—ˆ๐—‡๐—Œ ๐–ผ๐–บ๐—‡ ๐–ป๐–พ ๐–ป๐—‹๐—ˆ๐—Ž๐—€๐—๐— ๐–ฝ๐—ˆ๐—๐—‡ ๐–ผ๐—…๐—ˆ๐—Œ๐–พ ๐—๐—ˆ ๐–ผ๐—ˆ๐—‡๐—Œ๐—๐–บ๐—‡๐— ๐—๐—‚๐—†๐–พ, O(ฮฑ(n)), where ฮฑ ๐—‚๐—Œ ๐—๐—๐–พ ๐—‚๐—‡๐—๐–พ๐—‹๐—Œ๐–พ ๐– ๐–ผ๐—„๐–พ๐—‹๐—†๐–บ๐—‡๐—‡ ๐–ฟ๐—Ž๐—‡๐–ผ๐—๐—‚๐—ˆ๐—‡, ๐—๐—๐—‚๐–ผ๐— ๐—€๐—‹๐—ˆ๐—๐—Œ ๐—๐–พ๐—‹๐—’ ๐—Œ๐—…๐—ˆ๐—๐—…๐—’ ๐–บ๐—‡๐–ฝ ๐—‚๐—Œ ๐—‰๐—‹๐–บ๐–ผ๐—๐—‚๐–ผ๐–บ๐—…๐—…๐—’ ๐–ผ๐—ˆ๐—‡๐—Œ๐—๐–บ๐—‡๐— ๐–ฟ๐—ˆ๐—‹ ๐–บ๐—…๐—… ๐—‹๐–พ๐–บ๐—Œ๐—ˆ๐—‡๐–บ๐–ป๐—…๐–พ ๐—‚๐—‡๐—‰๐—Ž๐— ๐—Œ๐—‚๐—“๐–พ๐—Œ.
๐™ธ๐š–๐š™๐š•๐šŽ๐š–๐šŽ๐š—๐š๐šŠ๐š๐š’๐š˜๐š—๐šœ:
rust
csharp


Graph Theory

graphs

Graphs are an abstract idea that represents connections between objects.

Note

Formal definition: An (undirected) graph is a collection ๏ผถ of vertices, and a collection ๏ผฅ of edges each of which connects a pair of verices.

Key Concepts

  • ๐š…๐šŽ๐š›๐š๐š’๐šŒ๐šŽ๐šœ (๐™ฝ๐š˜๐š๐šŽ๐šœ): The individual items or entities in a graph
  • ๐™ด๐š๐š๐šŽ๐šœ (๐™ป๐š’๐š—๐š”๐šœ): The connections between nodes
  • Loop: Loops connect a vertex to itself. This means that edge from vertex ๏ผก points to the same vertex ๏ผก

Representing graphs:

Adjacency Matrix

  • Vertices Representation: Each vertex in the graph is associated with one row and one column in the matrix. For a graph with ๏ฝŽ vertices, the adjacency matrix is an ๏ฝŽร—๏ฝŽ square matrix
  • Edges Representation:
    • In an undirected graph, if there is an edge between vertex ๐š’ and vertex ๐š“, then the matrix element adjacency matrix notation as well adjacency matrix notation (since the edge is bidirectional). If there's no edge, adjacency matrix notation
    • In a directed graph adjacency matrix notation if there is a directed edge from vertex ๐š’ to vertex ๐š“. If there's no directed edge from ๐š’ to ๐š“, than adjacency matrix notation
  • Weights and Multiple Edges: For weighted graphs, instead of using 1 to indicate an edge, the actual weight of the edge is used. In graphs with multiple edges, the matrix can contain values higher than 1.

adjacency matrix notation

Adjacency List

  • Vertices Representation: Graph is an array or a list of lists. Each element of this array (or list) corresponds to a vertex in the graph.
  • Edges Representation:
    • For each vertex ๐š’, the adjacency list stores a list of vertices that are adjacent to ๐š“.
    • Implemented using an array of linked lists, an array of arrays, hash table or a map where keys are vertices and values are lists of adjacent vertices.
  • Directed and Undirected Graphs:
    • In an undirected graph, if vertex ๐š’ is connected to vertex ๐š“, then ๐š’ will appear in ๐š“'s list and ๐š“ will appear in ๐š’'s list.
    • In a directed graph, if there is an edge from ๐š’ to ๐š“, then ๐š“ appears in ๐š’'s list but not necessarily vice versa.
  • Weights: If the graph is weighted, each entry in the list can be a pair (or a structure) containing the adjacent vertex and the weight of the edge.

*degree - The degree of a node in a graph is the number of edges that are connected to it.

Is edge List edge List neighbors
Adjacency Matrix ฮ˜(1) ฮ˜(|V|2) ฮ˜(|V|)
Adjacency List ฮ˜(degree) ฮ˜(|E|) ฮ˜(degree)

Algorithm runtime

Graph algorithm runtime depents on |V| and |E|

  • |V|: number on vertices
  • |E|: number on edges

Graph performance depends on the density. Graph density is a measure of how many edges are in the graph compared to the maximum possible number of edges between vertices.

  • Dense graph - |E| โ‰ˆ |V|2
  • Sparse graph - |E| โ‰ˆ |V|

Types of Graphs

  • ๐š„๐š—๐š๐š’๐š›๐šŽ๐šŒ๐š๐šŽ๐š ๐™ถ๐š›๐šŠ๐š™๐š‘๐šœ: Symmetric relationships
  • ๐™ณ๐š’๐š›๐šŽ๐šŒ๐š๐šŽ๐š ๐™ถ๐š›๐šŠ๐š™๐š‘๐šœ: Asymmetric relationships, like web links
  • ๐š†๐šŽ๐š’๐š๐š‘๐š๐šŽ๐š ๐™ถ๐š›๐šŠ๐š™๐š‘๐šœ: Graphs with edge weights, useful in routing problems

Graph Algorithms

graphs

Graph Implementations:

Graph Implementation in Rust
Graph Implementation in C#


DFS (depth-first search)

Note

Depth-First Search (DFS) is an algorithm used for traversing or searching tree or graph data structures. It starts at a selected node (root) and explores as far as possible along each branch before backtracking.

  • ๐™ฒ๐š˜๐š—๐šŒ๐šŽ๐š™๐š: Understand the Depth-first search
  • ๐™ธ๐š–๐š™๐š•๐šŽ๐š–๐šŽ๐š—๐š๐šŠ๐š๐š’๐š˜๐š—๐šœ: Rust - C#
  1. Initialize:
    • Start at the root node (or any node in a graph).
    • Create a Stack to keep track of the path.
    • Add the starting node to the Stack and mark it as visited.
  2. DFS Loop:
    • While the Stack is not empty:
      • Pop a node from the Stack.
      • For each unvisited neighbor of this node:
        • Mark the neighbor as visited.
        • Add the neighbor to the Stack.
  3. Complete:
    • Repeat until all reachable nodes are visited.

BFS (breadth-first search)

Note

Breadth-First Search (BFS) is an algorithm used for traversing or searching tree or graph data structures. It starts at a selected node and explores all neighbor nodes at the present depth before moving on to nodes at the next depth level.

  • ๐™ฒ๐š˜๐š—๐šŒ๐šŽ๐š™๐š: Understand the Breadth-first search
  • ๐™ธ๐š–๐š™๐š•๐šŽ๐š–๐šŽ๐š—๐š๐šŠ๐š๐š’๐š˜๐š—๐šœ: Rust - C#
  1. Initialize:
    • Start at the root node (or any node in a graph).
    • Create a Queue to keep track of the nodes to visit.
    • Add the starting node to the Queue and mark it as visited.
  2. BFS Loop:
    • While the Queue is not empty:
      • Dequeue a node from the Queue.
      • For each unvisited neighbor of this node:
        • Mark the neighbor as visited.
        • Add the neighbor to the Queue.
  3. Complete:
    • Repeat until all reachable nodes are visited.

Dijkstra's algorithm

Note

Dijkstra's Algorithm is a pathfinding algorithm that finds the shortest path from a starting node to all other nodes in a weighted graph.

  • ๐™ฒ๐š˜๐š—๐šŒ๐šŽ๐š™๐š: Understand the Dijkstra's Algorithm
  • ๐™ธ๐š–๐š™๐š•๐šŽ๐š–๐šŽ๐š—๐š๐šŠ๐š๐š’๐š˜๐š—๐šœ: Rust - C#

๐™ฒ๐š˜๐š–๐š™๐šž๐š๐šŽ๐š›๐š™๐š‘๐š’๐š•๐šŽ - Dijkstra's Algorithm by Dr Michael Pound

  1. Initialize:
    • Set initial distances to all nodes as infinity, except the start node which should be zero.
    • Create a priority queue and add the start node with distance 0.
  2. Algorithm Loop:
    • While the priority queue is not empty:
      • Remove the node with the smallest distance.
      • For each neighbor, calculate the new distance and update if it's smaller.
  3. Complete:
    • All shortest paths from the start node are determined.

Bellman-Ford algorithm

Note

The Bellman-Ford algorithm is used for computing shortest paths in a weighted graph. Unlike Dijkstra's, it can handle graphs with negative weight edges.

  • ๐™ฒ๐š˜๐š—๐šŒ๐šŽ๐š™๐š: Understand the Bellman-Ford Algorithm
  • ๐™ธ๐š–๐š™๐š•๐šŽ๐š–๐šŽ๐š—๐š๐šŠ๐š๐š’๐š˜๐š—๐šœ: Rust - C#
  1. Initialize:
    • Set the distance to the source as 0 and all other distances as infinite.
  2. Relaxation Loop:
    • For each edge (u, v), update the distance to v if a shorter path is found via u.
    • Repeat this for all edges |V|-1 times (|V| is the number of vertices).
  3. Negative Cycle Check:
    • Check for further distance improvements; if found, a negative cycle exists.

Floyd Warshall algorithm

Note

The Floyd-Warshall algorithm is used for finding shortest paths in a weighted graph with positive or negative edge weights (but with no negative cycles).

  • ๐™ฒ๐š˜๐š—๐šŒ๐šŽ๐š™๐š: Understand the Floyd Warshall Algorithm
  • ๐™ธ๐š–๐š™๐š•๐šŽ๐š–๐šŽ๐š—๐š๐šŠ๐š๐š’๐š˜๐š—๐šœ: Rust - C#
  1. Matrix Setup:
    • Initialize a matrix with distances between all pairs of vertices. Set 0 for self-loops and infinity for no direct path.
  2. Dynamic Programming:
    • Update the matrix to find the shortest distance between each pair using an intermediate vertex.
  3. Complete:
    • The matrix now contains the shortest distances between all pairs of nodes.

Kruskal algorithm

Note

Kruskal's Algorithm is a minimum spanning tree algorithm that finds an edge subset of a connected, weighted graph that connects all the vertices together, without any cycles and with the minimum possible total edge weight.

  • ๐™ฒ๐š˜๐š—๐šŒ๐šŽ๐š™๐š: Understand the Kruskal Algorithm
  • ๐™ธ๐š–๐š™๐š•๐šŽ๐š–๐šŽ๐š—๐š๐šŠ๐š๐š’๐š˜๐š—๐šœ: Rust - C#
  1. Sort Edges:
    • Sort all edges of the graph in non-decreasing order of their weight.
  2. Initialize Forest:
    • Create a forest, initially with each vertex as an individual tree. (use Disjoint-set/Union-Find)
  3. Edge Selection:
    • For each edge, check if it forms a cycle in the forest.
      • If not, add it to the forest.
  4. Complete:
    • Continue until the forest has (V-1) edges (V is the number of vertices).

Prim's algorithm

Note

Prim's Algorithm is a minimum spanning tree algorithm used in a connected, weighted graph. It builds the spanning tree by adding the next cheapest vertex to the existing tree until all vertices are included.

  • ๐™ฒ๐š˜๐š—๐šŒ๐šŽ๐š™๐š: Understand the Prim's Algorithm
  • ๐™ธ๐š–๐š™๐š•๐šŽ๐š–๐šŽ๐š—๐š๐šŠ๐š๐š’๐š˜๐š—๐šœ: Rust - C#
  1. Initialize Priority Queue:
    • Start from a root vertex and add all its edges to a priority queue.
  2. Select Cheapest Edge:
    • Repeatedly choose the edge with the smallest weight that connects a vertex in the tree to a vertex outside.
  3. Check for Cycles:
    • Ensure that adding the chosen edge doesnโ€™t create a cycle. (use Disjoint-set/Union-Find)
  4. Expand the Tree:
    • Add the selected edge and vertex to the spanning tree.
  5. Repeat:
    • Continue the process until all vertices are included in the spanning tree.

Kosaraju's algorithm

Note

Kosaraju's algorithm is a depth-first search based method used to find strongly connected components in a directed graph. The algorithm involves two passes of depth-first search. The first pass is used to calculate finishing times of vertices, and the second pass identifies the strongly connected components based on these times.

  • ๐™ฒ๐š˜๐š—๐šŒ๐šŽ๐š™๐š: Understand the Kosaraju's Algorithm
  • ๐™ธ๐š–๐š™๐š•๐šŽ๐š–๐šŽ๐š—๐š๐šŠ๐š๐š’๐š˜๐š—๐šœ: Rust - C#
  1. First DFS Pass:
    • Perform a depth-first search (DFS) on the original graph.
    • Track the completion order of vertices and push them onto a stack.
  2. Second DFS Pass:
    • Pop nodes from the stack in the order they were completed in the first DFS.
    • Perform DFS on the transposed graph starting from each popped node, if it hasn't been visited.
  3. Identify Strongly Connected Components:
    • Each DFS call in the second pass identifies a strongly connected component.
    • Collect the nodes visited in each DFS call as a single SCC.
  4. Complete:
    • The algorithm finishes when all vertices have been popped and processed in the second DFS pass. At this point, all SCCs in the graph have been identified.

Tarjan's algorithm

Note

Tarjan's algorithm is a depth-first search based algorithm used to find strongly connected components (SCCs) in a directed graph. An SCC is a component where every vertex is reachable from every other vertex in the same component. This algorithm is efficient and can find all SCCs in a graph in linear time.

  • ๐™ฒ๐š˜๐š—๐šŒ๐šŽ๐š™๐š: Understand the Tarjan's SCCs Algorithm (emre.me)
  • ๐™ธ๐š–๐š™๐š•๐šŽ๐š–๐šŽ๐š—๐š๐šŠ๐š๐š’๐š˜๐š—๐šœ: Rust - C#
  1. Initialize:
    • Assign a unique integer index to each node, initially undefined.
    • Define a low-link value for each node, initially set to its index.
  2. Graph Traversal
    • Increment discovery time for each visited node.
    • Store discovery time and initial low-link value for each node.
  3. DFS Loop:
    • For each node, count its children and track its parent.
    • Apply DFS recursively to unvisited successors.
    • Update the node's low-link value based on children's low-link values.
  4. Handling Back Edges
    • Update the low-link value of the current node based on the discovery time of previously visited nodes that are not the parent.
  5. Repeat:
    • Repeat this process for all nodes in the graph.
  6. Complete:
    • The algorithm terminates when all nodes have been processed.

Open section -> Tarjan's algorithm use cases

Articulation Points

To find articulation points using Tarjan's algorithm, an additional step of identifying vertices that, if removed, increase the number of connected components is needed.

  • ๐™ด๐šก๐š๐šŽ๐š—๐š๐šŽ๐š ๐š‚๐š๐šŽ๐š™: After completing the DFS loop, check each node. If it's a root node with two or more children, it's an articulation point. For other nodes, if the low-link value of a child is greater than or equal to the index of the node, then this node is an articulation point.
  • ๐™ธ๐š–๐š™๐š•๐šŽ๐š–๐šŽ๐š—๐š๐šŠ๐š๐š’๐š˜๐š—๐šœ: Rust | C#

Subgraph Components

To find subgraph components using Tarjan's algorithm, it's essential to focus on grouping nodes into their respective SCCs.

  • ๐™ด๐šก๐š๐šŽ๐š—๐š๐šŽ๐š ๐š‚๐š๐šŽ๐š™: Upon finishing the DFS for a node, if the node's low-link value equals its index, it indicates the start of a new SCC. Collect all nodes explored since then into a separate SCC group.
  • ๐™ธ๐š–๐š™๐š•๐šŽ๐š–๐šŽ๐š—๐š๐šŠ๐š๐š’๐š˜๐š—๐šœ: Rust | C#

algorithms's People

Contributors

anastaskosstow avatar

Stargazers

Vanina Teneva avatar ะ”ะธะผะธั‚ัŠั€ ะฏะฝะฐะบะธะตะฒ avatar  avatar  avatar  avatar Marieta Stoycheva avatar Miriam avatar  avatar IMRadev19 avatar  avatar VIktor Kirov avatar Petar Spasov avatar Dqdo | Georgi I. avatar LUBOMIR BOZUKOV avatar  avatar  avatar Alexandar Bozhikov avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar ะ“ะฐะฑั€ะธะตะปะฐ avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.