mirror of
https://github.com/mfocko/blog.git
synced 2024-11-14 01:59:42 +01:00
1 line
No EOL
23 KiB
JavaScript
1 line
No EOL
23 KiB
JavaScript
"use strict";(self.webpackChunkfi=self.webpackChunkfi||[]).push([[9898],{35610:(e,n,a)=>{a.r(n),a.d(n,{assets:()=>o,contentTitle:()=>i,default:()=>h,frontMatter:()=>r,metadata:()=>l,toc:()=>c});var t=a(85893),s=a(11151);const r={id:"applications",title:"Pou\u017eit\xed \u010derveno-\u010dern\xfdch strom\u016f",description:"Uk\xe1zka pou\u017eit\xed \u010derveno-\u010dern\xfdch strom\u016f v standardn\xedch kni\u017enic\xedch zn\xe1m\xfdch jazyk\u016f.\n",tags:["balanced trees","red-black trees","applications"],last_update:{date:new Date("2022-04-05T00:00:00.000Z")}},i=void 0,l={id:"rb-trees/applications",title:"Pou\u017eit\xed \u010derveno-\u010dern\xfdch strom\u016f",description:"Uk\xe1zka pou\u017eit\xed \u010derveno-\u010dern\xfdch strom\u016f v standardn\xedch kni\u017enic\xedch zn\xe1m\xfdch jazyk\u016f.\n",source:"@site/algorithms/08-rb-trees/2022-04-05-applications.md",sourceDirName:"08-rb-trees",slug:"/rb-trees/applications",permalink:"/algorithms/rb-trees/applications",draft:!1,unlisted:!1,editUrl:"https://github.com/mfocko/blog/tree/main/algorithms/08-rb-trees/2022-04-05-applications.md",tags:[{label:"balanced trees",permalink:"/algorithms/tags/balanced-trees"},{label:"red-black trees",permalink:"/algorithms/tags/red-black-trees"},{label:"applications",permalink:"/algorithms/tags/applications"}],version:"current",lastUpdatedAt:1649116800,formattedLastUpdatedAt:"Apr 5, 2022",frontMatter:{id:"applications",title:"Pou\u017eit\xed \u010derveno-\u010dern\xfdch strom\u016f",description:"Uk\xe1zka pou\u017eit\xed \u010derveno-\u010dern\xfdch strom\u016f v standardn\xedch kni\u017enic\xedch zn\xe1m\xfdch jazyk\u016f.\n",tags:["balanced trees","red-black trees","applications"],last_update:{date:"2022-04-05T00:00:00.000Z"}},sidebar:"autogeneratedBar",previous:{title:"Red-Black Trees",permalink:"/algorithms/category/red-black-trees"},next:{title:"On the rules of the red-black tree",permalink:"/algorithms/rb-trees/rules"}},o={},c=[{value:"Pou\u017eit\xed",id:"pou\u017eit\xed",level:2},{value:"R\u016fzn\xe9 implementace",id:"r\u016fzn\xe9-implementace",level:2},{value:"C++",id:"c",level:3},{value:"clang",id:"clang",level:4},{value:"gcc",id:"gcc",level:4},{value:"Java",id:"java",level:3},{value:"C#",id:"c-1",level:3},{value:"Vztah mezi mno\u017einou a mapou",id:"vztah-mezi-mno\u017einou-a-mapou",level:2}];function d(e){const n={a:"a",annotation:"annotation",code:"code",em:"em",h2:"h2",h3:"h3",h4:"h4",li:"li",math:"math",mi:"mi",mo:"mo",mrow:"mrow",p:"p",pre:"pre",semantics:"semantics",span:"span",table:"table",tbody:"tbody",td:"td",th:"th",thead:"thead",tr:"tr",ul:"ul",...(0,s.a)(),...e.components};return(0,t.jsxs)(t.Fragment,{children:[(0,t.jsx)(n.h2,{id:"pou\u017eit\xed",children:"Pou\u017eit\xed"}),"\n",(0,t.jsx)(n.p,{children:"\u010cerveno-\u010dern\xe9 stromy jsou celkem obl\xedben\xe9 pro implementaci ADT mno\u017einy nebo slovn\xedku za p\u0159edpokladu, \u017ee nad vkl\xe1dan\xfdmi kl\xed\u010di existuje uspo\u0159\xe1d\xe1n\xed. Jazyky n\xed\u017ee implementuj\xed dan\xe9 datov\xe9 struktury v 2 variant\xe1ch a to:"}),"\n",(0,t.jsxs)(n.ul,{children:["\n",(0,t.jsx)(n.li,{children:"se\u0159azen\xe9: pou\u017e\xedvaj\xed na pozad\xed pr\xe1v\u011b \u010derveno-\u010dern\xfd strom"}),"\n",(0,t.jsx)(n.li,{children:"nese\u0159azen\xe9: pou\u017e\xedvaj\xed na pozad\xed ha\u0161ovac\xed tabulku"}),"\n"]}),"\n",(0,t.jsxs)(n.p,{children:["Pro srovn\xe1n\xed, jak jsme si \u0159\xedkali na cvi\u010den\xed, \u010derveno-\u010dern\xfd strom m\xe1 operace hled\xe1n\xed, vkl\xe1d\xe1n\xed a maz\xe1n\xed v \u010dasov\xe9 slo\u017eitosti ",(0,t.jsxs)(n.span,{className:"katex",children:[(0,t.jsx)(n.span,{className:"katex-mathml",children:(0,t.jsx)(n.math,{xmlns:"http://www.w3.org/1998/Math/MathML",children:(0,t.jsxs)(n.semantics,{children:[(0,t.jsxs)(n.mrow,{children:[(0,t.jsx)(n.mi,{mathvariant:"script",children:"O"}),(0,t.jsx)(n.mo,{stretchy:"false",children:"("}),(0,t.jsx)(n.mi,{children:"log"}),(0,t.jsx)(n.mo,{children:"\u2061"}),(0,t.jsx)(n.mi,{children:"n"}),(0,t.jsx)(n.mo,{stretchy:"false",children:")"})]}),(0,t.jsx)(n.annotation,{encoding:"application/x-tex",children:"\\mathcal{O}(\\log n)"})]})})}),(0,t.jsx)(n.span,{className:"katex-html","aria-hidden":"true",children:(0,t.jsxs)(n.span,{className:"base",children:[(0,t.jsx)(n.span,{className:"strut",style:{height:"1em",verticalAlign:"-0.25em"}}),(0,t.jsx)(n.span,{className:"mord mathcal",style:{marginRight:"0.02778em"},children:"O"}),(0,t.jsx)(n.span,{className:"mopen",children:"("}),(0,t.jsxs)(n.span,{className:"mop",children:["lo",(0,t.jsx)(n.span,{style:{marginRight:"0.01389em"},children:"g"})]}),(0,t.jsx)(n.span,{className:"mspace",style:{marginRight:"0.1667em"}}),(0,t.jsx)(n.span,{className:"mord mathnormal",children:"n"}),(0,t.jsx)(n.span,{className:"mclose",children:")"})]})})]}),". Na druhou stranu ha\u0161ovac\xed tabulka m\xe1 ide\xe1ln\u011b konstantn\xed \u010dasovou slo\u017eitost, ale v nejhor\u0161\xedm p\u0159\xedpad\u011b (detaily na posledn\xedm cvi\u010den\xed v semestru) je to bohu\u017eel ",(0,t.jsxs)(n.span,{className:"katex",children:[(0,t.jsx)(n.span,{className:"katex-mathml",children:(0,t.jsx)(n.math,{xmlns:"http://www.w3.org/1998/Math/MathML",children:(0,t.jsxs)(n.semantics,{children:[(0,t.jsxs)(n.mrow,{children:[(0,t.jsx)(n.mi,{mathvariant:"script",children:"O"}),(0,t.jsx)(n.mo,{stretchy:"false",children:"("}),(0,t.jsx)(n.mi,{children:"n"}),(0,t.jsx)(n.mo,{stretchy:"false",children:")"})]}),(0,t.jsx)(n.annotation,{encoding:"application/x-tex",children:"\\mathcal{O}(n)"})]})})}),(0,t.jsx)(n.span,{className:"katex-html","aria-hidden":"true",children:(0,t.jsxs)(n.span,{className:"base",children:[(0,t.jsx)(n.span,{className:"strut",style:{height:"1em",verticalAlign:"-0.25em"}}),(0,t.jsx)(n.span,{className:"mord mathcal",style:{marginRight:"0.02778em"},children:"O"}),(0,t.jsx)(n.span,{className:"mopen",children:"("}),(0,t.jsx)(n.span,{className:"mord mathnormal",children:"n"}),(0,t.jsx)(n.span,{className:"mclose",children:")"})]})})]}),"."]}),"\n",(0,t.jsx)(n.p,{children:"V\xfd\u0161e jsme si uk\xe1zali n\u011bjak\xe9 p\u0159edpoklady nutn\xe9 pro ha\u0161ovac\xed tabulku i \u010derveno-\u010dern\xfd strom. Co je tedy lep\u0161\xed?"}),"\n",(0,t.jsxs)(n.ul,{children:["\n",(0,t.jsxs)(n.li,{children:["\u010derveno-\u010dern\xfd strom n\xe1m poskytuje ",(0,t.jsx)(n.em,{children:"stabiln\xed \u010dasovou slo\u017eitost"}),", ale za cenu po\u017eadavku ",(0,t.jsx)(n.em,{children:"uspo\u0159\xe1d\xe1n\xed"})," nad prvky"]}),"\n",(0,t.jsxs)(n.li,{children:["ha\u0161ovac\xed tabulka n\xe1m poskytuje ",(0,t.jsx)(n.em,{children:"pomyslnou perfektn\xed \u010dasovou slo\u017eitost"})]}),"\n"]}),"\n",(0,t.jsx)(n.h2,{id:"r\u016fzn\xe9-implementace",children:"R\u016fzn\xe9 implementace"}),"\n",(0,t.jsx)(n.p,{children:"Pro uk\xe1zku pou\u017eit\xed \u010derveno-\u010dern\xfdch strom\u016f v implementac\xedch standardn\xedch knihoven\njsme vybrali n\u011bkolik jazyk\u016f."}),"\n",(0,t.jsxs)(n.p,{children:["Pokud V\xe1s zaj\xedmaj\xed r\u016fzn\xe9 implementace, tak bychom doporu\u010dili \u201eprohrab\xe1vat\u201c se p\u0159es n\u011b v n\xe1sleduj\xedc\xedm po\u0159ad\xed: ",(0,t.jsx)(n.code,{children:"C# \u2192 Java \u2192 C++"}),". D\u016fvod pro zvolen\xe9 po\u0159ad\xed vych\xe1z\xed z toho, \u017ee C# implementace je pom\u011brn\u011b \u010diteln\xe1 a obsahuje mno\u017estv\xed vysv\u011btluj\xedc\xedch koment\xe1\u0159\u016f. Implementace v Jav\u011b je stejn\u011b \u010diteln\xe1, a\u010dkoli ji\u017e s minimem koment\xe1\u0159\u016f, kter\xe9 se maxim\xe1ln\u011b odkazuj\xed na CLRS. C++ implementace je \u201ezna\u010dn\u011b pozna\u010den\xe1\u201c podtr\u017e\xedtky ;)"]}),"\n",(0,t.jsx)(n.h3,{id:"c",children:"C++"}),"\n",(0,t.jsxs)(n.p,{children:["V C++ si m\u016f\u017eeme vybrat mezi 2 r\u016fzn\xfdmi implementacemi (",(0,t.jsx)(n.code,{children:"clang"})," nebo ",(0,t.jsx)(n.code,{children:"gcc"}),")."]}),"\n",(0,t.jsx)(n.h4,{id:"clang",children:"clang"}),"\n",(0,t.jsx)(n.p,{children:"Hlavi\u010dkov\xe9 soubory, kter\xe9 pou\u017e\xedv\xe1me p\u0159i pr\xe1ci s mno\u017einou nebo slovn\xedkem (zaj\xedmav\xe9 sekce jsou vyta\u017eeny):"}),"\n",(0,t.jsxs)(n.ul,{children:["\n",(0,t.jsxs)(n.li,{children:["\n",(0,t.jsx)(n.p,{children:(0,t.jsx)(n.a,{href:"https://github.com/llvm/llvm-project/blob/main/libcxx/include/map",children:(0,t.jsx)(n.code,{children:"map"})})}),"\n",(0,t.jsx)(n.pre,{children:(0,t.jsx)(n.code,{className:"language-cpp",children:"template <class _Key, class _Tp, class _Compare = less<_Key>,\n class _Allocator = allocator<pair<const _Key, _Tp> > >\nclass _LIBCPP_TEMPLATE_VIS map\n{\npublic:\n // types:\n typedef _Key key_type;\n typedef _Tp mapped_type;\n typedef pair<const key_type, mapped_type> value_type;\n\n// \u2026\n\nprivate:\n typedef __tree<__value_type, __vc, __allocator_type> __base;\n"})}),"\n"]}),"\n",(0,t.jsxs)(n.li,{children:["\n",(0,t.jsx)(n.p,{children:(0,t.jsx)(n.a,{href:"https://github.com/llvm/llvm-project/blob/main/libcxx/include/set",children:(0,t.jsx)(n.code,{children:"set"})})}),"\n",(0,t.jsx)(n.pre,{children:(0,t.jsx)(n.code,{className:"language-cpp",children:"template <class _Key, class _Compare = less<_Key>,\n class _Allocator = allocator<_Key> >\nclass _LIBCPP_TEMPLATE_VIS set\n{\npublic:\n // types:\n typedef _Key key_type;\n typedef key_type value_type;\n\n// \u2026\n\nprivate:\n typedef __tree<value_type, value_compare, allocator_type> __base;\n"})}),"\n"]}),"\n"]}),"\n",(0,t.jsxs)(n.p,{children:["U obou hlavi\u010dek si m\u016f\u017eeme v\u0161imnout, \u017ee deklaruj\xed n\u011bjak\xfd soukrom\xfd typ ",(0,t.jsx)(n.code,{children:"__base"}),", kter\xfd je aliasem pro ",(0,t.jsx)(n.code,{children:"__tree"}),". Ten n\xe1s pak vede k hlavi\u010dce ",(0,t.jsx)(n.a,{href:"https://github.com/llvm/llvm-project/blob/main/libcxx/include/__tree",children:(0,t.jsx)(n.code,{children:"__tree"})}),"."]}),"\n",(0,t.jsx)(n.p,{children:"V\xfd\u0148atek:"}),"\n",(0,t.jsx)(n.pre,{children:(0,t.jsx)(n.code,{className:"language-cpp",children:"/*\n\n_NodePtr algorithms\n\nThe algorithms taking _NodePtr are red black tree algorithms. Those\nalgorithms taking a parameter named __root should assume that __root\npoints to a proper red black tree (unless otherwise specified).\n\n\u2026\n\n*/\n"})}),"\n",(0,t.jsx)(n.h4,{id:"gcc",children:"gcc"}),"\n",(0,t.jsxs)(n.p,{children:["Pro ",(0,t.jsx)(n.code,{children:"gcc"})," je postup t\xe9m\u011b\u0159 stejn\xfd. Pro zm\u011bnu v hlavi\u010dk\xe1ch ",(0,t.jsx)(n.code,{children:"map"})," a ",(0,t.jsx)(n.code,{children:"set"})," nenajdeme nic, deklarace jsou a\u017e v hlavi\u010dkov\xfdch souborech:"]}),"\n",(0,t.jsxs)(n.ul,{children:["\n",(0,t.jsx)(n.li,{children:(0,t.jsx)(n.a,{href:"https://gcc.gnu.org/git/?p=gcc.git;a=blob;f=libstdc%2B%2B-v3/include/bits/stl_map.h;h=9c2b0745673431b4b396ba27982170478838137e;hb=HEAD",children:(0,t.jsx)(n.code,{children:"bits/stl_map.h"})})}),"\n",(0,t.jsx)(n.li,{children:(0,t.jsx)(n.a,{href:"https://gcc.gnu.org/git/?p=gcc.git;a=blob;f=libstdc%2B%2B-v3/include/bits/stl_set.h;h=9c2b0745673431b4b396ba27982170478838137e;hb=HEAD",children:(0,t.jsx)(n.code,{children:"bits/stl_set.h"})})}),"\n"]}),"\n",(0,t.jsxs)(n.p,{children:["V obou se zase odkazuje na n\u011bjakou hlavi\u010dku ",(0,t.jsx)(n.a,{href:"https://gcc.gnu.org/git/?p=gcc.git;a=blob;f=libstdc%2B%2B-v3/include/bits/stl_tree.h;h=a4de61417652a288e361a55fcc8bb7a9838c58a5;hb=HEAD",children:(0,t.jsx)(n.code,{children:"bits/stl_tree.h"})}),", zase v\xfd\u0148atek:"]}),"\n",(0,t.jsx)(n.pre,{children:(0,t.jsx)(n.code,{className:"language-cpp",children:" // Red-black tree class, designed for use in implementing STL\n // associative containers (set, multiset, map, and multimap). The\n // insertion and deletion algorithms are based on those in Cormen,\n // Leiserson, and Rivest, Introduction to Algorithms (MIT Press,\n // 1990), except that\n //\n // (1) the header cell is maintained with links not only to the root\n // but also to the leftmost node of the tree, to enable constant\n // time begin(), and to the rightmost node of the tree, to enable\n // linear time performance when used with the generic set algorithms\n // (set_union, etc.)\n //\n // (2) when a node being deleted has two children its successor node\n // is relinked into its place, rather than copied, so that the only\n // iterators invalidated are those referring to the deleted node.\n\n enum _Rb_tree_color { _S_red = false, _S_black = true };\n\n struct _Rb_tree_node_base\n {\n typedef _Rb_tree_node_base* _Base_ptr;\n typedef const _Rb_tree_node_base* _Const_Base_ptr;\n\n _Rb_tree_color\t_M_color;\n _Base_ptr\t\t_M_parent;\n _Base_ptr\t\t_M_left;\n _Base_ptr\t\t_M_right;\n\n static _Base_ptr\n _S_minimum(_Base_ptr __x) _GLIBCXX_NOEXCEPT\n {\n while (__x->_M_left != 0) __x = __x->_M_left;\n return __x;\n }\n\n static _Const_Base_ptr\n _S_minimum(_Const_Base_ptr __x) _GLIBCXX_NOEXCEPT\n {\n while (__x->_M_left != 0) __x = __x->_M_left;\n return __x;\n }\n\n static _Base_ptr\n _S_maximum(_Base_ptr __x) _GLIBCXX_NOEXCEPT\n {\n while (__x->_M_right != 0) __x = __x->_M_right;\n return __x;\n }\n\n static _Const_Base_ptr\n _S_maximum(_Const_Base_ptr __x) _GLIBCXX_NOEXCEPT\n {\n while (__x->_M_right != 0) __x = __x->_M_right;\n return __x;\n }\n"})}),"\n",(0,t.jsxs)(n.p,{children:["Tady u\u017e taky vid\xedme n\u011bjak\xfd k\xf3d pro nalezen\xed minima/maxima ve strom\u011b. Mimo jin\xe9\nje\u0161t\u011b existuje ",(0,t.jsx)(n.a,{href:"https://gcc.gnu.org/git/?p=gcc.git;a=blob;f=libstdc%2B%2B-v3/src/c%2B%2B98/tree.cc;h=fd14991589a57c6aa847f57105a938cd2bf4df6f;hb=HEAD",children:(0,t.jsx)(n.code,{children:"tree.cc"})}),", kde je lze nal\xe9zt t\u0159eba funkci s n\xe1sleduj\xedc\xed hlavi\u010dkou:"]}),"\n",(0,t.jsx)(n.pre,{children:(0,t.jsx)(n.code,{className:"language-cpp",children:"void\n_Rb_tree_insert_and_rebalance(const bool __insert_left,\n _Rb_tree_node_base* __x,\n _Rb_tree_node_base* __p,\n _Rb_tree_node_base& __header) throw ();\n"})}),"\n",(0,t.jsx)(n.h3,{id:"java",children:"Java"}),"\n",(0,t.jsxs)(n.p,{children:["V Jav\u011b jsou pro n\xe1s kl\xed\u010dov\xe9 implementace ",(0,t.jsx)(n.a,{href:"https://github.com/openjdk/jdk/blob/master/src/java.base/share/classes/java/util/TreeSet.java",children:(0,t.jsx)(n.code,{children:"TreeSet"})})," a ",(0,t.jsx)(n.a,{href:"https://github.com/openjdk/jdk/blob/master/src/java.base/share/classes/java/util/TreeMap.java",children:(0,t.jsx)(n.code,{children:"TreeMap"})}),"."]}),"\n",(0,t.jsxs)(n.p,{children:["V implementaci ",(0,t.jsx)(n.code,{children:"TreeSet"})," si m\u016f\u017eete pov\u0161imnout:"]}),"\n",(0,t.jsx)(n.pre,{children:(0,t.jsx)(n.code,{className:"language-java",children:"public class TreeSet<E> extends AbstractSet<E>\n implements NavigableSet<E>, Cloneable, java.io.Serializable\n{\n /**\n * The backing map.\n */\n private transient NavigableMap<E,Object> m;\n\n // Dummy value to associate with an Object in the backing Map\n private static final Object PRESENT = new Object();\n"})}),"\n",(0,t.jsxs)(n.p,{children:[(0,t.jsx)(n.code,{children:"TreeSet"})," v Jav\u011b tedy pou\u017e\xedv\xe1 na pozad\xed ",(0,t.jsx)(n.code,{children:"TreeMap"})," (co\u017e je vid\u011bt ve v\xfdchoz\xedm konstruktoru, kde se vol\xe1 konstruktor p\u0159eb\xedraj\xedc\xed ",(0,t.jsx)(n.code,{children:"NavigableMap<E, Object>"}),", a je mu p\u0159ed\xe1no ",(0,t.jsx)(n.code,{children:"new TreeMap<>()"}),")."]}),"\n",(0,t.jsxs)(n.p,{children:["Co se t\xfd\u010de ",(0,t.jsx)(n.code,{children:"TreeMap"}),", tak hned ze za\u010d\xe1tku definice ",(0,t.jsx)(n.code,{children:"TreeMap"})," je vid\u011bt:"]}),"\n",(0,t.jsx)(n.pre,{children:(0,t.jsx)(n.code,{className:"language-java",children:'public class TreeMap<K,V>\n extends AbstractMap<K,V>\n implements NavigableMap<K,V>, Cloneable, java.io.Serializable\n{\n /**\n * The comparator used to maintain order in this tree map, or\n * null if it uses the natural ordering of its keys.\n *\n * @serial\n */\n @SuppressWarnings("serial") // Conditionally serializable\n private final Comparator<? super K> comparator;\n\n private transient Entry<K,V> root;\n'})}),"\n",(0,t.jsxs)(n.p,{children:["Tak\u017ee m\xe1me \u201en\u011bjak\xfd ko\u0159en\u201c typu ",(0,t.jsx)(n.code,{children:"Entry<K,V>"}),". Zkus\xedme si naj\xedt definici dan\xe9ho typu\u2026"]}),"\n",(0,t.jsx)(n.pre,{children:(0,t.jsx)(n.code,{className:"language-java",children:" // Red-black mechanics\n\n private static final boolean RED = false;\n private static final boolean BLACK = true;\n\n /**\n * Node in the Tree. Doubles as a means to pass key-value pairs back to\n * user (see Map.Entry).\n */\n\n static final class Entry<K,V> implements Map.Entry<K,V> {\n K key;\n V value;\n Entry<K,V> left;\n Entry<K,V> right;\n Entry<K,V> parent;\n boolean color = BLACK;\n"})}),"\n",(0,t.jsx)(n.p,{children:"A m\xe1me RB-tree."}),"\n",(0,t.jsx)(n.p,{children:"(Implementace vych\xe1z\xed z projektu OpenJDK.)"}),"\n",(0,t.jsx)(n.h3,{id:"c-1",children:"C#"}),"\n",(0,t.jsx)(n.p,{children:"V C# se zam\u011b\u0159\xedme na nejnov\u011bj\u0161\xed vyd\xe1n\xed (.NET), kter\xe9 je open-source a podporov\xe1no i na opera\u010dn\xedch syst\xe9mech zalo\u017een\xfdch na Linuxu."}),"\n",(0,t.jsxs)(n.p,{children:["Nejd\u0159\xedve se pod\xedv\xe1me na implementaci slovn\xedku (",(0,t.jsx)(n.a,{href:"https://github.com/dotnet/runtime/blob/main/src/libraries/System.Collections/src/System/Collections/Generic/SortedDictionary.cs",children:(0,t.jsx)(n.code,{children:"SortedDictionary"})}),")."]}),"\n",(0,t.jsx)(n.pre,{children:(0,t.jsx)(n.code,{className:"language-csharp",children:" public class SortedDictionary<TKey, TValue> : IDictionary<TKey, TValue>, IDictionary, IReadOnlyDictionary<TKey, TValue> where TKey : notnull\n {\n [NonSerialized]\n private KeyCollection? _keys;\n [NonSerialized]\n private ValueCollection? _values;\n\n private readonly TreeSet<KeyValuePair<TKey, TValue>> _set; // Do not rename (binary serialization)\n"})}),"\n",(0,t.jsxs)(n.p,{children:["Na prvn\xed pohled m\xe1me probl\xe9m, proto\u017ee ",(0,t.jsx)(n.code,{children:"TreeSet"})," nen\xed ",(0,t.jsx)(n.code,{children:"SortedSet"}),", kter\xfd by jsme \u010dekali. Kdy\u017e se p\u0159esuneme na konec souboru, tak zjist\xedme, \u017ee ",(0,t.jsx)(n.code,{children:"TreeSet"})," je jenom ",(0,t.jsx)(n.em,{children:"backward-compatible wrapper"})," pro ",(0,t.jsx)(n.code,{children:"SortedSet"}),"."]}),"\n",(0,t.jsxs)(n.p,{children:["P\u0159esuneme se k ",(0,t.jsx)(n.a,{href:"https://github.com/dotnet/runtime/blob/main/src/libraries/System.Collections/src/System/Collections/Generic/SortedSet.cs",children:(0,t.jsx)(n.code,{children:"SortedSet"})}),". A hned ze za\u010d\xe1tku vid\xedme:"]}),"\n",(0,t.jsx)(n.pre,{children:(0,t.jsx)(n.code,{className:"language-cs",children:' // A binary search tree is a red-black tree if it satisfies the following red-black properties:\n // 1. Every node is either red or black\n // 2. Every leaf (nil node) is black\n // 3. If a node is red, the both its children are black\n // 4. Every simple path from a node to a descendant leaf contains the same number of black nodes\n //\n // The basic idea of a red-black tree is to represent 2-3-4 trees as standard BSTs but to add one extra bit of information\n // per node to encode 3-nodes and 4-nodes.\n // 4-nodes will be represented as: B\n // R R\n //\n // 3 -node will be represented as: B or B\n // R B B R\n //\n // For a detailed description of the algorithm, take a look at "Algorithms" by Robert Sedgewick.\n\n internal enum NodeColor : byte\n {\n Black,\n Red\n }\n\n internal delegate bool TreeWalkPredicate<T>(SortedSet<T>.Node node);\n\n internal enum TreeRotation : byte\n {\n Left,\n LeftRight,\n Right,\n RightLeft\n }\n'})}),"\n",(0,t.jsx)(n.p,{children:"Vysv\u011btlen\xed v koment\xe1\u0159i trochu p\u0159edb\xedh\xe1 n\xe1pl\u0148 cvi\u010den\xed zam\u011b\u0159en\xe9ho na B-stromy ;)"}),"\n",(0,t.jsx)(n.h2,{id:"vztah-mezi-mno\u017einou-a-mapou",children:"Vztah mezi mno\u017einou a mapou"}),"\n",(0,t.jsx)(n.p,{children:"P\u0159i ka\u017ed\xe9 implementaci ve standardn\xed knihovn\u011b jsme si mohli v\u0161imnout, \u017ee strom implementuje v\u017edy jenom jeden typ:"}),"\n",(0,t.jsxs)(n.table,{children:[(0,t.jsx)(n.thead,{children:(0,t.jsxs)(n.tr,{children:[(0,t.jsx)(n.th,{children:"Jazyk"}),(0,t.jsx)(n.th,{children:"Zp\u016fsob implementace"})]})}),(0,t.jsxs)(n.tbody,{children:[(0,t.jsxs)(n.tr,{children:[(0,t.jsx)(n.td,{children:"C++"}),(0,t.jsx)(n.td,{children:"mapa ukl\xe1d\xe1 dvojice do mno\u017einy"})]}),(0,t.jsxs)(n.tr,{children:[(0,t.jsx)(n.td,{children:"Java"}),(0,t.jsx)(n.td,{children:"mno\u017eina ukl\xe1d\xe1 prvky s \u201edummy\u201c hodnotou do mapy"})]}),(0,t.jsxs)(n.tr,{children:[(0,t.jsx)(n.td,{children:"C#"}),(0,t.jsx)(n.td,{children:"mapa ukl\xe1d\xe1 dvojice do mno\u017einy"})]})]})]}),"\n",(0,t.jsxs)(n.p,{children:["Mapa vy\u017eaduje, aby ka\u017ed\xfd kl\xed\u010d m\u011bl p\u0159i\u0159azenou pr\xe1v\u011b jednu hodnotu, tedy kl\xed\u010de jsou navz\xe1jem mezi sebou unik\xe1tn\xed. To n\xe1m umo\u017e\u0148uje organizovat kl\xed\u010de do mno\u017einy, zde ale naraz\xedme na nep\u0159\xedjemn\xfd probl\xe9m spo\u010d\xedvaj\xedc\xed v tom, \u017ee mus\xedme do mno\u017einy vkladat dvojice prvk\u016f: ",(0,t.jsx)(n.code,{children:"(key, value)"}),". Tenhle p\u0159\xedstup m\xe1 ale z\xe1sadn\xed probl\xe9m:"]}),"\n",(0,t.jsx)(n.pre,{children:(0,t.jsx)(n.code,{className:"language-py",metastring:"showLineNumbers",children:"# let's represent dictionary/map as a set\nset_of_values = set()\n\n# let's insert few pairs\nset_of_values.add((1, 2))\nset_of_values.add((0, 42))\n\n# let's set key 1 to value 6\nset_of_values.add((1, 6))\n\nset_of_values\n"})}),"\n",(0,t.jsx)(n.p,{children:"A dostaneme:"}),"\n",(0,t.jsx)(n.pre,{children:(0,t.jsx)(n.code,{children:"{(1, 6), (1, 2), (0, 42)}\n"})}),"\n",(0,t.jsx)(n.p,{children:"V jednotliv\xfdch implementac\xedch, kter\xe9 jste mohli vid\u011bt v\xfd\u0161e, se vyu\u017e\xedv\xe1 nasleduj\xedc\xed, kdy\u017e:"}),"\n",(0,t.jsxs)(n.ul,{children:["\n",(0,t.jsxs)(n.li,{children:[(0,t.jsx)(n.em,{children:"mapa ukl\xe1d\xe1 dvojice do mno\u017einy"}),": Dvojice je obalen\xe1 v samostatn\xedm typu, kter\xfd porovn\xe1v\xe1 jenom kl\xed\u010de"]}),"\n",(0,t.jsxs)(n.li,{children:[(0,t.jsx)(n.em,{children:"mno\u017eina ukl\xe1d\xe1 kl\xed\u010de do mapy"}),": V map\u011b se ignoruj\xed hodnoty p\u0159i\u0159azen\xe9 kl\xed\u010d\u016fm"]}),"\n"]})]})}function h(e={}){const{wrapper:n}={...(0,s.a)(),...e.components};return n?(0,t.jsx)(n,{...e,children:(0,t.jsx)(d,{...e})}):d(e)}},11151:(e,n,a)=>{a.d(n,{Z:()=>l,a:()=>i});var t=a(67294);const s={},r=t.createContext(s);function i(e){const n=t.useContext(r);return t.useMemo((function(){return"function"==typeof e?e(n):{...n,...e}}),[n,e])}function l(e){let n;return n=e.disableParentContext?"function"==typeof e.components?e.components(s):e.components||s:i(e.components),t.createElement(r.Provider,{value:n},e.children)}}}]); |