Hello, I'm stuck at converting a flat list to a hierarchical structure.
#[derive(Debug, Clone)]
struct Node {
level: u8,
children: Vec<Node>
}
fn node_list_to_hierarrchy(nodes: Vec<Node>) -> Option<Node>{
// check if we even have nodes
if nodes.len() == 0{
return None;
}
// the first node is always the lone root node
let mut root_node = nodes[0].clone();
// init vars
let mut parent: &mut Node = &mut root_node;
let mut prev: &mut Node = parent;
let mut parent_stack: Vec<&mut Node> = Vec::new();
parent_stack.push(parent);
// start converting the list to a hierachy
for node in nodes.iter().skip(1){
// if the node is a child of the previous node
if node.level > prev.level{
// add the previous node to the parent stack
parent_stack.push(prev);
// set the previous node as the parent
parent = prev;
}
// if the node is of the same or lower level than the parent node
else if node.level <= parent.level{
// remove nodes from the parent stack until we find the parent of the node
while node.level <= parent.level{
parent_stack.pop();
parent = *parent_stack.last().unwrap();
}
}
// add the node to the parent
parent.children.push(node.clone());
// set the previous node
prev = parent.children.last_mut().unwrap();
}
Some(root_node)
}