{"id":370369,"date":"2024-05-21T04:23:43","date_gmt":"2024-05-21T04:23:43","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=370369"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=370369","title":{"rendered":"<span>\u0420\u0430\u0437\u0431\u0438\u0440\u0430\u0435\u043c\u0441\u044f \u0432 \u0410\u0410-\u0434\u0435\u0440\u0435\u0432\u044c\u044f\u0445 (Python)<\/span>"},"content":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u0414\u043b\u044f \u043f\u043e\u043d\u0438\u043c\u0430\u043d\u0438\u044f \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0438 \u0440\u0435\u043a\u043e\u043c\u0435\u043d\u0434\u0443\u044e \u043e\u0437\u043d\u0430\u043a\u043e\u043c\u0438\u0442\u044c\u0441\u044f \u0441 \u0442\u0435\u043c, \u0447\u0442\u043e \u0442\u0430\u043a\u043e\u0435 \u043a\u0440\u0430\u0441\u043d\u043e-\u0447\u0435\u0440\u043d\u044b\u0435 \u0434\u0435\u0440\u0435\u0432\u044c\u044f (\u041a\u0427\u0414) \u0438 \u0442\u0435\u043c, \u043a\u0430\u043a \u043e\u043d\u0438 \u0440\u0430\u0431\u043e\u0442\u0430\u044e\u0442<\/p>\n<p>\u041f\u0440\u0438 \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u0438\u0438 \u043f\u0430\u0440 \u043f\u043e \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430\u043c \u0438 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430\u043c \u0434\u0430\u043d\u043d\u044b\u0445, \u044f \u0441\u0442\u043e\u043b\u043a\u043d\u0443\u043b\u0441\u044f \u0441 \u0442\u0435\u043c, \u0447\u0442\u043e \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442 \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u043c\u0430\u043b\u043e \u043c\u0430\u0442\u0435\u0440\u0438\u0430\u043b\u043e\u0432 \u043f\u043e AA-\u0434\u0435\u0440\u0435\u0432\u044c\u044f\u043c, \u0430 \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u044b\u0445 \u043f\u0440\u0438\u043c\u0435\u0440\u043e\u0432 \u0438 \u0435\u0449\u0435 \u043c\u0435\u043d\u044c\u0448\u0435. \u0422\u0430\u043a \u0447\u0442\u043e \u044d\u0442\u043e \u0441\u0442\u0430\u0442\u044c\u044f \u0434\u043b\u044f \u0442\u0430\u043a\u0438\u0445 \u0436\u0435 &#171;\u0438\u0449\u0443\u0449\u0438\u0445&#187; \u043a\u0430\u043a \u0438 \u044f \ud83d\ude42<\/p>\n<h2>\u0427\u0442\u043e \u0442\u0430\u043a\u043e\u0435 AA-\u0434\u0435\u0440\u0435\u0432\u043e?<\/h2>\n<p>\u0410\u0410-\u0434\u0435\u0440\u0435\u0432\u043e &#8212; \u044d\u0442\u043e \u043c\u043e\u0434\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u044f \u043a\u0440\u0430\u0441\u043d\u043e-\u0447\u0435\u0440\u043d\u043e\u0433\u043e \u0434\u0435\u0440\u0435\u0432\u0430 \u0441 \u0446\u0435\u043b\u044c\u044e \u0443\u043f\u0440\u043e\u0449\u0435\u043d\u0438\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438<\/p>\n<p>\u0414\u043b\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u0431\u0443\u0434\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0443\u0437\u0435\u043b (\u043d\u043e\u0434\u0443), \u0447\u0442\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0434\u043b\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u041a\u0427\u0414<\/p>\n<pre><code class=\"python\">from __future__ import annotations from dataclasses import dataclass, field from typing import Any, Literal   @dataclass class Node:     key: Any     parent: Node | None = field(default=None)     right: Node | None = field(default=None)     left: Node | None = field(default=None)     height: int = field(default=1)     color: Literal[\"red\", \"black\"] = field(default=\"black\")<\/code><\/pre>\n<ul>\n<li>\n<p>key &#8212; \u043f\u043e\u043b\u0435\u0437\u043d\u0430\u044f \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044f \u0432 \u043d\u043e\u0434\u0435 (\u0441\u0442\u0440\u043e\u043a\u0430, \u0447\u0438\u0441\u043b\u043e, etc.)<\/p>\n<\/li>\n<li>\n<p>parent &#8212; \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u0441\u043a\u0443\u044e \u043d\u043e\u0434\u0443<\/p>\n<\/li>\n<li>\n<p>right &#8212; \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043f\u0440\u0430\u0432\u043e\u0433\u043e \u0440\u0435\u0431\u0435\u043d\u043a\u0430<\/p>\n<\/li>\n<li>\n<p>left &#8212; \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043b\u0435\u0432\u043e\u0433\u043e \u0440\u0435\u0431\u0435\u043d\u043a\u0430<\/p>\n<\/li>\n<li>\n<p>height &#8212; \u0432\u044b\u0441\u043e\u0442\u0430<\/p>\n<\/li>\n<li>\n<p>color &#8212; \u0446\u0432\u0435\u0442<\/p>\n<\/li>\n<\/ul>\n<p><strong>\u041f\u0440\u0430\u0432\u0438\u043b\u0430 \u0410\u0410-\u0434\u0435\u0440\u0435\u0432\u0430:<\/strong><\/p>\n<ul>\n<li>\n<p>\u0426\u0432\u0435\u0442 \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u0447\u0435\u0440\u043d\u044b\u043c \u0438\u043b\u0438 \u043a\u0440\u0430\u0441\u043d\u044b\u043c<\/p>\n<\/li>\n<li>\n<p>\u041a\u043e\u0440\u0435\u043d\u044c \u0432\u0441\u0435\u0433\u0434\u0430 \u0447\u0435\u0440\u043d\u044b\u0439<\/p>\n<\/li>\n<li>\n<p>\u041b\u0438\u0441\u0442\u044c\u044f \u0432\u0441\u0435\u0433\u0434\u0430 \u0447\u0435\u0440\u043d\u044b\u0435<\/p>\n<\/li>\n<li>\n<p>\u041a\u0430\u0436\u0434\u0430\u044f \u043a\u0440\u0430\u0441\u043d\u0430\u044f \u0432\u0435\u0440\u0448\u0438\u043d\u0430 \u0434\u043e\u043b\u0436\u043d\u0430 \u0438\u043c\u0435\u0442\u044c \u0434\u0432\u0443\u0445 \u0447\u0435\u0440\u043d\u044b\u0445 \u0441\u044b\u043d\u043e\u0432\u0435\u0439<\/p>\n<\/li>\n<li>\n<p>\u041f\u0443\u0442\u0438 \u043e\u0442 \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u043a \u0435\u0435 \u043b\u0438\u0441\u0442\u044c\u044f\u043c \u0434\u043e\u043b\u0436\u043d\u044b \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0442\u044c \u043e\u0434\u0438\u043d\u0430\u043a\u043e\u0432\u043e\u0435 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0447\u0435\u0440\u043d\u044b\u0445 \u0432\u0435\u0440\u0448\u0438\u043d (\u0447\u0435\u0440\u043d\u0430\u044f \u0432\u044b\u0441\u043e\u0442\u0430)<\/p>\n<\/li>\n<li>\n<p>\u0412 \u0434\u0435\u0440\u0435\u0432\u0435 \u043d\u0435 \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u043b\u0435\u0432\u043e\u0433\u043e \u043a\u0440\u0430\u0441\u043d\u043e\u0433\u043e \u0441\u044b\u043d\u0430<\/p>\n<\/li>\n<\/ul>\n<p>\u0422\u0430\u043a\u0436\u0435 \u0432 \u0410\u0410-\u0434\u0435\u0440\u0435\u0432\u0435 \u043c\u0435\u043d\u044f\u0435\u0442\u0441\u044f \u043f\u043e\u043d\u044f\u0442\u0438\u0435 <strong>\u0432\u044b\u0441\u043e\u0442\u044b<\/strong><\/p>\n<p>\u0412\u044b\u0441\u043e\u0442\u0430 \u0437\u0434\u0435\u0441\u044c &#8212; \u044d\u0442\u043e \u043d\u0435\u00a0\u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u043d\u043e\u0434 \u043e\u0442\u00a0\u043a\u043e\u0440\u043d\u044f \u0434\u043e\u00a0\u0443\u0437\u043b\u0430, \u0430 \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u0430\u044f \u0432\u0435\u043b\u0438\u0447\u0438\u043d\u0430 \u0434\u043b\u044f \u0443\u0437\u043b\u0430 \u0438 \u0443\u0432\u0435\u043b\u0438\u0447\u0438\u0432\u0430\u0435\u0442\u0441\u044f \u043f\u043e\u0441\u0440\u0435\u0434\u0441\u0442\u0432\u043e\u043c \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0439 \u043f\u0440\u0438 \u043f\u0435\u0440\u0435\u0431\u0430\u043b\u0430\u043d\u0441\u0438\u0440\u043e\u0432\u043a\u0435<\/p>\n<p><strong>\u041a\u0430\u043a \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0435\u0442\u0441\u044f \u0432\u044b\u0441\u043e\u0442\u0430:<\/strong><\/p>\n<ul>\n<li>\n<p>\u0423 None \u043b\u0438\u0441\u0442\u044c\u0435\u0432 \u0432\u044b\u0441\u043e\u0442\u0430 = 0<\/p>\n<\/li>\n<li>\n<p>\u0423 \u043b\u0438\u0441\u0442\u044c\u0435\u0432 \u0432\u044b\u0441\u043e\u0442\u0430 = 1<\/p>\n<\/li>\n<li>\n<p>\u0423 \u043a\u0440\u0430\u0441\u043d\u044b\u0445 \u043d\u043e\u0434 \u0432\u044b\u0441\u043e\u0442\u0430 \u0442\u0430 \u0436\u0435, \u0447\u0442\u043e \u0438 \u0443 \u0435\u0433\u043e \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044f<\/p>\n<\/li>\n<li>\n<p>\u0423 \u0447\u0435\u0440\u043d\u044b\u0445 \u043d\u043e\u0434 \u0432\u044b\u0441\u043e\u0442\u0430 \u043d\u0430 1 \u043c\u0435\u043d\u044c\u0448\u0435, \u0447\u0435\u043c \u0443 \u0438\u0445 \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u0435\u0439<\/p>\n<\/li>\n<\/ul>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/315\/b8c\/a0d\/315b8ca0d508db36820a83d65c645bac.png\" alt=\"\u041f\u0440\u0438\u043c\u0435\u0440 AA-\u0434\u0435\u0440\u0435\u0432\u0430\" title=\"\u041f\u0440\u0438\u043c\u0435\u0440 AA-\u0434\u0435\u0440\u0435\u0432\u0430\" width=\"2598\" height=\"628\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/315\/b8c\/a0d\/315b8ca0d508db36820a83d65c645bac.png\"\/><\/p>\n<div><figcaption>\u041f\u0440\u0438\u043c\u0435\u0440 AA-\u0434\u0435\u0440\u0435\u0432\u0430<\/figcaption><\/div>\n<\/figure>\n<p>\u041a\u043e\u0440\u0435\u043d\u044c &#8212; 46<br \/>\u0412\u044b\u0441\u043e\u0442\u0430 1: 8, 9, 24, 35, &#8230;<br \/>\u0412\u044b\u0441\u043e\u0442\u0430 2: 18, 29, 57, 72<br \/>\u0412\u044b\u0441\u043e\u0442\u0430 3: 46, 68<\/p>\n<p>\u0421\u0432\u044f\u0437\u0438 \u043c-\u0434\u0443 \u043d\u043e\u0434\u0430\u043c\u0438 \u043d\u0430 \u043e\u0434\u043d\u043e\u0439 \u0432\u044b\u0441\u043e\u0442\u0435 (46 \u0438 68 \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440) \u043d\u0430\u0437\u044b\u0432\u0430\u044e\u0442\u0441\u044f\u00a0<strong>\u0433\u043e\u0440\u0438\u0437\u043e\u043d\u0442\u0430\u043b\u044c\u043d\u044b\u043c\u0438<br \/><\/strong>\u0413\u043e\u0440\u0438\u0437\u043e\u043d\u0442\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u0432\u044f\u0437\u044c <strong>\u0432\u0441\u0435\u0433\u0434\u0430<\/strong> \u0432\u0435\u0434\u0435\u0442 \u043a \u043a\u0440\u0430\u0441\u043d\u043e\u0439 \u043d\u043e\u0434\u0435<\/p>\n<h2>\u0412\u0441\u0442\u0430\u0432\u043a\u0430 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u0432 AA-\u0434\u0435\u0440\u0435\u0432\u043e<\/h2>\n<p>\u0414\u0435\u043b\u0430\u0435\u0442\u0441\u044f \u043f\u043e \u0430\u043d\u0430\u043b\u043e\u0433\u0438\u0438 \u0441 \u043a\u0440\u0430\u0441\u043d\u043e-\u0447\u0435\u0440\u043d\u044b\u043c \u0434\u0435\u0440\u0435\u0432\u043e\u043c<\/p>\n<ul>\n<li>\n<p>\u0418\u0449\u0435\u043c \u043a\u0443\u0434\u0430 \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u043d\u043e\u0434\u0443<\/p>\n<\/li>\n<li>\n<p>\u0414\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u043a\u0440\u0430\u0441\u043d\u0443\u044e \u043d\u043e\u0434\u0443<\/p>\n<\/li>\n<li>\n<p>\u041f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u043c \u0431\u0430\u043b\u0430\u043d\u0441\u0438\u0440\u043e\u0432\u043a\u0443<\/p>\n<\/li>\n<\/ul>\n<p>\u0411\u043b\u0430\u0433\u043e\u0434\u0430\u0440\u044f \u043d\u043e\u0432\u043e\u043c\u0443 \u0443\u0441\u043b\u043e\u0432\u0438\u044e (\u043b\u0435\u0432\u044b\u0439 \u0440\u0435\u0431\u0435\u043d\u043e\u043a \u043d\u0435 \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u043a\u0440\u0430\u0441\u043d\u044b\u043c), \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u044b \u043f\u0440\u0438 \u0431\u0430\u043b\u0430\u043d\u0441\u0438\u0440\u043e\u0432\u043a\u0435 \u0432\u044b\u0437\u044b\u0432\u0430\u044e\u0442 \u0432\u0441\u0435\u0433\u043e 3 (\u0432\u043c\u0435\u0441\u0442\u043e \u043f\u0440\u043e\u0448\u043b\u044b\u0445 7) \u043a\u0435\u0439\u0441\u0430, \u0434\u0430\u0432\u0430\u0439\u0442\u0435 \u0438\u0445 \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c<\/p>\n<h4>\u0421\u043b\u0443\u0447\u0430\u0439 1. \u041a\u043e\u0440\u0435\u043d\u044c \u043a\u0440\u0430\u0441\u043d\u044b\u0439<\/h4>\n<p>\u041f\u043e \u0430\u043d\u0430\u043b\u043e\u0433\u0438\u0438 \u0441 \u041a\u0427\u0414, \u043f\u0440\u043e\u0441\u0442\u043e \u043a\u0440\u0430\u0441\u0438\u043c \u043d\u043e\u0434\u0443 \u0432 \u043a\u0440\u0430\u0441\u043d\u044b\u0439<\/p>\n<h4>\u0421\u043b\u0443\u0447\u0430\u0439 2. \u041b\u0435\u0432\u0430\u044f \u0433\u043e\u0440\u0438\u0437\u043e\u043d\u0442\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u0432\u044f\u0437\u044c<\/h4>\n<p>\u0414\u043b\u044f \u043d\u0430\u0447\u0430\u043b\u0430 \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u0441\u043b\u0443\u0447\u0430\u0439 \u0435\u0441\u043b\u0438 \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044c \u0447\u0435\u0440\u043d\u044b\u0439<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/bee\/d88\/271\/beed882713f69d8f96e429e92542ec1b.png\" alt=\"\u041b\u0435\u0432\u0430\u044f \u0433\u043e\u0440\u0438\u0437\u043e\u043d\u0442\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u0432\u0437\u044f\u044c\" title=\"\u041b\u0435\u0432\u0430\u044f \u0433\u043e\u0440\u0438\u0437\u043e\u043d\u0442\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u0432\u0437\u044f\u044c\" width=\"832\" height=\"408\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/bee\/d88\/271\/beed882713f69d8f96e429e92542ec1b.png\"\/><\/p>\n<div><figcaption>\u041b\u0435\u0432\u0430\u044f \u0433\u043e\u0440\u0438\u0437\u043e\u043d\u0442\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u0432\u0437\u044f\u044c<\/figcaption><\/div>\n<\/figure>\n<p>\u041e\u0431\u043e\u0431\u0449\u0438\u043c \u0442\u0430\u043a\u043e\u0439 \u0441\u043b\u0443\u0447\u0430\u0439<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/cec\/a0d\/246\/ceca0d24627b12e86e6f6bd17af91bd5.png\" alt=\"\u041e\u0431\u043e\u0431\u0449\u0435\u043d\u043d\u0430\u044f \u043b\u0435\u0432\u0430\u044f \u0433\u043e\u0440\u0438\u0437\u043e\u043d\u0442\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u0432\u044f\u0437\u044c\" title=\"\u041e\u0431\u043e\u0431\u0449\u0435\u043d\u043d\u0430\u044f \u043b\u0435\u0432\u0430\u044f \u0433\u043e\u0440\u0438\u0437\u043e\u043d\u0442\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u0432\u044f\u0437\u044c\" width=\"1178\" height=\"834\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/cec\/a0d\/246\/ceca0d24627b12e86e6f6bd17af91bd5.png\"\/><\/p>\n<div><figcaption>\u041e\u0431\u043e\u0431\u0449\u0435\u043d\u043d\u0430\u044f \u043b\u0435\u0432\u0430\u044f \u0433\u043e\u0440\u0438\u0437\u043e\u043d\u0442\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u0432\u044f\u0437\u044c<\/figcaption><\/div>\n<\/figure>\n<p>X, Y, Z &#8212; \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u044c\u044f<\/p>\n<p>\u0427\u0442\u043e \u0434\u0435\u043b\u0430\u0442\u044c \u0432 \u0442\u0430\u043a\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435?<\/p>\n<ul>\n<li>\n<p>\u041f\u0440\u0430\u0432\u044b\u0439 \u043f\u043e\u0432\u043e\u0440\u043e\u0442 \u043c-\u0434\u0443 \u043b\u0435\u0432\u044b\u043c \u0441\u044b\u043d\u043e\u043c (A) \u0438 \u043e\u0442\u0446\u043e\u043c (B)<\/p>\n<\/li>\n<li>\n<p>\u041f\u0435\u0440\u0435\u043a\u0440\u0430\u0448\u0438\u0432\u0430\u0435\u043c \u0441\u044b\u043d\u0430 \u0432 \u0447\u0435\u0440\u043d\u043e\u0435<\/p>\n<\/li>\n<li>\n<p>\u041f\u0435\u0440\u0435\u043a\u0440\u0430\u0448\u0438\u0432\u0430\u0435\u043c \u043e\u0442\u0446\u0430 \u0432 \u043a\u0440\u0430\u0441\u043d\u043e\u0435<\/p>\n<\/li>\n<\/ul>\n<p><strong>\u0412\u044b\u0441\u043e\u0442\u0430 \u0432 \u044d\u0442\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u043d\u0438 \u0443 \u043a\u043e\u0433\u043e \u043d\u0435 \u0438\u0437\u043c\u0435\u043d\u044f\u0435\u0442\u0441\u044f<\/strong><\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/7e7\/5d4\/c7e\/7e75d4c7ef8b46c103a476c42bf31e88.png\" alt=\"\u0423\u0431\u0438\u0440\u0430\u0435\u043c \u043b\u0435\u0432\u0443\u044e \u0433\u043e\u0440\u0438\u0437\u043e\u043d\u0442\u0430\u043b\u044c\u043d\u0443\u044e \u0441\u0432\u044f\u0437\u044c, \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044f skew\" title=\"\u0423\u0431\u0438\u0440\u0430\u0435\u043c \u043b\u0435\u0432\u0443\u044e \u0433\u043e\u0440\u0438\u0437\u043e\u043d\u0442\u0430\u043b\u044c\u043d\u0443\u044e \u0441\u0432\u044f\u0437\u044c, \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044f skew\" width=\"1168\" height=\"802\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/7e7\/5d4\/c7e\/7e75d4c7ef8b46c103a476c42bf31e88.png\"\/><\/p>\n<div><figcaption>\u0423\u0431\u0438\u0440\u0430\u0435\u043c \u043b\u0435\u0432\u0443\u044e \u0433\u043e\u0440\u0438\u0437\u043e\u043d\u0442\u0430\u043b\u044c\u043d\u0443\u044e \u0441\u0432\u044f\u0437\u044c, \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044f skew<\/figcaption><\/div>\n<\/figure>\n<p>\u041d\u0430\u0437\u043e\u0432\u0435\u043c \u044d\u0442\u043e \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u00a0<strong>skew<\/strong><\/p>\n<p>\u0411\u043b\u0430\u0433\u043e\u0434\u0430\u0440\u044f \u0442\u043e\u043c\u0443, \u0447\u0442\u043e \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0435\u0435 \u043f\u0440\u0430\u0432\u0438\u043b\u043e \u0410\u0410-\u0434\u0435\u0440\u0435\u0432\u043e \u043d\u0435\u0441\u0438\u043c\u0435\u0442\u0440\u0438\u0447\u043d\u043e (\u043b\u0435\u0432\u043e\u0433\u043e \u043a\u0440\u0430\u0441\u043d\u043e\u0433\u043e \u0441\u044b\u043d\u0430 \u0431\u044b\u0442\u044c \u043d\u0435 \u043c\u043e\u0436\u0435\u0442), \u0441\u0438\u043c\u043c\u0435\u0442\u0440\u0438\u0447\u043d\u044b\u0439 \u043a\u0435\u0439\u0441 \u043f\u0440\u043e\u0431\u043b\u0435\u043c \u0443 \u043d\u0430\u0441 \u043d\u0435 \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u0442<\/p>\n<p>\u041f\u0440\u0438\u043c\u0435\u043d\u0438\u043c skew \u043a \u043d\u0430\u0448\u0435\u043c\u0443 \u043f\u0440\u0438\u043c\u0435\u0440\u0443<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/689\/054\/8cd\/6890548cd63d091255628cedaca8a630.png\" alt=\"\u041f\u0440\u0438\u043c\u0435\u043d\u0438\u043b\u0438 skew, \u0443\u0431\u0440\u0430\u043b\u0438 \u043b\u0435\u0432\u0443\u044e \u0433\u043e\u0440\u0438\u0437\u043e\u043d\u0442\u0430\u043b\u044c\u043d\u0443\u044e \u0441\u0432\u044f\u0437\u044c\" title=\"\u041f\u0440\u0438\u043c\u0435\u043d\u0438\u043b\u0438 skew, \u0443\u0431\u0440\u0430\u043b\u0438 \u043b\u0435\u0432\u0443\u044e \u0433\u043e\u0440\u0438\u0437\u043e\u043d\u0442\u0430\u043b\u044c\u043d\u0443\u044e \u0441\u0432\u044f\u0437\u044c\" width=\"780\" height=\"328\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/689\/054\/8cd\/6890548cd63d091255628cedaca8a630.png\"\/><\/p>\n<div><figcaption>\u041f\u0440\u0438\u043c\u0435\u043d\u0438\u043b\u0438 skew, \u0443\u0431\u0440\u0430\u043b\u0438 \u043b\u0435\u0432\u0443\u044e \u0433\u043e\u0440\u0438\u0437\u043e\u043d\u0442\u0430\u043b\u044c\u043d\u0443\u044e \u0441\u0432\u044f\u0437\u044c<\/figcaption><\/div>\n<\/figure>\n<p>\u0420\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u043c skew<\/p>\n<pre><code class=\"python\">def skew(node: Node) -> Node:   child = node.left   subtree = child.right    node.left = subtree   subtree.parent = node    child.right = node   child.parent = node.parent   node.parent = child    child.color = \"black\"   node.color = \"red\"    return child<\/code><\/pre>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u0441\u043b\u0443\u0447\u0430\u0439, \u0435\u0441\u043b\u0438 \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044c \u043a\u0440\u0430\u0441\u043d\u044b\u0439<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/646\/551\/95a\/64655195a800240197aa135216253113.png\" alt=\"AA-\u0434\u0435\u0440\u0435\u0432\u043e, \u0434\u043e\u0431\u0430\u0432\u0438\u043c \u043d\u043e\u0434\u0443 46\" title=\"AA-\u0434\u0435\u0440\u0435\u0432\u043e, \u0434\u043e\u0431\u0430\u0432\u0438\u043c \u043d\u043e\u0434\u0443 46\" width=\"1600\" height=\"676\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/646\/551\/95a\/64655195a800240197aa135216253113.png\"\/><\/p>\n<div><figcaption>AA-\u0434\u0435\u0440\u0435\u0432\u043e, \u0434\u043e\u0431\u0430\u0432\u0438\u043c \u043d\u043e\u0434\u0443 46<\/figcaption><\/div>\n<\/figure>\n<p>\u0414\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u0432 \u044d\u0442\u043e \u0434\u0435\u0440\u0435\u0432\u043e \u043d\u043e\u0434\u0443 46 (\u0441\u043b\u0435\u0432\u0430 \u043e\u0442 55)<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/172\/908\/79a\/17290879a380f9dff3840dc35c70b70e.png\" width=\"1644\" height=\"802\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/172\/908\/79a\/17290879a380f9dff3840dc35c70b70e.png\"\/><\/figure>\n<p>\u041d\u043e\u0434\u0430 46 \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043d\u0430 \u043e\u0434\u043d\u043e\u0439 \u0432\u044b\u0441\u043e\u0442\u0435 \u0441 55<\/p>\n<p>\u041f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u043b\u0435\u0432\u0443\u044e \u0433\u043e\u0440\u0438\u0437\u043e\u043d\u0442\u0430\u043b\u044c\u043d\u0443\u044e \u0441\u0432\u044f\u0437\u044c \u0441 \u043a\u0440\u0430\u0441\u043d\u044b\u043c \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u0435\u043c 55<\/p>\n<p>\u0427\u0442\u043e \u0434\u0435\u043b\u0430\u0442\u044c?<\/p>\n<ul>\n<li>\n<p>\u041f\u0440\u0430\u0432\u044b\u0439 \u043f\u043e\u0432\u043e\u0440\u043e\u0442 \u043c-\u0434\u0443 \u0441\u044b\u043d\u043e\u043c \u0438 \u043e\u0442\u0446\u043e\u043c<\/p>\n<\/li>\n<\/ul>\n<p>\u041f\u0435\u0440\u0435\u043a\u0440\u0430\u0441\u043a\u0430 \u0432 \u044d\u0442\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 <strong>\u043d\u0435 \u0434\u0435\u043b\u0430\u0435\u0442\u0441\u044f<\/strong><\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/fd9\/32e\/c04\/fd932ec0438dfaf32050bbfd45099a85.png\" alt=\"\u0423\u0431\u0440\u0430\u043b\u0438 \u043d\u043e\u0432\u0443\u044e \u043b\u0435\u0432\u0443\u044e \u0433\u043e\u0440\u0438\u0437\u043e\u043d\u0442\u0430\u043b\u044c\u043d\u0443\u044e \u0441\u0432\u044f\u0437\u044c\" title=\"\u0423\u0431\u0440\u0430\u043b\u0438 \u043d\u043e\u0432\u0443\u044e \u043b\u0435\u0432\u0443\u044e \u0433\u043e\u0440\u0438\u0437\u043e\u043d\u0442\u0430\u043b\u044c\u043d\u0443\u044e \u0441\u0432\u044f\u0437\u044c\" width=\"1568\" height=\"646\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/fd9\/32e\/c04\/fd932ec0438dfaf32050bbfd45099a85.png\"\/><\/p>\n<div><figcaption>\u0423\u0431\u0440\u0430\u043b\u0438 \u043d\u043e\u0432\u0443\u044e \u043b\u0435\u0432\u0443\u044e \u0433\u043e\u0440\u0438\u0437\u043e\u043d\u0442\u0430\u043b\u044c\u043d\u0443\u044e \u0441\u0432\u044f\u0437\u044c<\/figcaption><\/div>\n<\/figure>\n<p>\u0414\u043e\u0440\u0430\u0431\u043e\u0442\u0430\u0435\u043c \u0444\u0443\u043d\u043a\u0446\u0438\u044e skew<\/p>\n<pre><code class=\"python\">def skew(node: Node) -> Node:   child = node.left   subtree = child.right    node.left = subtree   subtree.parent = node    child.right = node   child.parent = node.parent   node.parent = child    if node.color == \"black\":     child.color = \"black\"     node.color = \"red\"    return child<\/code><\/pre>\n<h4>\u0421\u043b\u0443\u0447\u0430\u0439 3. \u0414\u0432\u043e\u0439\u043d\u0430\u044f \u0433\u043e\u0440\u0438\u0437\u043e\u043d\u0442\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u0432\u044f\u0437\u044c<\/h4>\n<p>\u0420\u0435\u0447\u044c \u043f\u043e\u0439\u0434\u0435\u0442 \u043e \u0434\u0432\u043e\u0439\u043d\u043e\u0439\u00a0<strong>\u043f\u0440\u0430\u0432\u043e\u0439<\/strong>\u00a0\u0433\u043e\u0440\u0438\u0437\u043e\u043d\u0442\u0430\u043b\u044c\u043d\u043e\u0439 \u0441\u0432\u044f\u0437\u0438<br \/>\u0414\u0432\u043e\u0439\u043d\u0430\u044f \u043b\u0435\u0432\u0430\u044f \u0433\u043e\u0440\u0438\u0437\u043e\u043d\u0442\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u0432\u044f\u0437\u044c \u043d\u0435\u0432\u043e\u0437\u043c\u043e\u0436\u043d\u0430, \u0444\u0438\u043a\u0441\u0438\u0442\u0441\u044f \u043a\u0430\u043a 2 \u0441\u043b\u0443\u0447\u0430\u044f \u043b\u0435\u0432\u043e\u0439 \u0433\u043e\u0440\u0438\u0437\u043e\u043d\u0442\u0430\u043b\u044c\u043d\u043e\u0439 \u0441\u0432\u044f\u0437\u0438<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/54f\/76f\/f10\/54f76ff10cc8a81ac1adaa30eaef172e.png\" alt=\"\u0414\u0432\u043e\u0439\u043d\u0430\u044f \u0433\u043e\u0440\u0438\u0437\u043e\u043d\u0442\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u0432\u044f\u0437\u044c\" title=\"\u0414\u0432\u043e\u0439\u043d\u0430\u044f \u0433\u043e\u0440\u0438\u0437\u043e\u043d\u0442\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u0432\u044f\u0437\u044c\" width=\"1136\" height=\"364\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/54f\/76f\/f10\/54f76ff10cc8a81ac1adaa30eaef172e.png\"\/><\/p>\n<div><figcaption>\u0414\u0432\u043e\u0439\u043d\u0430\u044f \u0433\u043e\u0440\u0438\u0437\u043e\u043d\u0442\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u0432\u044f\u0437\u044c<\/figcaption><\/div>\n<\/figure>\n<p>\u041e\u0431\u043e\u0431\u0449\u0438\u043c \u044d\u0442\u043e\u0442 \u0441\u043b\u0443\u0447\u0430\u0439<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/7c9\/65d\/bca\/7c965dbcafa748bf0df0072850c5d56e.png\" alt=\"\u041e\u0431\u043e\u0431\u0449\u0435\u043d\u043d\u044b\u0439 \u0441\u043b\u0443\u0447\u0430\u0439 \u0434\u0432\u043e\u0439\u043d\u043e\u0439 \u0433\u043e\u0440\u0438\u0437\u043e\u043d\u0442\u0430\u043b\u044c\u043d\u043e\u0439 \u0441\u0432\u044f\u0437\u0438\" title=\"\u041e\u0431\u043e\u0431\u0449\u0435\u043d\u043d\u044b\u0439 \u0441\u043b\u0443\u0447\u0430\u0439 \u0434\u0432\u043e\u0439\u043d\u043e\u0439 \u0433\u043e\u0440\u0438\u0437\u043e\u043d\u0442\u0430\u043b\u044c\u043d\u043e\u0439 \u0441\u0432\u044f\u0437\u0438\" width=\"1318\" height=\"692\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/7c9\/65d\/bca\/7c965dbcafa748bf0df0072850c5d56e.png\"\/><\/p>\n<div><figcaption>\u041e\u0431\u043e\u0431\u0449\u0435\u043d\u043d\u044b\u0439 \u0441\u043b\u0443\u0447\u0430\u0439 \u0434\u0432\u043e\u0439\u043d\u043e\u0439 \u0433\u043e\u0440\u0438\u0437\u043e\u043d\u0442\u0430\u043b\u044c\u043d\u043e\u0439 \u0441\u0432\u044f\u0437\u0438<\/figcaption><\/div>\n<\/figure>\n<p>X, Y, Z, W &#8212; \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u044c\u044f<\/p>\n<p>\u0427\u0442\u043e \u0434\u0435\u043b\u0430\u0435\u043c?<\/p>\n<ul>\n<li>\n<p>\u041b\u0435\u0432\u044b\u0439 \u043f\u043e\u0432\u043e\u0440\u043e\u0442 \u043c-\u0434\u0443 A \u0438 B<\/p>\n<\/li>\n<li>\n<p>\u0423\u0432\u0435\u043b\u0438\u0447\u0438\u0432\u0430\u0435\u043c \u0432\u044b\u0441\u043e\u0442\u0443 \u0443 B<\/p>\n<\/li>\n<li>\n<p>\u041f\u0435\u0440\u0435\u043a\u0440\u0430\u0448\u0438\u0432\u0430\u0435\u043c C \u0432 \u0447\u0435\u0440\u043d\u044b\u0439<\/p>\n<\/li>\n<\/ul>\n<p>\u0412 \u044d\u0442\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u0432\u044b\u0441\u043e\u0442\u0430 \u0443\u0432\u0435\u043b\u0438\u0447\u0438\u0432\u0430\u0435\u0442\u0441\u044f\u00a0<strong>\u0442\u043e\u043b\u044c\u043a\u043e \u0443 B<\/strong><\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/c66\/7aa\/93f\/c667aa93f24db355ee6505e08a07cd01.png\" alt=\"\u0420\u0430\u0437\u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u0434\u0432\u043e\u0439\u043d\u043e\u0439 \u0433\u043e\u0440\u0438\u0437\u043e\u043d\u0442\u0430\u043b\u044c\u043d\u043e\u0439 \u0441\u0432\u044f\u0437\u0438\" title=\"\u0420\u0430\u0437\u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u0434\u0432\u043e\u0439\u043d\u043e\u0439 \u0433\u043e\u0440\u0438\u0437\u043e\u043d\u0442\u0430\u043b\u044c\u043d\u043e\u0439 \u0441\u0432\u044f\u0437\u0438\" width=\"1352\" height=\"970\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/c66\/7aa\/93f\/c667aa93f24db355ee6505e08a07cd01.png\"\/><\/p>\n<div><figcaption>\u0420\u0430\u0437\u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u0434\u0432\u043e\u0439\u043d\u043e\u0439 \u0433\u043e\u0440\u0438\u0437\u043e\u043d\u0442\u0430\u043b\u044c\u043d\u043e\u0439 \u0441\u0432\u044f\u0437\u0438<\/figcaption><\/div>\n<\/figure>\n<p>\u041d\u0430\u0437\u043e\u0432\u0435\u043c \u044d\u0442\u043e \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u00a0<strong>split<\/strong><\/p>\n<p>\u041f\u043e \u0438\u0442\u043e\u0433\u0443 \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u043c\u043e\u0436\u0435\u043c \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c 3 \u0441\u043b\u0443\u0447\u0430\u044f:<\/p>\n<ul>\n<li>\n<p>B \u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u0441\u044f \u043a\u043e\u0440\u043d\u0435\u043c \u0434\u0435\u0440\u0435\u0432\u0430<\/p>\n<ul>\n<li>\n<p>\u041e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0435\u043c \u043a\u0430\u043a \u043f\u0435\u0440\u0432\u044b\u0439 \u0441\u043b\u0443\u0447\u0430\u0439<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>B \u043e\u0431\u0440\u0430\u0437\u043e\u0432\u044b\u0432\u0430\u0435\u0442\u00a0<strong>\u043b\u0435\u0432\u0443\u044e<\/strong>\u00a0\u0433\u043e\u0440\u0438\u0437\u043e\u043d\u0442\u0430\u043b\u044c\u043d\u0443\u044e \u0441\u0432\u044f\u0437\u044c<\/p>\n<ul>\n<li>\n<p>\u041e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0435\u043c \u043a\u0430\u043a \u0432\u0442\u043e\u0440\u043e\u0439 \u0441\u043b\u0443\u0447\u0430\u0439<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>B \u043e\u0431\u0440\u0430\u0437\u043e\u0432\u044b\u0432\u0430\u0435\u0442\u00a0<strong>\u043f\u0440\u0430\u0432\u0443\u044e<\/strong>\u00a0\u0433\u043e\u0440\u0438\u0437\u043e\u043d\u0442\u0430\u043b\u044c\u043d\u0443\u044e \u0441\u0432\u044f\u0437\u044c<\/p>\n<ul>\n<li>\n<p>\u0422\u0443\u0442 \u043f\u0440\u043e\u0431\u043b\u0435\u043c \u043d\u0435\u0442<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>\u0412\u0435\u0440\u043d\u0435\u043c\u0441\u044f \u043a \u043f\u0440\u0438\u043c\u0435\u0440\u0443 \u0438 \u0440\u0430\u0437\u0440\u0435\u0448\u0438\u043c \u0434\u0432\u043e\u0439\u043d\u0443\u044e \u0433\u043e\u0440\u0438\u0437\u043e\u043d\u0442\u0430\u043b\u044c\u043d\u0443\u044e \u0441\u0432\u044f\u0437\u044c<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/221\/504\/900\/221504900732ef86a904d37f2aacae2e.png\" alt=\"\u0420\u0430\u0437\u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u0434\u0432\u043e\u0439\u043d\u043e\u0439 \u0433\u043e\u0440\u0438\u0437\u043e\u043d\u0442\u0430\u043b\u044c\u043d\u043e\u0439 \u0441\u0432\u044f\u0437\u0438 \u0438\u0437 \u043f\u0440\u0438\u043c\u0435\u0440\u0430\" title=\"\u0420\u0430\u0437\u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u0434\u0432\u043e\u0439\u043d\u043e\u0439 \u0433\u043e\u0440\u0438\u0437\u043e\u043d\u0442\u0430\u043b\u044c\u043d\u043e\u0439 \u0441\u0432\u044f\u0437\u0438 \u0438\u0437 \u043f\u0440\u0438\u043c\u0435\u0440\u0430\" width=\"924\" height=\"618\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/221\/504\/900\/221504900732ef86a904d37f2aacae2e.png\"\/><\/p>\n<div><figcaption>\u0420\u0430\u0437\u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u0434\u0432\u043e\u0439\u043d\u043e\u0439 \u0433\u043e\u0440\u0438\u0437\u043e\u043d\u0442\u0430\u043b\u044c\u043d\u043e\u0439 \u0441\u0432\u044f\u0437\u0438 \u0438\u0437 \u043f\u0440\u0438\u043c\u0435\u0440\u0430<\/figcaption><\/div>\n<\/figure>\n<p>\u0420\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u043c split<\/p>\n<pre><code class=\"python\">def split(node: Node) -> Node:   child = node.right   subtree = child.left    child.parent = node.parent    node.parent = child   child.left = node    node.right = subtree    child.height += 1    child.right.color = \"black\"    return child<\/code><\/pre>\n<p>\u0421 \u043f\u043e\u043b\u043d\u044b\u043c \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u043e\u043c \u043f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u0438\u044f AA-\u0434\u0435\u0440\u0435\u0432\u0430 \u0440\u0443\u043a\u0430\u043c\u0438 \u043c\u043e\u0436\u043d\u043e \u043e\u0437\u043d\u0430\u043a\u043e\u043c\u0438\u0442\u044c\u0441\u044f \u0432 \u0432\u0438\u0434\u0435\u043e \u043d\u0438\u0436\u0435<\/p>\n<p>\u0417\u0434\u0435\u0441\u044c \u0441\u0442\u0440\u043e\u0438\u0442\u0441\u044f \u0434\u0435\u0440\u0435\u0432\u043e \u0434\u043b\u044f \u043c\u0430\u0441\u0441\u0438\u0432\u0430 [57, 55, 72, 18, 46, 68, 24, 9, 59, 97, 29, 35, 89, 8]<\/p>\n<div class=\"tm-iframe_temp\" data-src=\"https:\/\/embedd.srv.habr.com\/iframe\/65eb190387bb36d4b35dfbb1\" data-style=\"\" id=\"65eb190387bb36d4b35dfbb1\" width=\"\"><\/div>\n<h4>\u0421\u043e\u0431\u0435\u0440\u0435\u043c \u0432\u0435\u0441\u044c \u043a\u043e\u0434 \u0432\u0441\u0442\u0430\u0432\u043a\u0438<\/h4>\n<p>\u0421\u043d\u0430\u0447\u0430\u043b\u0430 \u0441\u0434\u0435\u043b\u0430\u0435\u043c \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0431\u0443\u0434\u0435\u0442 \u0438\u0441\u043f\u0440\u0430\u0432\u043b\u044f\u0442\u044c \u0432\u0441\u0435 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u044b \u043f\u0440\u0438 \u043f\u0435\u0440\u0435\u0431\u0430\u043b\u0430\u043d\u0441\u0438\u0440\u043e\u0432\u043a\u0435<\/p>\n<pre><code class=\"python\">def insert_fix(tree: Node, node: Node):   while node.parent is not None:     if node.left is not None and node.left.color == \"red\":       node = skew(node)     elif node.right is not None and node.right.color == \"red\" and node.right.right is not None and node.right.right.color == \"red\":       node = split(node)     else:       node = node.parent     tree.color = \"black\"<\/code><\/pre>\n<p>\u0410 \u0442\u0435\u043f\u0435\u0440\u044c \u0441\u0434\u0435\u043b\u0430\u0435\u043c \u043d\u0435\u043f\u043e\u0441\u0440\u0435\u0434\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u0432\u0441\u0442\u0430\u0432\u043a\u0438<\/p>\n<pre><code class=\"python\">def insert(tree: Node, key: Any):   parent = None   curr = tree    while curr is not None:     parent = curr     if key &lt; curr.key:       curr = curr.left     else:       curr = curr.right    node = Node(key=key, color=\"red\")    if parent is None:     tree = node   else:     node.parent = parent     node.height = parent.height     if key &lt; parent.key:       parent.left = node     else:       parent.right = node      insert_fix(tree, node)<\/code><\/pre>\n<h2>\u0423\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u0438\u0437 AA-\u0434\u0435\u0440\u0435\u0432\u0430<\/h2>\n<p>\u0420\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c 4 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u044b\u0445 \u0441\u043b\u0443\u0447\u0430\u044f<\/p>\n<p>\u0411\u0443\u0434\u0435\u043c \u0440\u0430\u0441\u0441\u043c\u0430\u0442\u0440\u0438\u0432\u0430\u0442\u044c \u0441\u043b\u0443\u0447\u0430\u0438 \u043d\u0430 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u043c \u0434\u0435\u0440\u0435\u0432\u0435<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/ed4\/f9c\/116\/ed4f9c116f2063b0c1ed1c2b9fae0e27.png\" alt=\"\u041f\u0440\u0438\u043c\u0435\u0440 \u0434\u0435\u0440\u0435\u0432\u0430 \u0434\u043b\u044f \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f\" title=\"\u041f\u0440\u0438\u043c\u0435\u0440 \u0434\u0435\u0440\u0435\u0432\u0430 \u0434\u043b\u044f \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f\" width=\"1620\" height=\"600\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/ed4\/f9c\/116\/ed4f9c116f2063b0c1ed1c2b9fae0e27.png\"\/><\/p>\n<div><figcaption>\u041f\u0440\u0438\u043c\u0435\u0440 \u0434\u0435\u0440\u0435\u0432\u0430 \u0434\u043b\u044f \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f<\/figcaption><\/div>\n<\/figure>\n<h4>\u0421\u043b\u0443\u0447\u0430\u0439 1. \u0423\u0434\u0430\u043b\u044f\u0435\u043c \u043a\u0440\u0430\u0441\u043d\u044b\u0439 \u043b\u0438\u0441\u0442<\/h4>\n<p>\u041f\u0440\u043e\u0441\u0442\u043e \u0443\u0434\u0430\u043b\u044f\u0435\u043c \u043b\u0438\u0441\u0442, \u043d\u0438\u0447\u0435\u0433\u043e \u0431\u043e\u043b\u044c\u0448\u0435 \u043d\u0435 \u043d\u0443\u0436\u043d\u043e \u0442.\u043a. \u0447\u0435\u0440\u043d\u0430\u044f \u0432\u044b\u0441\u043e\u0442\u0430 \u043d\u0435 \u0438\u0437\u043c\u0435\u043d\u044f\u0435\u0442\u0441\u044f<\/p>\n<p>\u0412 \u043d\u0430\u0448\u0435\u043c \u043f\u0440\u0438\u043c\u0435\u0440\u0435 &#8212; \u043d\u043e\u0434\u0430\u00a0<strong>9<\/strong><\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/c98\/d90\/c1e\/c98d90c1e35d09f69e780aad7a5011b6.png\" alt=\"\u0414\u0435\u0440\u0435\u0432\u043e \u043f\u043e\u0441\u043b\u0435 \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f \u043a\u0440\u0430\u0441\u043d\u043e\u0433\u043e \u043b\u0438\u0441\u0442\u0430\" title=\"\u0414\u0435\u0440\u0435\u0432\u043e \u043f\u043e\u0441\u043b\u0435 \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f \u043a\u0440\u0430\u0441\u043d\u043e\u0433\u043e \u043b\u0438\u0441\u0442\u0430\" width=\"1646\" height=\"602\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/c98\/d90\/c1e\/c98d90c1e35d09f69e780aad7a5011b6.png\"\/><\/p>\n<div><figcaption>\u0414\u0435\u0440\u0435\u0432\u043e \u043f\u043e\u0441\u043b\u0435 \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f \u043a\u0440\u0430\u0441\u043d\u043e\u0433\u043e \u043b\u0438\u0441\u0442\u0430<\/figcaption><\/div>\n<\/figure>\n<p>\u0420\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u043c \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f \u043a\u0440\u0430\u0441\u043d\u043e\u0433\u043e \u043b\u0438\u0441\u0442\u0430<\/p>\n<pre><code class=\"python\">def delete_red_leaf(node: Node):   parent = node.parent   parent.right = None    node.parent = None<\/code><\/pre>\n<h4>\u0421\u043b\u0443\u0447\u0430\u0439 2. \u0423\u0434\u0430\u043b\u044f\u0435\u043c \u0447\u0435\u0440\u043d\u044b\u0439 \u043b\u0438\u0441\u0442 \u0441 \u043a\u0440\u0430\u0441\u043d\u043e\u0439 \u043d\u043e\u0434\u043e\u0439<\/h4>\n<ul>\n<li>\n<p>\u0423\u0434\u0430\u043b\u044f\u0435\u043c \u0440\u0435\u0431\u0435\u043d\u043a\u0430<\/p>\n<\/li>\n<li>\n<p>\u0417\u0430\u043c\u0435\u043d\u044f\u0435\u043c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043d\u043e\u0434\u044b \u043d\u0430 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0440\u0435\u0431\u0435\u043d\u043a\u0430<\/p>\n<\/li>\n<li>\n<p>\u041f\u0435\u0440\u0435\u043a\u0440\u0430\u0448\u0438\u0432\u0430\u0435\u043c \u043d\u043e\u0434\u0443 \u0432 \u0447\u0435\u0440\u043d\u044b\u0439<\/p>\n<\/li>\n<\/ul>\n<p>\u0412 \u043d\u0430\u0448\u0435\u043c \u043f\u0440\u0438\u043c\u0435\u0440\u0435 &#8212; \u043d\u043e\u0434\u0430\u00a0<strong>8<\/strong><\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/c9d\/b31\/3bb\/c9db313bbf4b65f2baafd08c8d7b5459.png\" alt=\"\u041f\u0440\u0438\u043c\u0435\u0440 \u0434\u0435\u0440\u0435\u0432\u0430\" title=\"\u041f\u0440\u0438\u043c\u0435\u0440 \u0434\u0435\u0440\u0435\u0432\u0430\" width=\"1620\" height=\"600\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/c9d\/b31\/3bb\/c9db313bbf4b65f2baafd08c8d7b5459.png\"\/><\/p>\n<div><figcaption>\u041f\u0440\u0438\u043c\u0435\u0440 \u0434\u0435\u0440\u0435\u0432\u0430<\/figcaption><\/div>\n<\/figure>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/ea2\/92a\/a72\/ea292aa724804174e4a7d66dd113231a.png\" alt=\"\u0414\u0435\u0440\u0435\u0432\u043e \u043f\u043e\u0441\u043b\u0435 \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f \u043d\u043e\u0434\u044b 8\" title=\"\u0414\u0435\u0440\u0435\u0432\u043e \u043f\u043e\u0441\u043b\u0435 \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f \u043d\u043e\u0434\u044b 8\" width=\"1594\" height=\"602\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/ea2\/92a\/a72\/ea292aa724804174e4a7d66dd113231a.png\"\/><\/p>\n<div><figcaption>\u0414\u0435\u0440\u0435\u0432\u043e \u043f\u043e\u0441\u043b\u0435 \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f \u043d\u043e\u0434\u044b 8<\/figcaption><\/div>\n<\/figure>\n<p>\u0420\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u043c \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u043f\u043e\u0434 \u044d\u0442\u043e\u0442 \u0441\u043b\u0443\u0447\u0430\u0439<\/p>\n<pre><code class=\"python\">def remove_black_leaf_red_right(node: Node):   node.key = node.right.key   delete_red_leaf(node.right)<\/code><\/pre>\n<h4>\u0421\u043b\u0443\u0447\u0430\u0439 3. \u0423\u0434\u0430\u043b\u044f\u0435\u043c \u0443\u0437\u0435\u043b \u0441 \u0434\u0432\u0443\u043c\u044f \u0434\u0435\u0442\u044c\u043c\u0438<\/h4>\n<p>\u0414\u043b\u044f \u0437\u0430\u043c\u0435\u043d\u044b \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0432 \u0443\u0434\u0430\u043b\u044f\u0435\u043c\u043e\u0439 \u043d\u043e\u0434\u0435 \u043d\u0430\u043c \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u043d\u0430\u0439\u0442\u0438 \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0443\u0437\u0435\u043b<br \/>\u041d\u0430\u0437\u043e\u0432\u0435\u043c \u044d\u0442\u043e\u0442 \u0443\u0437\u0435\u043b\u00a0<strong>\u043d\u0430\u0441\u043b\u0435\u0434\u043d\u0438\u043a\u043e\u043c<\/strong><\/p>\n<p>\u0412 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0438 \u043e\u0442 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u043d\u0430\u0441\u043b\u0435\u0434\u043d\u0438\u043a\u0430\u043c\u0438 \u043c\u043e\u0433\u0443\u0442 \u0431\u044b\u0442\u044c:<\/p>\n<ul>\n<li>\n<p>\u041c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435, \u0431\u043e\u043b\u044c\u0448\u0435\u0435 \u0447\u0435\u043c \u0442\u0435\u043a\u0443\u0449\u0435\u0435 (&#171;\u043f\u043e\u0442\u043e\u043c\u043e\u043a&#187;)<\/p>\n<\/li>\n<li>\n<p>\u041c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435, \u043c\u0435\u043d\u044c\u0448\u0435\u0435 \u0447\u0435\u043c \u0442\u0435\u043a\u0443\u0449\u0435\u0435 (&#171;\u043f\u0440\u0435\u0434\u0448\u0435\u0441\u0442\u0432\u0435\u043d\u043d\u0438\u043a&#187;)<\/p>\n<\/li>\n<\/ul>\n<p>\u0414\u043b\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u0431\u0443\u0434\u0435\u043c \u0442\u0440\u0430\u043a\u0442\u043e\u0432\u0430\u0442\u044c \u043d\u0430\u0441\u043b\u0435\u0434\u043d\u0438\u043a\u0430 \u043a\u0430\u043a <strong>\u043f\u043e\u0442\u043e\u043c\u043a\u0430<\/strong><\/p>\n<p>\u0417\u0430\u043c\u0435\u043d\u044f\u0435\u043c \u0443\u0434\u0430\u043b\u044f\u0435\u043c\u0443\u044e \u043d\u043e\u0434\u0443 \u043d\u0430\u0441\u043b\u0435\u0434\u043d\u0438\u043a\u043e\u043c \u0438 \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0435\u043c \u043d\u0430\u0441\u043b\u0435\u0434\u043d\u0438\u043a\u0430:<\/p>\n<ul>\n<li>\n<p>\u0415\u0441\u043b\u0438 \u043d\u0430\u0441\u043b\u0435\u0434\u043d\u0438\u043a &#8212; \u0447\u0435\u0440\u043d\u044b\u0439 \u043b\u0438\u0441\u0442 \u0441 \u043a\u0440\u0430\u0441\u043d\u043e\u0439 \u043d\u043e\u0434\u043e\u0439, \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0435\u043c \u043a\u0430\u043a \u0432\u0442\u043e\u0440\u043e\u0439 \u0441\u043b\u0443\u0447\u0430\u0439<\/p>\n<\/li>\n<li>\n<p>\u0418\u043d\u0430\u0447\u0435 &#8212; \u0441\u043c. \u0441\u043b\u0443\u0447\u0430\u0439 4 (\u0441\u043c. \u043d\u0438\u0436\u0435)<\/p>\n<\/li>\n<\/ul>\n<p>\u0420\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u043c \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f \u043d\u0430\u0441\u043b\u0435\u0434\u043d\u0438\u043a\u0430<\/p>\n<pre><code class=\"python\">def find_successor(node: Node):   successor = node.right    while successor.left is not None:     successor = successor.left      return successor<\/code><\/pre>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/dd4\/2f7\/006\/dd42f7006e116f32d6916f788f54adc5.png\" alt=\"\u041f\u0440\u0438\u043c\u0435\u0440 \u0434\u0435\u0440\u0435\u0432\u0430\" title=\"\u041f\u0440\u0438\u043c\u0435\u0440 \u0434\u0435\u0440\u0435\u0432\u0430\" width=\"1620\" height=\"600\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/dd4\/2f7\/006\/dd42f7006e116f32d6916f788f54adc5.png\"\/><\/p>\n<div><figcaption>\u041f\u0440\u0438\u043c\u0435\u0440 \u0434\u0435\u0440\u0435\u0432\u0430<\/figcaption><\/div>\n<\/figure>\n<p>\u0423\u0434\u0430\u043b\u0438\u043c \u043d\u043e\u0434\u0443 18, \u043d\u0430\u0441\u043b\u0435\u0434\u043d\u0438\u043a \u0434\u043b\u044f \u043d\u0435\u0435 &#8212; 24<\/p>\n<p>\u0417\u0430\u043c\u0435\u043d\u0438\u043c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0438 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0430\u0435\u043c \u043d\u0430\u0441\u043b\u0435\u0434\u043d\u0438\u043a\u0430 \u043f\u043e \u0441\u043b\u0443\u0447\u0430\u044e 2<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/756\/814\/0d1\/7568140d170751530811298224dd9d1d.png\" alt=\"\u0423\u0434\u0430\u043b\u044f\u0435\u043c 18 \u0438\u0437 \u0434\u0435\u0440\u0435\u0432\u0430\" title=\"\u0423\u0434\u0430\u043b\u044f\u0435\u043c 18 \u0438\u0437 \u0434\u0435\u0440\u0435\u0432\u0430\" width=\"1654\" height=\"628\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/756\/814\/0d1\/7568140d170751530811298224dd9d1d.png\"\/><\/p>\n<div><figcaption>\u0423\u0434\u0430\u043b\u044f\u0435\u043c 18 \u0438\u0437 \u0434\u0435\u0440\u0435\u0432\u0430<\/figcaption><\/div>\n<\/figure>\n<p>\u0420\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u043c \u0444\u0443\u043d\u043a\u0446\u0438\u044e<\/p>\n<pre><code class=\"python\">def delete_with_red_node_successor(node: Node)   successor = find_successor(node)    node.key = successor.key   remove_black_leaf_red_right(successor)<\/code><\/pre>\n<h4>\u0421\u043b\u0443\u0447\u0430\u0439 4. \u0423\u0434\u0430\u043b\u044f\u0435\u043c \u0447\u0435\u0440\u043d\u044b\u0439 \u043b\u0438\u0441\u0442<\/h4>\n<p>\u0421\u0430\u043c\u044b\u0439 \u0441\u043b\u043e\u0436\u043d\u044b\u0439 \u0441\u043b\u0443\u0447\u0430\u0439<\/p>\n<ul>\n<li>\n<p>\u0423\u0434\u0430\u043b\u044f\u0435\u043c \u043d\u043e\u0434\u0443<\/p>\n<\/li>\n<li>\n<p>\u0418\u0434\u0435\u043c \u043e\u0442 \u0443\u0434\u0430\u043b\u0435\u043d\u043d\u043e\u0439 \u043d\u043e\u0434\u044b \u0434\u043e \u043a\u043e\u0440\u043d\u044f<\/p>\n<\/li>\n<li>\n<p>\u0414\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0439 \u043d\u043e\u0434\u044b (?) \u043d\u0430 \u043f\u0443\u0442\u0438 \u0441 \u0434\u0432\u0443\u043c\u044f \u0434\u0435\u0442\u044c\u043c\u0438 \u0434\u0435\u043b\u0430\u0435\u043c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0435:<\/p>\n<ul>\n<li>\n<p>\u0415\u0441\u043b\u0438 \u0435\u0441\u0442\u044c \u0440\u0435\u0431\u0435\u043d\u043e\u043a \u0441 \u0432\u044b\u0441\u043e\u0442\u043e\u0439 \u0441 \u0440\u0430\u0437\u043d\u0438\u0446\u0435\u0439 \u0432 2 \u043f\u043e \u0441\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u044e \u0441 \u0442\u0435\u043a\u0443\u0449\u0438\u043c:<\/p>\n<ul>\n<li>\n<p>\u0423\u043c\u0435\u043d\u044c\u0448\u0430\u0435\u043c \u0432\u044b\u0441\u043e\u0442\u0443\u00a0?<\/p>\n<\/li>\n<li>\n<p>\u0415\u0441\u043b\u0438 \u043f\u0440\u0430\u0432\u044b\u0439 \u0440\u0435\u0431\u0435\u043d\u043e\u043a &#8212; \u043a\u0440\u0430\u0441\u043d\u044b\u0439, \u0443\u043c\u0435\u043d\u044c\u0448\u0430\u0435\u043c \u0438 \u0435\u0433\u043e \u0432\u044b\u0441\u043e\u0442\u0443<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>\u041f\u0440\u0438 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e\u0441\u0442\u0438 \u0434\u0435\u043b\u0430\u0435\u043c<\/p>\n<ul>\n<li>\n<p>skew(?)<\/p>\n<\/li>\n<li>\n<p>skew(?.???\u210e?)<\/p>\n<\/li>\n<li>\n<p>skew(?.???\u210e?.???\u210e?)<\/p>\n<\/li>\n<li>\n<p>split(?)<\/p>\n<\/li>\n<li>\n<p>split(?.???\u210e?)<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>\u0415\u0441\u043b\u0438 ?\u00a0&#8212; \u043a\u0440\u0430\u0441\u043d\u044b\u0439 \u0438 \u0443 \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044f \u0432\u044b\u0441\u043e\u0442\u0430 \u0431\u043e\u043b\u044c\u0448\u0435, \u043a\u0440\u0430\u0441\u0438\u043c\u00a0?\u00a0\u0432 \u0447\u0435\u0440\u043d\u044b\u0439<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>\u041f\u0440\u0438 \u0443\u043c\u0435\u043d\u044c\u0448\u0435\u043d\u0438\u0438 \u0432\u044b\u0441\u043e\u0442\u044b, \u043a\u0440\u0430\u0441\u0438\u043c \u0434\u0435\u0442\u0435\u0439 \u0442\u043e\u0439 \u0436\u0435 \u0432\u044b\u0441\u043e\u0442\u044b \u0432 \u043a\u0440\u0430\u0441\u043d\u044b\u0439 \u0446\u0432\u0435\u0442<\/p>\n<p>\u0414\u043b\u044f \u043f\u0440\u0438\u043c\u0435\u0440\u0430 \u0432\u043e\u0437\u044c\u043c\u0435\u043c \u043f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u043d\u043e\u0435 \u0432\u044b\u0448\u0435 \u0434\u0435\u0440\u0435\u0432\u043e \u0438 \u043f\u043e\u043f\u0440\u043e\u0431\u0443\u0435\u043c \u0443\u0434\u0430\u043b\u0438\u0442\u044c \u043a\u043e\u0440\u0435\u043d\u044c &#8212;\u00a0<strong>46<\/strong><\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/e2d\/a88\/be6\/e2da88be6c513a1c967483108247fdec.png\" alt=\"\u041f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u043d\u043e\u0435 \u0434\u0435\u0440\u0435\u0432\u043e \u0438\u0437 \u0432\u0438\u0434\u0435\u043e\" title=\"\u041f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u043d\u043e\u0435 \u0434\u0435\u0440\u0435\u0432\u043e \u0438\u0437 \u0432\u0438\u0434\u0435\u043e\" width=\"2062\" height=\"508\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/e2d\/a88\/be6\/e2da88be6c513a1c967483108247fdec.png\"\/><\/p>\n<div><figcaption>\u041f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u043d\u043e\u0435 \u0434\u0435\u0440\u0435\u0432\u043e \u0438\u0437 \u0432\u0438\u0434\u0435\u043e<\/figcaption><\/div>\n<\/figure>\n<p>\u041d\u0430\u0441\u043b\u0435\u0434\u043d\u0438\u043a \u043a\u043e\u0440\u043d\u044f &#8212;\u00a0<strong>55<\/strong><br \/>\u0417\u0430\u043c\u0435\u043d\u0438\u043c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0438 \u0443\u0434\u0430\u043b\u0438\u043c \u043d\u0430\u0441\u043b\u0435\u0434\u043d\u0438\u043a\u0430<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/fa2\/c74\/6b2\/fa2c746b2ed0cdb170b254a4365e772c.png\" alt=\"\u0414\u0435\u0440\u0435\u0432\u043e \u043f\u043e\u0441\u043b\u0435 \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f \u043d\u043e\u0434\u044b 55\" title=\"\u0414\u0435\u0440\u0435\u0432\u043e \u043f\u043e\u0441\u043b\u0435 \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f \u043d\u043e\u0434\u044b 55\" width=\"2318\" height=\"546\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/fa2\/c74\/6b2\/fa2c746b2ed0cdb170b254a4365e772c.png\"\/><\/p>\n<div><figcaption>\u0414\u0435\u0440\u0435\u0432\u043e \u043f\u043e\u0441\u043b\u0435 \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f \u043d\u043e\u0434\u044b 55<\/figcaption><\/div>\n<\/figure>\n<p>\u0423 \u043d\u043e\u0434\u044b 57 \u0435\u0441\u0442\u044c \u043b\u0435\u0432\u044b\u0439 \u0440\u0435\u0431\u0435\u043d\u043e\u043a, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043e\u0442\u043b\u0438\u0447\u0430\u0435\u0442\u0441\u044f \u043f\u043e \u0432\u044b\u0441\u043e\u0442\u0435 \u043e\u0442 \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044f \u043d\u0430 2<br \/>(\u0443 None \u0432\u044b\u0441\u043e\u0442\u0430 = 0, \u0443 \u043d\u043e\u0434\u044b 57 \u0432\u044b\u0441\u043e\u0442\u0430 = 2)<\/p>\n<p>\u0423\u043c\u0435\u043d\u044c\u0448\u0430\u0435\u043c \u0432\u044b\u0441\u043e\u0442\u0443 \u0443 \u043d\u043e\u0434\u044b 57<br \/>\u0422\u043a \u0443 57 \u0435\u0441\u0442\u044c \u043f\u0440\u0430\u0432\u044b\u0439 \u0440\u0435\u0431\u0435\u043d\u043e\u043a \u0442\u043e\u0439 \u0436\u0435 \u0432\u044b\u0441\u043e\u0442\u044b (\u0432\u044b\u0441\u043e\u0442\u044b 1), \u043a\u0440\u0430\u0441\u0438\u043c \u0435\u0433\u043e \u0432 \u043a\u0440\u0430\u0441\u043d\u044b\u0439<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/8be\/aa9\/968\/8beaa9968e95d6c3c924cfde99eaa2e5.png\" alt=\"\u0423\u043c\u0435\u043d\u044c\u0448\u0430\u0435\u043c \u0432\u044b\u0441\u043e\u0442\u0443 \u0443 \u043d\u043e\u0434\u044b 57\" title=\"\u0423\u043c\u0435\u043d\u044c\u0448\u0430\u0435\u043c \u0432\u044b\u0441\u043e\u0442\u0443 \u0443 \u043d\u043e\u0434\u044b 57\" width=\"2112\" height=\"492\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/8be\/aa9\/968\/8beaa9968e95d6c3c924cfde99eaa2e5.png\"\/><\/p>\n<div><figcaption>\u0423\u043c\u0435\u043d\u044c\u0448\u0430\u0435\u043c \u0432\u044b\u0441\u043e\u0442\u0443 \u0443 \u043d\u043e\u0434\u044b 57<\/figcaption><\/div>\n<\/figure>\n<p>\u0417\u0430\u043c\u0435\u0442\u0438\u043c, \u0447\u0442\u043e \u0443 \u043d\u043e\u0434\u044b 68 \u0435\u0441\u0442\u044c \u0440\u0435\u0431\u0435\u043d\u043e\u043a, \u043e\u0442\u043b\u0438\u0447\u0430\u044e\u0449\u0438\u0439\u0441\u044f \u043f\u043e \u0432\u044b\u0441\u043e\u0442\u0435 \u043d\u0430 2 (\u043d\u043e\u0434\u0430 57)<br \/>\u041f\u043e\u043d\u0438\u0437\u0438\u043c \u0432\u044b\u0441\u043e\u0442\u0443 \u0443 \u043d\u043e\u0434\u044b 68<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/0d5\/e54\/416\/0d5e54416f5709ebe0159b327de7c117.png\" alt=\"\u0423\u043c\u0435\u043d\u044c\u0448\u0430\u0435\u043c \u0432\u044b\u0441\u043e\u0442\u0443 68\" title=\"\u0423\u043c\u0435\u043d\u044c\u0448\u0430\u0435\u043c \u0432\u044b\u0441\u043e\u0442\u0443 68\" width=\"1770\" height=\"450\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/0d5\/e54\/416\/0d5e54416f5709ebe0159b327de7c117.png\"\/><\/p>\n<div><figcaption>\u0423\u043c\u0435\u043d\u044c\u0448\u0430\u0435\u043c \u0432\u044b\u0441\u043e\u0442\u0443 68<\/figcaption><\/div>\n<\/figure>\n<p>\u041f\u043e\u043a\u0440\u0430\u0441\u0438\u043c \u043d\u043e\u0434\u044b \u043d\u0430 \u043e\u0434\u043d\u043e\u0439 \u0432\u044b\u0441\u043e\u0442\u0435 \u0432 \u043a\u0440\u0430\u0441\u043d\u044b\u0439<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/99e\/f34\/b2d\/99ef34b2dc7509e520b47a4a1e558582.png\" width=\"1734\" height=\"458\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/99e\/f34\/b2d\/99ef34b2dc7509e520b47a4a1e558582.png\"\/><\/figure>\n<p>\u041d\u043e\u0434\u0430 68 \u043a\u0440\u0430\u0441\u043d\u0430\u044f, \u0443 \u043d\u043e\u0434\u044b 55 \u0432\u044b\u0441\u043e\u0442\u0430 \u043d\u0430 1 \u0432\u044b\u0448\u0435 => \u043a\u0440\u0430\u0441\u0438\u043c 68 \u0432 \u0447\u0435\u0440\u043d\u043e\u0435<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/9fd\/ce2\/46e\/9fdce246ead6ec32d5d4e3e958fd189c.png\" alt=\"\u041f\u0435\u0440\u0435\u043a\u0440\u0430\u0448\u0438\u0432\u0430\u0435\u043c 68\" title=\"\u041f\u0435\u0440\u0435\u043a\u0440\u0430\u0448\u0438\u0432\u0430\u0435\u043c 68\" width=\"1752\" height=\"430\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/9fd\/ce2\/46e\/9fdce246ead6ec32d5d4e3e958fd189c.png\"\/><\/p>\n<div><figcaption>\u041f\u0435\u0440\u0435\u043a\u0440\u0430\u0448\u0438\u0432\u0430\u0435\u043c 68<\/figcaption><\/div>\n<\/figure>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u043f\u043e\u043f\u0440\u043e\u0431\u0443\u0435\u043c \u0443\u0434\u0430\u043b\u0438\u0442\u044c \u043d\u043e\u0434\u0443 35, \u0447\u0442\u043e\u0431\u044b \u0443\u0432\u0438\u0434\u0435\u0442\u044c \u043a\u0430\u043a \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 <strong>skew<\/strong> \u043f\u0440\u0438 \u043f\u0435\u0440\u0435\u0431\u0430\u043b\u0430\u043d\u0441\u0438\u0440\u043e\u0432\u043a\u0435<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/559\/1d2\/058\/5591d205830a2a44cc80db7ec3f0b400.png\" alt=\"\u0423\u0434\u0430\u043b\u044f\u0435\u043c 35\" title=\"\u0423\u0434\u0430\u043b\u044f\u0435\u043c 35\" width=\"2076\" height=\"504\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/559\/1d2\/058\/5591d205830a2a44cc80db7ec3f0b400.png\"\/><\/p>\n<div><figcaption>\u0423\u0434\u0430\u043b\u044f\u0435\u043c 35<\/figcaption><\/div>\n<\/figure>\n<p>\u0423 29 \u0435\u0441\u0442\u044c \u0440\u0435\u0431\u0435\u043d\u043e\u043a \u0441 \u0432\u044b\u0441\u043e\u0442\u043e\u0439 0 => \u043f\u043e\u043d\u0438\u0436\u0430\u0435\u043c \u0432\u044b\u0441\u043e\u0442\u0443 29 \u0438 \u043a\u0440\u0430\u0441\u0438\u043c 24 \u0432 \u043a\u0440\u0430\u0441\u043d\u043e\u0435<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/186\/d44\/fe9\/186d44fe98b678cb48cfae4c6c1025f5.png\" alt=\"\u0414\u0435\u0440\u0435\u0432\u043e \u043f\u043e\u0441\u043b\u0435 \u043f\u043e\u043d\u0438\u0436\u0435\u043d\u0438\u044f \u0432\u044b\u0441\u043e\u0442\u044b\" title=\"\u0414\u0435\u0440\u0435\u0432\u043e \u043f\u043e\u0441\u043b\u0435 \u043f\u043e\u043d\u0438\u0436\u0435\u043d\u0438\u044f \u0432\u044b\u0441\u043e\u0442\u044b\" width=\"2080\" height=\"498\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/186\/d44\/fe9\/186d44fe98b678cb48cfae4c6c1025f5.png\"\/><\/p>\n<div><figcaption>\u0414\u0435\u0440\u0435\u0432\u043e \u043f\u043e\u0441\u043b\u0435 \u043f\u043e\u043d\u0438\u0436\u0435\u043d\u0438\u044f \u0432\u044b\u0441\u043e\u0442\u044b<\/figcaption><\/div>\n<\/figure>\n<p>\u0414\u0435\u043b\u0430\u0435\u043c skew(29), \u043d\u0430\u043f\u043e\u043c\u043d\u044e \u0447\u0442\u043e \u0432 \u044d\u0442\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u043f\u0435\u0440\u0435\u043a\u0440\u0430\u0441\u043a\u0430 \u043d\u0435 \u043f\u0440\u043e\u0438\u0441\u043e\u0445\u0434\u0438\u0442<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/3b6\/7f4\/1b1\/3b67f41b12f844ad8abe7125f199e15e.png\" alt=\"\u041f\u0440\u043e\u0438\u0437\u0432\u0435\u043b\u0438 skew\" title=\"\u041f\u0440\u043e\u0438\u0437\u0432\u0435\u043b\u0438 skew\" width=\"2048\" height=\"476\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/3b6\/7f4\/1b1\/3b67f41b12f844ad8abe7125f199e15e.png\"\/><\/p>\n<div><figcaption>\u041f\u0440\u043e\u0438\u0437\u0432\u0435\u043b\u0438 skew<\/figcaption><\/div>\n<\/figure>\n<p>\u041d\u043e\u0434\u0430 24 \u043a\u0440\u0430\u0441\u043d\u0430\u044f, \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044c &#8212; \u0447\u0435\u0440\u043d\u044b\u0439 \u0441 \u0432\u044b\u0441\u043e\u0442\u043e\u0439 \u043d\u0430 1 \u0431\u043e\u043b\u044c\u0448\u0435 => \u043a\u0440\u0430\u0441\u0438\u043c 24 \u0432 \u0447\u0435\u0440\u043d\u043e\u0435<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/1d7\/355\/602\/1d7355602f799e731c9014ebdf5f6cdc.png\" alt=\"\u041f\u0435\u0440\u0435\u043a\u0440\u0430\u0448\u0438\u0432\u0430\u0435\u043c 24\" title=\"\u041f\u0435\u0440\u0435\u043a\u0440\u0430\u0448\u0438\u0432\u0430\u0435\u043c 24\" width=\"2050\" height=\"482\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/1d7\/355\/602\/1d7355602f799e731c9014ebdf5f6cdc.png\"\/><\/p>\n<div><figcaption>\u041f\u0435\u0440\u0435\u043a\u0440\u0430\u0448\u0438\u0432\u0430\u0435\u043c 24<\/figcaption><\/div>\n<\/figure>\n<p>\u0422\u0430\u043a\u0436\u0435 \u0435\u0441\u0442\u044c <a href=\"https:\/\/web.eecs.umich.edu\/~sugih\/courses\/eecs281\/f11\/lectures\/12-AAtrees+Treaps.pdf\" rel=\"noopener noreferrer nofollow\">\u0432\u0438\u0437\u0443\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f<\/a> \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430 \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f, \u0433\u0434\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0442\u0441\u044f \u0438 <strong>skew<\/strong>, \u0438 <strong>split<\/strong>, \u0440\u0435\u043a\u043e\u043c\u0435\u043d\u0434\u0443\u044e \u043e\u0437\u043d\u0430\u043a\u043e\u043c\u0438\u0442\u044c\u0441\u044f<\/p>\n<p>\u0420\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u043c \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u0434\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u0441\u043b\u0443\u0447\u0430\u044f<\/p>\n<p>\u0414\u043b\u044f \u043d\u0430\u0447\u0430\u043b\u0430 \u0441\u0434\u0435\u043b\u0430\u0435\u043c \u0432\u0441\u043f\u043e\u043c\u043e\u0433\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u0438<\/p>\n<pre><code class=\"python\">def get_height(node: Node | None):   if node is None:     return 0   return node.height  def check_skew(node: Node):   return node.left.height == node.height and node.left.color == \"left\"  def check_split(node: Node):   child = node.right   grandson = child.right    return node.height == child.height and child.height == grandson.height and child.color == \"red\" and grandson.color == \"red\"  def decrease_height(node: Node):   node.height -= 1   if get_height(node.right) == node.height:     node.right = \"red\"   elif get_height(node.left) == node.height:     node.left = \"red\"   return node<\/code><\/pre>\n<p>\u0410 \u0442\u0435\u043f\u0435\u0440\u044c \u043d\u0435\u043f\u043e\u0441\u0440\u0435\u0434\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u0444\u0443\u043d\u043a\u0446\u0438\u044f \u0434\u043b\u044f \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0435\u043d\u043d\u043e\u0433\u043e \u0441\u043b\u0443\u0447\u0430\u044f<\/p>\n<pre><code class=\"python\">def delete_black_leaf(node: Node):   successor = find_successor(node)    node.key = successor.key      p = successor.parent    if p.left = successor:     p.left = None   else:     p.right = None      while p.parent is not None:     height = get_height(p)     if height - get_height(p.right) > 1 or height - get_height(p.left) > 1:       p = decrease_height(p)       if get_height(p.right) > node.height or p.right.color == \"red\":         decrease_height(node.right)     if p.left is not None and check_skew(p):       p = skew(p)     if p.right is not None and p.right.left is not None and check_skew(p.right):       p.right = skew(p.right)     if p.right is not None and p.right.right is not None and p.right.right.left is not None and check_skew(p.right.right):       p.right.right = skew(p.right.right)     if p.right is not None and p.right.right is not None check_split(p):       p = split(p)     if p.right is not None and p.right.right is not None and p.right.right.right is not None check_split(p.right):       p.right = split(p.right)     if p.color == \"red\" and p.parent.height > p.height:       p.color = \"black\"          p = p.parent<\/code><\/pre>\n<h3>\u0421\u043e\u0431\u0435\u0440\u0435\u043c \u0432\u0435\u0441\u044c \u043a\u043e\u0434 \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f<\/h3>\n<pre><code class=\"python\">def delete(tree: Node, key: Any):   node = tree    if key &lt; node.key:     node = node.left   elif key > node.key:     node = node.right   else:     if node.color == \"red\" and node.height == 1:       delete_red_leaf(node)     elif node.color == \"black\" and node.height == 1 and node.right is not None:       remove_black_leaf_red_right(node)     elif node.right is not None and node.left is not None:       successor = find_successor(node)        if successor.height == 1 and successor.right is not None:         delete_with_red_node_successor(node)       else:         delete_black_leaf(node)     else:       delete_black_leaf(node)<\/code><\/pre>\n<h2>\u041f\u043e\u043b\u0435\u0437\u043d\u044b\u0435 \u0441\u0441\u044b\u043b\u043a\u0438<\/h2>\n<ul>\n<li>\n<p><a href=\"https:\/\/web.eecs.umich.edu\/~sugih\/courses\/eecs281\/f11\/lectures\/12-AAtrees+Treaps.pdf\" rel=\"noopener noreferrer nofollow\">\u041b\u0435\u043a\u0446\u0438\u044f \u043e AA-\u0434\u0435\u0440\u0435\u0432\u044c\u044f\u0445<\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"https:\/\/neerc.ifmo.ru\/wiki\/index.php?title=AA-%D0%B4%D0%B5%D1%80%D0%B5%D0%B2%D0%BE\" rel=\"noopener noreferrer nofollow\">\u0412\u0438\u043a\u0438\u043a\u043e\u043d\u0441\u043f\u0435\u043a\u0442 \u043f\u0440\u043e AA-\u0434\u0435\u0440\u0435\u0432\u044c\u044f<\/a><\/p>\n<\/li>\n<\/ul>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/799137\/\"> https:\/\/habr.com\/ru\/articles\/799137\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u0414\u043b\u044f \u043f\u043e\u043d\u0438\u043c\u0430\u043d\u0438\u044f \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0438 \u0440\u0435\u043a\u043e\u043c\u0435\u043d\u0434\u0443\u044e \u043e\u0437\u043d\u0430\u043a\u043e\u043c\u0438\u0442\u044c\u0441\u044f \u0441 \u0442\u0435\u043c, \u0447\u0442\u043e \u0442\u0430\u043a\u043e\u0435 \u043a\u0440\u0430\u0441\u043d\u043e-\u0447\u0435\u0440\u043d\u044b\u0435 \u0434\u0435\u0440\u0435\u0432\u044c\u044f (\u041a\u0427\u0414) \u0438 \u0442\u0435\u043c, \u043a\u0430\u043a \u043e\u043d\u0438 \u0440\u0430\u0431\u043e\u0442\u0430\u044e\u0442<\/p>\n<p>\u041f\u0440\u0438 \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u0438\u0438 \u043f\u0430\u0440 \u043f\u043e \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430\u043c \u0438 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430\u043c \u0434\u0430\u043d\u043d\u044b\u0445, \u044f \u0441\u0442\u043e\u043b\u043a\u043d\u0443\u043b\u0441\u044f \u0441 \u0442\u0435\u043c, \u0447\u0442\u043e \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442 \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u043c\u0430\u043b\u043e \u043c\u0430\u0442\u0435\u0440\u0438\u0430\u043b\u043e\u0432 \u043f\u043e AA-\u0434\u0435\u0440\u0435\u0432\u044c\u044f\u043c, \u0430 \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u044b\u0445 \u043f\u0440\u0438\u043c\u0435\u0440\u043e\u0432 \u0438 \u0435\u0449\u0435 \u043c\u0435\u043d\u044c\u0448\u0435. \u0422\u0430\u043a \u0447\u0442\u043e \u044d\u0442\u043e \u0441\u0442\u0430\u0442\u044c\u044f \u0434\u043b\u044f \u0442\u0430\u043a\u0438\u0445 \u0436\u0435 &#171;\u0438\u0449\u0443\u0449\u0438\u0445&#187; \u043a\u0430\u043a \u0438 \u044f \ud83d\ude42<\/p>\n<h2>\u0427\u0442\u043e \u0442\u0430\u043a\u043e\u0435 AA-\u0434\u0435\u0440\u0435\u0432\u043e?<\/h2>\n<p>\u0410\u0410-\u0434\u0435\u0440\u0435\u0432\u043e &#8212; \u044d\u0442\u043e \u043c\u043e\u0434\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u044f \u043a\u0440\u0430\u0441\u043d\u043e-\u0447\u0435\u0440\u043d\u043e\u0433\u043e \u0434\u0435\u0440\u0435\u0432\u0430 \u0441 \u0446\u0435\u043b\u044c\u044e \u0443\u043f\u0440\u043e\u0449\u0435\u043d\u0438\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438<\/p>\n<p>\u0414\u043b\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u0431\u0443\u0434\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0443\u0437\u0435\u043b (\u043d\u043e\u0434\u0443), \u0447\u0442\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0434\u043b\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u041a\u0427\u0414<\/p>\n<pre><code class=\"python\">from __future__ import annotations from dataclasses import dataclass, field from typing import Any, Literal   @dataclass class Node:     key: Any     parent: Node | None = field(default=None)     right: Node | None = field(default=None)     left: Node | None = field(default=None)     height: int = field(default=1)     color: Literal[\"red\", \"black\"] = field(default=\"black\")<\/code><\/pre>\n<ul>\n<li>\n<p>key &#8212; \u043f\u043e\u043b\u0435\u0437\u043d\u0430\u044f \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044f \u0432 \u043d\u043e\u0434\u0435 (\u0441\u0442\u0440\u043e\u043a\u0430, \u0447\u0438\u0441\u043b\u043e, etc.)<\/p>\n<\/li>\n<li>\n<p>parent &#8212; \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u0441\u043a\u0443\u044e \u043d\u043e\u0434\u0443<\/p>\n<\/li>\n<li>\n<p>right &#8212; \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043f\u0440\u0430\u0432\u043e\u0433\u043e \u0440\u0435\u0431\u0435\u043d\u043a\u0430<\/p>\n<\/li>\n<li>\n<p>left &#8212; \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043b\u0435\u0432\u043e\u0433\u043e \u0440\u0435\u0431\u0435\u043d\u043a\u0430<\/p>\n<\/li>\n<li>\n<p>height &#8212; \u0432\u044b\u0441\u043e\u0442\u0430<\/p>\n<\/li>\n<li>\n<p>color &#8212; \u0446\u0432\u0435\u0442<\/p>\n<\/li>\n<\/ul>\n<p><strong>\u041f\u0440\u0430\u0432\u0438\u043b\u0430 \u0410\u0410-\u0434\u0435\u0440\u0435\u0432\u0430:<\/strong><\/p>\n<ul>\n<li>\n<p>\u0426\u0432\u0435\u0442 \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u0447\u0435\u0440\u043d\u044b\u043c \u0438\u043b\u0438 \u043a\u0440\u0430\u0441\u043d\u044b\u043c<\/p>\n<\/li>\n<li>\n<p>\u041a\u043e\u0440\u0435\u043d\u044c \u0432\u0441\u0435\u0433\u0434\u0430 \u0447\u0435\u0440\u043d\u044b\u0439<\/p>\n<\/li>\n<li>\n<p>\u041b\u0438\u0441\u0442\u044c\u044f \u0432\u0441\u0435\u0433\u0434\u0430 \u0447\u0435\u0440\u043d\u044b\u0435<\/p>\n<\/li>\n<li>\n<p>\u041a\u0430\u0436\u0434\u0430\u044f \u043a\u0440\u0430\u0441\u043d\u0430\u044f \u0432\u0435\u0440\u0448\u0438\u043d\u0430 \u0434\u043e\u043b\u0436\u043d\u0430 \u0438\u043c\u0435\u0442\u044c \u0434\u0432\u0443\u0445 \u0447\u0435\u0440\u043d\u044b\u0445 \u0441\u044b\u043d\u043e\u0432\u0435\u0439<\/p>\n<\/li>\n<li>\n<p>\u041f\u0443\u0442\u0438 \u043e\u0442 \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u043a \u0435\u0435 \u043b\u0438\u0441\u0442\u044c\u044f\u043c \u0434\u043e\u043b\u0436\u043d\u044b \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0442\u044c \u043e\u0434\u0438\u043d\u0430\u043a\u043e\u0432\u043e\u0435 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0447\u0435\u0440\u043d\u044b\u0445 \u0432\u0435\u0440\u0448\u0438\u043d (\u0447\u0435\u0440\u043d\u0430\u044f \u0432\u044b\u0441\u043e\u0442\u0430)<\/p>\n<\/li>\n<li>\n<p>\u0412 \u0434\u0435\u0440\u0435\u0432\u0435 \u043d\u0435 \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u043b\u0435\u0432\u043e\u0433\u043e \u043a\u0440\u0430\u0441\u043d\u043e\u0433\u043e \u0441\u044b\u043d\u0430<\/p>\n<\/li>\n<\/ul>\n<p>\u0422\u0430\u043a\u0436\u0435 \u0432 \u0410\u0410-\u0434\u0435\u0440\u0435\u0432\u0435 \u043c\u0435\u043d\u044f\u0435\u0442\u0441\u044f \u043f\u043e\u043d\u044f\u0442\u0438\u0435 <strong>\u0432\u044b\u0441\u043e\u0442\u044b<\/strong><\/p>\n<p>\u0412\u044b\u0441\u043e\u0442\u0430 \u0437\u0434\u0435\u0441\u044c &#8212; \u044d\u0442\u043e \u043d\u0435\u00a0\u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u043d\u043e\u0434 \u043e\u0442\u00a0\u043a\u043e\u0440\u043d\u044f \u0434\u043e\u00a0\u0443\u0437\u043b\u0430, \u0430 \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u0430\u044f \u0432\u0435\u043b\u0438\u0447\u0438\u043d\u0430 \u0434\u043b\u044f \u0443\u0437\u043b\u0430 \u0438 \u0443\u0432\u0435\u043b\u0438\u0447\u0438\u0432\u0430\u0435\u0442\u0441\u044f \u043f\u043e\u0441\u0440\u0435\u0434\u0441\u0442\u0432\u043e\u043c \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0439 \u043f\u0440\u0438 \u043f\u0435\u0440\u0435\u0431\u0430\u043b\u0430\u043d\u0441\u0438\u0440\u043e\u0432\u043a\u0435<\/p>\n<p><strong>\u041a\u0430\u043a \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0435\u0442\u0441\u044f \u0432\u044b\u0441\u043e\u0442\u0430:<\/strong><\/p>\n<ul>\n<li>\n<p>\u0423 None \u043b\u0438\u0441\u0442\u044c\u0435\u0432 \u0432\u044b\u0441\u043e\u0442\u0430 = 0<\/p>\n<\/li>\n<li>\n<p>\u0423 \u043b\u0438\u0441\u0442\u044c\u0435\u0432 \u0432\u044b\u0441\u043e\u0442\u0430 = 1<\/p>\n<\/li>\n<li>\n<p>\u0423 \u043a\u0440\u0430\u0441\u043d\u044b\u0445 \u043d\u043e\u0434 \u0432\u044b\u0441\u043e\u0442\u0430 \u0442\u0430 \u0436\u0435, \u0447\u0442\u043e \u0438 \u0443 \u0435\u0433\u043e \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044f<\/p>\n<\/li>\n<li>\n<p>\u0423 \u0447\u0435\u0440\u043d\u044b\u0445 \u043d\u043e\u0434 \u0432\u044b\u0441\u043e\u0442\u0430 \u043d\u0430 1 \u043c\u0435\u043d\u044c\u0448\u0435, \u0447\u0435\u043c \u0443 \u0438\u0445 \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u0435\u0439<\/p>\n<\/li>\n<\/ul>\n<figure class=\"full-width\">\n<div><figcaption>\u041f\u0440\u0438\u043c\u0435\u0440 AA-\u0434\u0435\u0440\u0435\u0432\u0430<\/figcaption><\/div>\n<\/figure>\n<p>\u041a\u043e\u0440\u0435\u043d\u044c &#8212; 46<br \/>\u0412\u044b\u0441\u043e\u0442\u0430 1: 8, 9, 24, 35, &#8230;<br \/>\u0412\u044b\u0441\u043e\u0442\u0430 2: 18, 29, 57, 72<br \/>\u0412\u044b\u0441\u043e\u0442\u0430 3: 46, 68<\/p>\n<p>\u0421\u0432\u044f\u0437\u0438 \u043c-\u0434\u0443 \u043d\u043e\u0434\u0430\u043c\u0438 \u043d\u0430 \u043e\u0434\u043d\u043e\u0439 \u0432\u044b\u0441\u043e\u0442\u0435 (46 \u0438 68 \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440) \u043d\u0430\u0437\u044b\u0432\u0430\u044e\u0442\u0441\u044f\u00a0<strong>\u0433\u043e\u0440\u0438\u0437\u043e\u043d\u0442\u0430\u043b\u044c\u043d\u044b\u043c\u0438<br \/><\/strong>\u0413\u043e\u0440\u0438\u0437\u043e\u043d\u0442\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u0432\u044f\u0437\u044c <strong>\u0432\u0441\u0435\u0433\u0434\u0430<\/strong> \u0432\u0435\u0434\u0435\u0442 \u043a \u043a\u0440\u0430\u0441\u043d\u043e\u0439 \u043d\u043e\u0434\u0435<\/p>\n<h2>\u0412\u0441\u0442\u0430\u0432\u043a\u0430 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u0432 AA-\u0434\u0435\u0440\u0435\u0432\u043e<\/h2>\n<p>\u0414\u0435\u043b\u0430\u0435\u0442\u0441\u044f \u043f\u043e \u0430\u043d\u0430\u043b\u043e\u0433\u0438\u0438 \u0441 \u043a\u0440\u0430\u0441\u043d\u043e-\u0447\u0435\u0440\u043d\u044b\u043c \u0434\u0435\u0440\u0435\u0432\u043e\u043c<\/p>\n<ul>\n<li>\n<p>\u0418\u0449\u0435\u043c \u043a\u0443\u0434\u0430 \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u043d\u043e\u0434\u0443<\/p>\n<\/li>\n<li>\n<p>\u0414\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u043a\u0440\u0430\u0441\u043d\u0443\u044e \u043d\u043e\u0434\u0443<\/p>\n<\/li>\n<li>\n<p>\u041f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u043c \u0431\u0430\u043b\u0430\u043d\u0441\u0438\u0440\u043e\u0432\u043a\u0443<\/p>\n<\/li>\n<\/ul>\n<p>\u0411\u043b\u0430\u0433\u043e\u0434\u0430\u0440\u044f \u043d\u043e\u0432\u043e\u043c\u0443 \u0443\u0441\u043b\u043e\u0432\u0438\u044e (\u043b\u0435\u0432\u044b\u0439 \u0440\u0435\u0431\u0435\u043d\u043e\u043a \u043d\u0435 \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u043a\u0440\u0430\u0441\u043d\u044b\u043c), \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u044b \u043f\u0440\u0438 \u0431\u0430\u043b\u0430\u043d\u0441\u0438\u0440\u043e\u0432\u043a\u0435 \u0432\u044b\u0437\u044b\u0432\u0430\u044e\u0442 \u0432\u0441\u0435\u0433\u043e 3 (\u0432\u043c\u0435\u0441\u0442\u043e \u043f\u0440\u043e\u0448\u043b\u044b\u0445 7) \u043a\u0435\u0439\u0441\u0430, \u0434\u0430\u0432\u0430\u0439\u0442\u0435 \u0438\u0445 \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c<\/p>\n<h4>\u0421\u043b\u0443\u0447\u0430\u0439 1. \u041a\u043e\u0440\u0435\u043d\u044c \u043a\u0440\u0430\u0441\u043d\u044b\u0439<\/h4>\n<p>\u041f\u043e \u0430\u043d\u0430\u043b\u043e\u0433\u0438\u0438 \u0441 \u041a\u0427\u0414, \u043f\u0440\u043e\u0441\u0442\u043e \u043a\u0440\u0430\u0441\u0438\u043c \u043d\u043e\u0434\u0443 \u0432 \u043a\u0440\u0430\u0441\u043d\u044b\u0439<\/p>\n<h4>\u0421\u043b\u0443\u0447\u0430\u0439 2. \u041b\u0435\u0432\u0430\u044f \u0433\u043e\u0440\u0438\u0437\u043e\u043d\u0442\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u0432\u044f\u0437\u044c<\/h4>\n<p>\u0414\u043b\u044f \u043d\u0430\u0447\u0430\u043b\u0430 \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u0441\u043b\u0443\u0447\u0430\u0439 \u0435\u0441\u043b\u0438 \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044c \u0447\u0435\u0440\u043d\u044b\u0439<\/p>\n<figure class=\"full-width\">\n<div><figcaption>\u041b\u0435\u0432\u0430\u044f \u0433\u043e\u0440\u0438\u0437\u043e\u043d\u0442\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u0432\u0437\u044f\u044c<\/figcaption><\/div>\n<\/figure>\n<p>\u041e\u0431\u043e\u0431\u0449\u0438\u043c \u0442\u0430\u043a\u043e\u0439 \u0441\u043b\u0443\u0447\u0430\u0439<\/p>\n<figure class=\"full-width\">\n<div><figcaption>\u041e\u0431\u043e\u0431\u0449\u0435\u043d\u043d\u0430\u044f \u043b\u0435\u0432\u0430\u044f \u0433\u043e\u0440\u0438\u0437\u043e\u043d\u0442\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u0432\u044f\u0437\u044c<\/figcaption><\/div>\n<\/figure>\n<p>X, Y, Z &#8212; \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u044c\u044f<\/p>\n<p>\u0427\u0442\u043e \u0434\u0435\u043b\u0430\u0442\u044c \u0432 \u0442\u0430\u043a\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435?<\/p>\n<ul>\n<li>\n<p>\u041f\u0440\u0430\u0432\u044b\u0439 \u043f\u043e\u0432\u043e\u0440\u043e\u0442 \u043c-\u0434\u0443 \u043b\u0435\u0432\u044b\u043c \u0441\u044b\u043d\u043e\u043c (A) \u0438 \u043e\u0442\u0446\u043e\u043c (B)<\/p>\n<\/li>\n<li>\n<p>\u041f\u0435\u0440\u0435\u043a\u0440\u0430\u0448\u0438\u0432\u0430\u0435\u043c \u0441\u044b\u043d\u0430 \u0432 \u0447\u0435\u0440\u043d\u043e\u0435<\/p>\n<\/li>\n<li>\n<p>\u041f\u0435\u0440\u0435\u043a\u0440\u0430\u0448\u0438\u0432\u0430\u0435\u043c \u043e\u0442\u0446\u0430 \u0432 \u043a\u0440\u0430\u0441\u043d\u043e\u0435<\/p>\n<\/li>\n<\/ul>\n<p><strong>\u0412\u044b\u0441\u043e\u0442\u0430 \u0432 \u044d\u0442\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u043d\u0438 \u0443 \u043a\u043e\u0433\u043e \u043d\u0435 \u0438\u0437\u043c\u0435\u043d\u044f\u0435\u0442\u0441\u044f<\/strong><\/p>\n<figure class=\"full-width\">\n<div><figcaption>\u0423\u0431\u0438\u0440\u0430\u0435\u043c \u043b\u0435\u0432\u0443\u044e \u0433\u043e\u0440\u0438\u0437\u043e\u043d\u0442\u0430\u043b\u044c\u043d\u0443\u044e \u0441\u0432\u044f\u0437\u044c, \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044f skew<\/figcaption><\/div>\n<\/figure>\n<p>\u041d\u0430\u0437\u043e\u0432\u0435\u043c \u044d\u0442\u043e \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u00a0<strong>skew<\/strong><\/p>\n<p>\u0411\u043b\u0430\u0433\u043e\u0434\u0430\u0440\u044f \u0442\u043e\u043c\u0443, \u0447\u0442\u043e \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0435\u0435 \u043f\u0440\u0430\u0432\u0438\u043b\u043e \u0410\u0410-\u0434\u0435\u0440\u0435\u0432\u043e \u043d\u0435\u0441\u0438\u043c\u0435\u0442\u0440\u0438\u0447\u043d\u043e (\u043b\u0435\u0432\u043e\u0433\u043e \u043a\u0440\u0430\u0441\u043d\u043e\u0433\u043e \u0441\u044b\u043d\u0430 \u0431\u044b\u0442\u044c \u043d\u0435 \u043c\u043e\u0436\u0435\u0442), \u0441\u0438\u043c\u043c\u0435\u0442\u0440\u0438\u0447\u043d\u044b\u0439 \u043a\u0435\u0439\u0441 \u043f\u0440\u043e\u0431\u043b\u0435\u043c \u0443 \u043d\u0430\u0441 \u043d\u0435 \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u0442<\/p>\n<p>\u041f\u0440\u0438\u043c\u0435\u043d\u0438\u043c skew \u043a \u043d\u0430\u0448\u0435\u043c\u0443 \u043f\u0440\u0438\u043c\u0435\u0440\u0443<\/p>\n<figure class=\"full-width\">\n<div><figcaption>\u041f\u0440\u0438\u043c\u0435\u043d\u0438\u043b\u0438 skew, \u0443\u0431\u0440\u0430\u043b\u0438 \u043b\u0435\u0432\u0443\u044e \u0433\u043e\u0440\u0438\u0437\u043e\u043d\u0442\u0430\u043b\u044c\u043d\u0443\u044e \u0441\u0432\u044f\u0437\u044c<\/figcaption><\/div>\n<\/figure>\n<p>\u0420\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u043c skew<\/p>\n<pre><code class=\"python\">def skew(node: Node) -> Node:   child = node.left   subtree = child.right    node.left = subtree   subtree.parent = node    child.right = node   child.parent = node.parent   node.parent = child    child.color = \"black\"   node.color = \"red\"    return child<\/code><\/pre>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u0441\u043b\u0443\u0447\u0430\u0439, \u0435\u0441\u043b\u0438 \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044c \u043a\u0440\u0430\u0441\u043d\u044b\u0439<\/p>\n<figure class=\"full-width\">\n<div><figcaption>AA-\u0434\u0435\u0440\u0435\u0432\u043e, \u0434\u043e\u0431\u0430\u0432\u0438\u043c \u043d\u043e\u0434\u0443 46<\/figcaption><\/div>\n<\/figure>\n<p>\u0414\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u0432 \u044d\u0442\u043e \u0434\u0435\u0440\u0435\u0432\u043e \u043d\u043e\u0434\u0443 46 (\u0441\u043b\u0435\u0432\u0430 \u043e\u0442 55)<\/p>\n<figure class=\"full-width\"><\/figure>\n<p>\u041d\u043e\u0434\u0430 46 \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043d\u0430 \u043e\u0434\u043d\u043e\u0439 \u0432\u044b\u0441\u043e\u0442\u0435 \u0441 55<\/p>\n<p>\u041f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u043b\u0435\u0432\u0443\u044e \u0433\u043e\u0440\u0438\u0437\u043e\u043d\u0442\u0430\u043b\u044c\u043d\u0443\u044e \u0441\u0432\u044f\u0437\u044c \u0441 \u043a\u0440\u0430\u0441\u043d\u044b\u043c \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u0435\u043c 55<\/p>\n<p>\u0427\u0442\u043e \u0434\u0435\u043b\u0430\u0442\u044c?<\/p>\n<ul>\n<li>\n<p>\u041f\u0440\u0430\u0432\u044b\u0439 \u043f\u043e\u0432\u043e\u0440\u043e\u0442 \u043c-\u0434\u0443 \u0441\u044b\u043d\u043e\u043c \u0438 \u043e\u0442\u0446\u043e\u043c<\/p>\n<\/li>\n<\/ul>\n<p>\u041f\u0435\u0440\u0435\u043a\u0440\u0430\u0441\u043a\u0430 \u0432 \u044d\u0442\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 <strong>\u043d\u0435 \u0434\u0435\u043b\u0430\u0435\u0442\u0441\u044f<\/strong><\/p>\n<figure class=\"full-width\">\n<div><figcaption>\u0423\u0431\u0440\u0430\u043b\u0438 \u043d\u043e\u0432\u0443\u044e \u043b\u0435\u0432\u0443\u044e \u0433\u043e\u0440\u0438\u0437\u043e\u043d\u0442\u0430\u043b\u044c\u043d\u0443\u044e \u0441\u0432\u044f\u0437\u044c<\/figcaption><\/div>\n<\/figure>\n<p>\u0414\u043e\u0440\u0430\u0431\u043e\u0442\u0430\u0435\u043c \u0444\u0443\u043d\u043a\u0446\u0438\u044e skew<\/p>\n<pre><code class=\"python\">def skew(node: Node) -> Node:   child = node.left   subtree = child.right    node.left = subtree   subtree.parent = node    child.right = node   child.parent = node.parent   node.parent = child    if node.color == \"black\":     child.color = \"black\"     node.color = \"red\"    return child<\/code><\/pre>\n<h4>\u0421\u043b\u0443\u0447\u0430\u0439 3. \u0414\u0432\u043e\u0439\u043d\u0430\u044f \u0433\u043e\u0440\u0438\u0437\u043e\u043d\u0442\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u0432\u044f\u0437\u044c<\/h4>\n<p>\u0420\u0435\u0447\u044c \u043f\u043e\u0439\u0434\u0435\u0442 \u043e \u0434\u0432\u043e\u0439\u043d\u043e\u0439\u00a0<strong>\u043f\u0440\u0430\u0432\u043e\u0439<\/strong>\u00a0\u0433\u043e\u0440\u0438\u0437\u043e\u043d\u0442\u0430\u043b\u044c\u043d\u043e\u0439 \u0441\u0432\u044f\u0437\u0438<br \/>\u0414\u0432\u043e\u0439\u043d\u0430\u044f \u043b\u0435\u0432\u0430\u044f \u0433\u043e\u0440\u0438\u0437\u043e\u043d\u0442\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u0432\u044f\u0437\u044c \u043d\u0435\u0432\u043e\u0437\u043c\u043e\u0436\u043d\u0430, \u0444\u0438\u043a\u0441\u0438\u0442\u0441\u044f \u043a\u0430\u043a 2 \u0441\u043b\u0443\u0447\u0430\u044f \u043b\u0435\u0432\u043e\u0439 \u0433\u043e\u0440\u0438\u0437\u043e\u043d\u0442\u0430\u043b\u044c\u043d\u043e\u0439 \u0441\u0432\u044f\u0437\u0438<\/p>\n<figure class=\"full-width\">\n<div><figcaption>\u0414\u0432\u043e\u0439\u043d\u0430\u044f \u0433\u043e\u0440\u0438\u0437\u043e\u043d\u0442\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u0432\u044f\u0437\u044c<\/figcaption><\/div>\n<\/figure>\n<p>\u041e\u0431\u043e\u0431\u0449\u0438\u043c \u044d\u0442\u043e\u0442 \u0441\u043b\u0443\u0447\u0430\u0439<\/p>\n<figure class=\"full-width\">\n<div><figcaption>\u041e\u0431\u043e\u0431\u0449\u0435\u043d\u043d\u044b\u0439 \u0441\u043b\u0443\u0447\u0430\u0439 \u0434\u0432\u043e\u0439\u043d\u043e\u0439 \u0433\u043e\u0440\u0438\u0437\u043e\u043d\u0442\u0430\u043b\u044c\u043d\u043e\u0439 \u0441\u0432\u044f\u0437\u0438<\/figcaption><\/div>\n<\/figure>\n<p>X, Y, Z, W &#8212; \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u044c\u044f<\/p>\n<p>\u0427\u0442\u043e \u0434\u0435\u043b\u0430\u0435\u043c?<\/p>\n<ul>\n<li>\n<p>\u041b\u0435\u0432\u044b\u0439 \u043f\u043e\u0432\u043e\u0440\u043e\u0442 \u043c-\u0434\u0443 A \u0438 B<\/p>\n<\/li>\n<li>\n<p>\u0423\u0432\u0435\u043b\u0438\u0447\u0438\u0432\u0430\u0435\u043c \u0432\u044b\u0441\u043e\u0442\u0443 \u0443 B<\/p>\n<\/li>\n<li>\n<p>\u041f\u0435\u0440\u0435\u043a\u0440\u0430\u0448\u0438\u0432\u0430\u0435\u043c C \u0432 \u0447\u0435\u0440\u043d\u044b\u0439<\/p>\n<\/li>\n<\/ul>\n<p>\u0412 \u044d\u0442\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u0432\u044b\u0441\u043e\u0442\u0430 \u0443\u0432\u0435\u043b\u0438\u0447\u0438\u0432\u0430\u0435\u0442\u0441\u044f\u00a0<strong>\u0442\u043e\u043b\u044c\u043a\u043e \u0443 B<\/strong><\/p>\n<figure class=\"full-width\">\n<div><figcaption>\u0420\u0430\u0437\u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u0434\u0432\u043e\u0439\u043d\u043e\u0439 \u0433\u043e\u0440\u0438\u0437\u043e\u043d\u0442\u0430\u043b\u044c\u043d\u043e\u0439 \u0441\u0432\u044f\u0437\u0438<\/figcaption><\/div>\n<\/figure>\n<p>\u041d\u0430\u0437\u043e\u0432\u0435\u043c \u044d\u0442\u043e \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u00a0<strong>split<\/strong><\/p>\n<p>\u041f\u043e \u0438\u0442\u043e\u0433\u0443 \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u043c\u043e\u0436\u0435\u043c \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c 3 \u0441\u043b\u0443\u0447\u0430\u044f:<\/p>\n<ul>\n<li>\n<p>B \u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u0441\u044f \u043a\u043e\u0440\u043d\u0435\u043c \u0434\u0435\u0440\u0435\u0432\u0430<\/p>\n<ul>\n<li>\n<p>\u041e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0435\u043c \u043a\u0430\u043a \u043f\u0435\u0440\u0432\u044b\u0439 \u0441\u043b\u0443\u0447\u0430\u0439<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>B \u043e\u0431\u0440\u0430\u0437\u043e\u0432\u044b\u0432\u0430\u0435\u0442\u00a0<strong>\u043b\u0435\u0432\u0443\u044e<\/strong>\u00a0\u0433\u043e\u0440\u0438\u0437\u043e\u043d\u0442\u0430\u043b\u044c\u043d\u0443\u044e \u0441\u0432\u044f\u0437\u044c<\/p>\n<ul>\n<li>\n<p>\u041e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0435\u043c \u043a\u0430\u043a \u0432\u0442\u043e\u0440\u043e\u0439 \u0441\u043b\u0443\u0447\u0430\u0439<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>B \u043e\u0431\u0440\u0430\u0437\u043e\u0432\u044b\u0432\u0430\u0435\u0442\u00a0<strong>\u043f\u0440\u0430\u0432\u0443\u044e<\/strong>\u00a0\u0433\u043e\u0440\u0438\u0437\u043e\u043d\u0442\u0430\u043b\u044c\u043d\u0443\u044e \u0441\u0432\u044f\u0437\u044c<\/p>\n<ul>\n<li>\n<p>\u0422\u0443\u0442 \u043f\u0440\u043e\u0431\u043b\u0435\u043c \u043d\u0435\u0442<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>\u0412\u0435\u0440\u043d\u0435\u043c\u0441\u044f \u043a \u043f\u0440\u0438\u043c\u0435\u0440\u0443 \u0438 \u0440\u0430\u0437\u0440\u0435\u0448\u0438\u043c \u0434\u0432\u043e\u0439\u043d\u0443\u044e \u0433\u043e\u0440\u0438\u0437\u043e\u043d\u0442\u0430\u043b\u044c\u043d\u0443\u044e \u0441\u0432\u044f\u0437\u044c<\/p>\n<figure class=\"full-width\">\n<div><figcaption>\u0420\u0430\u0437\u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u0434\u0432\u043e\u0439\u043d\u043e\u0439 \u0433\u043e\u0440\u0438\u0437\u043e\u043d\u0442\u0430\u043b\u044c\u043d\u043e\u0439 \u0441\u0432\u044f\u0437\u0438 \u0438\u0437 \u043f\u0440\u0438\u043c\u0435\u0440\u0430<\/figcaption><\/div>\n<\/figure>\n<p>\u0420\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u043c split<\/p>\n<pre><code class=\"python\">def split(node: Node) -> Node:   child = node.right   subtree = child.left    child.parent = node.parent    node.parent = child   child.left = node    node.right = subtree    child.height += 1    child.right.color = \"black\"    return child<\/code><\/pre>\n<p>\u0421 \u043f\u043e\u043b\u043d\u044b\u043c \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u043e\u043c \u043f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u0438\u044f AA-\u0434\u0435\u0440\u0435\u0432\u0430 \u0440\u0443\u043a\u0430\u043c\u0438 \u043c\u043e\u0436\u043d\u043e \u043e\u0437\u043d\u0430\u043a\u043e\u043c\u0438\u0442\u044c\u0441\u044f \u0432 \u0432\u0438\u0434\u0435\u043e \u043d\u0438\u0436\u0435<\/p>\n<p>\u0417\u0434\u0435\u0441\u044c \u0441\u0442\u0440\u043e\u0438\u0442\u0441\u044f \u0434\u0435\u0440\u0435\u0432\u043e \u0434\u043b\u044f \u043c\u0430\u0441\u0441\u0438\u0432\u0430 [57, 55, 72, 18, 46, 68, 24, 9, 59, 97, 29, 35, 89, 8]<\/p>\n<div class=\"tm-iframe_temp\" data-src=\"https:\/\/embedd.srv.habr.com\/iframe\/65eb190387bb36d4b35dfbb1\" data-style=\"\" id=\"65eb190387bb36d4b35dfbb1\" width=\"\"><\/div>\n<h4>\u0421\u043e\u0431\u0435\u0440\u0435\u043c \u0432\u0435\u0441\u044c \u043a\u043e\u0434 \u0432\u0441\u0442\u0430\u0432\u043a\u0438<\/h4>\n<p>\u0421\u043d\u0430\u0447\u0430\u043b\u0430 \u0441\u0434\u0435\u043b\u0430\u0435\u043c \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0431\u0443\u0434\u0435\u0442 \u0438\u0441\u043f\u0440\u0430\u0432\u043b\u044f\u0442\u044c \u0432\u0441\u0435 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u044b \u043f\u0440\u0438 \u043f\u0435\u0440\u0435\u0431\u0430\u043b\u0430\u043d\u0441\u0438\u0440\u043e\u0432\u043a\u0435<\/p>\n<pre><code class=\"python\">def insert_fix(tree: Node, node: Node):   while node.parent is not None:     if node.left is not None and node.left.color == \"red\":       node = skew(node)     elif node.right is not None and node.right.color == \"red\" and node.right.right is not None and node.right.right.color == \"red\":       node = split(node)     else:       node = node.parent     tree.color = \"black\"<\/code><\/pre>\n<p>\u0410 \u0442\u0435\u043f\u0435\u0440\u044c \u0441\u0434\u0435\u043b\u0430\u0435\u043c \u043d\u0435\u043f\u043e\u0441\u0440\u0435\u0434\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u0432\u0441\u0442\u0430\u0432\u043a\u0438<\/p>\n<pre><code class=\"python\">def insert(tree: Node, key: Any):   parent = None   curr = tree    while curr is not None:     parent = curr     if key &lt; curr.key:       curr = curr.left     else:       curr = curr.right    node = Node(key=key, color=\"red\")    if parent is None:     tree = node   else:     node.parent = parent     node.height = parent.height     if key &lt; parent.key:       parent.left = node     else:       parent.right = node      insert_fix(tree, node)<\/code><\/pre>\n<h2>\u0423\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u0438\u0437 AA-\u0434\u0435\u0440\u0435\u0432\u0430<\/h2>\n<p>\u0420\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c 4 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u044b\u0445 \u0441\u043b\u0443\u0447\u0430\u044f<\/p>\n<p>\u0411\u0443\u0434\u0435\u043c \u0440\u0430\u0441\u0441\u043c\u0430\u0442\u0440\u0438\u0432\u0430\u0442\u044c \u0441\u043b\u0443\u0447\u0430\u0438 \u043d\u0430 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u043c \u0434\u0435\u0440\u0435\u0432\u0435<\/p>\n<figure class=\"full-width\">\n<div><figcaption>\u041f\u0440\u0438\u043c\u0435\u0440 \u0434\u0435\u0440\u0435\u0432\u0430 \u0434\u043b\u044f \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f<\/figcaption><\/div>\n<\/figure>\n<h4>\u0421\u043b\u0443\u0447\u0430\u0439 1. \u0423\u0434\u0430\u043b\u044f\u0435\u043c \u043a\u0440\u0430\u0441\u043d\u044b\u0439 \u043b\u0438\u0441\u0442<\/h4>\n<p>\u041f\u0440\u043e\u0441\u0442\u043e \u0443\u0434\u0430\u043b\u044f\u0435\u043c \u043b\u0438\u0441\u0442, \u043d\u0438\u0447\u0435\u0433\u043e \u0431\u043e\u043b\u044c\u0448\u0435 \u043d\u0435 \u043d\u0443\u0436\u043d\u043e \u0442.\u043a. \u0447\u0435\u0440\u043d\u0430\u044f \u0432\u044b\u0441\u043e\u0442\u0430 \u043d\u0435 \u0438\u0437\u043c\u0435\u043d\u044f\u0435\u0442\u0441\u044f<\/p>\n<p>\u0412 \u043d\u0430\u0448\u0435\u043c \u043f\u0440\u0438\u043c\u0435\u0440\u0435 &#8212; \u043d\u043e\u0434\u0430\u00a0<strong>9<\/strong><\/p>\n<figure class=\"full-width\">\n<div><figcaption>\u0414\u0435\u0440\u0435\u0432\u043e \u043f\u043e\u0441\u043b\u0435 \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f \u043a\u0440\u0430\u0441\u043d\u043e\u0433\u043e \u043b\u0438\u0441\u0442\u0430<\/figcaption><\/div>\n<\/figure>\n<p>\u0420\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u043c \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f \u043a\u0440\u0430\u0441\u043d\u043e\u0433\u043e \u043b\u0438\u0441\u0442\u0430<\/p>\n<pre><code class=\"python\">def delete_red_leaf(node: Node):   parent = node.parent   parent.right = None    node.parent = None<\/code><\/pre>\n<h4>\u0421\u043b\u0443\u0447\u0430\u0439 2. \u0423\u0434\u0430\u043b\u044f\u0435\u043c \u0447\u0435\u0440\u043d\u044b\u0439 \u043b\u0438\u0441\u0442 \u0441 \u043a\u0440\u0430\u0441\u043d\u043e\u0439 \u043d\u043e\u0434\u043e\u0439<\/h4>\n<ul>\n<li>\n<p>\u0423\u0434\u0430\u043b\u044f\u0435\u043c \u0440\u0435\u0431\u0435\u043d\u043a\u0430<\/p>\n<\/li>\n<li>\n<p>\u0417\u0430\u043c\u0435\u043d\u044f\u0435\u043c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043d\u043e\u0434\u044b \u043d\u0430 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0440\u0435\u0431\u0435\u043d\u043a\u0430<\/p>\n<\/li>\n<li>\n<p>\u041f\u0435\u0440\u0435\u043a\u0440\u0430\u0448\u0438\u0432\u0430\u0435\u043c \u043d\u043e\u0434\u0443 \u0432 \u0447\u0435\u0440\u043d\u044b\u0439<\/p>\n<\/li>\n<\/ul>\n<p>\u0412 \u043d\u0430\u0448\u0435\u043c \u043f\u0440\u0438\u043c\u0435\u0440\u0435 &#8212; \u043d\u043e\u0434\u0430\u00a0<strong>8<\/strong><\/p>\n<figure class=\"full-width\">\n<div><figcaption>\u041f\u0440\u0438\u043c\u0435\u0440 \u0434\u0435\u0440\u0435\u0432\u0430<\/figcaption><\/div>\n<\/figure>\n<figure class=\"full-width\">\n<div><figcaption>\u0414\u0435\u0440\u0435\u0432\u043e \u043f\u043e\u0441\u043b\u0435 \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f \u043d\u043e\u0434\u044b 8<\/figcaption><\/div>\n<\/figure>\n<p>\u0420\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u043c \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u043f\u043e\u0434 \u044d\u0442\u043e\u0442 \u0441\u043b\u0443\u0447\u0430\u0439<\/p>\n<pre><code class=\"python\">def remove_black_leaf_red_right(node: Node):   node.key = node.right.key   delete_red_leaf(node.right)<\/code><\/pre>\n<h4>\u0421\u043b\u0443\u0447\u0430\u0439 3. \u0423\u0434\u0430\u043b\u044f\u0435\u043c \u0443\u0437\u0435\u043b \u0441 \u0434\u0432\u0443\u043c\u044f \u0434\u0435\u0442\u044c\u043c\u0438<\/h4>\n<p>\u0414\u043b\u044f \u0437\u0430\u043c\u0435\u043d\u044b \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0432 \u0443\u0434\u0430\u043b\u044f\u0435\u043c\u043e\u0439 \u043d\u043e\u0434\u0435 \u043d\u0430\u043c \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u043d\u0430\u0439\u0442\u0438 \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0443\u0437\u0435\u043b<br \/>\u041d\u0430\u0437\u043e\u0432\u0435\u043c \u044d\u0442\u043e\u0442 \u0443\u0437\u0435\u043b\u00a0<strong>\u043d\u0430\u0441\u043b\u0435\u0434\u043d\u0438\u043a\u043e\u043c<\/strong><\/p>\n<p>\u0412 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0438 \u043e\u0442 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u043d\u0430\u0441\u043b\u0435\u0434\u043d\u0438\u043a\u0430\u043c\u0438 \u043c\u043e\u0433\u0443\u0442 \u0431\u044b\u0442\u044c:<\/p>\n<ul>\n<li>\n<p>\u041c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435, \u0431\u043e\u043b\u044c\u0448\u0435\u0435 \u0447\u0435\u043c \u0442\u0435\u043a\u0443\u0449\u0435\u0435 (&#171;\u043f\u043e\u0442\u043e\u043c\u043e\u043a&#187;)<\/p>\n<\/li>\n<li>\n<p>\u041c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435, \u043c\u0435\u043d\u044c\u0448\u0435\u0435 \u0447\u0435\u043c \u0442\u0435\u043a\u0443\u0449\u0435\u0435 (&#171;\u043f\u0440\u0435\u0434\u0448\u0435\u0441\u0442\u0432\u0435\u043d\u043d\u0438\u043a&#187;)<\/p>\n<\/li>\n<\/ul>\n<p>\u0414\u043b\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u0431\u0443\u0434\u0435\u043c \u0442\u0440\u0430\u043a\u0442\u043e\u0432\u0430\u0442\u044c \u043d\u0430\u0441\u043b\u0435\u0434\u043d\u0438\u043a\u0430 \u043a\u0430\u043a <strong>\u043f\u043e\u0442\u043e\u043c\u043a\u0430<\/strong><\/p>\n<p>\u0417\u0430\u043c\u0435\u043d\u044f\u0435\u043c \u0443\u0434\u0430\u043b\u044f\u0435\u043c\u0443\u044e \u043d\u043e\u0434\u0443 \u043d\u0430\u0441\u043b\u0435\u0434\u043d\u0438\u043a\u043e\u043c \u0438 \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0435\u043c \u043d\u0430\u0441\u043b\u0435\u0434\u043d\u0438\u043a\u0430:<\/p>\n<ul>\n<li>\n<p>\u0415\u0441\u043b\u0438 \u043d\u0430\u0441\u043b\u0435\u0434\u043d\u0438\u043a &#8212; \u0447\u0435\u0440\u043d\u044b\u0439 \u043b\u0438\u0441\u0442 \u0441 \u043a\u0440\u0430\u0441\u043d\u043e\u0439 \u043d\u043e\u0434\u043e\u0439, \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0435\u043c \u043a\u0430\u043a \u0432\u0442\u043e\u0440\u043e\u0439 \u0441\u043b\u0443\u0447\u0430\u0439<\/p>\n<\/li>\n<li>\n<p>\u0418\u043d\u0430\u0447\u0435 &#8212; \u0441\u043c. \u0441\u043b\u0443\u0447\u0430\u0439 4 (\u0441\u043c. \u043d\u0438\u0436\u0435)<\/p>\n<\/li>\n<\/ul>\n<p>\u0420\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u043c \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f \u043d\u0430\u0441\u043b\u0435\u0434\u043d\u0438\u043a\u0430<\/p>\n<pre><code class=\"python\">def find_successor(node: Node):   successor = node.right    while successor.left is not None:     successor = successor.left      return successor<\/code><\/pre>\n<figure class=\"full-width\">\n<div><figcaption>\u041f\u0440\u0438\u043c\u0435\u0440 \u0434\u0435\u0440\u0435\u0432\u0430<\/figcaption><\/div>\n<\/figure>\n<p>\u0423\u0434\u0430\u043b\u0438\u043c \u043d\u043e\u0434\u0443 18, \u043d\u0430\u0441\u043b\u0435\u0434\u043d\u0438\u043a \u0434\u043b\u044f \u043d\u0435\u0435 &#8212; 24<\/p>\n<p>\u0417\u0430\u043c\u0435\u043d\u0438\u043c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0438 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0430\u0435\u043c \u043d\u0430\u0441\u043b\u0435\u0434\u043d\u0438\u043a\u0430 \u043f\u043e \u0441\u043b\u0443\u0447\u0430\u044e 2<\/p>\n<figure class=\"full-width\">\n<div><figcaption>\u0423\u0434\u0430\u043b\u044f\u0435\u043c 18 \u0438\u0437 \u0434\u0435\u0440\u0435\u0432\u0430<\/figcaption><\/div>\n<\/figure>\n<p>\u0420\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u043c \u0444\u0443\u043d\u043a\u0446\u0438\u044e<\/p>\n<pre><code class=\"python\">def delete_with_red_node_successor(node: Node)   successor = find_successor(node)    node.key = successor.key   remove_black_leaf_red_right(successor)<\/code><\/pre>\n<h4>\u0421\u043b\u0443\u0447\u0430\u0439 4. \u0423\u0434\u0430\u043b\u044f\u0435\u043c \u0447\u0435\u0440\u043d\u044b\u0439 \u043b\u0438\u0441\u0442<\/h4>\n<p>\u0421\u0430\u043c\u044b\u0439 \u0441\u043b\u043e\u0436\u043d\u044b\u0439 \u0441\u043b\u0443\u0447\u0430\u0439<\/p>\n<ul>\n<li>\n<p>\u0423\u0434\u0430\u043b\u044f\u0435\u043c \u043d\u043e\u0434\u0443<\/p>\n<\/li>\n<li>\n<p>\u0418\u0434\u0435\u043c \u043e\u0442 \u0443\u0434\u0430\u043b\u0435\u043d\u043d\u043e\u0439 \u043d\u043e\u0434\u044b \u0434\u043e \u043a\u043e\u0440\u043d\u044f<\/p>\n<\/li>\n<li>\n<p>\u0414\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0439 \u043d\u043e\u0434\u044b (?) \u043d\u0430 \u043f\u0443\u0442\u0438 \u0441 \u0434\u0432\u0443\u043c\u044f \u0434\u0435\u0442\u044c\u043c\u0438 \u0434\u0435\u043b\u0430\u0435\u043c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0435:<\/p>\n<ul>\n<li>\n<p>\u0415\u0441\u043b\u0438 \u0435\u0441\u0442\u044c \u0440\u0435\u0431\u0435\u043d\u043e\u043a \u0441 \u0432\u044b\u0441\u043e\u0442\u043e\u0439 \u0441 \u0440\u0430\u0437\u043d\u0438\u0446\u0435\u0439 \u0432 2 \u043f\u043e \u0441\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u044e \u0441 \u0442\u0435\u043a\u0443\u0449\u0438\u043c:<\/p>\n<ul>\n<li>\n<p>\u0423\u043c\u0435\u043d\u044c\u0448\u0430\u0435\u043c \u0432\u044b\u0441\u043e\u0442\u0443\u00a0?<\/p>\n<\/li>\n<li>\n<p>\u0415\u0441\u043b\u0438 \u043f\u0440\u0430\u0432\u044b\u0439 \u0440\u0435\u0431\u0435\u043d\u043e\u043a &#8212; \u043a\u0440\u0430\u0441\u043d\u044b\u0439, \u0443\u043c\u0435\u043d\u044c\u0448\u0430\u0435\u043c \u0438 \u0435\u0433\u043e \u0432\u044b\u0441\u043e\u0442\u0443<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>\u041f\u0440\u0438 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e\u0441\u0442\u0438 \u0434\u0435\u043b\u0430\u0435\u043c<\/p>\n<ul>\n<li>\n<p>skew(?)<\/p>\n<\/li>\n<li>\n<p>skew(?.???\u210e?)<\/p>\n<\/li>\n<li>\n<p>skew(?.???\u210e?.???\u210e?)<\/p>\n<\/li>\n<li>\n<p>split(?)<\/p>\n<\/li>\n<li>\n<p>split(?.???\u210e?)<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>\u0415\u0441\u043b\u0438 ?\u00a0&#8212; \u043a\u0440\u0430\u0441\u043d\u044b\u0439 \u0438 \u0443 \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044f \u0432\u044b\u0441\u043e\u0442\u0430 \u0431\u043e\u043b\u044c\u0448\u0435, \u043a\u0440\u0430\u0441\u0438\u043c\u00a0?\u00a0\u0432 \u0447\u0435\u0440\u043d\u044b\u0439<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>\u041f\u0440\u0438 \u0443\u043c\u0435\u043d\u044c\u0448\u0435\u043d\u0438\u0438 \u0432\u044b\u0441\u043e\u0442\u044b, \u043a\u0440\u0430\u0441\u0438\u043c \u0434\u0435\u0442\u0435\u0439 \u0442\u043e\u0439 \u0436\u0435 \u0432\u044b\u0441\u043e\u0442\u044b \u0432 \u043a\u0440\u0430\u0441\u043d\u044b\u0439 \u0446\u0432\u0435\u0442<\/p>\n<p>\u0414\u043b\u044f \u043f\u0440\u0438\u043c\u0435\u0440\u0430 \u0432\u043e\u0437\u044c\u043c\u0435\u043c \u043f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u043d\u043e\u0435 \u0432\u044b\u0448\u0435 \u0434\u0435\u0440\u0435\u0432\u043e \u0438 \u043f\u043e\u043f\u0440\u043e\u0431\u0443\u0435\u043c \u0443\u0434\u0430\u043b\u0438\u0442\u044c \u043a\u043e\u0440\u0435\u043d\u044c &#8212;\u00a0<strong>46<\/strong><\/p>\n<figure class=\"full-width\">\n<div><figcaption>\u041f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u043d\u043e\u0435 \u0434\u0435\u0440\u0435\u0432\u043e \u0438\u0437 \u0432\u0438\u0434\u0435\u043e<\/figcaption><\/div>\n<\/figure>\n<p>\u041d\u0430\u0441\u043b\u0435\u0434\u043d\u0438\u043a \u043a\u043e\u0440\u043d\u044f &#8212;\u00a0<strong>55<\/strong><br \/>\u0417\u0430\u043c\u0435\u043d\u0438\u043c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0438 \u0443\u0434\u0430\u043b\u0438\u043c \u043d\u0430\u0441\u043b\u0435\u0434\u043d\u0438\u043a\u0430<\/p>\n<figure class=\"full-width\">\n<div><figcaption>\u0414\u0435\u0440\u0435\u0432\u043e \u043f\u043e\u0441\u043b\u0435 \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f \u043d\u043e\u0434\u044b 55<\/figcaption><\/div>\n<\/figure>\n<p>\u0423 \u043d\u043e\u0434\u044b 57 \u0435\u0441\u0442\u044c \u043b\u0435\u0432\u044b\u0439 \u0440\u0435\u0431\u0435\u043d\u043e\u043a, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043e\u0442\u043b\u0438\u0447\u0430\u0435\u0442\u0441\u044f \u043f\u043e \u0432\u044b\u0441\u043e\u0442\u0435 \u043e\u0442 \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044f \u043d\u0430 2<br \/>(\u0443 None \u0432\u044b\u0441\u043e\u0442\u0430 = 0, \u0443 \u043d\u043e\u0434\u044b 57 \u0432\u044b\u0441\u043e\u0442\u0430 = 2)<\/p>\n<p>\u0423\u043c\u0435\u043d\u044c\u0448\u0430\u0435\u043c \u0432\u044b\u0441\u043e\u0442\u0443 \u0443 \u043d\u043e\u0434\u044b 57<br \/>\u0422\u043a \u0443 57 \u0435\u0441\u0442\u044c \u043f\u0440\u0430\u0432\u044b\u0439 \u0440\u0435\u0431\u0435\u043d\u043e\u043a \u0442\u043e\u0439 \u0436\u0435 \u0432\u044b\u0441\u043e\u0442\u044b (\u0432\u044b\u0441\u043e\u0442\u044b 1), \u043a\u0440\u0430\u0441\u0438\u043c \u0435\u0433\u043e \u0432 \u043a\u0440\u0430\u0441\u043d\u044b\u0439<\/p>\n<figure class=\"full-width\">\n<div><figcaption>\u0423\u043c\u0435\u043d\u044c\u0448\u0430\u0435\u043c \u0432\u044b\u0441\u043e\u0442\u0443 \u0443 \u043d\u043e\u0434\u044b 57<\/figcaption><\/div>\n<\/figure>\n<p>\u0417\u0430\u043c\u0435\u0442\u0438\u043c, \u0447\u0442\u043e \u0443 \u043d\u043e\u0434\u044b 68 \u0435\u0441\u0442\u044c \u0440\u0435\u0431\u0435\u043d\u043e\u043a, \u043e\u0442\u043b\u0438\u0447\u0430\u044e\u0449\u0438\u0439\u0441\u044f \u043f\u043e \u0432\u044b\u0441\u043e\u0442\u0435 \u043d\u0430 2 (\u043d\u043e\u0434\u0430 57)<br \/>\u041f\u043e\u043d\u0438\u0437\u0438\u043c \u0432\u044b\u0441\u043e\u0442\u0443 \u0443 \u043d\u043e\u0434\u044b 68<\/p>\n<figure class=\"full-width\">\n<div><figcaption>\u0423\u043c\u0435\u043d\u044c\u0448\u0430\u0435\u043c \u0432\u044b\u0441\u043e\u0442\u0443 68<\/figcaption><\/div>\n<\/figure>\n<p>\u041f\u043e\u043a\u0440\u0430\u0441\u0438\u043c \u043d\u043e\u0434\u044b \u043d\u0430 \u043e\u0434\u043d\u043e\u0439 \u0432\u044b\u0441\u043e\u0442\u0435 \u0432 \u043a\u0440\u0430\u0441\u043d\u044b\u0439<\/p>\n<figure class=\"full-width\"><\/figure>\n<p>\u041d\u043e\u0434\u0430 68 \u043a\u0440\u0430\u0441\u043d\u0430\u044f, \u0443 \u043d\u043e\u0434\u044b 55 \u0432\u044b\u0441\u043e\u0442\u0430 \u043d\u0430 1 \u0432\u044b\u0448\u0435 => \u043a\u0440\u0430\u0441\u0438\u043c 68 \u0432 \u0447\u0435\u0440\u043d\u043e\u0435<\/p>\n<figure class=\"full-width\">\n<div><figcaption>\u041f\u0435\u0440\u0435\u043a\u0440\u0430\u0448\u0438\u0432\u0430\u0435\u043c 68<\/figcaption><\/div>\n<\/figure>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u043f\u043e\u043f\u0440\u043e\u0431\u0443\u0435\u043c \u0443\u0434\u0430\u043b\u0438\u0442\u044c \u043d\u043e\u0434\u0443 35, \u0447\u0442\u043e\u0431\u044b \u0443\u0432\u0438\u0434\u0435\u0442\u044c \u043a\u0430\u043a \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 <strong>skew<\/strong> \u043f\u0440\u0438 \u043f\u0435\u0440\u0435\u0431\u0430\u043b\u0430\u043d\u0441\u0438\u0440\u043e\u0432\u043a\u0435<\/p>\n<figure class=\"full-width\">\n<div><figcaption>\u0423\u0434\u0430\u043b\u044f\u0435\u043c 35<\/figcaption><\/div>\n<\/figure>\n<p>\u0423 29 \u0435\u0441\u0442\u044c \u0440\u0435\u0431\u0435\u043d\u043e\u043a \u0441 \u0432\u044b\u0441\u043e\u0442\u043e\u0439 0 => \u043f\u043e\u043d\u0438\u0436\u0430\u0435\u043c \u0432\u044b\u0441\u043e\u0442\u0443 29 \u0438 \u043a\u0440\u0430\u0441\u0438\u043c 24 \u0432 \u043a\u0440\u0430\u0441\u043d\u043e\u0435<\/p>\n<figure class=\"full-width\">\n<div><figcaption>\u0414\u0435\u0440\u0435\u0432\u043e \u043f\u043e\u0441\u043b\u0435 \u043f\u043e\u043d\u0438\u0436\u0435\u043d\u0438\u044f \u0432\u044b\u0441\u043e\u0442\u044b<\/figcaption><\/div>\n<\/figure>\n<p>\u0414\u0435\u043b\u0430\u0435\u043c skew(29), \u043d\u0430\u043f\u043e\u043c\u043d\u044e \u0447\u0442\u043e \u0432 \u044d\u0442\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u043f\u0435\u0440\u0435\u043a\u0440\u0430\u0441\u043a\u0430 \u043d\u0435 \u043f\u0440\u043e\u0438\u0441\u043e\u0445\u0434\u0438\u0442<\/p>\n<figure class=\"full-width\">\n<div><figcaption>\u041f\u0440\u043e\u0438\u0437\u0432\u0435\u043b\u0438 skew<\/figcaption><\/div>\n<\/figure>\n<p>\u041d\u043e\u0434\u0430 24 \u043a\u0440\u0430\u0441\u043d\u0430\u044f, \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044c &#8212; \u0447\u0435\u0440\u043d\u044b\u0439 \u0441 \u0432\u044b\u0441\u043e\u0442\u043e\u0439 \u043d\u0430 1 \u0431\u043e\u043b\u044c\u0448\u0435 => \u043a\u0440\u0430\u0441\u0438\u043c 24 \u0432 \u0447\u0435\u0440\u043d\u043e\u0435<\/p>\n<figure class=\"full-width\">\n<div><figcaption>\u041f\u0435\u0440\u0435\u043a\u0440\u0430\u0448\u0438\u0432\u0430\u0435\u043c 24<\/figcaption><\/div>\n<\/figure>\n<p>\u0422\u0430\u043a\u0436\u0435 \u0435\u0441\u0442\u044c <a href=\"https:\/\/web.eecs.umich.edu\/~sugih\/courses\/eecs281\/f11\/lectures\/12-AAtrees+Treaps.pdf\" rel=\"noopener noreferrer nofollow\">\u0432\u0438\u0437\u0443\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f<\/a> \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430 \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f, \u0433\u0434\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0442\u0441\u044f \u0438 <strong>skew<\/strong>, \u0438 <strong>split<\/strong>, \u0440\u0435\u043a\u043e\u043c\u0435\u043d\u0434\u0443\u044e \u043e\u0437\u043d\u0430\u043a\u043e\u043c\u0438\u0442\u044c\u0441\u044f<\/p>\n<p>\u0420\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u043c \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u0434\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u0441\u043b\u0443\u0447\u0430\u044f<\/p>\n<p>\u0414\u043b\u044f \u043d\u0430\u0447\u0430\u043b\u0430 \u0441\u0434\u0435\u043b\u0430\u0435\u043c \u0432\u0441\u043f\u043e\u043c\u043e\u0433\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u0438<\/p>\n<pre><code class=\"python\">def get_height(node: Node | None):   if node is None:     return 0   return node.height  def check_skew(node: Node):   return node.left.height == node.height and node.left.color == \"left\"  def check_split(node: Node):   child = node.right   grandson = child.right    return node.height == child.height and child.height == grandson.height and child.color == \"red\" and<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-370369","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/370369","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=370369"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/370369\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=370369"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=370369"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=370369"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}