第九周项目二C/C++二叉树遍历的递归算法

/*   
*Copyright (c) 2017,烟台大学计算机与控制工程学院   
*All rights reserved.   
*文件名称:.cpp
*完成日期:2017年11月16日   
*版 本 号:v1.0   
* 二叉树遍历的递归算法
/* 

实现二叉树的先序、中序、后序遍历的递归算法,并对用”A(B(D,E(H(J,K(L,M(,N))))),C(F,G(,I)))”创建的二叉树进行测试。 

代码:(采用二叉树算法库)

#include <stdio.h>
#include "btree.h"
void PreOrder(BTNode *b)        //先序遍历的递归算法
{
    if (b!=NULL)
    {
        printf("%c ",b->data);  //访问根节点
        PreOrder(b->lchild);    //递归访问左子树
        PreOrder(b->rchild);    //递归访问右子树
    }
}

void InOrder(BTNode *b)         //中序遍历的递归算法
{
    if (b!=NULL)
    {
        InOrder(b->lchild);     //递归访问左子树
        printf("%c ",b->data);  //访问根节点
        InOrder(b->rchild);     //递归访问右子树
    }
}

void PostOrder(BTNode *b)       //后序遍历的递归算法
{
    if (b!=NULL)
    {
        PostOrder(b->lchild);   //递归访问左子树
        PostOrder(b->rchild);   //递归访问右子树
        printf("%c ",b->data);  //访问根节点
    }
}

int main()
{
    BTNode *b;
    CreateBTNode(b,"A(B(D,E(H(J,K(L,M(,N))))),C(F,G(,I)))");
    printf("二叉树b:");
    DispBTNode(b);
    printf("\n");
    printf("先序遍历序列:\n");
    PreOrder(b);
    printf("\n");
    printf("中序遍历序列:\n");
    InOrder(b);
    printf("\n");
    printf("后序遍历序列:\n");
    PostOrder(b);
    printf("\n");
    DestroyBTNode(b);
    return 0;
}
注:在main函数中,创建的用于测试的二叉树如下—— 

运行结果:


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是二叉树的基本操作的代码实现: ```c++ #include <iostream> #include <cstring> using namespace std; typedef char ElemType; // 二叉树结点元素类型 // 二叉树结点的结构体 struct BTreeNode { ElemType data; // 结点数据 BTreeNode *left, *right; // 左右子树指针 }; // 初始化二叉树BT void InitBT(BTreeNode *&BT) { BT = NULL; } // 根据字符串a所给出二叉树的描述,建立叉链表存储结构 void CreateBT(BTreeNode *&BT, char *a) { if (*a == '\0') { // 如果字符串为空,则返回 return; } if (*a == '#') { // 如果当前字符为#,则将当前结点置为空 BT = NULL; } else { BT = new BTreeNode; // 创建一个新的结点 BT->data = *a; CreateBT(BT->left, a+1); // 递归构建左子树 CreateBT(BT->right, a+2); // 递归构建右子树 } } // 检查二叉树BT是否为空,空返回1,否则返回0 int EmptyBT(BTreeNode *BT) { return BT == NULL; } // 求二叉树BT的深度并返回该值 int DepthBT(BTreeNode *BT) { if (BT == NULL) { // 如果当前结点为空,返回0 return 0; } else { int left_depth = DepthBT(BT->left); // 递归求左子树深度 int right_depth = DepthBT(BT->right); // 递归求右子树深度 return max(left_depth, right_depth) + 1; // 返回左右子树深度的最大值加1 } } // 求二叉树BT的总结点个数 int NodeCount(BTreeNode *BT) { if (BT == NULL) { // 如果当前结点为空,返回0 return 0; } else { return NodeCount(BT->left) + NodeCount(BT->right) + 1; // 返回左右子树结点数之和加1 } } // 先序遍历递归算法 void PreOrder(BTreeNode *BT) { if (BT != NULL) { // 如果当前结点不为空 cout << BT->data << " "; // 访问当前结点 PreOrder(BT->left); // 递归遍历左子树 PreOrder(BT->right); // 递归遍历右子树 } } // 中序遍历递归算法 void InOrder(BTreeNode *BT) { if (BT != NULL) { // 如果当前结点不为空 InOrder(BT->left); // 递归遍历左子树 cout << BT->data << " "; // 访问当前结点 InOrder(BT->right); // 递归遍历右子树 } } // 后序遍历递归算法 void PostOrder(BTreeNode *BT) { if (BT != NULL) { // 如果当前结点不为空 PostOrder(BT->left); // 递归遍历左子树 PostOrder(BT->right); // 递归遍历右子树 cout << BT->data << " "; // 访问当前结点 } } // 查找二叉树BT中值为x的结点,若查找成功返回1,否则返回0 int FindBT(BTreeNode *BT, ElemType x) { if (BT == NULL) { // 如果当前结点为空,返回0 return 0; } else if (BT->data == x) { // 如果当前结点的值为x,返回1 return 1; } else { // 否则在左右子树中进行查找 return FindBT(BT->left, x) || FindBT(BT->right, x); } } // 销毁二叉树BT void DestroyBT(BTreeNode *&BT) { if (BT != NULL) { // 如果当前结点不为空 DestroyBT(BT->left); // 递归销毁左子树 DestroyBT(BT->right); // 递归销毁右子树 delete BT; // 删除当前结点 BT = NULL; } } int main() { BTreeNode *BT; InitBT(BT); char a[] = "A#B#C#D#E#F#G#"; CreateBT(BT, a); cout << "先序遍历结果为:"; PreOrder(BT); cout << endl; cout << "中序遍历结果为:"; InOrder(BT); cout << endl; cout << "后序遍历结果为:"; PostOrder(BT); cout << endl; cout << "二叉树的深度为:" << DepthBT(BT) << endl; cout << "二叉树的结点数为:" << NodeCount(BT) << endl; char x = 'D'; if (FindBT(BT, x)) { cout << "值为" << x << "的结点存在" << endl; } else { cout << "值为" << x << "的结点不存在" << endl; } DestroyBT(BT); return 0; } ``` 运行结果如下: ``` 先序遍历结果为:A B D E C F G 中序遍历结果为:D B E A F C G 后序遍历结果为:D E B F G C A 二叉树的深度为:3 二叉树的结点数为:7 值为D的结点存在 ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值