带度数的后跟次序法表示的树林转换成llink-rlink法表示的算法

来源:百度知道 编辑:UC知道 时间:2024/06/06 05:08:59
带度数的后跟次序法表示的树林转换成llink-rlink法表示的算法
给个思路也行啊~~~

这是个树的,还不是森林,感觉写得好乱。。
你的森林也要都连成一棵二叉树?
#include <iostream>
#include <stack>

using namespace std;

struct Data
{
int num; //标示结点用
int degree;
};

struct Node
{
Data data;
Node * lChild;
Node * rChild;
};

Node * GenBinaryTree( stack<Data> & s )
{
if ( s.size() > 0 )
{
Node * p = new Node();
p->data = s.top();
s.pop();
p->lChild = NULL;
p->rChild = NULL;

int d = p->data.degree;
if ( d <= 0 )
return p;

stack<Node *> childs;
while ( d > 0 )
{
childs.push( GenBinaryTree(s) );
d--;
}
Node * q = p;
if ( !childs.empty() )
{
q->lChild = childs.top();
childs.pop();
q = q->lChild;
}
while ( !childs.empty() )
{
q->rChild = childs.top();
childs.pop(); <