Skip to content

Instantly share code, notes, and snippets.

@cngkaygusuz
Created August 23, 2015 09:36
Show Gist options
  • Save cngkaygusuz/dc4d63c4d9b85dab9589 to your computer and use it in GitHub Desktop.
Save cngkaygusuz/dc4d63c4d9b85dab9589 to your computer and use it in GitHub Desktop.
DFS for some random struct.
func (bon* BONode) partition() ([]*BONode, []*BONode) {
// Partition a node, yielding her rank 0 children and rank >0 children.
// This should take O(logn) time, because every node except the root has at most 2 children, which one of those children has rank 0.
nodestack := make([]*BONode, 10) // this is to be used as stack
nodestack = appendList(nodestack, bon.children_head)
zeros := make([]*BONode, 10)
nonzeros := make([]*BONode, 10)
for len(nodestack) != 0 {
poppednode := nodestack[len(nodestack)-1]; nodestack = nodestack[0:len(nodestack)] // pop from stack
if poppednode.rank == 0 {
zeros = append(zeros, poppednode)
} else {
nonzeros = append(nonzeros, poppednode)
}
appendList(nodestack, poppednode.children_head)
}
return zeros, nonzeros
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment