Chinaunix首页 | 论坛 | 博客
  • 博客访问: 201363
  • 博文数量: 24
  • 博客积分: 608
  • 博客等级: 中士
  • 技术积分: 371
  • 用 户 组: 普通用户
  • 注册时间: 2012-04-22 21:10
文章分类

全部博文(24)

文章存档

2012年(24)

分类: C/C++

2012-09-15 16:20:54

树的相关基础算法,先序、中序、后序递归与非递归,层次遍历,建立二叉搜索树。

点击(此处)折叠或打开

  1. #include <iostream>
  2. #include <stack>
  3. #include <queue>
  4. using namespace std;

  5. typedef struct Tree {
  6.     int val;
  7.     struct Tree *lchild, *rchild;
  8.     int flag;
  9. }BSTree;

  10. BSTree* newBSTree(int n)
  11. {
  12.     BSTree *tree = new BSTree;
  13.     tree->val = n;
  14.     tree->lchild = NULL;
  15.     tree->rchild = NULL;
  16.     tree->flag = 0;
  17.     return tree;
  18. }

  19. void createTree(BSTree *&root, int val)
  20. {
  21.     if(NULL == root)
  22.         root = newBSTree(val);
  23.     else
  24.     {
  25.         BSTree* p = root;
  26.         while(p)
  27.         {
  28.             if(val < p->val)
  29.             {
  30.                 if(p->lchild)
  31.                     p = p->lchild;
  32.                 else
  33.                 {
  34.                     p->lchild = newBSTree(val);
  35.                     return;
  36.                 }
  37.             }
  38.             else if(val > p->val)
  39.             {
  40.                 if(p->rchild)
  41.                     p = p->rchild;
  42.                 else
  43.                 {
  44.                     p->rchild = newBSTree(val);
  45.                     return;
  46.                 }
  47.             }
  48.         }
  49.         
  50.     }
  51. }

  52. /***************************************************************************/
  53. /*递归遍历系列*/
  54. void PreOrderRecursion(BSTree* root)        //先序遍历:根——左——右
  55. {
  56.     if(root == NULL)
  57.         return;
  58.     cout << root->val << endl;        
  59.     if(root->lchild)
  60.         PreOrderRecursion(root->lchild);
  61.     if(root->rchild)
  62.         PreOrderRecursion(root->rchild);
  63. }

  64. void InOrderRecursion(BSTree* root)            //中序遍历:左——根——右
  65. {
  66.     if(root == NULL)
  67.         return;
  68.     if(root->lchild)
  69.         InOrderRecursion(root->lchild);
  70.     cout << root->val << ' ';
  71.     if(root->rchild)
  72.         InOrderRecursion(root->rchild);    
  73. }

  74. void PostOrderRecursion(BSTree* root)        //后序遍历:左——右——根
  75. {
  76.     if(root == NULL)
  77.         return;
  78.     if(root->lchild)
  79.         PostOrderRecursion(root->lchild);
  80.     if(root->rchild)
  81.         PostOrderRecursion(root->rchild);
  82.     cout << root->val << ' ';
  83. }

  84. /***************************************************************************/
  85. /*非递归遍历系列*/
  86. void PreOrderNonRecursion(BSTree* root)            //先序非递归
  87. {
  88.     if(!root)
  89.         return;
  90.     
  91.     BSTree* pRoot = root;
  92.     stack<BSTree*> st;
  93.     
  94.     while(pRoot || !st.empty())
  95.     {
  96.         while(pRoot)
  97.         {
  98.             cout << pRoot->val << ' ';
  99.             st.push(pRoot);
  100.             pRoot = pRoot->lchild;
  101.         }
  102.         
  103.         if(!st.empty())
  104.         {
  105.             pRoot = st.top();
  106.             st.pop();
  107.             pRoot = pRoot->rchild;
  108.         }
  109.     }
  110. }

  111. void InOrderNonRecursion(BSTree* root)        //中序非递归
  112. {
  113.     if(!root)
  114.         return;
  115.     
  116.     BSTree* pRoot = root;
  117.     stack<BSTree*> st;
  118.     
  119.     while(pRoot || !st.empty())
  120.     {
  121.         while(pRoot)
  122.         {
  123.             st.push(pRoot);
  124.             pRoot = pRoot->lchild;
  125.         }
  126.         
  127.         if(!st.empty())
  128.         {
  129.             pRoot = st.top();
  130.             st.pop();
  131.             cout << pRoot->val << ' ';
  132.             pRoot = pRoot->rchild;
  133.         }
  134.     }
  135. }

  136. void PostOrderNonRecursion(BSTree* root)        //后序非递归
  137. {
  138.     if(!root)
  139.         return;
  140.     
  141.     BSTree* pRoot = root, *q = NULL;
  142.     stack<BSTree*> st;
  143.     
  144.     while(pRoot || !st.empty())
  145.     {
  146.         while(pRoot)
  147.         {
  148.             st.push(pRoot);
  149.             pRoot = pRoot->lchild;
  150.         }
  151.         pRoot = st.top();
  152.         if(pRoot->rchild == NULL || pRoot->rchild == q)
  153.         {
  154.             cout << pRoot->val << ' ';
  155.             q = st.top();
  156.             st.pop();
  157.             pRoot = NULL;
  158.         }
  159.         else
  160.             pRoot = pRoot->rchild;
  161.     }
  162. }

  163. void LevelReverse(BSTree* root)        //层次递归
  164. {
  165.     if(!root)
  166.         return;
  167.     
  168.     queue<BSTree*> qu;
  169.     qu.push(root);
  170.     
  171.     while(!qu.empty())
  172.     {
  173.         BSTree* pRoot = qu.front();
  174.         cout << pRoot->val << ' ';
  175.         qu.pop();
  176.         
  177.         if(pRoot->lchild)
  178.             qu.push(pRoot->lchild);
  179.         if(pRoot->rchild)
  180.             qu.push(pRoot->rchild);
  181.     }
  182. }

  183. /***************************************************************************/
  184. int main()
  185. {
  186.     int array[] = {10, 8, 12, 6, 9, 11, 13};
  187.     BSTree *root = NULL;
  188.     int i, length = sizeof array / sizeof array[0];
  189.     
  190.     for(i = 0; i < length; i++)
  191.         createTree(root, array[i]);
  192.     
  193.     LevelReverse(root);
  194.     cout << endl;
  195. }

阅读(1873) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~