Skip to content
Snippets Groups Projects
Commit c3e99e90 authored by moritz.plonka's avatar moritz.plonka
Browse files

Implemented syntree.

parent 2ac386de
Branches main
No related merge requests found
......@@ -14,15 +14,22 @@ pub struct Syntree<T> {
// Hint: Start with seek_node_mut
impl<'a, T> Syntree<T> {
pub fn new(value: T, id: ID) -> Syntree<T> {
todo!()
let children = Vec::new();
Syntree{id, value, children}
}
pub fn push_node(&mut self, parent_id: ID, new_node: Syntree<T>) -> Result<(), String> {
todo!()
pub fn push_node(&mut self, parent_id: ID, new_node: Syntree<T>) -> Result<(), &str> {
let current_node = self.seek_node_mut(&parent_id);
if current_node.is_none() {
return Result::Err("The listed node does not exist.");
}
let current_node = current_node.unwrap();
current_node.children.push(new_node);
return Result::Ok(());
}
pub fn prepend_node(&mut self, parent_id: ID, new_node: Syntree<T>) -> Result<(), String> {
todo!()
pub fn prepend_node(&mut self, parent_id: ID, new_node: Syntree<T>) -> Result<(), &str> {
self.insert_node(parent_id, 0, new_node)
}
pub fn insert_node(
......@@ -30,8 +37,14 @@ impl<'a, T> Syntree<T> {
parent_id: ID,
index: usize,
new_node: Syntree<T>,
) -> Result<(), String> {
todo!()
) -> Result<(), &str> {
let current_node = self.seek_node_mut(&parent_id);
if current_node.is_none() {
return Result::Err("The listed node does not exist.");
}
let current_node = current_node.unwrap();
current_node.children.insert(index, new_node);
return Result::Ok(());
}
// Anmerkung: `'a` Is ein Lebenszeit angabe für die Referenzen
......@@ -50,7 +63,16 @@ impl<'a, T> Syntree<T> {
}
pub fn seek_node_mut(&'a mut self, id: &ID) -> Option<&'a mut Syntree<T>> {
todo!()
if self.id == *id {
Some(self)
} else {
for child in &mut self.children {
if let Some(result) = child.seek_node_mut(id) {
return Some(result);
}
}
None
}
}
}
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment