二叉链表

本文详细介绍了一种通用的二叉树数据结构及其遍历算法,包括前序、中序、后序和层序遍历的递归与非递归实现,并提供了完整的类定义和成员函数说明。
二叉链表类的声明
template <class T>
struct BiNode   //二叉树的结点结构
{
 T data;      
 BiNode<T> *lchild, *rchild;
};
template <class T>
struct element
{
   BiNode<T> *ptr;
   int flag;
};
template <class T>
class BiTree
{
public:
    BiTree( );             //构造函数,初始化一棵二叉树,其前序序列由键盘输入
    ~BiTree(void);         //析构函数,释放二叉链表中各结点的存储空间
     BiNode<T>* Getroot(); //获得指向根结点的指针
    void PreOrder(BiNode<T> *root);     //前序遍历二叉树
    void InOrder(BiNode<T> *root);      //中序遍历二叉树
    void PostOrder(BiNode<T> *root);    //后序遍历二叉树
    void LeverOrder(BiNode<T> *root);   //层序遍历二叉树
private:
    BiNode<T> *root;         //指向根结点的头指针
    BiNode<T> *Creat();     //有参构造函数调用
    void Release(BiNode<T> *root);   //析构函数调用
};
构造函数算法:
template<class T>
BiTree<T>::BiTree()
{
     this->root = Creat();
}
template<class T>
BiNode<T>* BiTree<T>::Getroot( )
{
     return root;
}
template <class T>
BiNode<T>* BiTree<T>::Creat( )
{
     BiNode<T>* root;
     T ch;
     cout<<"请输入创建一棵二叉树的结点数据"<<endl;
     cin >> ch;
    if (ch=="#") root = NULL;
    else{
          root = new BiNode<T>;       //生成一个结点
         root->data=ch;
         root->lchild = Creat();    //递归建立左子树
         root->rchild = Creat();    //递归建立右子树
    }
    return root;
}
析构函数算法:

template<class T>
BiTree<T>::~BiTree(void)
{
     Release(root);
}
template<class T>
void BiTree<T>::Release(BiNode<T>* root)
{
 if (root != NULL){                 
      Release(root->lchild);   //释放左子树
      Release(root->rchild);   //释放右子树
      delete root;
 } 
}
 
前序遍历
(1)       访问根结点;
(2)       前序遍历根结点的左子树;
(3)       前序遍历根结点的右子树;
递归算法:
 
template <class T>
void BiTree<T>::PreOrder(BiNode<T> *root)
{
   if (root == NULL) return;
   else {
         cout << root->data << ' ';
          PreOrder(root->lchild);
          PreOrder(root->rchild);
   }
}
非递归算法:
template <class T>
void BiTree<T>::PreOrder(BiNode<T> *root)
{
   int top = -1;
   const int MaxSize = 100;
   BiNode<T> *s[MaxSize];
   while (root != NULL || top != -1)
   {
      while (root != NULL)
      {
         cout << root->data << " ";
         s[++top] = root;
         root = root->lchild;
      }
      if (top != -1)
      {
          root = s[top--];
          root = root->rchild;
      }
   }
}
中序遍历:
(1)       中序遍历根结点的左子树
(2)       访问根结点;
(3)       中序遍历根结点的右子树
 递归算法
template <class T>
void BiTree<T>::InOrder(BiNode<T> * root)
{
   if (root == NULL) return;
   else {
        InOrder(root->lchild);
         cout << root->data << " ";
         InOrder(root->rchild);
   }
}
非递归算法:
template <class T>
void BiTree<T>::InOrder(BiNode<T> *root)
{
   int top = -1;
   const int MaxSize = 100;
   BiNode<T> *s[MaxSize];
   while (root != NULL || top != -1)
   {
        while (root != NULL){
            s[++top] = root;
            root = root->lchild;
        }
        if (top != -1)
        {
           root = s[top--];
          cout << root->data << " ";
          root = root->rchild;
        }
   }
}
后序遍历:
1)后序遍历根结点的左子树;
2)后序遍历根结点的右子树;
3)访问根结点;
递归算法:
template <class T>
void BiTree<T>::PostOrder(BiNode<T> *root)
{
   if (root == NULL) return;
   else
   {
      PostOrder(root->lchild);
      PostOrder(root->rchild);
      cout << root->data << " ";
   }
}
非递归算法:
Flag=1 第一次出栈,只遍历完左子树,该结点不能被访问
Flag=2 第二次出栈,遍历完右子树,该结点可以被访问
template <class T>
struct BiNode   //二叉树的结点结构
{
 T data;      
 BiNode<T> *lchild, *rchild;
};
template <class T>
void BiTree<T>::PostOrder(BiNode<T> *root)
{
   int top = -1;
   const int MaxSize = 100;
   element<T> s[MaxSize];
   while (root != NULL || top != -1)
   {
      while (root != NULL)
      {
          top ++;
          s[top].ptr = root;
          s[top].flag = 1;
          root = root->lchild;
      }
      while (top != -1 && s[top].flag == 2)
      {
          root = s[top--].ptr;       
          cout << root->data << " ";
      }
      if (top != -1)
      {
          s[top].flag = 2;
          root = s[top].ptr;
          root = root->rchild;
      } 
      else return;
      
   }
}
层序遍历:
用队列来实现:
template <class T>
void BiTree<T>::LeverOrder(BiNode<T> *root)
{
   const int MaxSize = 100;
   int front = 0;
   int rear = 0;   
   BiNode<T>* Q[MaxSize];
   BiNode<T>* q;
   if (root == NULL) return;
    else{
         Q[rear++] = root;
         while (front != rear)
         {
              q = Q[front++];
              cout<< q->data << " ";     
         if (q->lchild != NULL)    Q[rear++] = q->lchild;       
              if (q->rchild != NULL)    Q[rear++] = q->rchild;
         }
     }
}
 
 
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值