node.rs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. use bit_vec::{self, BitVec};
  2. #[derive(Debug, Eq, PartialEq)]
  3. pub struct Node {
  4. left: Option<Box<Self>>,
  5. right: Option<Box<Self>>,
  6. character: Option<char>,
  7. frequency: i32,
  8. }
  9. impl Ord for Node {
  10. fn cmp(&self, other: &Self) -> std::cmp::Ordering {
  11. return self.frequency.cmp(&other.frequency)
  12. }
  13. }
  14. impl PartialOrd for Node {
  15. fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
  16. return Some(self.frequency.cmp(&other.frequency))
  17. }
  18. }
  19. impl Node {
  20. pub fn new() -> Node {
  21. Self {
  22. left: None,
  23. right: None,
  24. character: None,
  25. frequency: 0,
  26. }
  27. }
  28. pub fn new_with_character(character: char, frequency: i32) -> Node {
  29. Self {
  30. character: Some(character),
  31. left: None,
  32. right: None,
  33. frequency
  34. }
  35. }
  36. pub fn join_left(self: &mut Node, left: Box<Node>) {
  37. assert!(self.left.is_none(), "Tried to join a node to the left, but the branch was not empty.");
  38. self.frequency += left.get_frequency();
  39. self.left = Some(left);
  40. }
  41. pub fn join_right(self: &mut Node, right: Box<Node>) {
  42. assert!(self.right.is_none(), "Tried to join a node to the right, but the branch was not empty.");
  43. self.frequency += right.get_frequency();
  44. self.right = Some(right);
  45. }
  46. pub fn is_leaf(self: &Node) -> bool {
  47. self.left.is_none() & self.right.is_none()
  48. }
  49. pub fn get_frequency(self: &Node) -> i32 {
  50. self.frequency
  51. }
  52. pub fn get_character(self: &Node) -> Result<char, &str> {
  53. if !self.is_leaf() {
  54. return Err("This node is not a leaf node.");
  55. }
  56. Ok(self.character.unwrap())
  57. }
  58. pub fn get_character_code(self: &Node, character: char) -> Result<BitVec, &str> {
  59. let output = BitVec::new();
  60. if self.is_leaf() && self.get_character().unwrap() == character {
  61. return Ok(output);
  62. }
  63. if let Some(n) = &self.left {
  64. let left = n.get_character_code(character);
  65. if left.is_ok() {
  66. let mut bit_result = left.unwrap();
  67. bit_result.push(false);
  68. return Ok(bit_result)
  69. }
  70. }
  71. if let Some(n) = &self.right {
  72. let right = n.get_character_code(character);
  73. if right.is_ok() {
  74. let mut bit_result = right.unwrap();
  75. bit_result.push(true);
  76. return Ok(bit_result)
  77. }
  78. }
  79. Err("Could not find character.")
  80. }
  81. }
  82. #[cfg(test)]
  83. mod test {
  84. use super::*;
  85. #[test]
  86. fn make_new_node_with_character_works() {
  87. let example = Node::new_with_character('a', 5);
  88. assert!(example.is_leaf());
  89. assert_eq!(example.get_character().unwrap(), 'a');
  90. }
  91. #[test]
  92. fn make_small_tree_with_leaf_nodes() {
  93. let mut root = Node::new();
  94. let left = Node::new_with_character('a', 5);
  95. let right = Node::new_with_character('b', 10);
  96. root.join_left(Box::new(left));
  97. root.join_right(Box::new(right));
  98. assert!(!root.is_leaf());
  99. assert_eq!(root.get_frequency(), 15);
  100. }
  101. }