// -------------------------
// 二叉树的建立与先序遍历
// ------------------------
#include <iostream>
#include <malloc.h>
using namespace std;
typedef struct BiTreeNode {
char elem;
struct BiTreeNode *lchild, *rchild;
}BiTreeNode,*BiTree;
void CreatBiTree(BiTree &bt) {
char ch;
cin >> ch;
if (ch == ‘#‘)
bt = NULL;
else {
bt = new BiTreeNode;
bt->elem = ch;
CreatBiTree(bt->lchild);
CreatBiTree(bt->rchild);
}
}
void preTraverseBiTree(BiTree T) {
if (T) {
cout << T->elem << ",";
preTraverseBiTree(T->lchild);
preTraverseBiTree(T->rchild);
}
}
int main() {
BiTree T;
//实例输入:ab#d##c#e##
// 输出:abdce
CreatBiTree(T);
cout << "\n-------------------------" << endl;
preTraverseBiTree(T);
system("pause");
return 0;
}
时间: 2024-10-14 05:14:14