Ver Fonte

Started

AvariceLHubris há 1 ano atrás
commit
98b7dd7905
5 ficheiros alterados com 63 adições e 0 exclusões
  1. 1 0
      .gitignore
  2. 7 0
      Cargo.lock
  3. 6 0
      Cargo.toml
  4. 46 0
      src/lib.rs
  5. 3 0
      src/main.rs

+ 1 - 0
.gitignore

@@ -0,0 +1 @@
+/target

+ 7 - 0
Cargo.lock

@@ -0,0 +1,7 @@
+# This file is automatically @generated by Cargo.
+# It is not intended for manual editing.
+version = 3
+
+[[package]]
+name = "huffman"
+version = "0.1.0"

+ 6 - 0
Cargo.toml

@@ -0,0 +1,6 @@
+[package]
+name = "huffman"
+version = "0.1.0"
+edition = "2021"
+
+[dependencies]

+ 46 - 0
src/lib.rs

@@ -0,0 +1,46 @@
+// pub struct hufftree {
+// }
+
+struct Node {
+    left: Option<Box<Self>>,
+    right: Option<Box<Self>>,
+    character: Option<char>,
+}
+
+impl Node {
+    pub fn new() -> Node {
+        Self {
+            left: None,
+            right: None,
+            character: None,
+        }
+    }
+
+    pub fn new_with_character(character: char) -> Node {
+        Self {
+            character: Some(character),
+            left: None,
+            right: None,
+        }
+    }
+
+    pub fn join_left(self: &mut Node, left: Box<Node>) {
+        self.left = Some(left);
+    }
+
+    pub fn join_right(self: &mut Node, right: Box<Node>) {
+        self.right = Some(right);
+    }
+
+    pub fn is_leaf(self: &Node) -> bool {
+        self.left.is_none() & self.right.is_none()
+    }
+
+    pub fn get_character(self: &Node) -> Result<char, &str> {
+        if !self.is_leaf() {
+            return Err("This node is not a leaf node.");
+        }
+
+        Ok(self.character.unwrap())
+    }
+}

+ 3 - 0
src/main.rs

@@ -0,0 +1,3 @@
+fn main() {
+    println!("Hello, world!");
+}