github.com/wangyougui/gf/v2@v2.6.5/container/gtree/gtree_z_example_avltree_test.go (about) 1 // Copyright GoFrame Author(https://goframe.org). All Rights Reserved. 2 // 3 // This Source Code Form is subject to the terms of the MIT License. 4 // If a copy of the MIT was not distributed with gm file, 5 // You can obtain one at https://github.com/wangyougui/gf. 6 7 package gtree_test 8 9 import ( 10 "fmt" 11 12 "github.com/wangyougui/gf/v2/container/gtree" 13 "github.com/wangyougui/gf/v2/internal/json" 14 "github.com/wangyougui/gf/v2/util/gconv" 15 "github.com/wangyougui/gf/v2/util/gutil" 16 ) 17 18 func ExampleAVLTree_Clone() { 19 avl := gtree.NewAVLTree(gutil.ComparatorString) 20 for i := 0; i < 6; i++ { 21 avl.Set("key"+gconv.String(i), "val"+gconv.String(i)) 22 } 23 24 tree := avl.Clone() 25 26 fmt.Println(tree.Map()) 27 fmt.Println(tree.Size()) 28 29 // Output: 30 // map[key0:val0 key1:val1 key2:val2 key3:val3 key4:val4 key5:val5] 31 // 6 32 } 33 34 func ExampleAVLTree_Set() { 35 tree := gtree.NewAVLTree(gutil.ComparatorString) 36 for i := 0; i < 6; i++ { 37 tree.Set("key"+gconv.String(i), "val"+gconv.String(i)) 38 } 39 40 fmt.Println(tree.Map()) 41 fmt.Println(tree.Size()) 42 43 // Output: 44 // map[key0:val0 key1:val1 key2:val2 key3:val3 key4:val4 key5:val5] 45 // 6 46 } 47 48 func ExampleAVLTree_Sets() { 49 tree := gtree.NewAVLTree(gutil.ComparatorString) 50 51 tree.Sets(map[interface{}]interface{}{ 52 "key1": "val1", 53 "key2": "val2", 54 }) 55 56 fmt.Println(tree.Map()) 57 fmt.Println(tree.Size()) 58 59 // Output: 60 // map[key1:val1 key2:val2] 61 // 2 62 } 63 64 func ExampleAVLTree_Get() { 65 tree := gtree.NewAVLTree(gutil.ComparatorString) 66 for i := 0; i < 6; i++ { 67 tree.Set("key"+gconv.String(i), "val"+gconv.String(i)) 68 } 69 70 fmt.Println(tree.Get("key1")) 71 fmt.Println(tree.Get("key10")) 72 73 // Output: 74 // val1 75 // <nil> 76 } 77 78 func ExampleAVLTree_GetOrSet() { 79 tree := gtree.NewAVLTree(gutil.ComparatorString) 80 for i := 0; i < 6; i++ { 81 tree.Set("key"+gconv.String(i), "val"+gconv.String(i)) 82 } 83 84 fmt.Println(tree.GetOrSet("key1", "newVal1")) 85 fmt.Println(tree.GetOrSet("key6", "val6")) 86 87 // Output: 88 // val1 89 // val6 90 } 91 92 func ExampleAVLTree_GetOrSetFunc() { 93 tree := gtree.NewAVLTree(gutil.ComparatorString) 94 for i := 0; i < 6; i++ { 95 tree.Set("key"+gconv.String(i), "val"+gconv.String(i)) 96 } 97 98 fmt.Println(tree.GetOrSetFunc("key1", func() interface{} { 99 return "newVal1" 100 })) 101 fmt.Println(tree.GetOrSetFunc("key6", func() interface{} { 102 return "val6" 103 })) 104 105 // Output: 106 // val1 107 // val6 108 } 109 110 func ExampleAVLTree_GetOrSetFuncLock() { 111 tree := gtree.NewAVLTree(gutil.ComparatorString) 112 for i := 0; i < 6; i++ { 113 tree.Set("key"+gconv.String(i), "val"+gconv.String(i)) 114 } 115 116 fmt.Println(tree.GetOrSetFuncLock("key1", func() interface{} { 117 return "newVal1" 118 })) 119 fmt.Println(tree.GetOrSetFuncLock("key6", func() interface{} { 120 return "val6" 121 })) 122 123 // Output: 124 // val1 125 // val6 126 } 127 128 func ExampleAVLTree_GetVar() { 129 tree := gtree.NewAVLTree(gutil.ComparatorString) 130 for i := 0; i < 6; i++ { 131 tree.Set("key"+gconv.String(i), "val"+gconv.String(i)) 132 } 133 134 fmt.Println(tree.GetVar("key1").String()) 135 136 // Output: 137 // val1 138 } 139 140 func ExampleAVLTree_GetVarOrSet() { 141 tree := gtree.NewAVLTree(gutil.ComparatorString) 142 for i := 0; i < 6; i++ { 143 tree.Set("key"+gconv.String(i), "val"+gconv.String(i)) 144 } 145 146 fmt.Println(tree.GetVarOrSet("key1", "newVal1")) 147 fmt.Println(tree.GetVarOrSet("key6", "val6")) 148 149 // Output: 150 // val1 151 // val6 152 } 153 154 func ExampleAVLTree_GetVarOrSetFunc() { 155 tree := gtree.NewAVLTree(gutil.ComparatorString) 156 for i := 0; i < 6; i++ { 157 tree.Set("key"+gconv.String(i), "val"+gconv.String(i)) 158 } 159 160 fmt.Println(tree.GetVarOrSetFunc("key1", func() interface{} { 161 return "newVal1" 162 })) 163 fmt.Println(tree.GetVarOrSetFunc("key6", func() interface{} { 164 return "val6" 165 })) 166 167 // Output: 168 // val1 169 // val6 170 } 171 172 func ExampleAVLTree_GetVarOrSetFuncLock() { 173 tree := gtree.NewAVLTree(gutil.ComparatorString) 174 for i := 0; i < 6; i++ { 175 tree.Set("key"+gconv.String(i), "val"+gconv.String(i)) 176 } 177 178 fmt.Println(tree.GetVarOrSetFuncLock("key1", func() interface{} { 179 return "newVal1" 180 })) 181 fmt.Println(tree.GetVarOrSetFuncLock("key6", func() interface{} { 182 return "val6" 183 })) 184 185 // Output: 186 // val1 187 // val6 188 } 189 190 func ExampleAVLTree_SetIfNotExist() { 191 tree := gtree.NewAVLTree(gutil.ComparatorString) 192 for i := 0; i < 6; i++ { 193 tree.Set("key"+gconv.String(i), "val"+gconv.String(i)) 194 } 195 196 fmt.Println(tree.SetIfNotExist("key1", "newVal1")) 197 fmt.Println(tree.SetIfNotExist("key6", "val6")) 198 199 // Output: 200 // false 201 // true 202 } 203 204 func ExampleAVLTree_SetIfNotExistFunc() { 205 tree := gtree.NewAVLTree(gutil.ComparatorString) 206 for i := 0; i < 6; i++ { 207 tree.Set("key"+gconv.String(i), "val"+gconv.String(i)) 208 } 209 210 fmt.Println(tree.SetIfNotExistFunc("key1", func() interface{} { 211 return "newVal1" 212 })) 213 fmt.Println(tree.SetIfNotExistFunc("key6", func() interface{} { 214 return "val6" 215 })) 216 217 // Output: 218 // false 219 // true 220 } 221 222 func ExampleAVLTree_SetIfNotExistFuncLock() { 223 tree := gtree.NewAVLTree(gutil.ComparatorString) 224 for i := 0; i < 6; i++ { 225 tree.Set("key"+gconv.String(i), "val"+gconv.String(i)) 226 } 227 228 fmt.Println(tree.SetIfNotExistFuncLock("key1", func() interface{} { 229 return "newVal1" 230 })) 231 fmt.Println(tree.SetIfNotExistFuncLock("key6", func() interface{} { 232 return "val6" 233 })) 234 235 // Output: 236 // false 237 // true 238 } 239 240 func ExampleAVLTree_Contains() { 241 tree := gtree.NewAVLTree(gutil.ComparatorString) 242 for i := 0; i < 6; i++ { 243 tree.Set("key"+gconv.String(i), "val"+gconv.String(i)) 244 } 245 246 fmt.Println(tree.Contains("key1")) 247 fmt.Println(tree.Contains("key6")) 248 249 // Output: 250 // true 251 // false 252 } 253 254 func ExampleAVLTree_Remove() { 255 tree := gtree.NewAVLTree(gutil.ComparatorString) 256 for i := 0; i < 6; i++ { 257 tree.Set("key"+gconv.String(i), "val"+gconv.String(i)) 258 } 259 260 fmt.Println(tree.Remove("key1")) 261 fmt.Println(tree.Remove("key6")) 262 fmt.Println(tree.Map()) 263 264 // Output: 265 // val1 266 // <nil> 267 // map[key0:val0 key2:val2 key3:val3 key4:val4 key5:val5] 268 } 269 270 func ExampleAVLTree_Removes() { 271 tree := gtree.NewAVLTree(gutil.ComparatorString) 272 for i := 0; i < 6; i++ { 273 tree.Set("key"+gconv.String(i), "val"+gconv.String(i)) 274 } 275 276 removeKeys := make([]interface{}, 2) 277 removeKeys = append(removeKeys, "key1") 278 removeKeys = append(removeKeys, "key6") 279 280 tree.Removes(removeKeys) 281 282 fmt.Println(tree.Map()) 283 284 // Output: 285 // map[key0:val0 key2:val2 key3:val3 key4:val4 key5:val5] 286 } 287 288 func ExampleAVLTree_IsEmpty() { 289 tree := gtree.NewAVLTree(gutil.ComparatorString) 290 291 fmt.Println(tree.IsEmpty()) 292 293 for i := 0; i < 6; i++ { 294 tree.Set("key"+gconv.String(i), "val"+gconv.String(i)) 295 } 296 297 fmt.Println(tree.IsEmpty()) 298 299 // Output: 300 // true 301 // false 302 } 303 304 func ExampleAVLTree_Size() { 305 tree := gtree.NewAVLTree(gutil.ComparatorString) 306 307 fmt.Println(tree.Size()) 308 309 for i := 0; i < 6; i++ { 310 tree.Set("key"+gconv.String(i), "val"+gconv.String(i)) 311 } 312 313 fmt.Println(tree.Size()) 314 315 // Output: 316 // 0 317 // 6 318 } 319 320 func ExampleAVLTree_Keys() { 321 tree := gtree.NewAVLTree(gutil.ComparatorString) 322 for i := 6; i > 0; i-- { 323 tree.Set("key"+gconv.String(i), "val"+gconv.String(i)) 324 } 325 326 fmt.Println(tree.Keys()) 327 328 // Output: 329 // [key1 key2 key3 key4 key5 key6] 330 } 331 332 func ExampleAVLTree_Values() { 333 tree := gtree.NewAVLTree(gutil.ComparatorString) 334 for i := 6; i > 0; i-- { 335 tree.Set("key"+gconv.String(i), "val"+gconv.String(i)) 336 } 337 338 fmt.Println(tree.Values()) 339 340 // Output: 341 // [val1 val2 val3 val4 val5 val6] 342 } 343 344 func ExampleAVLTree_Map() { 345 tree := gtree.NewAVLTree(gutil.ComparatorString) 346 for i := 0; i < 6; i++ { 347 tree.Set("key"+gconv.String(i), "val"+gconv.String(i)) 348 } 349 350 fmt.Println(tree.Map()) 351 352 // Output: 353 // map[key0:val0 key1:val1 key2:val2 key3:val3 key4:val4 key5:val5] 354 } 355 356 func ExampleAVLTree_MapStrAny() { 357 tree := gtree.NewAVLTree(gutil.ComparatorString) 358 for i := 0; i < 6; i++ { 359 tree.Set(1000+i, "val"+gconv.String(i)) 360 } 361 362 fmt.Println(tree.MapStrAny()) 363 364 // Output: 365 // map[1000:val0 1001:val1 1002:val2 1003:val3 1004:val4 1005:val5] 366 } 367 368 func ExampleAVLTree_Flip() { 369 tree := gtree.NewAVLTree(gutil.ComparatorInt) 370 for i := 1; i < 6; i++ { 371 tree.Set(i, i*10) 372 } 373 374 fmt.Println("Before Flip", tree.Map()) 375 376 tree.Flip() 377 378 fmt.Println("After Flip", tree.Map()) 379 380 // Output: 381 // Before Flip map[1:10 2:20 3:30 4:40 5:50] 382 // After Flip map[10:1 20:2 30:3 40:4 50:5] 383 } 384 385 func ExampleAVLTree_Clear() { 386 tree := gtree.NewAVLTree(gutil.ComparatorString) 387 for i := 0; i < 6; i++ { 388 tree.Set(1000+i, "val"+gconv.String(i)) 389 } 390 fmt.Println(tree.Size()) 391 392 tree.Clear() 393 fmt.Println(tree.Size()) 394 395 // Output: 396 // 6 397 // 0 398 } 399 400 func ExampleAVLTree_Replace() { 401 tree := gtree.NewAVLTree(gutil.ComparatorString) 402 for i := 0; i < 6; i++ { 403 tree.Set("key"+gconv.String(i), "val"+gconv.String(i)) 404 } 405 406 fmt.Println(tree.Map()) 407 408 data := map[interface{}]interface{}{ 409 "newKey0": "newVal0", 410 "newKey1": "newVal1", 411 "newKey2": "newVal2", 412 } 413 414 tree.Replace(data) 415 416 fmt.Println(tree.Map()) 417 418 // Output: 419 // map[key0:val0 key1:val1 key2:val2 key3:val3 key4:val4 key5:val5] 420 // map[newKey0:newVal0 newKey1:newVal1 newKey2:newVal2] 421 } 422 423 func ExampleAVLTree_Left() { 424 tree := gtree.NewAVLTree(gutil.ComparatorInt) 425 for i := 1; i < 100; i++ { 426 tree.Set(i, i) 427 } 428 fmt.Println(tree.Left().Key, tree.Left().Value) 429 430 emptyTree := gtree.NewAVLTree(gutil.ComparatorInt) 431 fmt.Println(emptyTree.Left()) 432 433 // Output: 434 // 1 1 435 // <nil> 436 } 437 438 func ExampleAVLTree_Right() { 439 tree := gtree.NewAVLTree(gutil.ComparatorInt) 440 for i := 1; i < 100; i++ { 441 tree.Set(i, i) 442 } 443 fmt.Println(tree.Right().Key, tree.Right().Value) 444 445 emptyTree := gtree.NewAVLTree(gutil.ComparatorInt) 446 fmt.Println(emptyTree.Left()) 447 448 // Output: 449 // 99 99 450 // <nil> 451 } 452 453 func ExampleAVLTree_Floor() { 454 tree := gtree.NewAVLTree(gutil.ComparatorInt) 455 for i := 1; i < 100; i++ { 456 if i != 50 { 457 tree.Set(i, i) 458 } 459 } 460 461 node, found := tree.Floor(95) 462 if found { 463 fmt.Println("Floor 95:", node.Key) 464 } 465 466 node, found = tree.Floor(50) 467 if found { 468 fmt.Println("Floor 50:", node.Key) 469 } 470 471 node, found = tree.Floor(100) 472 if found { 473 fmt.Println("Floor 100:", node.Key) 474 } 475 476 node, found = tree.Floor(0) 477 if found { 478 fmt.Println("Floor 0:", node.Key) 479 } 480 481 // Output: 482 // Floor 95: 95 483 // Floor 50: 49 484 // Floor 100: 99 485 } 486 487 func ExampleAVLTree_Ceiling() { 488 tree := gtree.NewAVLTree(gutil.ComparatorInt) 489 for i := 1; i < 100; i++ { 490 if i != 50 { 491 tree.Set(i, i) 492 } 493 } 494 495 node, found := tree.Ceiling(1) 496 if found { 497 fmt.Println("Ceiling 1:", node.Key) 498 } 499 500 node, found = tree.Ceiling(50) 501 if found { 502 fmt.Println("Ceiling 50:", node.Key) 503 } 504 505 node, found = tree.Ceiling(100) 506 if found { 507 fmt.Println("Ceiling 100:", node.Key) 508 } 509 510 node, found = tree.Ceiling(-1) 511 if found { 512 fmt.Println("Ceiling -1:", node.Key) 513 } 514 515 // Output: 516 // Ceiling 1: 1 517 // Ceiling 50: 51 518 // Ceiling -1: 1 519 } 520 521 func ExampleAVLTree_String() { 522 tree := gtree.NewAVLTree(gutil.ComparatorString) 523 for i := 0; i < 6; i++ { 524 tree.Set("key"+gconv.String(i), "val"+gconv.String(i)) 525 } 526 527 fmt.Println(tree.String()) 528 529 // Output: 530 // │ ┌── key5 531 // │ ┌── key4 532 // └── key3 533 // │ ┌── key2 534 // └── key1 535 // └── key0 536 } 537 538 func ExampleAVLTree_Search() { 539 tree := gtree.NewAVLTree(gutil.ComparatorString) 540 for i := 0; i < 6; i++ { 541 tree.Set("key"+gconv.String(i), "val"+gconv.String(i)) 542 } 543 544 fmt.Println(tree.Search("key0")) 545 fmt.Println(tree.Search("key6")) 546 547 // Output: 548 // val0 true 549 // <nil> false 550 } 551 552 func ExampleAVLTree_Print() { 553 tree := gtree.NewAVLTree(gutil.ComparatorString) 554 for i := 0; i < 6; i++ { 555 tree.Set("key"+gconv.String(i), "val"+gconv.String(i)) 556 } 557 558 tree.Print() 559 560 // Output: 561 // │ ┌── key5 562 // │ ┌── key4 563 // └── key3 564 // │ ┌── key2 565 // └── key1 566 // └── key0 567 } 568 569 func ExampleAVLTree_Iterator() { 570 tree := gtree.NewAVLTree(gutil.ComparatorString) 571 for i := 0; i < 10; i++ { 572 tree.Set(i, 10-i) 573 } 574 575 var totalKey, totalValue int 576 tree.Iterator(func(key, value interface{}) bool { 577 totalKey += key.(int) 578 totalValue += value.(int) 579 580 return totalValue < 20 581 }) 582 583 fmt.Println("totalKey:", totalKey) 584 fmt.Println("totalValue:", totalValue) 585 586 // Output: 587 // totalKey: 3 588 // totalValue: 27 589 } 590 591 func ExampleAVLTree_IteratorFrom() { 592 m := make(map[interface{}]interface{}) 593 for i := 1; i <= 5; i++ { 594 m[i] = i * 10 595 } 596 tree := gtree.NewAVLTreeFrom(gutil.ComparatorInt, m) 597 598 tree.IteratorFrom(1, true, func(key, value interface{}) bool { 599 fmt.Println("key:", key, ", value:", value) 600 return true 601 }) 602 603 // Output: 604 // key: 1 , value: 10 605 // key: 2 , value: 20 606 // key: 3 , value: 30 607 // key: 4 , value: 40 608 // key: 5 , value: 50 609 } 610 611 func ExampleAVLTree_IteratorAsc() { 612 tree := gtree.NewAVLTree(gutil.ComparatorString) 613 for i := 0; i < 10; i++ { 614 tree.Set(i, 10-i) 615 } 616 617 tree.IteratorAsc(func(key, value interface{}) bool { 618 fmt.Println("key:", key, ", value:", value) 619 return true 620 }) 621 622 // Output: 623 // key: 0 , value: 10 624 // key: 1 , value: 9 625 // key: 2 , value: 8 626 // key: 3 , value: 7 627 // key: 4 , value: 6 628 // key: 5 , value: 5 629 // key: 6 , value: 4 630 // key: 7 , value: 3 631 // key: 8 , value: 2 632 // key: 9 , value: 1 633 } 634 635 func ExampleAVLTree_IteratorAscFrom_Normal() { 636 m := make(map[interface{}]interface{}) 637 for i := 1; i <= 5; i++ { 638 m[i] = i * 10 639 } 640 tree := gtree.NewAVLTreeFrom(gutil.ComparatorInt, m) 641 642 tree.IteratorAscFrom(1, true, func(key, value interface{}) bool { 643 fmt.Println("key:", key, ", value:", value) 644 return true 645 }) 646 647 // Output: 648 // key: 1 , value: 10 649 // key: 2 , value: 20 650 // key: 3 , value: 30 651 // key: 4 , value: 40 652 // key: 5 , value: 50 653 } 654 655 func ExampleAVLTree_IteratorAscFrom_NoExistKey() { 656 m := make(map[interface{}]interface{}) 657 for i := 1; i <= 5; i++ { 658 m[i] = i * 10 659 } 660 tree := gtree.NewAVLTreeFrom(gutil.ComparatorInt, m) 661 662 tree.IteratorAscFrom(0, true, func(key, value interface{}) bool { 663 fmt.Println("key:", key, ", value:", value) 664 return true 665 }) 666 667 // Output: 668 } 669 670 func ExampleAVLTree_IteratorAscFrom_NoExistKeyAndMatchFalse() { 671 m := make(map[interface{}]interface{}) 672 for i := 1; i <= 5; i++ { 673 m[i] = i * 10 674 } 675 tree := gtree.NewAVLTreeFrom(gutil.ComparatorInt, m) 676 677 tree.IteratorAscFrom(6, false, func(key, value interface{}) bool { 678 fmt.Println("key:", key, ", value:", value) 679 return true 680 }) 681 682 // Output: 683 } 684 685 func ExampleAVLTree_IteratorDesc() { 686 tree := gtree.NewAVLTree(gutil.ComparatorString) 687 for i := 0; i < 10; i++ { 688 tree.Set(i, 10-i) 689 } 690 691 tree.IteratorDesc(func(key, value interface{}) bool { 692 fmt.Println("key:", key, ", value:", value) 693 return true 694 }) 695 696 // Output: 697 // key: 9 , value: 1 698 // key: 8 , value: 2 699 // key: 7 , value: 3 700 // key: 6 , value: 4 701 // key: 5 , value: 5 702 // key: 4 , value: 6 703 // key: 3 , value: 7 704 // key: 2 , value: 8 705 // key: 1 , value: 9 706 // key: 0 , value: 10 707 } 708 709 func ExampleAVLTree_IteratorDescFrom() { 710 m := make(map[interface{}]interface{}) 711 for i := 1; i <= 5; i++ { 712 m[i] = i * 10 713 } 714 tree := gtree.NewAVLTreeFrom(gutil.ComparatorInt, m) 715 716 tree.IteratorDescFrom(5, true, func(key, value interface{}) bool { 717 fmt.Println("key:", key, ", value:", value) 718 return true 719 }) 720 721 // Output: 722 // key: 5 , value: 50 723 // key: 4 , value: 40 724 // key: 3 , value: 30 725 // key: 2 , value: 20 726 // key: 1 , value: 10 727 } 728 729 func ExampleAVLTree_MarshalJSON() { 730 tree := gtree.NewAVLTree(gutil.ComparatorString) 731 for i := 0; i < 6; i++ { 732 tree.Set("key"+gconv.String(i), "val"+gconv.String(i)) 733 } 734 735 bytes, err := json.Marshal(tree) 736 if err == nil { 737 fmt.Println(gconv.String(bytes)) 738 } 739 740 // Output: 741 // {"key0":"val0","key1":"val1","key2":"val2","key3":"val3","key4":"val4","key5":"val5"} 742 }