输入:测试几颗不同的树,输入零结束程序(OJ测试题)
abcd00e00f00ig00h00
abd00e00cf00g00
0
代码:
#include <iostream>
#include <stdlib.h>
#include <malloc.h>
#include <Stack>
#include <Queue>
#define OK 1
using namespace std;
typedef struct Tnode {
char data;
struct Tnode* lchild;
struct Tnode* rchild;
} *Btree;
//Tree 是一个node指针的类型定义
//二叉树构造器,按先序遍历顺序构造二叉树
//无左子树或右子树用'0'表示
int CreateTree(Btree& T, char chars[], int& i) {
if (chars[i] == '0') {
T = NULL;
}
else {
T = new Tnode;
T->data = chars[i];
CreateTree(T->lchild, chars, ++i); //递归构建左子树
CreateTree(T->rchild, chars, ++i); //递归构建右子树
}
return OK;
}
//深度优先遍历
int depthFirstSearch(Btree root) {
stack<Tnode*> nodeStack; //使用C++的STL标准模板库
nodeStack.push(root);
Tnode* node;
while (!nodeStack.empty()) {
node = nodeStack.top();
cout << node->data;//遍历根结点
nodeStack.pop();
if (node->rchild) {
nodeStack.push(node->rchild); //先将右子树压栈
}
if (node->lchild) {
nodeStack.push(node->lchild); //再将左子树压栈
}
}
return OK;
}
//广度优先遍历
int breadthFirstSearch(Btree root) {
queue<Tnode*> nodeQueue; //使用C++的STL标准模板库
nodeQueue.push(root);
Tnode* node;
while (!nodeQueue.empty()) {
node = nodeQueue.front();
nodeQueue.pop();
cout << node->data;//遍历根结点
if (node->lchild) {
nodeQueue.push(node->lchild); //先将左子树入队
}
if (node->rchild) {
nodeQueue.push(node->rchild); //再将右子树入队
}
}
return OK;
}
int main() {
//上图所示的二叉树先序遍历序列,其中用'#'表示结点无左子树或无右子树
char ch[30];
while (cin >> ch && ch[0] != '0')
{
Btree T;
int index = 0;
CreateTree(T, ch, index);
cout << "深度优先遍历二叉树结果: " << endl;
depthFirstSearch(T);
cout << endl;
cout << "广度优先遍历的二叉树结果:" << endl;
breadthFirstSearch(T);
cout << endl;
}
return 0;
}
//ABD00E00CF00G00
第二个版本,正常的一棵树求解广度、深度优先遍历
#include <iostream>
#include <stdlib.h>
#include <malloc.h>
#include <Stack>
#include <Queue>
using namespace std;
typedef struct Tnode {
char data;
struct Tnode* lchild;
struct Tnode* rchild;
} *Tree;
//Tree 是一个node指针的类型定义
//二叉树构造器,按先序遍历顺序构造二叉树
//无左子树或右子树用'0'表示
void CreateTree(Tree& T, char chars[],int &i) {
cin >> chars[i];
if (chars[i] == '0') {
T = NULL;
}
else {
T = new Tnode;
T->data = chars[i];
CreateTree(T->lchild, chars,++i); //递归构建左子树
CreateTree(T->rchild, chars,++i); //递归构建右子树
}
}
//深度优先遍历
void depthFirstSearch(Tree root) {
stack<Tnode*> nodeStack; //使用C++的STL标准模板库
nodeStack.push(root);
Tnode* node;
while (!nodeStack.empty()) {
node = nodeStack.top();
cout << node->data;//遍历根结点
nodeStack.pop();
if (node->rchild) {
nodeStack.push(node->rchild); //先将右子树压栈
}
if (node->lchild) {
nodeStack.push(node->lchild); //再将左子树压栈
}
}
}
//广度优先遍历
void breadthFirstSearch(Tree root) {
queue<Tnode*> nodeQueue; //使用C++的STL标准模板库
nodeQueue.push(root);
Tnode* node;
while (!nodeQueue.empty()) {
node = nodeQueue.front();
nodeQueue.pop();
cout << node->data;//遍历根结点
if (node->lchild) {
nodeQueue.push(node->lchild); //先将左子树入队
}
if (node->rchild) {
nodeQueue.push(node->rchild); //再将右子树入队
}
}
}
int main() {
//上图所示的二叉树先序遍历序列,其中用'#'表示结点无左子树或无右子树
char ch[30];
Tree T=nullptr;
int index = 0;
CreateTree(T, ch, index);
cout << "深度优先遍历二叉树结果: " << endl;
depthFirstSearch(T);
cout << endl;
cout << "广度优先遍历的二叉树结果:" << endl;
breadthFirstSearch(T);
cout << endl;
return 0;
}
//ABD00E00CF00G00