第12章 高级数据结构及其实现
Posted tjj-love-world
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了第12章 高级数据结构及其实现相关的知识,希望对你有一定的参考价值。
自顶向下伸展树
1 // SplayTree class 2 // 3 // CONSTRUCTION: with no initializer 4 // 5 // ******************PUBLIC OPERATIONS********************* 6 // void insert( x ) --> Insert x 7 // void remove( x ) --> Remove x 8 // boolean contains( x ) --> Return true if x is found 9 // Comparable findMin( ) --> Return smallest item 10 // Comparable findMax( ) --> Return largest item 11 // boolean isEmpty( ) --> Return true if empty; else false 12 // void makeEmpty( ) --> Remove all items 13 // ******************ERRORS******************************** 14 // Throws UnderflowException as appropriate 15 16 /** 17 * Implements a top-down splay tree. 18 * Note that all "matching" is based on the compareTo method. 19 * @author Mark Allen Weiss 20 */ 21 public class SplayTree<AnyType extends Comparable<? super AnyType>> 22 { 23 /** 24 * Construct the tree. 25 */ 26 public SplayTree( ) 27 { 28 nullNode = new BinaryNode<AnyType>( null ); 29 nullNode.left = nullNode.right = nullNode; 30 root = nullNode; 31 } 32 33 private BinaryNode<AnyType> newNode = null; // Used between different inserts 34 35 /** 36 * Insert into the tree. 37 * @param x the item to insert. 38 */ 39 public void insert( AnyType x ) 40 { 41 if( newNode == null ) 42 newNode = new BinaryNode<AnyType>( null ); 43 newNode.element = x; 44 45 if( root == nullNode ) 46 { 47 newNode.left = newNode.right = nullNode; 48 root = newNode; 49 } 50 else 51 { 52 root = splay( x, root ); 53 54 int compareResult = x.compareTo( root.element ); 55 56 if( compareResult < 0 ) 57 { 58 newNode.left = root.left; 59 newNode.right = root; 60 root.left = nullNode; 61 root = newNode; 62 } 63 else 64 if( compareResult > 0 ) 65 { 66 newNode.right = root.right; 67 newNode.left = root; 68 root.right = nullNode; 69 root = newNode; 70 } 71 else 72 return; // No duplicates 73 } 74 newNode = null; // So next insert will call new 75 } 76 77 /** 78 * Remove from the tree. 79 * @param x the item to remove. 80 */ 81 public void remove( AnyType x ) 82 { 83 if( !contains( x ) ) 84 return; 85 86 BinaryNode<AnyType> newTree; 87 88 // If x is found, it will be splayed to the root by contains 89 if( root.left == nullNode ) 90 newTree = root.right; 91 else 92 { 93 // Find the maximum in the left subtree 94 // Splay it to the root; and then attach right child 95 newTree = root.left; 96 newTree = splay( x, newTree ); 97 newTree.right = root.right; 98 } 99 root = newTree; 100 } 101 102 /** 103 * Find the smallest item in the tree. 104 * Not the most efficient implementation (uses two passes), but has correct 105 * amortized behavior. 106 * A good alternative is to first call find with parameter 107 * smaller than any item in the tree, then call findMin. 108 * @return the smallest item or throw UnderflowException if empty. 109 */ 110 public AnyType findMin( ) 111 { 112 if( isEmpty( ) ) 113 throw new UnderflowException( ); 114 115 BinaryNode<AnyType> ptr = root; 116 117 while( ptr.left != nullNode ) 118 ptr = ptr.left; 119 120 root = splay( ptr.element, root ); 121 return ptr.element; 122 } 123 124 /** 125 * Find the largest item in the tree. 126 * Not the most efficient implementation (uses two passes), but has correct 127 * amortized behavior. 128 * A good alternative is to first call find with parameter 129 * larger than any item in the tree, then call findMax. 130 * @return the largest item or throw UnderflowException if empty. 131 */ 132 public AnyType findMax( ) 133 { 134 if( isEmpty( ) ) 135 throw new UnderflowException( ); 136 137 BinaryNode<AnyType> ptr = root; 138 139 while( ptr.right != nullNode ) 140 ptr = ptr.right; 141 142 root = splay( ptr.element, root ); 143 return ptr.element; 144 } 145 146 /** 147 * Find an item in the tree. 148 * @param x the item to search for. 149 * @return true if x is found; otherwise false. 150 */ 151 public boolean contains( AnyType x ) 152 { 153 if( isEmpty( ) ) 154 return false; 155 156 root = splay( x, root ); 157 158 return root.element.compareTo( x ) == 0; 159 } 160 161 /** 162 * Make the tree logically empty. 163 */ 164 public void makeEmpty( ) 165 { 166 root = nullNode; 167 } 168 169 /** 170 * Test if the tree is logically empty. 171 * @return true if empty, false otherwise. 172 */ 173 public boolean isEmpty( ) 174 { 175 return root == nullNode; 176 } 177 178 private BinaryNode<AnyType> header = new BinaryNode<AnyType>( null ); // For splay 179 180 /** 181 * Internal method to perform a top-down splay. 182 * The last accessed node becomes the new root. 183 * @param x the target item to splay around. 184 * @param t the root of the subtree to splay. 185 * @return the subtree after the splay. 186 */ 187 private BinaryNode<AnyType> splay( AnyType x, BinaryNode<AnyType> t ) 188 { 189 BinaryNode<AnyType> leftTreeMax, rightTreeMin; 190 191 header.left = header.right = nullNode; 192 leftTreeMax = rightTreeMin = header; 193 194 nullNode.element = x; // Guarantee a match 195 196 for( ; ; ) 197 { 198 int compareResult = x.compareTo( t.element ); 199 200 if( compareResult < 0 ) 201 { 202 if( x.compareTo( t.left.element ) < 0 ) 203 t = rotateWithLeftChild( t ); 204 if( t.left == nullNode ) 205 break; 206 // Link Right 207 rightTreeMin.left = t; 208 rightTreeMin = t; 209 t = t.left; 210 } 211 else if( compareResult > 0 ) 212 { 213 if( x.compareTo( t.right.element ) > 0 ) 214 t = rotateWithRightChild( t ); 215 if( t.right == nullNode ) 216 break; 217 // Link Left 218 leftTreeMax.right = t; 219 leftTreeMax = t; 220 t = t.right; 221 } 222 else 223 break; 224 } 225 226 leftTreeMax.right = t.left; 227 rightTreeMin.left = t.right; 228 t.left = header.right; 229 t.right = header.left; 230 return t; 231 } 232 233 /** 234 * Rotate binary tree node with left child. 235 * For AVL trees, this is a single rotation for case 1. 236 */ 237 private static <AnyType> BinaryNode<AnyType> rotateWithLeftChild( BinaryNode<AnyType> k2 ) 238 { 239 BinaryNode<AnyType> k1 = k2.left; 240 k2.left = k1.right; 241 k1.right = k2; 242 return k1; 243 } 244 245 /** 246 * Rotate binary tree node with right child. 247 * For AVL trees, this is a single rotation for case 4. 248 */ 249 private static <AnyType> BinaryNode<AnyType> rotateWithRightChild( BinaryNode<AnyType> k1 ) 250 { 251 BinaryNode<AnyType> k2 = k1.right; 252 k1.right = k2.left; 253 k2.left = k1; 254 return k2; 255 } 256 257 // Basic node stored in unbalanced binary search trees 258 private static class BinaryNode<AnyType> 259 { 260 // Constructors 261 BinaryNode( AnyType theElement ) 262 { 263 this( theElement, null, null ); 264 } 265 266 BinaryNode( AnyType theElement, BinaryNode<AnyType> lt, BinaryNode<AnyType> rt ) 267 { 268 element = theElement; 269 left = lt; 270 right = rt; 271 } 272 273 AnyType element; // The data in the node 274 BinaryNode<AnyType> left; // Left child 275 BinaryNode<AnyType> right; // Right child 276 } 277 278 private BinaryNode<AnyType> root; 279 private BinaryNode<AnyType> nullNode; 280 281 282 // Test program; should print min and max and nothing else 283 public static void main( String [ ] args ) 284 { 285 SplayTree<Integer> t = new SplayTree<Integer>( ); 286 final int NUMS = 40000; 287 final int GAP = 307; 288 289 System.out.println( "Checking... (no bad output means success)" ); 290 291 for( int i = GAP; i != 0; i = ( i + GAP ) % NUMS ) 292 t.insert( i ); 293 System.out.println( "Inserts complete" ); 294 295 for( int i = 1; i < NUMS; i += 2 ) 296 t.remove( i ); 297 System.out.println( "Removes complete" ); 298 299 if( t.findMin( ) != 2 || t.findMax( ) != NUMS - 2 ) 300 System.out.println( "FindMin or FindMax error!" ); 301 302 for( int i = 2; i < NUMS; i += 2 ) 303 if( !t.contains( i ) ) 304 System.out.println( "Error: find fails for " + i ); 305 306 for( int i = 1; i < NUMS; i += 2 ) 307 if( t.contains( i ) ) 308 System.out.println( "Error: Found deleted item " + i ); 309 } 310 }
以上是关于第12章 高级数据结构及其实现的主要内容,如果未能解决你的问题,请参考以下文章
《Javascript高级程序设计(第四版)》学习笔记第12章