解答

  1. use std::cmp::Ordering;
  2. /// A node in the binary tree.
  3. #[derive(Debug)]
  4. struct Node<T: Ord> {
  5. value: T,
  6. left: Subtree<T>,
  7. right: Subtree<T>,
  8. }
  9. /// A possibly-empty subtree.
  10. #[derive(Debug)]
  11. struct Subtree<T: Ord>(Option<Box<Node<T>>>);
  12. /// A container storing a set of values, using a binary tree.
  13. ///
  14. /// If the same value is added multiple times, it is only stored once.
  15. #[derive(Debug)]
  16. pub struct BinaryTree<T: Ord> {
  17. root: Subtree<T>,
  18. }
  19. impl<T: Ord> BinaryTree<T> {
  20. fn new() -> Self {
  21. Self { root: Subtree::new() }
  22. }
  23. fn insert(&mut self, value: T) {
  24. self.root.insert(value);
  25. }
  26. fn has(&self, value: &T) -> bool {
  27. self.root.has(value)
  28. }
  29. fn len(&self) -> usize {
  30. self.root.len()
  31. }
  32. }
  33. impl<T: Ord> Subtree<T> {
  34. fn new() -> Self {
  35. Self(None)
  36. }
  37. fn insert(&mut self, value: T) {
  38. match &mut self.0 {
  39. None => self.0 = Some(Box::new(Node::new(value))),
  40. Some(n) => match value.cmp(&n.value) {
  41. Ordering::Less => n.left.insert(value),
  42. Ordering::Equal => {}
  43. Ordering::Greater => n.right.insert(value),
  44. },
  45. }
  46. }
  47. fn has(&self, value: &T) -> bool {
  48. match &self.0 {
  49. None => false,
  50. Some(n) => match value.cmp(&n.value) {
  51. Ordering::Less => n.left.has(value),
  52. Ordering::Equal => true,
  53. Ordering::Greater => n.right.has(value),
  54. },
  55. }
  56. }
  57. fn len(&self) -> usize {
  58. match &self.0 {
  59. None => 0,
  60. Some(n) => 1 + n.left.len() + n.right.len(),
  61. }
  62. }
  63. }
  64. impl<T: Ord> Node<T> {
  65. fn new(value: T) -> Self {
  66. Self { value, left: Subtree::new(), right: Subtree::new() }
  67. }
  68. }
  69. fn main() {
  70. let mut tree = BinaryTree::new();
  71. tree.insert("foo");
  72. assert_eq!(tree.len(), 1);
  73. tree.insert("bar");
  74. assert!(tree.has(&"foo"));
  75. }
  76. #[cfg(test)]
  77. mod tests {
  78. use super::*;
  79. #[test]
  80. fn len() {
  81. let mut tree = BinaryTree::new();
  82. assert_eq!(tree.len(), 0);
  83. tree.insert(2);
  84. assert_eq!(tree.len(), 1);
  85. tree.insert(1);
  86. assert_eq!(tree.len(), 2);
  87. tree.insert(2); // not a unique item
  88. assert_eq!(tree.len(), 2);
  89. }
  90. #[test]
  91. fn has() {
  92. let mut tree = BinaryTree::new();
  93. fn check_has(tree: &BinaryTree<i32>, exp: &[bool]) {
  94. let got: Vec<bool> =
  95. (0..exp.len()).map(|i| tree.has(&(i as i32))).collect();
  96. assert_eq!(&got, exp);
  97. }
  98. check_has(&tree, &[false, false, false, false, false]);
  99. tree.insert(0);
  100. check_has(&tree, &[true, false, false, false, false]);
  101. tree.insert(4);
  102. check_has(&tree, &[true, false, false, false, true]);
  103. tree.insert(4);
  104. check_has(&tree, &[true, false, false, false, true]);
  105. tree.insert(3);
  106. check_has(&tree, &[true, false, false, true, true]);
  107. }
  108. #[test]
  109. fn unbalanced() {
  110. let mut tree = BinaryTree::new();
  111. for i in 0..100 {
  112. tree.insert(i);
  113. }
  114. assert_eq!(tree.len(), 100);
  115. assert!(tree.has(&50));
  116. }
  117. }