typedef struct PTNode { char data; int parent; }PTNode;
typedef struct { PTNode nodes[MAX_TREE_SIZE]; int r, n; }PTree;
void Create_Parentree(PTree &p) { char data; int parent; for (int i = 0; i < p.n; i++) { cout << "请输入节点"<<i <<"的值及其父节点" << endl; cin >> data >> parent; p.nodes[i].data = data; p.nodes[i].parent = parent; } cout << "输入结束" << endl; }
void Traversal_Parentree(PTree &p) { for (int i = 0; i < p.n; i++) { cout << "节点" << p.nodes[i].data << "的子节点为:" << endl; int flag = 0; int flag2 = 0; for(int j = i; ;j++){ if (j == p.n - 1) break; if (p.nodes[j + 1].parent == i) { cout << p.nodes[j + 1].data << " "; flag = 1; flag2 = 1; } else if(p.nodes[j + 1].parent != i && flag == 1) break; } if (!flag2) cout << "无"; cout << endl; } cout << endl; }
|