1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008
//! This module contains an implementation of a classic binary search tree //! with a large set of methods, including view iterators. use std::fmt; use std::cmp::{ Ordering, PartialEq }; use std::iter::{ FromIterator, Extend }; use std::collections::VecDeque; /// In this crate, binary search trees store only one valuable value, which is also /// used as a key, so all elements must have the ```Ord``` trait implementation. /// /// # Examples /// /// ``` /// extern crate binary_search_tree; /// /// use binary_search_tree::BinarySearchTree; /// /// // Create a new binary search tree and fill it with numbers from 1 to 5: /// let mut tree: BinarySearchTree<i32> = BinarySearchTree::new(); /// for i in vec![3, 1, 2, 5, 4] { /// tree.insert(i); /// } /// /// // Get them in sorted order /// assert_eq!(tree.sorted_vec(), vec![&1, &2, &3, &4, &5]); /// /// // Let's extract the minimum and maximum. /// assert_eq!(tree.extract_min(), Some(1)); /// assert_eq!(tree.extract_max(), Some(5)); /// assert_eq!(tree.sorted_vec(), vec![&2, &3, &4]); /// /// // We can also extend the tree with elements from the iterator. /// tree.extend((0..6).map(|x| if x%2 == 0 { x } else { -x })); /// assert_eq!(tree.len(), 9); /// /// // If the elements must be unique, you should use insert_without_dup(). /// tree.insert_without_dup(0); /// assert_eq!(tree.len(), 9); /// /// // Delete the value 0 from the tree and make sure that the deletion actually occurred. /// tree.remove(&0); /// assert!(!tree.contains(&0)); /// /// // We can clear the tree of any remaining items. /// tree.clear(); /// /// // The tree should now be empty. /// assert!(tree.is_empty()); /// ``` #[derive(Debug)] pub struct BinarySearchTree<T: Ord> { root: Tree<T>, pub size: usize, } #[derive(Debug)] struct Node<T: Ord> { value: T, left: Tree<T>, right: Tree<T>, } #[derive(Debug)] struct Tree<T: Ord>(Option<Box<Node<T>>>); impl<T: Ord> PartialEq for BinarySearchTree<T> { fn eq(&self, other: &BinarySearchTree<T>) -> bool { self.sorted_vec() == other.sorted_vec() } } impl<T: Ord + fmt::Debug> fmt::Display for BinarySearchTree<T> { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "{:?}", self.sorted_vec()) } } impl<T: Ord> Extend<T> for BinarySearchTree<T> { /// Extend bst with elements from the iterator. /// /// Note: extend doesn't keep track of duplicates, but just uses the whole iterator. /// /// # Examples /// /// ``` /// use binary_search_tree::BinarySearchTree; /// use std::iter::Extend; /// /// let mut tree: BinarySearchTree<i32> = BinarySearchTree::new(); /// tree.extend(vec![7, 1, 0, 4, 5, 3].into_iter()); /// assert_eq!(tree.sorted_vec(), [&0, &1, &3, &4, &5, &7]); /// ``` fn extend<I: IntoIterator<Item=T>>(&mut self, iter: I) { iter.into_iter().for_each(move |elem| { self.insert(elem); }); } } impl<T: Ord> FromIterator<T> for BinarySearchTree<T> { /// Сreating a bst from an iterator. /// /// # Examples /// /// ``` /// use binary_search_tree::BinarySearchTree; /// use std::iter::FromIterator; /// /// let tree: BinarySearchTree<i32> = BinarySearchTree::from_iter( /// vec![7, 1, 0, 4, 5, 3].into_iter()); /// assert_eq!(tree.sorted_vec(), [&0, &1, &3, &4, &5, &7]); /// /// let tree: BinarySearchTree<i32> = vec![7, 1, 0, 4, 5, 3].into_iter().collect(); /// assert_eq!(tree.sorted_vec(), [&0, &1, &3, &4, &5, &7]); /// ``` fn from_iter<I: IntoIterator<Item=T>>(iter: I) -> Self { let mut tree = BinarySearchTree::new(); tree.extend(iter); tree } } impl<T: Ord + Clone> Clone for BinarySearchTree<T> { fn clone(&self) -> BinarySearchTree<T> { let mut new_tree = BinarySearchTree::new(); for elem in self.sorted_vec().iter() { new_tree.insert((*elem).clone()); } new_tree } } impl<T: Ord> BinarySearchTree<T> { /// Makes a new empty BST. /// /// Does not allocate anything on its own. /// /// # Examples /// /// ``` /// use binary_search_tree::BinarySearchTree; /// /// // New bst that will be contains i32 /// let mut tree: BinarySearchTree<i32> = BinarySearchTree::new(); /// ``` pub fn new() -> Self { BinarySearchTree { root: Tree(None), size: 0 } } /// Сhecking if the tree is empty. /// /// # Examples /// /// ``` /// use binary_search_tree::BinarySearchTree; /// /// let mut tree: BinarySearchTree<i32> = BinarySearchTree::new(); /// assert!(tree.is_empty()); /// /// tree.insert(1); /// assert!(!tree.is_empty()); /// ``` pub fn is_empty(&self) -> bool { self.size == 0 } /// Returns the number of elements in the tree. /// /// # Examples /// /// ``` /// use binary_search_tree::BinarySearchTree; /// /// let mut tree: BinarySearchTree<i32> = BinarySearchTree::new(); /// assert_eq!(tree.len(), 0); /// tree.insert(1); /// assert_eq!(tree.len(), 1); /// ``` pub fn len(&self) -> usize { self.size } /// Clears the binary search tree, removing all elements. /// /// # Examples /// ``` /// use binary_search_tree::BinarySearchTree; /// /// let mut tree: BinarySearchTree<i32> = BinarySearchTree::new(); /// tree.insert(1); /// tree.clear(); /// assert!(tree.is_empty()); /// ``` pub fn clear(&mut self) { *self = BinarySearchTree::new(); } /// Viewing the root element of the tree. /// /// # Examples /// /// ``` /// use binary_search_tree::BinarySearchTree; /// /// let mut tree: BinarySearchTree<i32> = BinarySearchTree::new(); /// assert!(tree.root().is_none()); // is empty /// /// tree.insert(1); tree.insert(0); tree.insert(2); /// /// // the first element inserted becomes the root /// assert_eq!(tree.root(), Some(&1)); /// ``` pub fn root(&self) -> Option<&T> { self.root.0.as_ref().map(|node| &node.value) } /// Inserting a new element. /// /// Returns true if an element with the same value already exists in the tree, false otherwise. /// /// # Examples /// /// ``` /// use binary_search_tree::BinarySearchTree; /// /// let mut tree: BinarySearchTree<i32> = BinarySearchTree::new(); /// /// assert!(!tree.insert(1)); /// assert!(!tree.insert(0)); /// assert!(!tree.insert(2)); /// assert!(tree.insert(1)); // element 1 is already in the tree /// /// assert_eq!(tree.size, 4); /// /// // Elements in sorted order (inorder traversal) /// assert_eq!(tree.sorted_vec(), vec![&0, &1, &1, &2]); /// ``` pub fn insert(&mut self, value: T) -> bool { self.size += 1; self.root.insert(value, true) } /// Inserting a new unique element. /// /// If an element with the same value is already in the tree, the insertion will not happen. /// Returns true if an element with the same value already exists in the tree, false otherwise. /// /// # Examples /// /// ``` /// use binary_search_tree::BinarySearchTree; /// /// let mut tree: BinarySearchTree<i32> = BinarySearchTree::new(); /// /// assert!(!tree.insert_without_dup(1)); /// assert!(!tree.insert_without_dup(0)); /// assert!(!tree.insert_without_dup(2)); /// assert!(tree.insert_without_dup(1)); // element 1 is already in the tree /// /// assert_eq!(tree.size, 3); /// /// // Elements in sorted order (inorder traversal) /// assert_eq!(tree.sorted_vec(), vec![&0, &1, &2]); /// ``` pub fn insert_without_dup(&mut self, value: T) -> bool { let res = self.root.insert(value, false); if !res { self.size += 1; } res } /// Checks whether the tree contains an element with the specified value. /// /// # Examples /// /// ``` /// use binary_search_tree::BinarySearchTree; /// /// let mut tree: BinarySearchTree<i32> = BinarySearchTree::new(); /// assert_eq!(tree.contains(&1), false); /// /// tree.insert(1); tree.insert(0); tree.insert(2); tree.insert(1); /// /// // The contains() method accepts a reference to a value /// assert!(tree.contains(&2)); /// assert!(tree.contains(&1)); /// assert!(!tree.contains(&999)); /// ``` pub fn contains(&self, target: &T) -> bool { self.root.contains(target) } /// The minimum element of the tree. /// /// Returns a reference to the minimum element. /// /// # Examples /// /// ``` /// use binary_search_tree::BinarySearchTree; /// /// let mut tree: BinarySearchTree<i32> = BinarySearchTree::new(); /// assert_eq!(tree.min(), None); /// /// tree.insert(1); tree.insert(0); tree.insert(2); tree.insert(1); /// assert_eq!(tree.min(), Some(&0)); pub fn min(&self) -> Option<&T> { self.root.min() } /// The maximum element of the tree. /// /// Returns a reference to the maximum element. /// /// # Examples /// /// ``` /// use binary_search_tree::BinarySearchTree; /// /// let mut tree: BinarySearchTree<i32> = BinarySearchTree::new(); /// assert_eq!(tree.max(), None); /// /// tree.insert(1); tree.insert(0); tree.insert(2); tree.insert(1); /// assert_eq!(tree.max(), Some(&2)); /// ``` pub fn max(&self) -> Option<&T> { self.root.max() } /// Inorder successor of the element with the specified value /// /// In Binary Search Tree, inorder successor of an input node can be defined as /// the node with the smallest value greater than the value of the input node. /// In another way, we can say that the successor of element x is the element /// immediately following it in sorted order. /// /// # Examples /// /// ``` /// use binary_search_tree::BinarySearchTree; /// /// let mut tree: BinarySearchTree<i32> = BinarySearchTree::new(); /// tree.insert(25); tree.insert(15); tree.insert(40); tree.insert(10); /// tree.insert(18); tree.insert(45); tree.insert(35); /// /// // We have a binary tree with the following structure: /// // 25 /// // 15 40 /// // 10 18 35 45 /// /// assert_eq!(tree.sorted_vec(), vec![&10, &15, &18, &25, &35, &40, &45]); /// /// // and successor of 25 will be element 35. /// assert_eq!(tree.successor(&25), Some(&35)); /// /// assert_eq!(tree.successor(&40), Some(&45)); /// assert!(tree.successor(&45).is_none()); // Element 45 has no successors /// /// // We can also find successors for missing values in the tree /// assert_eq!(tree.successor(&20), Some(&25)); // [..., &18, vv &20 vv, &25, ...] /// ``` pub fn successor(&self, val: &T) -> Option<&T> { self.root.successor(val) } /// Inorder predecessor of the element with the specified value /// /// In Binary Search Tree, inorder predecessor of an input node can be defined as /// the node with the greatest value smaller than the value of the input node. /// In another way, we can say that the predecessor of element x is the element /// immediately preceding it in sorted order. /// /// # Examples /// /// ``` /// use binary_search_tree::BinarySearchTree; /// /// let mut tree: BinarySearchTree<i32> = BinarySearchTree::new(); /// tree.insert(25); tree.insert(15); tree.insert(40); tree.insert(10); /// tree.insert(18); tree.insert(45); tree.insert(35); /// /// // We have a binary tree with the following structure: /// // 25 /// // 15 40 /// // 10 18 35 45 /// /// assert_eq!(tree.sorted_vec(), vec![&10, &15, &18, &25, &35, &40, &45]); /// /// // and predecessor of 25 will be element 35. /// assert_eq!(tree.predecessor(&25), Some(&18)); /// /// assert_eq!(tree.predecessor(&40), Some(&35)); /// assert!(tree.predecessor(&10).is_none()); // Element 10 has no predecessors /// /// // We can also find predecessors for missing values in the tree /// assert_eq!(tree.predecessor(&20), Some(&18)); // [..., &18, vv &20 vv, &25, ...] /// ``` pub fn predecessor(&self, val: &T) -> Option<&T> { self.root.predecessor(val) } /// Remove and return the minimum element. /// /// This operation is much more efficient than searching for the /// minimum and then deleting it, since only one pass is performed /// and there are no comparisons between elements at all. /// /// # Examples /// /// ``` /// use binary_search_tree::BinarySearchTree; /// /// let mut tree: BinarySearchTree<i32> = BinarySearchTree::new(); /// assert!(tree.extract_min().is_none()); /// /// tree.insert(25); tree.insert(15); tree.insert(40); tree.insert(10); /// /// assert_eq!(tree.extract_min(), Some(10)); /// assert_eq!(tree.extract_min(), Some(15)); /// /// assert_eq!(tree.sorted_vec(), vec![&25, &40]); /// ``` pub fn extract_min(&mut self) -> Option<T> { let res = self.root.extract_min(); if res.is_some() { self.size -= 1; } res } /// Remove and return the maximum element. /// /// This operation is much more efficient than searching for the /// maximum and then deleting it, since only one pass is performed /// and there are no comparisons between elements at all. /// /// # Examples /// /// ``` /// use binary_search_tree::BinarySearchTree; /// /// let mut tree: BinarySearchTree<i32> = BinarySearchTree::new(); /// assert!(tree.extract_max().is_none()); /// /// tree.insert(25); tree.insert(15); tree.insert(40); tree.insert(10); /// /// assert_eq!(tree.extract_max(), Some(40)); /// assert_eq!(tree.extract_max(), Some(25)); /// /// assert_eq!(tree.sorted_vec(), vec![&10, &15]); /// ``` pub fn extract_max(&mut self) -> Option<T> { let res = self.root.extract_max(); if res.is_some() { self.size -= 1; } res } /// Remove the first occurrence of an element with the target value. /// /// Returns true if deletion occurred and false if target is missing from the tree. /// /// # Examples /// /// ``` /// use binary_search_tree::BinarySearchTree; /// /// let mut tree: BinarySearchTree<i32> = BinarySearchTree::new(); /// tree.insert(25); tree.insert(15); tree.insert(40); tree.insert(10); /// tree.insert(18); tree.insert(45); tree.insert(35); tree.insert(18); /// /// assert!(tree.remove(&18)); /// assert_eq!(tree.sorted_vec(), vec![&10, &15, &18, &25, &35, &40, &45]); /// assert_eq!(tree.size, 7); /// /// tree.remove(&25); /// assert_eq!(tree.sorted_vec(), vec![&10, &15, &18, &35, &40, &45]); /// assert_eq!(tree.size, 6); /// /// // If you try to delete a value that is missing from the tree, nothing will change /// assert!(!tree.remove(&99)); /// assert_eq!(tree.sorted_vec(), vec![&10, &15, &18, &35, &40, &45]); /// assert_eq!(tree.size, 6); /// ``` pub fn remove(&mut self, target: &T) -> bool { let res = self.root.remove(target); if res { self.size -= 1; } res } /// Vector of references to elements in the tree in non-decreasing order. /// /// # Examples /// /// ``` /// use binary_search_tree::BinarySearchTree; /// /// let mut tree: BinarySearchTree<i32> = BinarySearchTree::new(); /// tree.insert(25); tree.insert(15); tree.insert(40); tree.insert(10); /// tree.insert(18); tree.insert(45); tree.insert(35); tree.insert(18); /// /// assert_eq!(tree.sorted_vec(), vec![&10, &15, &18, &18, &25, &35, &40, &45]); /// ``` pub fn sorted_vec(&self) -> Vec<&T> { self.root.sorted_vec() } /// Moving the tree to a sorted vector. /// /// # Examples /// /// ``` /// use binary_search_tree::BinarySearchTree; /// /// let tree: BinarySearchTree<i32> = BinarySearchTree::new(); /// /// let mut tree: BinarySearchTree<i32> = BinarySearchTree::new(); /// tree.insert(25); tree.insert(15); tree.insert(40); tree.insert(10); /// ///assert_eq!(tree.into_sorted_vec(), vec![10, 15, 25, 40]); /// ``` pub fn into_sorted_vec(self) -> Vec<T> { self.root.into_sorted_vec() } /// Inorder traverse iterator of binary search tree. /// /// # Examples /// /// ``` /// use binary_search_tree::BinarySearchTree; /// /// let tree: BinarySearchTree<i32> = vec![5, 7, 3, 4, 8, 6, 1].into_iter().collect(); /// // Now we have a tree that looks like this: /// // 5 /// // 3 7 /// // 1 4 6 8 /// /// // And we should get the following sequence of its elements: 1, 3, 4, 5, 6, 7, 8 /// assert_eq!(tree.inorder().collect::<Vec<&i32>>(), vec![&1, &3, &4, &5, &6, &7, &8]); /// ``` pub fn inorder(&self) -> InorderTraversal<T> { InorderTraversal { stack: Vec::new(), current: self.root.0.as_ref(), } } /// Preorder traverse iterator of binary search tree. /// /// # Examples /// /// ``` /// use binary_search_tree::BinarySearchTree; /// /// let tree: BinarySearchTree<i32> = vec![5, 7, 3, 4, 8, 6, 1].into_iter().collect(); /// // Now we have a tree that looks like this: /// // 5 /// // 3 7 /// // 1 4 6 8 /// /// // And we should get the following sequence of its elements: 5, 3, 1, 4, 7, 6, 8 /// assert_eq!(tree.preorder().collect::<Vec<&i32>>(), vec![&5, &3, &1, &4, &7, &6, &8]); /// ``` pub fn preorder(&self) -> PreorderTraversal<T> { PreorderTraversal { stack: vec![self.root.0.as_ref()], current: self.root.0.as_ref(), } } /// Postorder traverse iterator of binary search tree. /// /// # Examples /// /// ``` /// use binary_search_tree::BinarySearchTree; /// /// let tree: BinarySearchTree<i32> = vec![5, 7, 3, 4, 8, 6, 1].into_iter().collect(); /// // Now we have a tree that looks like this: /// // 5 /// // 3 7 /// // 1 4 6 8 /// /// // And we should get the following sequence of its elements: 1, 4, 3, 6, 8, 7, 5 /// assert_eq!(tree.postorder().collect::<Vec<&i32>>(), vec![&1, &4, &3, &6, &8, &7, &5]); /// ``` pub fn postorder(&self) -> PostorderTraversal<T> { PostorderTraversal { stack: Vec::new(), current: self.root.0.as_ref(), } } /// Level order binary tree traversal. /// /// # Examples /// /// ``` /// use binary_search_tree::BinarySearchTree; /// /// let tree: BinarySearchTree<i32> = vec![5, 7, 3, 4, 8, 6, 1].into_iter().collect(); /// // Now we have a tree that looks like this: /// // 5 /// // 3 7 /// // 1 4 6 8 /// /// // And we should get the following sequence of its elements: 5, 3, 7, 1, 4, 6, 8 /// assert_eq!(tree.level_order().collect::<Vec<&i32>>(), vec![&5, &3, &7, &1, &4, &6, &8]); /// ``` pub fn level_order(&self) -> LevelOrderTraversal<T> { let mut deque = VecDeque::new(); if let Some(root) = self.root.0.as_ref() { deque.push_back(root); } LevelOrderTraversal { deque: deque } } } impl<T: Ord> Node<T> { pub fn new(value: T) -> Self { Node { value: value, left: Tree(None), right: Tree(None), } } } impl<T: Ord> Tree<T> { /// Inserting a new element in the tree /// Returns true if an element with the same value already exists in the tree pub fn insert(&mut self, value: T, allow_duplicate: bool) -> bool { let mut current = self; let mut is_duplicate = false; // Follow from the root to the leaves in search of a place to insert while let Some(ref mut node) = current.0 { match node.value.cmp(&value) { Ordering::Greater => current = &mut node.left, Ordering::Less => current = &mut node.right, Ordering::Equal => { if allow_duplicate { is_duplicate = true; current = &mut node.right; } else { return true; } } } } // A suitable position is found, replace None with a new node current.0 = Some(Box::new(Node::new(value))); is_duplicate } /// Checks whether the tree contains an element with the specified value pub fn contains(&self, target: &T) -> bool { let mut current = self; // Follow from the root to the leaves in search of the set value while let Some(ref node) = current.0 { match node.value.cmp(target) { Ordering::Greater => current = &node.left, Ordering::Less => current = &node.right, Ordering::Equal => return true, } } false } /// The minimum element of the tree pub fn min(&self) -> Option<&T> { if self.0.is_some() { let mut current = self.0.as_ref(); let mut left = current.unwrap().left.0.as_ref(); while let Some(node) = left { current = left; left = node.left.0.as_ref(); } current.map(|node| &node.value) } else { None } } /// The maximum element of the tree pub fn max(&self) -> Option<&T> { if self.0.is_some() { let mut current = self.0.as_ref(); let mut right = current.unwrap().right.0.as_ref(); while let Some(node) = right { current = right; right = node.right.0.as_ref(); } current.map(|node| &node.value) } else { None } } /// Inorder successor of the element with the specified value pub fn successor(&self, val: &T) -> Option<&T> { let mut current = self.0.as_ref(); let mut successor = None; while current.is_some() { let node = current.unwrap(); if node.value > *val { successor = current; current = node.left.0.as_ref(); } else { current = node.right.0.as_ref(); } } successor.map(|node| &node.value) } /// Inorder predecessor of the element with the specified value pub fn predecessor(&self, val: &T) -> Option<&T> { let mut current = self.0.as_ref(); let mut predecessor = None; while current.is_some() { let node = current.unwrap(); if node.value < *val { predecessor = current; current = node.right.0.as_ref(); } else { current = node.left.0.as_ref(); } } predecessor.map(|node| &node.value) } /// Remove and return the minimum element pub fn extract_min(&mut self) -> Option<T> { let mut to_return = None; if self.0.is_some() { let mut current = self; // Finding the last non-empty node in the left branch while current.0.as_ref().unwrap().left.0.is_some() { current = &mut current.0.as_mut().unwrap().left; } // The minimum element is replaced by its right child (the right child can be empty) let node = current.0.take().unwrap(); to_return = Some(node.value); current.0 = node.right.0; } to_return } /// Remove and return the maximum element pub fn extract_max(&mut self) -> Option<T> { let mut to_return = None; if self.0.is_some() { let mut current = self; // Finding the last non-empty node in the right branch while current.0.as_ref().unwrap().right.0.is_some() { current = &mut current.0.as_mut().unwrap().right; } // The maximum element is replaced by its left child (the left child can be empty) let node = current.0.take().unwrap(); to_return = Some(node.value); current.0 = node.left.0; } to_return } /// Deleting the first occurrence of an element with the specified value pub fn remove(&mut self, target: &T) -> bool { let mut current: *mut Tree<T> = self; unsafe { while let Some(ref mut node) = (*current).0 { match node.value.cmp(target) { Ordering::Greater => { current = &mut node.left; }, Ordering::Less => { current = &mut node.right; }, Ordering::Equal => { match (node.left.0.as_mut(), node.right.0.as_mut()) { // The node has no childrens, so we'll just make it empty. (None, None) => { (*current).0 = None; }, // Replace the node with its left child (Some(_), None) => { (*current).0 = node.left.0.take(); }, // Replace the node with its left child (None, Some(_)) => { (*current).0 = node.right.0.take(); }, // The most complexity case: replace the value of the current node with // its successor and then remove the successor's node. // The BST invariant is not violated, which is easy to verify (Some(_), Some(_)) => { (*current).0.as_mut().unwrap().value = node.right.extract_min().unwrap(); } } return true; // The removal occurred }, } } } false // The element with the 'target' value was not found } // Vector of links to tree elements in sorted order pub fn sorted_vec(&self) -> Vec<&T> { let mut elements = Vec::new(); if let Some(node) = self.0.as_ref() { elements.extend(node.left.sorted_vec()); elements.push(&node.value); elements.extend(node.right.sorted_vec()); } elements } /// Moving the tree into a sorted vector pub fn into_sorted_vec(self) -> Vec<T> { let mut elements = Vec::new(); if let Some(node) = self.0 { elements.extend(node.left.into_sorted_vec()); elements.push(node.value); elements.extend(node.right.into_sorted_vec()); } elements } } pub struct InorderTraversal<'a, T: 'a + Ord> { stack: Vec<Option<&'a Box<Node<T>>>>, current: Option<&'a Box<Node<T>>>, } pub struct PreorderTraversal<'a, T: 'a + Ord> { stack: Vec<Option<&'a Box<Node<T>>>>, current: Option<&'a Box<Node<T>>>, } pub struct PostorderTraversal<'a, T: 'a + Ord> { stack: Vec<Option<&'a Box<Node<T>>>>, current: Option<&'a Box<Node<T>>>, } pub struct LevelOrderTraversal<'a, T: 'a + Ord> { deque: VecDeque<&'a Box<Node<T>>>, } impl<'a, T: 'a + Ord> Iterator for InorderTraversal<'a, T> { type Item = &'a T; fn next(&mut self) -> Option<&'a T> { loop { if let Some(current) = self.current { self.stack.push(self.current); self.current = current.left.0.as_ref(); } else { if let Some(node) = self.stack.pop() { let current = node.unwrap(); let elem = ¤t.value; self.current = current.right.0.as_ref(); return Some(elem); } else { return None; } } } } } impl<'a, T: 'a + Ord> Iterator for PreorderTraversal<'a, T> { type Item = &'a T; fn next(&mut self) -> Option<&'a T> { loop { if let Some(current) = self.current { let elem = ¤t.value; self.current = current.left.0.as_ref(); self.stack.push(self.current); return Some(elem); } else { if let Some(node) = self.stack.pop() { if let Some(current) = node { self.current = current.right.0.as_ref(); if self.current.is_some() { self.stack.push(self.current); } } } else { return None; } } } } } impl<'a, T: 'a + Ord> Iterator for PostorderTraversal<'a, T> { type Item = &'a T; fn next(&mut self) -> Option<&'a T> { loop { // Go down the left branch and add nodes along with their right chilfren to the stack while let Some(current) = self.current { if current.right.0.is_some() { self.stack.push(current.right.0.as_ref()); } self.stack.push(self.current); self.current = current.left.0.as_ref(); } if self.stack.len() == 0 { return None; } if let Some(root) = self.stack.pop().unwrap() { // If the popped item has a right child and the // right child is not processed yet, then make sure // right child is processed before root if self.stack.len() > 0 && root.right.0.is_some() && self.stack.get(self.stack.len()-1) .unwrap().unwrap().value == root.right.0.as_ref().unwrap().value { self.stack.pop(); // Remove right child from stack self.stack.push(Some(root)); // Push root back to stack // Changing the current node so that the root's right child is viewed first self.current = root.right.0.as_ref(); } else { let elem = &root.value; self.current = None; return Some(elem); } } else { return None; // Only empty nodes remain } } } } impl<'a, T: 'a + Ord> Iterator for LevelOrderTraversal<'a, T> { type Item = &'a T; fn next(&mut self) -> Option<&'a T> { if let Some(boxed_node) = self.deque.pop_front() { if let Some(left) = boxed_node.left.0.as_ref() { self.deque.push_back(left); } if let Some(right) = boxed_node.right.0.as_ref() { self.deque.push_back(right); } Some(&boxed_node.value) } else { return None } } } #[cfg(test)] mod test;