algorithm - To check whether a tree is binary or not -


given (n-ary) tree. how check whether binary or not?

how check whether binary or not?

check if every node has @ 2 children.

some untested (!) pseudo-code:

struct ntree {    root: node    boolean isbinary() {     return isbinary(root)   }    private boolean isbinary(node n) {     if (n has no child)       return true     elif (n has 1 child)       return isbinary(n.children[0])     elif (n has 2 children)       return isbinary(n.children[0]) && isbinary(n.children[1])     else       return false   }    struct node {     children: list   } } 

Comments

Popular posts from this blog

javascript - Iterate over array and calculate average values of array-parts -

iphone - Using nested NSDictionary with Picker -

objective c - Newbie question -multiple parameters -