// 孩子兄弟表示法 #include#include using namespace std; #define ElemType char typedef struct Node{ ElemType data; struct Node * firstchild,*nextsibling; }Node,*Tree; Tree init_data2(Tree tree){ ElemType data; cin >> data; if (data=='#') { tree = NULL; }else{ tree = (Node *)malloc(sizeof(Node)); tree->data = data; tree->firstchild = NULL; tree->nextsibling = NULL; cout << tree->data << "他的第一个孩子, 没有#" < firstchild = init_data2(tree->firstchild); cout << tree->data << "他的下一个兄弟" < nextsibling = init_data2(tree->nextsibling); } return tree; } void init_data(Tree &tree){ tree = (Node *)malloc(sizeof(Node)); tree->data = ' ';//头结点 tree->firstchild = NULL; tree->nextsibling = NULL; cout << "请输入根结点:" << endl; tree->firstchild = init_data2(NULL);//头结点只需要1个指针即可 } void print_data(Tree tree){ if (tree!=NULL) { cout << "element:" << tree->data << endl; print_data(tree->firstchild); print_data(tree->nextsibling); } } int main(){ std::cout << "welcome, to my world!" << std::endl; Tree tree = NULL; // cout << "size of:" << sizeof(tree) < firstchild);//传头结点 return 0; }
输出
welcome, to my world!
请输入根结点:
R
R他的第一个孩子, 没有#
A
A他的第一个孩子, 没有#
B
B他的第一个孩子, 没有#
B他的下一个兄弟
A他的下一个兄弟
C
C他的第一个孩子, 没有#
C他的下一个兄弟
R他的下一个兄弟
element:R
element:A
element:B
element:C
Program ended with exit code: 0



