c/c++语言开发共享纯C++代码详解二叉树相关操作

前言大家好,今天给大家带来的是二叉树的相关操作,希望能够给大家带来帮助。二叉树的概念二叉树(binary tree)是树形结构的一个重要类型。许多实际问题抽象出来的数据结构往往是二叉树形式,即使是一般

前言 

大家好,今天给大家带来的是二叉树的相关操作,希望能够给大家带来帮助。

二叉树的概念

二叉树(binary tree)是树形结构的一个重要类型。许多实际问题抽象出来的数据结构往往是二叉树形式,即使是一般的树也能简单地转换为二叉树,而且二叉树的存储结构及其算法都较为简单,因此二叉树显得特别重要。二叉树特点是每个节点最多只能有两棵子树,且有左右之分 。

二叉树的相关术语

①节点:包含一个数据元素及若干指向子树分支的信息 。

②节点的度:一个节点拥有子树的数目称为节点的度 。

③叶子节点:也称为终端节点,没有子树的节点或者度为零的节点 。

④分支节点:也称为非终端节点,度不为零的节点称为非终端节点 。

⑤树的度:树中所有节点的度的最大值 。

⑥节点的层次:从根节点开始,假设根节点为第1层,根节点的子节点为第2层,依此类推,如果某一个节点位于第l层,则其子节点位于第l+1层 。

⑦树的深度:也称为树的高度,树中所有节点的层次最大值称为树的深度  。

相关操作菜单

//菜单  void menu()  {      cout << "ttt******************************************************************" << endl;      cout << "ttt****************  1.输入-1  退出程序           *******************" << endl;      cout << "ttt****************  2.输入1   初始化二叉树       *******************" << endl;      cout << "ttt****************  3.输入2   对二叉树先序遍历   *******************" << endl;      cout << "ttt****************  4.输入3   对二叉树中序遍历   *******************" << endl;      cout << "ttt****************  5.输入4   对二叉树后序遍历   *******************" << endl;      cout << "ttt****************  6.输入5   对二叉树层次遍历   *******************" << endl;      cout << "ttt****************  7.输入6   二叉树深度         *******************" << endl;      cout << "ttt****************  8.输入7   二叉树叶子结点数   *******************" << endl;      cout << "ttt****************  9.输入8   二叉树的结点数     *******************" << endl;      cout << "ttt******************************************************************" << endl;  }  

二叉树的构造

//构造二叉树  typedef struct binode  {      //数据域      char data;      //定义左孩子和右孩子      struct binode*lchid, *rchid;  }binode, *strbinode;  

创建二叉树

//先序遍历创建二叉树  void creatbinode(strbinode&t)  {      cin >> ch;      if (ch == '#')      {          //如果输入是#的话就说明根结点就是叶子结点          //就没必要再去进行开辟一个二叉树空间          t = null;      }      else      {          t = new binode;          t->data = ch;          creatbinode(t->lchid);          creatbinode(t->rchid);      }  }  

先序遍历二叉树  

//先序遍历二叉树  void visitbinode(strbinode&t)  {      if (t!=null)      {          cout << t->data << " ";          visitbinode(t->lchid);          visitbinode(t->rchid);      }      if(t==null)      {          cout << "#" << " ";      }  }  

中序遍历二叉树

//中序遍历二叉树  void midvisitbinode(strbinode&t)  {      if (t != null)      {          visitbinode(t->lchid);          cout << t->data << " ";          visitbinode(t->rchid);      }      if (t == null)      {          cout << "#" << " ";      }  }  

后序遍历二叉树

//后序遍历二叉树  void backvisitbinode(strbinode&t)  {      if (t != null)      {          visitbinode(t->lchid);          visitbinode(t->rchid);          cout << t->data << " ";      }      if (t == null)      {          cout << "#" << " ";      }  }  

层次遍历二叉树

//二叉树的层次遍历  void levelorder(strbinode&ht)  {      strbinode t;      t = new binode;      //创建一个队列qu      queue<strbinode> qu;      //将根结点的指针压入队列      qu.push(ht);      //当队列不为空的时候就继续进行循环      while (!qu.empty())      {          //让t里面存放队列中第一个元素的值          t = qu.front();          //c++自带的队列出队的话是删除值不返回值          qu.pop();          //访问出队元素的值          cout << t->data << " ";          //当该节点左孩子不为空的时候就让左孩子入队          if (t->lchid != null)          {              qu.push(t->lchid);          }          //当该节点右孩子不为空的时候就让左孩子入队          if (t->rchid != null)          {              qu.push(t->rchid);          }      }        }  

二叉树的深度

//二叉树的深度  int deep(strbinode&t)  {      if (t == null)      {          return 0;      }      else      {          int m = deep(t->lchid);          int n = deep(t->rchid);          if (m > n)          {              return (m + 1);          }          else          {              return (n + 1);          }      }  }  

二叉树的叶子结点数

//求二叉树的叶子结点  int leaf(strbinode&t)  {      //如果是空树      if (t == null)      {          //返回0          return 0;      }      //如果是叶子结点      if (t->lchid == null && t->rchid == null)      {          //返回1          return 1;      }      return leaf(t->lchid) + leaf(t->rchid);  }  

二叉树的结点数

//求二叉树的结点数  int nodecount(strbinode&t)  {      //如果是根结点没有数据      if (t == null)      {          return 0;      }      else      {          return nodecount(t->lchid) + nodecount(t->rchid) + 1;      }  }  

整体代码

