Chinaunix首页 | 论坛 | 博客
  • 博客访问: 519553
  • 博文数量: 184
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 1172
  • 用 户 组: 普通用户
  • 注册时间: 2016-06-21 13:40
个人简介

技术改变命运

文章分类

全部博文(184)

文章存档

2020年(16)

2017年(12)

2016年(156)

我的朋友

分类: C/C++

2016-07-04 21:12:44

树的一些基本操作(遍历,构造,高度,节点数,销毁)
c语言代码如下:

点击(此处)折叠或打开

  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<unistd.h>
  4. struct BinaryTree
  5. {
  6.     struct BinaryTree *pLeft,*pRight;
  7.     int p_value;
  8. };
  9. void createTree(struct BinaryTree **pRoot)
  10. {//这里是双重指针,因为*p会变
  11.     struct BinaryTree *root = *pRoot;
  12.     int val = 0 ;
  13.     scanf("%d",&val);
  14.     if (val == -1)
  15.         root = NULL;
  16.     else
  17.     {
  18.         *pRoot = (struct BinaryTree*)malloc(sizeof(struct BinaryTree));
  19.         (*pRoot)->p_value = val;
  20.         createTree(&(*pRoot)->pLeft);
  21.         createTree(&(*pRoot)->pRight);
  22.     }
  23. }
  24. void preOrder(struct BinaryTree *pRoot)
  25. {
  26.     //printf("fffffff\n");
  27.     if (pRoot != NULL)
  28.     {
  29.         printf("%d ",pRoot->p_value);
  30.         preOrder(pRoot->pLeft);
  31.         preOrder(pRoot->pRight);
  32.       //  printf("\n");
  33.     }
  34. }
  35. void inOrder(struct BinaryTree *pRoot)
  36. {
  37.     if (pRoot != NULL)
  38.     {
  39.         inOrder(pRoot->pLeft);
  40.         printf("%d ",pRoot->p_value);    
  41.         inOrder(pRoot->pRight);
  42.         //printf("\n");
  43.     }
  44. }
  45. void postOrder(struct BinaryTree *pRoot)
  46. {
  47.     if (pRoot != NULL)
  48.     {
  49.         postOrder(pRoot->pLeft);
  50.         postOrder(pRoot->pRight);
  51.         printf("%d ",pRoot->p_value);
  52.         //printf("\n");
  53.     }
  54. }
  55. int sumofNode(struct BinaryTree *pRoot)
  56. {
  57.     if (pRoot == NULL)
  58.         return 0;
  59.     else
  60.     {
  61.         return sumofNode(pRoot->pLeft)+sumofNode(pRoot->pRight)+1;
  62.     }
  63. }
  64. int heightofTree(struct BinaryTree *pRoot)
  65. {
  66.     if (pRoot == NULL)
  67.     {
  68.         return 0;
  69.     }
  70.     else
  71.     {
  72.         return (heightofTree(pRoot->pLeft)>heightofTree(pRoot->pRight)?heightofTree(pRoot->pLeft):heightofTree(pRoot->pRight))+1;
  73.     }
  74. }
  75. void destroy(struct BinaryTree *pRoot)
  76. {
  77.     if (pRoot)
  78.     {
  79.             destroy(pRoot->pLeft);
  80.             destroy(pRoot->pRight);
  81.             free(pRoot);
  82.             pRoot = NULL;
  83.     }
  84. }

  85. int main()
  86. {
  87.      struct BinaryTree * rootNode = NULL;
  88.     int choiced = 0;
  89.     while(true)//很重要的哦!!!!!!!!!!!!!!
  90.           {
  91.         system("clear"); //清屏
  92.         printf("\n\n\n ---主界面---\n\n\n");
  93.         printf(" 1、创建二叉树 2、先序遍历二叉树\n");
  94.         printf(" 3、中序遍历二叉树 4、后序遍历二叉树\n");
  95.         printf(" 5、统计结点总数 6、查看树深度 \n");
  96.         printf(" 7、消毁二叉树 0、退出\n\n");
  97.         printf(" 请选择操作:");
  98.         scanf("%d",&choiced);
  99.         if(choiced == 0)
  100.             return 0;
  101.         else if(choiced == 1)
  102.                {
  103.             system("clear");
  104.             printf("请输入每个结点,回车确认,并以-1结束:\n");
  105.             createTree(&rootNode );
  106.                      }
  107.         else if(choiced == 2)
  108.                      {
  109.             system("clear");
  110.             printf("先序遍历二叉树结果:\n");
  111.             preOrder(rootNode);
  112.             printf("\n");
  113.           // system("pause"); //暂停屏幕
  114.                      }
  115.         else if(choiced == 3)
  116.                {
  117.             system("clear");
  118.             printf("中序遍历二叉树结果:\n");
  119.             inOrder(rootNode);
  120.             printf("\n");
  121.         // system("pause");
  122.                       }
  123.         else if(choiced == 4)
  124.                      {
  125.             system("clear");
  126.             printf("后序遍历二叉树结果:\n");
  127.             postOrder(rootNode);
  128.             printf("\n");
  129.            // system("pause");
  130.                       }
  131.         else if(choiced == 5)
  132.                 {
  133.             system("clear");
  134.             int count = sumofNode(rootNode);
  135.             printf("二叉树中结点总数为%d",count);
  136.             //system("pause");
  137.                 }
  138.         else if(choiced == 6)
  139.                      {
  140.             system("clear");
  141.             int dep = heightofTree(rootNode);
  142.             printf("此二叉树的深度为%d",dep);
  143.         // system("pause");
  144.                      }
  145.         else if(choiced == 7)
  146.                      {
  147.             system("clear");
  148.             printf("二叉树已被消毁!\n");
  149.             destroy(rootNode);
  150.             printf("\n");
  151.       // system("pause");
  152.                     }
  153.         else
  154.                      {
  155.             system("clear");
  156.             printf("\n\n\n\n\n\t错误选择!\n");
  157.                      }
  158.          
  159.     }
  160.     return 0;
  161. }
