RemoveNode removes a node with no children in the graph
(id string)
| 82 | |
| 83 | // RemoveNode removes a node with no children in the graph |
| 84 | func (g *Graph) RemoveNode(id string) error { |
| 85 | if node, ok := g.Nodes[id]; ok { |
| 86 | if len(node.Childs) > 0 { |
| 87 | return errors.Errorf("Cannot remove %s from graph because it has still children", getNameOrID(node)) |
| 88 | } |
| 89 | |
| 90 | // Remove child from parents |
| 91 | for _, parent := range node.Parents { |
| 92 | i := -1 |
| 93 | for idx, c := range parent.Childs { |
| 94 | if c.ID == id { |
| 95 | i = idx |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | if i != -1 { |
| 100 | parent.Childs = append(parent.Childs[:i], parent.Childs[i+1:]...) |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | // Remove from graph nodes |
| 105 | delete(g.Nodes, id) |
| 106 | } |
| 107 | |
| 108 | return nil |
| 109 | } |
| 110 | |
| 111 | // GetNextLeaf returns the next leaf in the graph from node start |
| 112 | func (g *Graph) GetNextLeaf(start *Node) *Node { |