function ChildChildrenLists = ChildTrees(ChildrenList) % ChildChildrenLists = ChildTrees(ChildrenList) %Input: ChildrenList: m + 1 column matrix (of integers) for an m-ary rooted tree %Output: ChildChildrenLists: an ordered list of s m + 1 column matrices each being a %children list for the subtree of the s child subtrees of the root of the inputted tree %Although the child trees can typically get smaller, we use the same %dimensions for each of them as for the original tree %Each child tree will basically take all the rows of the main tree %Children list corresponding to itself and all of its descendant vertices. [n t] = size(ChildrenList); %n = number of vertices if n > 1 %There will be children trees %In order to create a well defined array, we will use the same number of %rows for each of the outputted child tree matrix. The number of rows will %be the depth of the original tree. Unallocated rows in %outputted child tree matrices will be filled with zeros. ParentList = ChildrenList2ParentList(ChildrenList); DepthList = ParentList2DepthList(ParentList); maxRows = max(DepthList(:,2)); %First we find the children of the root: RootChildren = ChildrenList(1, find(ChildrenList(1,2:t)>0)+1); ChildChildrenLists = zeros(maxRows,t,length(RootChildren)); %PreAllocate size of output array for i = 1:length(RootChildren) DescendantList = DescendantFinder(ChildrenList, RootChildren(i)); DescendantList = [RootChildren(i) DescendantList]; %need to adjoin root child to its decendants %need corresponding rows of ChildrenList Rows = []; for j = 1:length(DescendantList) newRow = find(ChildrenList(:,1) == DescendantList(j)); Rows = [Rows newRow]; end ChildChildrenLists(1:length(Rows),:,i) = ChildrenList(Rows,:); end else %no children trees ChildChildrenLists = []; end