树的深度、广度优先搜索C++实现

输入:测试几颗不同的树,输入零结束程序(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

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值