2006060513055926291.cpp
上传用户:beiled
上传日期:2013-07-20
资源大小:2k
文件大小:3k
源码类别:

DNA

开发平台:

Visual C++

  1. #include<iostream.h>
  2. #include<stdlib.h>
  3. #define max 100
  4. typedef int BinTreeNode[max+1];
  5. class BinTree
  6. {
  7. BinTreeNode BT;
  8. public:
  9. void InitBT(BinTreeNode BT);
  10. void InsertBT(BinTreeNode BT);
  11.     void createBT(BinTreeNode BT);
  12. void Inorder(BinTreeNode BT,int i);
  13. void Preorder(BinTreeNode BT,int i);
  14. void Lastorder(BinTreeNode BT,int i);
  15. void Levelorder(BinTreeNode BT,int i);
  16. void menu();
  17. };
  18. void BinTree::Levelorder(BinTreeNode BT,int i)
  19. {
  20. for(i=1;i<=max;i++)
  21. if(BT[i])
  22. cout<<BT[i]<<"   ";
  23. }
  24. void BinTree::Lastorder(BinTreeNode BT,int i)
  25. {
  26. if(i>max||!BT[i])
  27. return;
  28. Lastorder(BT,2*i);
  29. Lastorder(BT,2*i+1);
  30. cout<<BT[i]<<"   ";
  31. }
  32. void BinTree::Preorder(BinTreeNode BT,int i)
  33. {
  34. if(i>max||!BT[i])
  35. return;
  36. cout<<BT[i]<<"   ";
  37. Preorder(BT,2*i);
  38. Preorder(BT,2*i+1);
  39. }
  40. void BinTree::Inorder(BinTreeNode BT,int i)
  41. {
  42. if(i>max||!BT[i])
  43. return;
  44. Inorder(BT,2*i);
  45. cout<<BT[i]<<"    ";
  46. Inorder(BT,2*i+1);
  47. }
  48. void BinTree::createBT(BinTreeNode BT)
  49. {
  50. int n;
  51. cout<<"please input the length BinaryTree:"<<endl;
  52. cin>>n;
  53. BT[0]=1;
  54. for(int i=1;i<=n;i++)
  55. InsertBT(BT);
  56. }
  57. void BinTree::InitBT(BinTreeNode BT)
  58. {
  59. for(int i=1;i<=max;i++)
  60. BT[i]=0;
  61. }
  62. void BinTree::InsertBT(BinTreeNode BT)
  63. {
  64. int k,done=1,e;
  65. while(done==1)
  66. {
  67. cout<<"input the number and data you want to insert  :"<<endl;
  68. cin>>k>>e;
  69. if(k<=max&&BT[k/2])
  70. {
  71. BT[k]=e;
  72. done=0;
  73. }
  74. else
  75. cout<<"you put the wrong number .please put it again:"<<endl;
  76. }
  77. }
  78. void BinTree::menu()
  79. {
  80. cout<<"  ********************************************************************"<<endl;
  81. cout<<"  &                欢迎进入二叉树遍历演示系统                        &"<<endl;
  82.     cout<<"  &                                                                  &"<<endl;
  83. cout<<"  &         1.创建二叉树                   2、前序遍历二叉树         &"<<endl;
  84. cout<<"  &         3.中序遍历二叉树        4.后序遍历二叉树     &"<<endl;
  85. cout<<"  &         5.层序遍历二叉树        6.退出          &"<<endl;
  86. cout<<"  &                                                                  &"<<endl;
  87. cout<<"  &  1-6键选择操作:                       &"<<endl;
  88. cout<<"  &                                                                  &"<<endl;
  89. cout<<"  ********************************************************************"<<endl;
  90. }
  91.  
  92. void main()
  93. {
  94. int i=1;
  95. BinTreeNode bt;
  96. BinTree binarytree;
  97. binarytree.menu();
  98. binarytree.InitBT(bt);
  99. int n;
  100. cin>>n;
  101. while(n!=6)
  102. {
  103. switch(n)
  104. {
  105. case 1:
  106. {
  107. binarytree.createBT(bt);
  108. cout<<"您输入的顺序为:";
  109. binarytree.Levelorder(bt,i);
  110. }break;
  111. case 2:
  112. {
  113. cout<<"您输入的顺序为:";
  114. binarytree.Levelorder(bt,i);
  115. cout<<"前序遍历顺序为:";
  116. binarytree.Preorder(bt,i);
  117. }break;
  118. case 3:
  119. {
  120. cout<<"您输入的顺序为:";
  121. binarytree.Levelorder(bt,i);
  122. cout<<"中序遍历顺序为:";
  123. binarytree.Inorder(bt,i);
  124. }break;
  125. case 4:
  126. {
  127. cout<<"您输入的顺序为:";
  128. binarytree.Levelorder(bt,i);
  129. cout<<"后序遍历顺序为:";
  130. binarytree.Lastorder(bt,i);
  131. }break;
  132. case 5:
  133. {
  134. cout<<"您输入的顺序为:";
  135. binarytree.Levelorder(bt,i);
  136. cout<<"层次遍历顺序为:";
  137. binarytree.Levelorder(bt,i);
  138. }break;
  139. default:cout<<"输入错误!请重新输入:"<<endl;
  140. break;
  141. }
  142. cout<<endl;
  143. binarytree.menu();
  144. cin>>n;
  145. }
  146. }