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<K,V> <font color=#8000a0><font color=#ff8000>extends</font> </font>LinkedBinaryTree<Entry<K,V>> <font color=#8000a0><font color=#ff8000>implements</font> </font>Dictionary<K,V> { <font color=#8000a0><font color=#8000a0>protected</font> </font>Comparator<K> C; <font color=#ff0080>// comparator</font> <font color=#8000a0><font color=#8000a0>protected</font> </font>Position<Entry<K,V>> 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<K><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<K> 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<K,V> <font color=#8000a0><font color=#ff8000>implements</font> </font>Entry<K,V> { <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<Entry<K,V>> 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<Entry<K,V>> 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<Entry<K,V>> <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<Entry<K,V>> 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<Entry<K,V>> 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<K,V> <font color=#0000ff>entry</font>(Position<Entry<K,V>> 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 <Entry<K,V>> pos, Entry<K,V> ent) {<font color=#0000ff> </font>(<font color=#0000ff></font>(BSTEntry<K,V>) ent).pos = pos; <font color=#0000ff>replace</font>(pos, ent); }</dl></body></html>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?