1 | initial version |
Ironically, the LabelledBinaryTree() command only lets you label the root. But you can apply a canonical labeling and then map those labels to the desired ones.
import re
words = re.compile('\w+')
# labelled binary tree from string
def lbt_from_str(lbstr):
labels = [Integer(w) for w in words.findall(lbstr)]
blank = words.sub('', lbstr)
work = LabelledBinaryTree(blank).canonical_labelling()
wstr = '%s'%work
canon = [Integer(w) for w in words.findall(wstr)]
fixit = dict((canon[i] , labels[i]) for i in range(len(canon)))
return work.map_labels(lambda z: fixit[z])
# end lbt_from_str
This doesn't check if the string has a valid structure, and it expects all nodes to be labeled (you should use 'None' for unlabeled ones). Please feel free to improve it.
Testing:
t = Permutation([1, 3, 2, 6, 5, 7, 4]).increasing_tree()
print t
show(t)
s = '%s'%t
test = lbt_from_str(s)
print test
show(test)
2 | No.2 Revision |
Ironically, the LabelledBinaryTree() command only lets you label the root. But you can apply a canonical labeling and then map those labels to the desired ones.
import re
words = re.compile('\w+')
# labelled binary tree from string
def lbt_from_str(lbstr):
labels = [Integer(w) for w in words.findall(lbstr)]
blank = words.sub('', lbstr)
work = LabelledBinaryTree(blank).canonical_labelling()
wstr = '%s'%work
canon = [Integer(w) for w in words.findall(wstr)]
fixit = dict((canon[i] , labels[i]) for i in range(len(canon)))
return work.map_labels(lambda z: fixit[z])
# end lbt_from_str
This doesn't check if the string has a valid structure, and it expects all nodes to be labeled (you should use 'None' for unlabeled ones). with integers. *I will fix that shortly ... Please feel free to improve it.
Testing:
t = Permutation([1, 3, 2, 6, 5, 7, 4]).increasing_tree()
print t
show(t)
s = '%s'%t
test = lbt_from_str(s)
print test
show(test)
3 | No.3 Revision |
Ironically, the LabelledBinaryTree() command only lets you label the root. But you can apply a canonical labeling and then map those labels to the desired ones.
import re
words = re.compile('\w+')
# keep int labels as ints
def safeInt(word):
try:
return Integer(word)
except TypeError:
return word
# labelled binary tree from string
def lbt_from_str(lbstr):
labels = [Integer(w) for w in words.findall(lbstr)]
blank = words.sub('', lbstr)
work = LabelledBinaryTree(blank).canonical_labelling()
wstr = '%s'%work
canon = [Integer(w) for w in words.findall(wstr)]
labels = [safeInt(w) for w in words.findall(lbstr)]
fixit = dict((canon[i] , dict((canon[i], labels[i]) for i in range(len(canon)))
return work.map_labels(lambda z: fixit[z])
# end lbt_from_str
This doesn't check if the string has a valid structure, and it expects all nodes to be labeled have labels.
You could replace ' [' with integers. *I will fix that shortly ... Please feel free to improve it.'None['
Testing:
t = Permutation([1, 3, 2, 6, 5, 7, 4]).increasing_tree()
print t
show(t)
s = '%s'%t
test = lbt_from_str(s)
print test
show(test)