General树的顺序表示法(")"代表一个结点的结束)
可以把图6.3中的树如下表示:
RAC)D)E))BF)))
读取的函数的算法代码:
1 template<class Elem> 2 void printhelp(BinNode*subroot) 3 { 4 if(subroot==NULL) {cout<<")";return;} 5 cout<<subroot->value(); 6 BinNode<Elem>*root=subroot->leftmost_child(); 7 printhelp(root); 8 if(root->right_ibling()!=NULL) 9 printhelp(root->right_sibling()); 10 }
而二叉树顺序表的解码函数代码为:
1 template <class Elem> 2 GTNode<Elem>* convert(char* inlist) { 3 int curr = 0; 4 return converthelp(inlist, curr); 5 } 6 7 template <class Elem> 8 GTNode<Elem>* converthelp(char* inlist,int& curr) 9 { 10 if (inlist[curr] == ’)') 11 { 12 curr++; 13 return NULL; 14 } 15 GTNode<Elem>* temp =new GTNode<Elem>(inlist[curr++]); 16 if (inlist[curr] == ’)') 17 { 18 temp->insert_first(NULL); 19 return temp; 20 } 21 temp->insert_first(converthelp(inlist, curr)); 22 while (inline[curr]!= ’)') 23 temp->insert_next(converthelp(inlist, curr)); 24 curr++; 25 return temp; 26 }
重要:::注意比较正反代码的区别