An Algebra of Hierarchical Graphs and its Application to Structural Encoding