zoukankan      html  css  js  c++  java
  • 二叉树的前中后层次遍历

    代码如下:

    首先是层次遍历需要用到的队列cirqueue.h的头文件

     1 #pragma once
     2 #include <iostream>
     3 template<class T>
     4 class Queue {
     5 // FIFO 对象
     6 public :
     7 Queue(int MaxQueueSize = 10);
     8 ~Queue() {delete [] queue;}
     9 bool IsEmpty() const {return front == rear;}
    10 bool IsFull() const {return (
    11 ((rear + 1) % MaxSize == front) ? 1 : 0);}
    12 T First() const; //返回队首元素
    13 T Last() const; // 返回队尾元素
    14 Queue<T>& Add(const T& x);
    15 Queue<T>& Delete(T& x);
    16 int front; //与第一个元素在反时针方向上相差一个位置
    17 int rear; // 指向最后一个元素
    18 int MaxSize; // 队列数组的大小
    19 T *queue; // 数组
    20 } ;
    21 
    22 template<class T>
    23 Queue<T>::Queue(int MaxQueueSize)
    24 {// 创建一个容量为 M a x Q u e u e S i z e的空队列
    25 MaxSize = MaxQueueSize + 1;
    26 queue = new T[MaxSize];
    27 front = rear = 0;
    28 }
    29 template<class T>
    30 T Queue<T>::First() const
    31 {// 返回队列的第一个元素
    32 // 如果队列为空,则引发异常O u t O f B o u n d s
    33 if (IsEmpty()) throw "OutOfBounds()";
    34 return queue[(front + 1) % MaxSize];
    35 }
    36 template<class T>
    37 T Queue<T>::Last() const
    38 {// 返回队列的最后一个元素
    39 // 如果队列为空,则引发异常O u t O f B o u n d s
    40 if (IsEmpty()) throw "OutOfBounds()";
    41 return queue[rear];
    42 }
    43 template<class T>
    44 Queue<T>& Queue<T>::Add(const T& x)
    45 {// 把 x 添加到队列的尾部
    46 // 如果队列满,则引发异常 NoMem 
    47 if (IsFull()) throw "NoMem()";
    48 rear = (rear + 1) % MaxSize;
    49 queue[rear] = x;
    50 return *this;
    51 }
    52 template<class T>
    53 Queue<T>& Queue<T>::Delete(T& x)
    54 {// 删除第一个元素,并将其送入 x
    55 // 如果队列为空,则引发异常 O u t O f B o u n d s
    56 if (IsEmpty()) throw "OutOfBounds()";
    57 front = (front + 1) % MaxSize;
    58 x = queue[front];
    59 return *this;
    60 }

    2.接下来是二叉树前中后以及层次遍历的主要代码 bitree.h的头文件

      1 #include "cirqueue.h"
      2 #pragma once
      3 #include <iostream>
      4 using namespace std;
      5 // 二叉链表的节点
      6 template<class T>
      7 struct BiNode
      8 {
      9     T data;    // 数据域
     10     BiNode<T>*lchild, *rchild;    // 左右指针域
     11 };
     12 // 二叉链表类实现
     13 template<class T>
     14 class BiTree
     15 {
     16 public:
     17     BiTree() { root = Creat(root); }        // 构造函数,建立一颗二叉树
     18     ~BiTree() { Release(root); }            // 析构函数,释放各节点的存储空间
     19     void PreOrder() { PreOrder(root); }      // 递归前序遍历二叉树
     20     void InOrder() { InOrder(root); }        // 递归中序遍历二叉树
     21     void PostOrder() { PostOrder(root); }    // 递归后序遍历二叉树
     22     void LeverOrder();                       // 层序遍历二叉树
     23 private:
     24     BiNode<T>* root;                        // 指向根节点的头节点
     25     BiNode<T>* Creat(BiNode<T>* bt);        // 构造函数调用
     26     void Release(BiNode<T>* bt);            // 析构函数调用
     27     void PreOrder(BiNode<T>* bt);           // 前序遍历函数调用
     28     void InOrder(BiNode<T>* bt);            // 中序遍历函数调用
     29     void PostOrder(BiNode<T>* bt);          // 后序遍历函数调用
     30 };
     31 
     32 template<class T>
     33 inline void BiTree<T>::LeverOrder()
     34 {
     35     Queue<BiNode<T>*> Q;    // 定义一个队列
     36     Q.front=Q.rear=-1;    // 顺序队列
     37     if (root == NULL)
     38         return;
     39     Q.queue[++Q.rear]=root;// 根指针入队 
     40     while (Q.front != Q.rear)
     41     {
     42         BiNode<T>* q = Q.queue[++Q.front];    // 出队
     43         cout<<q->data;
     44         if (q->lchild != NULL)
     45             Q.queue[++Q.rear] = q->lchild;    // 左孩子入队
     46         if (q->rchild != NULL)
     47             Q.queue[++Q.rear] = q->rchild;    // 右孩子入队
     48     }
     49     
     50 }
     51 
     52 template<class T>
     53 inline BiNode<T>* BiTree<T>::Creat(BiNode<T>* bt)
     54 {
     55     T ch;
     56     cin>>ch;// 输入结点的数据信息,假设为字符
     57     if(ch=='#')
     58        bt=NULL;
     59     else
     60     {
     61         bt=new BiNode<T>;// 生成一个结点,数据域为ch
     62         bt->data = ch;
     63         bt->lchild=Creat(bt->lchild);// 递归建立左子树
     64         bt->rchild=Creat(bt->rchild);// 递归建立右子树
     65     }
     66     return bt;
     67 }
     68 
     69 template<class T>
     70 inline void BiTree<T>::Release(BiNode<T>* bt)
     71 {
     72     if (bt != NULL)
     73     {
     74         Release(bt->lchild);// 释放左子树
     75         Release(bt->rchild);// 释放右子树
     76         delete bt;// 释放根节点
     77     }
     78 }
     79 
     80 template<class T>
     81 inline void BiTree<T>::PreOrder(BiNode<T>* bt)
     82 {
     83     if (bt == NULL)// 递归调用的结束条件
     84         return;
     85     cout << bt->data;// 访问根节点bt的数据域
     86     PreOrder(bt->lchild);// 前序递归遍历bt的左子树
     87     PreOrder(bt->rchild);// 前序递归遍历bt的右子树
     88 }
     89 
     90 template<class T>
     91 inline void BiTree<T>::InOrder(BiNode<T>* bt)
     92 {
     93     if (bt == NULL)
     94         return;
     95     InOrder(bt->lchild);
     96     cout << bt->data;
     97     InOrder(bt->rchild);
     98 }
     99
    100 template<class T>
    101 inline void BiTree<T>::PostOrder(BiNode<T>* bt)
    102 {
    103     if (bt == NULL)
    104         return;
    105     PostOrder(bt->lchild);
    106     PostOrder(bt->rchild);
    107     cout << bt->data;
    108 }

    3.main.cpp文件

     1 #include"bitree.h"
     2 using namespace std;
     3 
     4 int main()
     5 {
     6     BiTree<char>* bitree=new BiTree<char>();        // 创建一棵二叉树
     7     bitree->PreOrder();                             // 前序遍历
     8     cout << endl;
     9     bitree->InOrder();                              // 中序遍历
    10     cout << endl;
    11     bitree->PostOrder();                            // 后序遍历
    12     cout << endl;
    13     bitree->LeverOrder();                           // 层序遍历
    14     delete bitree;
    15 
    16     system("pause");
    17     return 0;
    18 }
  • 相关阅读:
    【SpringBoot学习笔记】无法解析parent POM——1.5.3.RELEASE
    【WPF学习日记——[DevExpress]】GridControl 行中使用按钮
    【Web学习日记】——C#引用WebService,从配置文件改变引用地址
    【Web学习日记】——在IIS上发布一个WebService
    【WPF学习日记】——Window的DataContext绑定ViewModel
    使用kubectl管理k8s集群
    使用Kubeadm创建k8s集群之节点部署
    使用Kubeadm创建k8s集群之部署规划
    kubernetes运维
    linux常用命令大全
  • 原文地址:https://www.cnblogs.com/kekexxr/p/10364137.html
Copyright © 2011-2022 走看看