当前位置:   article > 正文

数据结构 关于树的几个证明题及代码实现_数据结构证明题

数据结构证明题

一、求二叉树的深度

  1. typedef struct BTNode BTNode{
  2. Elemtype data;
  3. struct BTNode *lchild,*rchild;
  4. } BTNode;
  5. int depth(BTNode *T){
  6. if(!T)
  7. return 0;
  8. int ldepth = depth(T->lchild);
  9. int rdepth = depth(T->rchild);
  10. return ldepth>rchild ? (ldepth+1) : (rdepth+1);
  11. }

二、判断一棵树是否是排序二叉树(递归实现)

思路是:中序遍历排序二叉树,遍历序列有序

  1. typedef struct BTNode BTNode{
  2. Elemtype data;
  3. struct BTNode *lchild,*rchild;
  4. } BTNode;
  5. int preVal = MIN; //MIN表示int变量所能表示的最小值
  6. bool isBST(BTNode *T){
  7. if (!T)
  8. return true;
  9. bool l = isBST(T->lchild);
  10. if (preVal > T->data)
  11. return false;
  12. else
  13. preVal = T->data;
  14. bool r = isBST(T->rchild);
  15. return l&&r;
  16. }

三、判断一棵树是否是排序二叉树(非递归实现)

  1. typedef struct BTNode BTNode{
  2. Elemtype data;
  3. struct BTNode *lchild,*rchild;
  4. } BTNode;
  5. bool isBST(BTNode *T){
  6. BTNode* stack[NODE_NUM];
  7. int top = -1;
  8. while(T || top != -1){
  9. while(T){
  10. stack[++top] = T;
  11. t = t->lchild;
  12. }
  13. T = stack[top--];
  14. if (T->data > stack[top]->data)
  15. return false;
  16. T = stack[top--]->rchild;
  17. }
  18. return true;
  19. }

四、求Haffman树的带权路径长度

带权路径长度:树中每个  叶子节点的权值 *该叶子节点到根节点的路径长度(边数)  之和

思路:采用层序遍历,通过层数可以知道该层上的叶子节点的路径长度

  1. typedef struct BTNode BTNode{
  2. Elemtype data;
  3. struct BTNode *lchild,*rchild;
  4. } BTNode;
  5. int method(BTNode *T){
  6. BTNode* Q[NODE_NUM];
  7. int front = 0,rear = 0,sum = 0,level,node_num; //level为当前遍历的层数,node_num为当前层的结点数
  8. //树为空或者仅有一个根节点,则带权路径为0
  9. if(!T || (!T->lchild && !T->rchild))
  10. return 0;
  11. Q[rear++] = Q->lchild;
  12. Q[rear++] = Q->lchild;
  13. level = 2;
  14. node_num = 2;
  15. while(isNotEmpty(Q)){
  16. int num = 0; //用来记录下一层的结点个数
  17. BTNode* p;
  18. for(int i=0;i<node_num;i++){
  19. p = Q[front--];
  20. if (!p->lchild)
  21. sum += (level-1) *p->weight;
  22. else{
  23. Q[rear++] = Q->lchild;
  24. Q[rear++] = Q->lchild;
  25. num += 2;
  26. }
  27. }
  28. level++;
  29. node_num = num;
  30. }
  31. return sum;
  32. }
五、用递归算法判断叶子结点的个数
  1. typedef struct BTNode BTNode{
  2. Elemtype data;
  3. struct BTNode *lchild,*rchild;
  4. } BTNode;
  5. int count; //结点总数
  6. void method(BTNode *T){
  7. if (T){
  8. method(T->lchild);
  9. if (!T->lchild && !T->rchild)
  10. count++;
  11. method(T->rchild);
  12. }
  13. }

六、判断一棵二叉树是否平衡

  1. typedef struct BTNode BTNode{
  2. Elemtype data;
  3. struct BTNode *lchild,*rchild;
  4. } BTNode;
  5. bool main(BTNode *T){
  6. int depth = 0;
  7. return method(T,&depth);
  8. }
  9. bool method(BTNode *T){
  10. if (!T){
  11. &depth = 0;
  12. return true;
  13. }
  14. //存储每颗子树的高度
  15. int ldepth,rdepth;
  16. bool l = method(T->lchild,*ldepth);
  17. bool r = method(T->rchild,*rdepth);
  18. //如果有一颗子树不平衡,或者以当前递归结点为根的根树不平衡,则返回false,只要有一个false,
  19. //则会一直返回false,直到退出程序
  20. if (!(l&&r) || (ldepth-rdepth)<-1 || (ldepth-rdepth)>1)
  21. return false;
  22. //根树平衡,则算出该树的高度
  23. *depth = ldepth>rdepth ? (ldepth+1) : (rdepth+1);
  24. return true;
  25. }

七、非递归算法求二叉树只有一个孩子的结点个数(单分支)

  1. typedef struct BTNode BTNode{
  2. Elemtype data;
  3. struct BTNode *lchild,*rchild;
  4. } BTNode;
  5. bool method(BTNode *T){
  6. BTNode* stack[NODE_NUM];
  7. int top = -1,count = 0;
  8. if (!T)
  9. return 0;
  10. while (T || top != -1){
  11. while(T){
  12. stack[++top] = T; //入栈
  13. T = T->lchild;
  14. }
  15. T = stack[top--];
  16. if ((!T->lchild && T->rchild) || (T->lchild && !T->rchild))
  17. count++;
  18. T = T->rchild;
  19. }
  20. return count;
  21. }

八、骑士巡游(java)

  1. #include <stdio.h>
  2. const int n = 8; //棋盘的行列数
  3. int chess[n][n]; //1代表已遍历,0代表未遍历
  4. int traverse[n*n]; //存放遍历路径,坐标(x,y)代表chess中:从上往下,从左往右的第x*8+y+1个位置
  5. int index = 0;
  6. int step = n*n;
  7. bool move(int,int);
  8. int main(){
  9. int x=0,y=1;
  10. if (move(x,y)){
  11. //输出遍历序列
  12. for (int i=0;i<index;++i)
  13. printf("%d ",traverse[i]);
  14. printf("\n index = %d",index);
  15. return 0;
  16. }else {
  17. printf("遍历失败!");
  18. return 0;
  19. }
  20. return 0;
  21. }
  22. //x,y∈[0,7]
  23. bool move (int x,int y){
  24. if (chess[x][y] || x<0 || x>7 || y<0 ||y>7)
  25. return false;
  26. --step;
  27. chess[x][y] = 1;
  28. traverse[index++] = x*8+y+1;
  29. if (step==0)
  30. return true;
  31. if (move(x-1,y-2))
  32. return true;
  33. if (move(x-2,y-1))
  34. return true;
  35. if (move(x-2,y+1))
  36. return true;
  37. if (move(x-1,y+2))
  38. return true;
  39. if (move(x+1,y-2))
  40. return true;
  41. if (move(x+2,y-1))
  42. return true;
  43. if (move(x+2,y+1))
  44. return true;
  45. if (move(x+1,y+2))
  46. return true;
  47. return false;
  48. }

九、证明:正则二叉树只有奇数个定点,偶数条边

证:由定义可知,正则二叉树结点的度要么为0,要么为2,设Ni为度为i的结点个数,令结点总数为n,则有:

        N0 = N2 + 1 ,  N0 + N2 = N ,推得:2 * N2 + 1 = n,故有奇数个顶点,又边数 = 顶点数 - 1,则有

        偶数条边

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/AllinToyou/article/detail/516318
推荐阅读
相关标签
  

闽ICP备14008679号