• 拓展:顺序存储结构相关


     1、二叉树的建立---顺序存储
     2、顺序存储结构的遍历
     3、二叉树的顺序存储结构转化为链式存储结构
     4、二叉树的链式存储结构转化为顺序存储结构

    1. /**
    2. * 拓展 :顺序存储结构相关
    3. *
    4. * ①算法思想
    5. * 1、二叉树的建立---顺序存储
    6. * 2、顺序存储结构的遍历
    7. * 3、二叉树的顺序存储结构转化为链式存储结构
    8. * 4、二叉树的链式存储结构转化为顺序存储结构
    9. *
    10. * ②算法设计
    11. */
    12. #include <stdio.h>
    13. #include <iostream>
    14. #define MaxSize 100
    15. typedef struct BiTreeNode{
    16. int data;
    17. BiTreeNode *lchild,*rchild;
    18. }BiTreeNode,*BiTree;
    19. //1、二叉树的建立---顺序存储
    20. #define MaxSize 100
    21. struct BiTreeNodeArr{
    22. int data[MaxSize];
    23. int length;
    24. };
    25. //假设下标从0开始
    26. void CreatBiTree_0(int arr[],int root,int n){//root用来记录下标
    27. int data;
    28. scanf("%d",&data);
    29. if(data == -1){
    30. arr[root] = data;
    31. return;
    32. }
    33. if(root * 2 + 1 <= n - 1)
    34. CreatBiTree_0(arr,root * 2 + 1,n);
    35. if(root * 2 + 2 <= n - 1)
    36. CreatBiTree_0(arr,root * 2 + 2,n);
    37. }
    38. //假设下标从1开始
    39. void CreatBiTree_1(int arr[],int root,int n){//root用来记录下标
    40. int data;
    41. scanf("%d",&data);
    42. if(data == -1){
    43. arr[root] = data;
    44. return;
    45. }
    46. if(root * 2 <= n - 1)
    47. CreatBiTree_1(arr,root * 2,n);
    48. if(root * 2 + 1 <= n - 1)
    49. CreatBiTree_1(arr,root * 2 + 1,n);
    50. }
    51. //2、顺序存储结构的遍历
    52. //假设下标从1开始
    53. //①先序遍历
    54. void PreOrderSq(int node[],int root,int n){
    55. if(root <= n - 1 && node[root] != -1){//当node数组中有节点时
    56. printf("%d ",node[root]);
    57. PreOrderSq(node,2 * root,n);
    58. PreOrderSq(node,2 * root + 1,n);
    59. }
    60. }
    61. //②中序遍历
    62. void InOrderSq(int node[],int root,int n){
    63. if(root <= n - 1 && node[root] != -1){//当node数组中有节点时
    64. InOrderSq(node,2 * root,n);
    65. printf("%d ",node[root]);
    66. InOrderSq(node,2 * root + 1,n);
    67. }
    68. }
    69. //③后序遍历
    70. void PostOrderSq(int node[],int root,int n){
    71. if(root <= n - 1 && node[root] != -1){//当node数组中有节点时
    72. PostOrderSq(node,2 * root,n);
    73. PostOrderSq(node,2 * root + 1,n);
    74. printf("%d ",node[root]);
    75. }
    76. }
    77. //3、二叉树的顺序存储结构转化为链式存储结构
    78. //假设下标从 1 开始,非递归转换
    79. struct Node{
    80. BiTree T;
    81. int num;//num代表T节点在顺序存储的节点数组中的位置
    82. };//用结构体把T和他在数组中的位置做一个绑定
    83. //采用层次建立的方式
    84. BiTree BuildSqToLinkByLevel(int arr[],int n){
    85. Node Queue[MaxSize],p;//p每次出队的时候要用到
    86. int front = -1,rear = -1;//用来标记Queue
    87. //先做一个深度的绑定
    88. BiTree T = (BiTree)malloc(sizeof(BiTreeNode));
    89. T -> data = arr[1];
    90. Queue[++rear] = {T,1};//相当于Queue[rear].T = T;Queue[rear].num = 1;
    91. //层次建立链式存储的二叉树
    92. while(front != rear){
    93. p = Queue[++front];//出队一个元素
    94. //创建左孩子
    95. if(arr[p.num * 2] == -1 || p.num * 2 > n)
    96. p.T -> lchild = NULL;
    97. else{
    98. p.T -> lchild = (BiTree)malloc(sizeof(BiTreeNode));
    99. p.T -> lchild -> data = arr[p.num * 2];
    100. Queue[++rear] = {p.T -> lchild,p.num * 2};
    101. }
    102. //创建右孩子
    103. if(arr[p.num * 2 + 1] == -1 || p.num * 2 + 1 > n)
    104. p.T -> rchild = NULL;
    105. else{
    106. p.T -> rchild = (BiTree)malloc(sizeof(BiTreeNode));
    107. p.T -> rchild -> data = arr[p.num * 2 + 1];
    108. Queue[++rear] = {p.T -> rchild,p.num * 2 + 1};
    109. }
    110. }
    111. }
    112. //4、二叉树的链式存储结构转化为顺序存储结构
    113. //假设下标从 1 开始,递归转换
    114. BiTree BuildLinkToSq(BiTree T,int arr[],int root){
    115. if(T){
    116. arr[root] = T -> data;
    117. BuildLinkToSq(T -> lchild,arr,root * 2);
    118. BuildLinkToSq(T -> rchild,arr,root * 2 + 1);
    119. }
    120. }

  • 相关阅读:
    vue3探索——5分钟快速上手大菠萝pinia
    对占用多字节和位的报文信号解析详解
    卸载Erlang和RabbitMQ
    西华大学计算机考研资料汇总
    Dash 2.17版本新特性介绍
    ClickHouse 物化视图
    比较两个相互垂直结构的迭代次数
    安科瑞无线计量模块AEW100指导性技术要求-Susie 周
    SQL Server 操作JSON数据库列
    docker的使用
  • 原文地址:https://blog.csdn.net/shengruyu/article/details/126677677