二叉树的顺序表示法("/"代表空指针)

 


 

对于图6.16中的二叉树,相应的顺序表示结点表示如下:

                              AB/D//CEG///FH//I//(数据结构课本中的135页)

从二叉树中用printhelp函数打印出来,printhelp函数的C++算法代码:

1 template<class Elem>
2 void printhelp(BinNode*subroot)
3 {
4     if(subroot==NULL) {cout<<"\\";return;}
5     cout<<subroot->value();
6     printhelp(subroot->left());
7     printhelp(subroot->right());
8 
9 }

而从顺序表中解码为二叉树,相应的convert函数C++算法代码:

template <class Elem>
BinNode<Elem>* convert(char* inlist) {
int curr = 0;
return converthelp(inlist, curr);
}
// As converthelp processes the node list, curr is
// incremented appropriately.
template <class Elem>
BinNode<Elem>* converthelp(char* inlist,
int& curr) {
if (inlist[curr] == ’/’) {
curr++;
return NULL;
}
BinNode<Elem>* temp = new BinNode(inlist[curr++],
NULL, NULL);
temp->left = converthelp(inlist, curr);
temp->right = converthelp(inlist, curr);
return temp;
}

  重点:::注意比较读码和解码之间的异同。

posted on 2012-12-01 20:49  Besion王  阅读(762)  评论(0编辑  收藏  举报

导航