@@ -70,7 +70,7 @@ function layout(para::Buchheim, adj_matrix::AbstractMatrix)
7070end
7171
7272function layout (para:: Buchheim{Ptype,T} , adj_list:: AbstractVector ) where {Ptype,T}
73- # TODO : check if adj_list represents directed tree? julia crashes for dir graph!
73+ assert_rooted_tree ( adj_list)
7474 nodesize = ones (T, length (adj_list))
7575 for i in 1 : min (length (adj_list), length (para. nodesize))
7676 nodesize[i] = para. nodesize[i]
8585function parent (v, t:: Tree )
8686 tree = t. nodes
8787 for i in 1 : length (tree)
88- y = findall (x -> (x == v), tree[i])
89- if length (y) != 0
88+ if v ∈ tree[i]
9089 return i
9190 end
9291 end
@@ -264,3 +263,32 @@ function next_right(v, t::Tree)
264263 return thread[v]
265264 end
266265end
266+
267+ """
268+ assert_rooted_tree(adj_list::AbstractVector{<:AbstractVector})
269+
270+ Check that
271+ - every node has only one parent
272+ - node 1 is head node (has no parent)
273+ - all nodes are part of the tree
274+
275+ Which are the 3 requirements for a "rooted tree" in the Buchheim paper.
276+ """
277+ function assert_rooted_tree (adj_list:: AbstractVector{<:AbstractVector} )
278+ visited = [false for _ in 1 : length (adj_list)]
279+ for childs in adj_list
280+ for child in childs
281+ if visited[child] == false
282+ visited[child] = true
283+ else # node was visited before
284+ throw (ArgumentError (" Buchheim assumption broken, this is not a rooted tree: Node $child has multiple parent nodes!" ))
285+ end
286+ end
287+ end
288+ if visited[1 ] != = false
289+ throw (ArgumentError (" Buchheim assumption broken, this is not a rooted tree: Node 1 needs to be the root!" ))
290+ end
291+ if ! all (view (visited, 2 : lastindex (visited)))
292+ throw (ArgumentError (" Buchheim assumption broken, this is not a rooted tree: Some nodes are not part of the tree." ))
293+ end
294+ end
0 commit comments