《数据结构》第五章 树和二叉树 扩展二叉实现代码示例

前端之家收集整理的这篇文章主要介绍了《数据结构》第五章 树和二叉树 扩展二叉实现代码示例前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

大家好。本例是一个扩展二叉树。实现了树的构造、前序遍历、中序遍历、后序遍历,计算叶子个数等操作。请大家参考。并能举一反三,灵活掌握程序思想。


  1. #include <iostream>
  2. using namespace std;
  3.  
  4. struct BiNode //二叉树的结点结构
  5. { char data;
  6. BiNode *lchild,*rchild;
  7. };
  8.  
  9.  
  10. class BiTree
  11. {
  12. public:
  13. BiTree( ){root = Creat(root);} //构造函数,建立一棵二叉树
  14. ~BiTree( ){Release(root);} //析构函数,释放各结点的存储空间
  15. void PreOrder( ){PreOrder(root);} //前序遍历二叉树
  16. void InOrder( ){InOrder(root);} //中序遍历二叉树
  17. void PostOrder( ){PostOrder(root);} //后序遍历二叉树
  18. void CountLeaf(){CountLeaf(root);}
  19. void PrintLeafCount();
  20. private:
  21. BiNode *root; //指向根结点的头指针
  22. int LeafCount;
  23. BiNode *Creat(BiNode *bt); //构造函数调用
  24. void Release(BiNode *bt); //析构函数调用
  25. void PreOrder(BiNode *bt); //前序遍历函数调用
  26. void InOrder(BiNode *bt); //中序遍历函数调用
  27. void PostOrder(BiNode *bt); //后序遍历函数调用
  28. void CountLeaf(BiNode *bt);
  29. };
  30.  
  31. BiNode *BiTree::Creat(BiNode *bt)
  32. { char ch;
  33. cout<<"请输入创建一棵二叉树的结点数据"<<endl;
  34. cin>>ch;
  35. if (ch=='#') return NULL;
  36. else{bt = new BiNode; //生成一个结点
  37. bt->data=ch;
  38. bt->lchild = Creat(bt->lchild); //递归建立左子树
  39. bt->rchild = Creat(bt->rchild); //递归建立右子树
  40. }
  41. LeafCount=0;
  42. return bt;
  43.  
  44. }
  45.  
  46. void BiTree::Release(BiNode *bt)
  47. {
  48. if (bt != NULL)
  1. {
  2. Release(bt->lchild); //释放左子树
  3. Release(bt->rchild); //释放右子树
  4. delete bt;
  5. }
  6. }
  7.  
  8. void BiTree::CountLeaf(BiNode *bt)
  9. { if (bt != NULL)
  10. {if (bt->lchild==NULL && bt->rchild==NULL)
  11. LeafCount++;
  12. CountLeaf(bt->lchild);
  13. CountLeaf(bt->rchild); //右子树
  14. }
  15. }
  16.  
  17. void BiTree::PrintLeafCount()
  18. {
  19. cout<<LeafCount;
  20. }
  21. void BiTree::PreOrder(BiNode *bt)
  22. { if(bt==NULL) return;
  23. else
  1. { cout<<bt->data<<" ";
  2. PreOrder(bt->lchild);
  3. PreOrder(bt->rchild);
  4. }
  5. }
  6.  
  7. void BiTree::InOrder(BiNode *bt)
  8. { if (bt==NULL) return; //递归调用的结束条件
  9. else {
  10. InOrder(bt->lchild); //中序递归遍历root的左子树
  11. cout<<bt->data<<" "; //访问根结点的数据域
  12. InOrder(bt->rchild); //中序递归遍历root的右子树
  13. }
  14. }
  15.  
  16. void BiTree::PostOrder(BiNode *bt)
  17. { if (bt==NULL) return; //递归调用的结束条件
  18. else {
  19. PostOrder(bt->lchild); //后序递归遍历root的左子树
  20. PostOrder(bt->rchild); //后序递归遍历root的右子树
  21. cout<<bt->data<<" "; //访问根结点的数据域
  22. }
  23. }
  24.  
  25. void main()
  26. { BiTree T; //创建一棵树
  27. //cout<<"------前序遍历------ "<<endl;
  28. T.PreOrder( );
  29. cout<<endl;
  30. cout<<"------中序遍历------ "<<endl;
  31. T.InOrder( );
  32. cout<<endl;
  33. cout<<"------后序遍历------ "<<endl;
  34. T.PostOrder( );
  35. cout<<endl;
  36. cout<<"------叶子个数------ "<<endl;
  37. T.CountLeaf();
  38. T.PrintLeafCount();
  39. cout<<endl;
  40. }
程序运行后,可以参照课本P119页,输入一个字符序列,建立如图5-26的二叉树。将分别输出各个遍历结点序列,和叶子的个数。 叶子个数为2.

大家可以输入其它树序列进行检验。

祝大家调试程序成功。

猜你在找的数据结构相关文章