一、先序遍历序列创建二叉树

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. typedef struct node//二叉树的结构
  4. {
  5. char data;
  6. struct node *lchild,*rchild;
  7. }Node, *Tree;
  8. void PreCreateTree(Tree &t)//先序遍历序列创建二叉树
  9. {
  10. char ch;
  11. cin>>ch;
  12. if(ch == '@')//@为特殊字符,表示空
  13. t=NULL;
  14. else
  15. {
  16. t=new node;
  17. t->data=ch;
  18. PreCreateTree(t->lchild);
  19. PreCreateTree(t->rchild);
  20. }
  21. }
  22. int main()
  23. {
  24. Tree t=new node;
  25. PreCreateTree(t);
  26. return 0;
  27. }

二、遍历二叉树

1.递归写法:

  1. void PreTraverse(Tree &t)
  2. {
  3. if(t)
  4. {
  5. cout<<t->data;//在这输出就是先序遍历
  6. PreTraverse(t->lchild);
  7. //放到这里输出就是中序遍历
  8. PreTraverse(t->rchild);
  9. //放到这里输出就是后序遍历
  10. }
  11. }

2.非递归写法:

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. typedef struct node
  4. {
  5. char data;
  6. struct node *lchild,*rchild;
  7. }Node, *Tree;
  8. Tree stk[100];//二叉树结构的结构体指针数组
  9. void PreCreateTree(Tree &t)//先序遍历序列创建二叉树
  10. {
  11. char ch;
  12. cin>>ch;
  13. if(ch == '@')
  14. t=NULL;
  15. else
  16. {
  17. t=new node;
  18. t->data=ch;
  19. PreCreateTree(t->lchild);
  20. PreCreateTree(t->rchild);
  21. }
  22. }
  23. void InoderTraverse(Tree &t)//非递归中序遍历
  24. {
  25. int hh=-1,tt=0;//模拟栈
  26. while(t!=NULL || hh>=tt)//当t不为空 或者 栈不为空
  27. {
  28. if(t!=NULL)//t不为空就把该结点压入栈,再移到左孩子
  29. {
  30. stk[++hh]=t;
  31. t=t->lchild;
  32. }
  33. else//t为空则退栈并输出,再移到右孩子
  34. {
  35. t=stk[hh--];
  36. cout<<t->data;
  37. t=t->rchild;
  38. }
  39. }
  40. cout<<endl;
  41. }
  42. int main()
  43. {
  44. Tree t=new node;
  45. PreCreateTree(t);
  46. InoderTraverse(t);
  47. return 0;
  48. }

3.按层次遍历:

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. typedef struct node
  4. {
  5. char data;
  6. struct node *lchild,*rchild;
  7. }Node, *Tree;
  8. Tree q[100];
  9. void PreCreateTree(Tree &t)
  10. {
  11. char ch;
  12. cin>>ch;
  13. if(ch == '@')
  14. t=NULL;
  15. else
  16. {
  17. t=new node;
  18. t->data=ch;
  19. PreCreateTree(t->lchild);
  20. PreCreateTree(t->rchild);
  21. }
  22. }
  23. void LayerTraverse(Tree t)//层次遍历
  24. {
  25. int hh=0,tt=0;//模拟对列
  26. if(t == NULL)
  27. return ;
  28. else
  29. {
  30. q[0]=t;//根结点入队
  31. while(hh<=tt)//当队不为空
  32. {
  33. t=q[hh++];//取队首并输出
  34. cout<<t->data;
  35. if(t->lchild!=NULL)//有左孩子就把左孩子入队
  36. q[++tt]=t->lchild;
  37. if(t->rchild!=NULL)//有右孩子就把右孩子入队
  38. q[++tt]=t->rchild;
  39. }
  40. }
  41. }
  42. int main()
  43. {
  44. Tree t=new node;
  45. PreCreateTree(t);
  46. LayerTraverse(t);
  47. return 0;
  48. }

三、一些操作

1.根据森林的孩子兄弟表示法的先序序列找叶子数量:

关于树及树的基础操作 - 图1

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. typedef struct node
  4. {
  5. char data;
  6. struct node *lchild,*rchild;
  7. }Node,*Tree;
  8. void CreateTree(Tree &t)
  9. {
  10. char ch;
  11. cin>>ch;
  12. if(ch == '@')
  13. t=NULL;
  14. else
  15. {
  16. t = new node;
  17. t->data = ch;
  18. CreateTree(t->lchild);
  19. CreateTree(t->rchild);
  20. }
  21. }
  22. int TreeLeaf(Tree &t)
  23. {
  24. if(t == NULL)//根结点为空返回0
  25. return 0;
  26. if(t->lchild == NULL)//如果没有左孩子,返回1+右孩子的情况
  27. return 1+TreeLeaf(t->rchild);
  28. else//有左孩子,返回左孩子的情况+右孩子的情况
  29. return TreeLeaf(t->lchild)+TreeLeaf(t->rchild);
  30. }
  31. int main()
  32. {
  33. Tree t=new node;
  34. CreateTree(t);
  35. cout<<TreeLeaf(t);
  36. return 0;
  37. }

2.找叶子:

  1. int ans=0;
  2. int PreTraverse(Tree t)
  3. {
  4. if(t == NULL)
  5. return ans;
  6. else
  7. {
  8. if(t->lchild==NULL&& t->rchild==NULL)
  9. ans++;
  10. PreTraverse(t->lchild);
  11. PreTraverse(t->rchild);
  12. }
  13. return ans;
  14. }

3.求二叉树的深度:

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. typedef struct node
  4. {
  5. char data;
  6. struct node *lchild,*rchild;
  7. }Node, *Tree;
  8. void PreCreateTree(Tree &t)
  9. {
  10. char ch;
  11. cin>>ch;
  12. if(ch == '@')
  13. t=NULL;
  14. else
  15. {
  16. t=new node;
  17. t->data=ch;
  18. PreCreateTree(t->lchild);
  19. PreCreateTree(t->rchild);
  20. }
  21. }
  22. int Depth(Tree t)
  23. {
  24. int n,m;//注意,这里的n和m不能定义在depth函数外!会出错!!!
  25. if(t==NULL)
  26. return 0;
  27. else
  28. {
  29. m=Depth(t->lchild);
  30. n=Depth(t->rchild);
  31. return max(n,m)+1; //返回较大的值+1
  32. }
  33. }
  34. int main()
  35. {
  36. Tree t=new node;
  37. PreCreateTree(t);
  38. cout<<Depth(t)<<endl;
  39. return 0;
  40. }

4.复制二叉树:

  1. void copy(Tree &t,Tree newt)
  2. {
  3. if(t == NULL)
  4. {
  5. newt = NULL;
  6. return;
  7. }
  8. else
  9. {
  10. newt = new node;
  11. newt->data = t->data;
  12. copy(t->lchild, newt->lchild);
  13. copy(t->rchild, newt->rchild);
  14. }
  15. }