二叉树的练习

Code:
  1. //--- 二叉树的链式存储结构----  
  2. /* 实现二叉树的先序,中序和后序遍历, 
  3.    并按凹入法打印。打印层次遍历序列。 
  4.    交换树的左右子树*/  
  5. #include <iostream>  
  6. #include <stdio.h>  
  7. using namespace std;  
  8.   
  9.  typedef  char TElemType;// 树的元素类型  
  10.  class BiTree{  
  11.      public:  
  12.         BiTree()  
  13.         {  
  14.            data = '/0';  
  15.            parent = NULL;  
  16.            lchild = NULL;  
  17.            rchild = NULL;  
  18.         }  
  19.         void creatBiTree();// 构造链式的二叉树 T  
  20.         void preOrderTraverse(int);  
  21.          // 先序遍历二叉树T.凹入法打印  
  22.         void inOrderTraverse();  
  23.          // 中序遍历二叉树T.  
  24.         void postOrderTraverse();  
  25.          // 后序遍历二叉树T.  
  26.         void leveOrderTraverse();  
  27.          // 层次遍历二叉树T.  
  28.         void swapChild();  
  29.          // 交换左右子树  
  30.   
  31.      private:  
  32.         TElemType data; // 结点(字符串型)  
  33.         BiTree *parent, *lchild, *rchild; // 左孩子和又孩子  
  34.  };  
  35.   
  36.  typedef  BiTree * QElemType;// 队列的元素类型  
  37.  class QueueNode  
  38. {//队列结点  
  39.     private:  
  40.         friend class LinkQueue;  
  41.         QElemType data;  
  42.         QueueNode *next;  
  43.     public:  
  44.         QueueNode(QElemType e)  
  45.         {  
  46.           data = e;  
  47.           next = NULL;  
  48.         }  
  49.         QueueNode()  
  50.         {  
  51.           data = NULL;  
  52.           next = NULL;  
  53.         }  
  54. };  
  55.   
  56. class LinkQueue  
  57. {// 对列指针  
  58.     public:  
  59.         LinkQueue();//初始化队列  
  60.         void enQueue(QElemType);//入队  
  61.         void deQueue(QElemType &);//出队  
  62.         bool emputyQueue();//判断队是否空,队空TURE,非空FALSE  
  63.     private:  
  64.         QueueNode *front; // 队头指针  
  65.         QueueNode *rear;  // 队尾指针  
  66. };  
  67.   
  68.  void BiTree:: creatBiTree()  
  69.  {     
  70.      char str[5];  
  71.      scanf("%s",str);  
  72.      BiTree *currentT = this;  
  73.      if (str[0] == '*')// 左孩子为空  
  74.        {    
  75.           currentT->parent->lchild = NULL;  
  76.           delete currentT;  
  77.        }  
  78.        else if(str[0] == '#')// 右孩子为空  
  79.        {  
  80.            currentT->parent->rchild = NULL;  
  81.            delete currentT;  
  82.        }  
  83.        else  
  84.        {  
  85.          currentT->data = str[0]; // 构造根节点  
  86.          currentT->lchild = new BiTree();  
  87.          currentT->lchild->parent = currentT;  
  88.          currentT->lchild->creatBiTree(); // 构造左子树  
  89.          currentT->rchild = new BiTree();  
  90.          currentT->rchild->parent = currentT;  
  91.          currentT->rchild->creatBiTree(); // 够造右子树  
  92.        }  
  93.       
  94.          
  95.  }  
  96.   
  97.  void BiTree:: preOrderTraverse(int i)  
  98.  {  
  99.      if (this)  
  100.      {   int j;  
  101.          for(j = i; j > 0; j--)  
  102.            cout << " ";   
  103.            cout << data << endl;    // 遍历根节点  
  104.            lchild->preOrderTraverse(i+1); //先序遍历左子树  
  105.            rchild->preOrderTraverse(i+1); //先序遍历右子树  
  106.      }  
  107.  }  
  108.   
  109.  void BiTree:: inOrderTraverse()  
  110.  {  
  111.      if (this)  
  112.      {  
  113.          lchild->inOrderTraverse(); //先序遍历左子树  
  114.          cout << data;    // 遍历根节点  
  115.          rchild->inOrderTraverse(); //先序遍历右子树  
  116.      }  
  117.  }  
  118.   
  119.  void BiTree:: postOrderTraverse()  
  120.  {  
  121.      if (this)  
  122.      {  
  123.          lchild->postOrderTraverse(); //先序遍历左子树  
  124.          rchild->postOrderTraverse(); //先序遍历右子树  
  125.          cout << data;    // 遍历根节点  
  126.      }  
  127.  }  
  128.           
  129. void BiTree:: leveOrderTraverse()  
  130.  {  
  131.     LinkQueue *p = new LinkQueue();  
  132.     QElemType a = new BiTree();  
  133.   
  134.     if (this)  
  135.     {  
  136.         p->enQueue(this);  
  137.         while(!p->emputyQueue())  
  138.         {  
  139.           p->deQueue(a);  
  140.           cout << a->data;  
  141.           if(a->lchild!=NULL)  
  142.           p->enQueue(a->lchild);  
  143.           if(a->rchild!=NULL)  
  144.           p->enQueue(a->rchild);  
  145.         }  
  146.         cout << "/n";  
  147.     }  
  148.  }  
  149.   
  150.  LinkQueue:: LinkQueue()  
  151.  {  
  152.      QueueNode *p = new QueueNode();//创建一个内容为空的结点  
  153.      front = rear = p; //空队列  
  154.  }  
  155.   
  156.  void LinkQueue:: enQueue(QElemType e)  
  157.  {  
  158.      QueueNode *p = new QueueNode(e); //申请结点  
  159.      rear->next = p; //加入新结点  
  160.      rear = p; //队尾指针指向最后结点  
  161.  }  
  162.    
  163.  void LinkQueue:: deQueue(QElemType &e)  
  164.  {  
  165.      QueueNode *p = new QueueNode();  
  166.      if(!this->emputyQueue())//队非空才允许出队  
  167.          p = front->next;  
  168.          e = p->data;  
  169.          front->next = p->next;  
  170.          if(rear == p)rear = front;  
  171.          delete [] p;  
  172.  }  
  173.   
  174.  bool LinkQueue:: emputyQueue()  
  175.  {  
  176.      if(rear == front)//队空  
  177.          return true;                                                        
  178.      else return false;  
  179.  }  
  180.   
  181.   
  182.  void main()  
  183.  {  
  184.      BiTree T = BiTree();         
  185.   
  186.      T.creatBiTree();  
  187.      cout << "先序遍历(凹入法表示)/n";  
  188.      T.preOrderTraverse(0);  
  189.      cout << "/n中序遍历:/n";  
  190.      T.inOrderTraverse();  
  191.      cout << "/n后序遍历:/n";  
  192.      T.postOrderTraverse();  
  193.      cout << "/n层次遍历:/n";  
  194.      T.leveOrderTraverse();  
  195.  }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值