How do I implement a tree in rust where nodes are aware of their parent and children

284 Views Asked by At

I've been trying to implement this with Rc<RefCell<>> and such things but i always run into issues can anyone provide me with a link to an example implementation?\

This is what i've tried:

pub struct Node<T> {
    value: T,
    parent: Option<Rc<RefCell<Node<T>>>,
    children: Vec<Rc<RefCell<Node<T>>>>,
}

Most of the issues that i have faced come from the fact that a node refers to it's parent but it's parent also refers to the node.

Note: I haven't included functions such as add children because it is straight forward: Create the child that has an Rc::clone() to it's parent node and then push the child onto the children field of the parent node.

0

There are 0 best solutions below