编译后操作(centos 5.5)(以下是部分结果)
请输入每个结点,回车确认,并以-1结束:
1 2 -1 -1 3 -1 -1
先序遍历二叉树结果:
1 2 3 
c++代码如下:

点击(此处)折叠或打开

  1. #include<iostream>
  2. using namespace std;
  3. template<class T>
  4. struct BinaryTree
  5. {
  6.     T val;
  7.     BinaryTree<T> *left,*right;
  8. };
  9. template<class T>
  10. void createTree(BinaryTree<T> * &pRoot)//先序构造二叉树
  11. {
  12.     BinaryTree<T> *root = pRoot;
  13.     T val;
  14.     cin >> val;
  15.     if (val == -1)
  16.     {
  17.         root = NULL;
  18.     }
  19.     else
  20.     {
  21.         //BinaryTree<T> *pNod
  22.         pRoot = new BinaryTree<T>();
  23.         pRoot->val = val;
  24.         //pNode->left = NULL;
  25.         //pNode->right = NULL;
  26.         createTree(pRoot->left);
  27.         createTree(pRoot->right);
  28.     }

  29. }
  30. template<class T>
  31. void preOrder(BinaryTree<T> *&pRoot)
  32. {
  33.     if (pRoot)
  34.     {
  35.         cout << pRoot->val<< " ";
  36.         preOrder(pRoot->left);
  37.         preOrder(pRoot->right);
  38.     }
  39. }
  40. template<class T>
  41. void inOrder(BinaryTree<T> *&pRoot)
  42. {
  43.     if (pRoot)
  44.     {
  45.         inOrder(pRoot->left);
  46.         cout << pRoot->val<< " ";
  47.         inOrder(pRoot->right);
  48.     }
  49. }
  50. template<class T>
  51. void postOrder(BinaryTree<T> *&pRoot)
  52. {
  53.     if (pRoot)
  54.     {
  55.         postOrder(pRoot->left);
  56.         postOrder(pRoot->right);
  57.         cout << pRoot->val<< " ";
  58.     }
  59. }
  60. //树中节点总数
  61. template<class T>
  62. int countNode(BinaryTree<T> *&pRoot)
  63. {
  64.     if (pRoot == NULL)
  65.         return 0;
  66.     else{
  67.         return 1+countNode(pRoot->left)+countNode(pRoot->right);
  68.     }    
  69. }
  70. //树的深度
  71. template<class T>
  72. int depthOfTree(BinaryTree<T> *&pRoot)
  73. {
  74.     if (pRoot == NULL)
  75.         return 0;
  76.     else
  77.     {
  78.         return (depthOfTree(pRoot->left)>depthOfTree(pRoot->right)?depthOfTree(pRoot->left):depthOfTree(pRoot->right))+1;
  79.     }
  80. }
  81. //销毁二叉树
  82. template<class T>
  83. void destroy(BinaryTree<T> *pRoot)
  84. {
  85.     if (pRoot)
  86.     {
  87.         destroy(pRoot->left);
  88.         destroy(pRoot->right);
  89.         delete pRoot;
  90.         pRoot = NULL;
  91.     }
  92. }
  93. int main ()
  94. {
  95.     BinaryTree<int> * rootNode = NULL;
  96.     int choiced = 0;
  97.     while(true)//很重要的哦!!!!!!!!!!!!!!
  98.           {
  99.         system("clear"); //清屏
  100.         cout<<"\n\n\n ---主界面---\n\n\n";
  101.         cout<<" 1、创建二叉树 2、先序遍历二叉树\n";
  102.         cout<<" 3、中序遍历二叉树 4、后序遍历二叉树\n";
  103.         cout<<" 5、统计结点总数 6、查看树深度 \n";
  104.         cout<<" 7、消毁二叉树 0、退出\n\n";
  105.         cout<<" 请选择操作:";
  106.         cin>>choiced;
  107.         if(choiced == 0)
  108.             return 0;
  109.         else if(choiced == 1)
  110.         {
  111.             system("clear");
  112.             cout<<"请输入每个结点,回车确认,并以-1结束:\n";
  113.             createTree(rootNode );
  114.         }
  115.         else if(choiced == 2)
  116.         {
  117.             system("clear");
  118.             cout<<"先序遍历二叉树结果:\n";
  119.             preOrder(rootNode);
  120.             cout<<endl;
  121.           // system("pause"); //暂停屏幕
  122.         }
  123.         else if(choiced == 3)
  124.         {
  125.             system("clear");
  126.             cout<<"中序遍历二叉树结果:\n";
  127.             inOrder(rootNode);
  128.             cout<<endl;
  129.         // system("pause");
  130.         }
  131.         else if(choiced == 4)
  132.         {
  133.             system("clear");
  134.             cout<<"后序遍历二叉树结果:\n";
  135.             postOrder(rootNode);
  136.             cout<<endl;
  137.            // system("pause");
  138.         }
  139.         else if(choiced == 5)
  140.         {
  141.             system("clear");
  142.             int count = countNode(rootNode);
  143.             cout<<"二叉树中结点总数为"<<count<<endl;
  144.             system("pause");
  145.         }
  146.         else if(choiced == 6)
  147.         {
  148.             system("clear");
  149.             int dep = depthOfTree(rootNode);
  150.             cout<<"此二叉树的深度为"<<dep<<endl;
  151.         // system("pause");
  152.         }
  153.         else if(choiced == 7)
  154.         {
  155.             system("clear");
  156.             cout<<"二叉树已被消毁!\n";
  157.             destroy(rootNode);
  158.             cout<<endl;
  159.       // system("pause");
  160.         }
  161.         else
  162.         {
  163.             system("clear");
  164.             cout<<"\n\n\n\n\n\t错误选择!\n";
  165.         }
  166.          
  167.     }
  168. }





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