C++ 二叉树的基本操作
来源:互联网 发布:旅游分销平台源码 编辑:程序博客网 时间:2024/05/19 14:56
一:目的
用C++实现二叉树的基本操作,建立和遍历;
二:介绍
在计算机科学中,二叉树是每个节点最多有两个子树的树结构。通常子树被称作“左子树”(left subtree)和“右子树”(right subtree)。二叉树常被用于实现二叉查找树和二叉堆。
二叉树的遍历方式有三种,前序遍历,中序遍历,后序遍历。
它的前序遍历顺序为:ABDGHCEIF(规则是先是根结点,再前序遍历左子树,再前序遍历右子树)
它的中序遍历顺序为:GDHBAEICF(规则是先中序遍历左子树,再是根结点,再是中序遍历右子树)它的后序遍历顺序为:GHDBIEFCA(规则是先后序遍历左子树,再是后序遍历右子树,再是根结点)
三:实现
1. 首先创建二叉树结构,以及二叉树的类,定义在BinaryTree.h中
#include <iostream>using namespace std;typedef char ElemType;//定义树的节点typedef struct BiNode{ElemType data;//节点的数据类型BiNode * left;//左子树BiNode * right;//右子树BiNode(ElemType val){data = val;left = NULL;right = NULL;}}BiNode,*BiTree;//二叉树类class BinaryTree{public:void Create();//递归的创建二叉树的节点int getSize();//递归得到树的节点数目int getHeight();//递归得到树的高度 void preOrder();//前序遍历 void inOrder();//中序遍历 void postOrder();//后序遍历 void distroy();//删除二叉树private:BiTree create();//递归的创建二叉树的节点void preOrder(BiTree root);//前序遍历void inOrder(BiTree root);//中序遍历void postOrder(BiTree root);//后序遍历void distroy(BiTree root);//摧毁树int getHeight(BiTree root);//递归得到树的高度void AddNode(const ElemType key,int direction, BiTree root); //添加节点BiTree m_root; //根节点int size;//节点总数};2. 具体实现在BinaryTree.cpp中# include "BinaryTree.h"#pragma region 私有成员函数//添加节点//key为要插入的值,direction是从左子树插入还是右子树插入,root为从哪个节点插入void BinaryTree::AddNode(const ElemType key,int direction, BiTree root){if(direction == 0){//从左子树插入if(root->left == NULL)root->left = new BiNode(key);elseAddNode(key, direction,root->left);}else if(direction == 1){//从右子树插入if(root->right == NULL)root->right = new BiNode(key);elseAddNode(key, direction,root->right);}}////二叉树的建立,按前序遍历的方式建立二叉树BiTree BinaryTree::create(){ BiTree current = NULL; ElemType val; cin >> val;//输入键值 if(val == '#')//标识当前子树为空,转向下一节点 { return NULL; } else { //递归的创建左右子树 size++; current = new BiNode(val); current->left = create(); current->right = create(); return current; }}//删除二叉树(private 函数)void BinaryTree ::distroy(BiTree root){if(root){distroy(root->left);distroy(root->right);delete root;root = NULL;size = 0;}}//递归得到树的高度int BinaryTree::getHeight(BiTree root){if(root == NULL)return 0;int left_height = getHeight(root->left);int right_height = getHeight(root->right);return (left_height>right_height)? (left_height+1):(right_height+1);}//前序遍历void BinaryTree::preOrder(BiTree root){if(root == NULL)return;else{cout << root->data << " --> "; //首先打印根节点preOrder(root->left); //接着遍历左子树preOrder(root->right); //接着遍历右子树}}//中序遍历void BinaryTree::inOrder(BiTree root){if(root == NULL)return;else{inOrder(root->left); //首先遍历左子树cout << root->data << " --> "; //接着打印根节点inOrder(root->right); //接着遍历右子树}}//后序遍历void BinaryTree::postOrder(BiTree root){if(root == NULL)return;else{postOrder(root->left); //首先遍历左子树postOrder(root->right); //接着遍历右子树cout << root->data << " --> "; //接着打印根节点}}#pragma endregion#pragma region 公有成员函数////二叉树的建立void BinaryTree::Create(){size = 0;m_root = create();}//删除二叉树void BinaryTree ::distroy(){distroy(m_root);}//递归得到树的高度int BinaryTree::getHeight(){return getHeight(m_root);}//前序遍历void BinaryTree::preOrder(){cout << "前序遍历的结果如下:"<<endl;preOrder(m_root);cout << endl;}//中序遍历void BinaryTree::inOrder(){cout << "中序遍历的结果如下:"<<endl;inOrder(m_root);cout << endl;}//后序遍历void BinaryTree::postOrder(){cout << "后序遍历的结果如下:"<<endl;postOrder(m_root);cout << endl;}//获取大小int BinaryTree::getSize(){//这里是创建时候直接进行了计数//也可以利用遍历的方式获取,当节点有值,就加1return size;}#pragma endregion3. 简单测试前序遍历的方式创建如下图所示二叉树,应当输入:ABDG##H###CE#I##F##测试代码为:BinaryTree tree;cout << "按前序遍历方式创建树" <<endl;//"ABDG##H###CE#I##F##";tree.Create();cout << "树的高度为:" << tree.getHeight() <<endl;cout << "树的节点为:" << tree.getSize() <<endl;tree.preOrder();//前序遍历tree.inOrder();//中序遍历tree.postOrder();//后序遍历tree.distroy();//摧毁树system("pause");测试结果为:参考:https://www.cnblogs.com/SarahZhang0104/p/5827532.htmlhttps://www.cnblogs.com/liuamin/p/6269950.html
阅读全文
0 0
- 二叉树的基本操作(C++)
- C语言二叉树的基本操作
- 二叉树的基本操作C源代码
- 【C++】二叉树的基本操作
- 二叉树的基本操作 C语言
- c 二叉树基本操作
- 二叉搜索树的基本操作(C代码实现)
- 数据结构.二叉树的基本操作(C语言实现)
- C语言实现二叉树的基本操作
- c语言实现二叉树的基本操作
- C语言实现二叉树的基本操作
- C语言实现二叉树的基本操作
- 二叉树的基本操作(C语言版)
- 二叉树的基本操作
- 二叉树的基本操作
- 二叉树的基本操作
- 二叉树的基本操作
- 二叉树的基本操作
- JAVA面试——程序基础篇
- 营销运营人员的自我修炼——系列三(终篇)
- Mac配置JDK环境变量
- 百帮忙飞人端隐私政策
- Spring4.0 RestController Rest风格请求参数详解
- C++ 二叉树的基本操作
- 顺序队列
- 更换Actionbar,Toolbar的使用
- 蓝桥杯 回文数
- Oozie的简介及安装部署
- markdown sample
- SQLContext/HiveContext/SparkSession的使用(二)
- 保存网页内容至word,相关内容导入mysql
- Linux下命令行安装WebLogic 10.3.6