#include<iostream>  #include<queue>  using namespace std;  char ch = 0;     //构造二叉树  typedef struct binode  {      //数据域      char data;      //定义左孩子和右孩子      struct binode*lchid, *rchid;  }binode, *strbinode;     //先序遍历创建二叉树  void creatbinode(strbinode&t)  {      cin >> ch;      if (ch == '#')      {          //如果输入是#的话就说明根结点就是叶子结点          //就没必要再去进行开辟一个二叉树空间          t = null;      }      else      {          t = new binode;          t->data = ch;          creatbinode(t->lchid);          creatbinode(t->rchid);      }  }     //先序遍历二叉树  void visitbinode(strbinode&t)  {      if (t!=null)      {          cout << t->data << " ";          visitbinode(t->lchid);          visitbinode(t->rchid);      }      if(t==null)      {          cout << "#" << " ";      }  }     //中序遍历二叉树  void midvisitbinode(strbinode&t)  {      if (t != null)      {          visitbinode(t->lchid);          cout << t->data << " ";          visitbinode(t->rchid);      }      if (t == null)      {          cout << "#" << " ";      }  }     //后序遍历二叉树  void backvisitbinode(strbinode&t)  {      if (t != null)      {          visitbinode(t->lchid);          visitbinode(t->rchid);          cout << t->data << " ";      }      if (t == null)      {          cout << "#" << " ";      }  }     //二叉树的层次遍历  void levelorder(strbinode&ht)  {      strbinode t;      t = new binode;      //创建一个队列qu      queue<strbinode> qu;      //将根结点的指针压入队列      qu.push(ht);      //当队列不为空的时候就继续进行循环      while (!qu.empty())      {          //让t里面存放队列中第一个元素的值          t = qu.front();          //c++自带的队列出队的话是删除值不返回值          qu.pop();          //访问出队元素的值          cout << t->data << " ";          //当该节点左孩子不为空的时候就让左孩子入队          if (t->lchid != null)          {              qu.push(t->lchid);          }          //当该节点右孩子不为空的时候就让左孩子入队          if (t->rchid != null)          {              qu.push(t->rchid);          }      }        }     //二叉树的深度  int deep(strbinode&t)  {      if (t == null)      {          return 0;      }      else      {          int m = deep(t->lchid);          int n = deep(t->rchid);          if (m > n)          {              return (m + 1);          }          else          {              return (n + 1);          }      }  }     //求二叉树的叶子结点  int leaf(strbinode&t)  {      //如果是空树      if (t == null)      {          //返回0          return 0;      }      //如果是叶子结点      if (t->lchid == null && t->rchid == null)      {          //返回1          return 1;      }      return leaf(t->lchid) + leaf(t->rchid);  }     //求二叉树的结点数  int nodecount(strbinode&t)  {      //如果是根结点没有数据      if (t == null)      {          return 0;      }      else      {          return nodecount(t->lchid) + nodecount(t->rchid) + 1;      }  }     //菜单  void menu()  {      cout << "ttt******************************************************************" << endl;      cout << "ttt****************  1.输入-1  退出程序           *******************" << endl;      cout << "ttt****************  2.输入1   初始化二叉树       *******************" << endl;      cout << "ttt****************  3.输入2   对二叉树先序遍历   *******************" << endl;      cout << "ttt****************  4.输入3   对二叉树中序遍历   *******************" << endl;      cout << "ttt****************  5.输入4   对二叉树后序遍历   *******************" << endl;      cout << "ttt****************  6.输入5   对二叉树层次遍历   *******************" << endl;      cout << "ttt****************  7.输入6   二叉树深度         *******************" << endl;      cout << "ttt****************  8.输入7   二叉树叶子结点数   *******************" << endl;      cout << "ttt****************  9.输入8   二叉树的结点数     *******************" << endl;      cout << "ttt******************************************************************" << endl;  }     int main()  {      int n = 0;      strbinode t;      menu();      while (cin >> n)      {          if (n < 0)          {              break;          }          switch (n)          {          case 1:              //初始化二叉树              cout << "请输入值对二叉树进行初始化" << endl;              creatbinode(t);              cout << "初始化完成" << endl;              break;          case 2:              //先序遍历              cout << "先序遍历的结果为" << endl;              visitbinode(t);              cout << "先序遍历结束" << endl;              break;          case 3:              //中序遍历              cout << "中序遍历的结果为" << endl;              midvisitbinode(t);              cout << "中序遍历结束" << endl;              break;          case 4:              //后序遍历              cout << "后序遍历的结果为" << endl;              backvisitbinode(t);              cout << "后序遍历结束" << endl;              break;          case 5:              //层次遍历              cout << "层次遍历的结果为" << endl;              levelorder(t);              cout << "层次遍历结束" << endl;              break;          case 6:              cout << "二叉树的深度为:";              cout << deep(t) << endl;              break;          case 7:              cout << "二叉树的叶子结点数为:";              cout << leaf(t) << endl;              break;          case 8:              cout << "二叉树的结点数为;";              cout << nodecount(t) << endl;              break;          default:              cout << "您的输入有误,请重新输入" << endl;              break;          }      }      return 0;  }

结果展示

纯C++代码详解二叉树相关操作

纯C++代码详解二叉树相关操作

以上就是纯c++代码详解二叉树相关操作的详细内容,更多关于c++二叉树的资料请关注<计算机技术网(www.ctvol.com)!!>其它相关文章!

需要了解更多c/c++开发分享纯C++代码详解二叉树相关操作,都可以关注C/C++技术分享栏目—计算机技术网(www.ctvol.com)!

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/c-cdevelopment/1115604.html

(0)
上一篇 2022年7月10日
下一篇 2022年7月10日

精彩推荐