binarysearchtree-binarysearchtree.html

来自「经典的数据结构源代码(java 实现)」· HTML 代码 · 共 59 行

HTML
59
字号
<html><head><title>Code Fragment</title></head><body text=#000000><center></center><br><br><dl><dd><pre><font color=#ff0080>// Realization of a dictionary by means of a binary search tree</font><font color=#8000a0>public</font> <font color=#8000a0><font color=#ff8000>class</font> </font>BinarySearchTree&lt;K,V&gt;   <font color=#8000a0><font color=#ff8000>extends</font> </font>LinkedBinaryTree&lt;Entry&lt;K,V&gt;&gt; <font color=#8000a0><font color=#ff8000>implements</font> </font>Dictionary&lt;K,V&gt; {  <font color=#8000a0><font color=#8000a0>protected</font> </font>Comparator&lt;K&gt; C;	<font color=#ff0080>// comparator</font>  <font color=#8000a0><font color=#8000a0>protected</font> </font>Position&lt;Entry&lt;K,V&gt;&gt;               actionPos; <font color=#ff0080>// insert node or removed node's parent</font>  <font color=#8000a0><font color=#8000a0>protected</font> </font><font color=#8000a0>int</font> numEntries = 0;	<font color=#ff0080>// number of entries</font>  <font color = #ff0080>/** Creates a BinarySearchTree with a default comparator. */</font>  <font color=#8000a0><font color=#8000a0>public</font> </font><font color=#0000ff>BinarySearchTree</font>()  {     C = <font color=#8000a0><font color=#ff8000>new</font> </font>DefaultComparator&lt;K&gt;<font color=#0000ff></font>();     <font color=#0000ff>addRoot</font>(null);  }  <font color=#8000a0><font color=#8000a0>public</font> </font><font color=#0000ff>BinarySearchTree</font>(Comparator&lt;K&gt; c)  {     C = c;     <font color=#0000ff>addRoot</font>(null);  }  <font color = #ff0080>/** Nested class for location-aware binary search tree entries */</font>  <font color=#8000a0><font color=#8000a0>protected</font> </font><font color=#8000a0>static</font> <font color=#8000a0><font color=#ff8000>class</font> </font>BSTEntry&lt;K,V&gt; <font color=#8000a0><font color=#ff8000>implements</font> </font>Entry&lt;K,V&gt; {    <font color=#8000a0><font color=#8000a0>protected</font> </font>K key;    <font color=#8000a0><font color=#8000a0>protected</font> </font>V value;    <font color=#8000a0><font color=#8000a0>protected</font> </font>Position&lt;Entry&lt;K,V&gt;&gt; pos;    <font color=#0000ff>BSTEntry</font>() { <font color = #ff0080>/* default constructor */</font> }    <font color=#0000ff>BSTEntry</font>(K k, <font color=#8000a0>V </font>v, Position&lt;Entry&lt;K,V&gt;&gt; p) {       key = k; value = v; pos = p;    }    <font color=#8000a0><font color=#8000a0>public</font> </font>K <font color=#0000ff>getKey</font>() { <font color=#8000a0><font color=#ff8000>return</font> </font>key; }    <font color=#8000a0><font color=#8000a0>public</font> </font>V <font color=#0000ff>getValue</font>() { <font color=#8000a0><font color=#ff8000>return</font> </font>value; }    <font color=#8000a0><font color=#8000a0>public</font> </font>Position&lt;Entry&lt;K,V&gt;&gt; <font color=#0000ff>position</font>() { <font color=#8000a0><font color=#ff8000>return</font> </font>pos; }  }  <font color = #ff0080>/** Extracts the key of the entry at a given node of the tree. */</font>  <font color=#8000a0><font color=#8000a0>protected</font> </font>K <font color=#0000ff>key</font>(Position&lt;Entry&lt;K,V&gt;&gt; position)  {    <font color=#8000a0><font color=#ff8000>return</font> </font>position.<font color=#0000ff>element</font>().<font color=#0000ff>getKey</font>();  }  <font color = #ff0080>/** Extracts the value of the entry at a given node of the tree. */</font>    <font color=#8000a0><font color=#8000a0>protected</font> </font>V <font color=#0000ff>value</font>(Position&lt;Entry&lt;K,V&gt;&gt; position)  {     <font color=#8000a0><font color=#ff8000>return</font> </font>position.<font color=#0000ff>element</font>().<font color=#0000ff>getValue</font>();   }  <font color = #ff0080>/** Extracts the entry at a given node of the tree. */</font>    <font color=#8000a0><font color=#8000a0>protected</font> </font>Entry&lt;K,V&gt; <font color=#0000ff>entry</font>(Position&lt;Entry&lt;K,V&gt;&gt; position)  {     <font color=#8000a0><font color=#ff8000>return</font> </font>position.<font color=#0000ff>element</font>();  }  <font color = #ff0080>/** Replaces an entry with a new entry (and reset the entry's location) */</font>  <font color=#8000a0><font color=#8000a0>protected</font> </font><font color=#8000a0>void</font> <font color=#0000ff>replaceEntry</font>(Position &lt;Entry&lt;K,V&gt;&gt; pos, Entry&lt;K,V&gt; ent) {<font color=#0000ff>    </font>(<font color=#0000ff></font>(BSTEntry&lt;K,V&gt;) ent).pos = pos;    <font color=#0000ff>replace</font>(pos, ent);  }</dl></body></html>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?