InsertNodeAt inserts a new node at the given parent position
(parentID string, id string, data interface{})
| 57 | |
| 58 | // InsertNodeAt inserts a new node at the given parent position |
| 59 | func (g *Graph) InsertNodeAt(parentID string, id string, data interface{}) (*Node, error) { |
| 60 | parentNode, ok := g.Nodes[parentID] |
| 61 | if !ok { |
| 62 | return nil, errors.Errorf("Parent %s does not exist", parentID) |
| 63 | } |
| 64 | if existingNode, ok := g.Nodes[id]; ok { |
| 65 | err := g.AddEdge(parentNode.ID, existingNode.ID) |
| 66 | if err != nil { |
| 67 | return nil, err |
| 68 | } |
| 69 | |
| 70 | return existingNode, nil |
| 71 | } |
| 72 | |
| 73 | node := NewNode(id, data) |
| 74 | |
| 75 | g.Nodes[node.ID] = node |
| 76 | |
| 77 | parentNode.Childs = append(parentNode.Childs, node) |
| 78 | node.Parents = append(node.Parents, parentNode) |
| 79 | |
| 80 | return node, nil |
| 81 | } |
| 82 | |
| 83 | // RemoveNode removes a node with no children in the graph |
| 84 | func (g *Graph) RemoveNode(id string) error { |