#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include "list_queue.h"
#define TREE_TYPE char
#define PF "%c "
#define EMPTY '#'
// 顺序二叉树结构
typedef struct BinTree
{
TREE_TYPE* ptr;
size_t cap;
}BinTree;
// 构建二叉树
// 根据提供的完全二叉树层序遍历的结果,来构建堆内存中的二叉树
BinTree* create_tree(TREE_TYPE* arr,size_t len)
{
BinTree* tree = malloc(sizeof(BinTree));
tree->ptr = malloc(sizeof(TREE_TYPE)*len);
memcpy(tree->ptr,arr,sizeof(TREE_TYPE)*len);
tree->cap = len;
return tree;
}
// 前序遍历 pre_order mid_order post_order
// 在tree中的第index编号节点开始 进行前序遍历
// index是编号,需要-1换算成内存下标访问
void _dlr_show(BinTree* tree,size_t index)
{
if(index>tree->cap || EMPTY == tree->ptr[index-1]) return;
// 访问根节点
printf(PF,tree->ptr[index-1]);
// 对左子树前序遍历
_dlr_show(tree,index*2);
// 对右子树前序遍历
_dlr_show(tree,index*2+1);
}
void dlr_show(BinTree* tree)
{
_dlr_show(tree,1);
printf("\n");
}
void _ldr_show(BinTree* tree,size_t index)
{
if(index>tree->cap || EMPTY == tree->ptr[index-1]) return;
_ldr_show(tree,index*2);
printf(PF,tree->ptr[index-1]);
_ldr_show(tree,index*2+1);
}
// 中序遍历
void ldr_show(BinTree* tree)
{
_ldr_show(tree,1);
printf("\n");
}
void _lrd_show(BinTree* tree,size_t index)
{
if(index>tree->cap || EMPTY == tree->ptr[index-1]) return;
_lrd_show(tree,index*2);
_lrd_show(tree,index*2+1);
printf(PF,tree->ptr[index-1]);
}
// 后序遍历
void lrd_show(BinTree* tree)
{
_lrd_show(tree,1);
printf("\n");
}
// 层序遍历 借助队列
void layer_show(BinTree* tree)
{
// 创建队列
ListQueue* queue = create_list_queue();
// 把根节点编号入队
push_list_queue(queue,1);
// 一直持续到队列为空结束
while(!empty_list_queue(queue))
{
// 获取队头
int index = front_list_queue(queue);
// 计算出左子树编号,检查后入队
int left = index*2;
if(left <= tree->cap && EMPTY != tree->ptr[left-1])
push_list_queue(queue,left);
// 计算出右子树编号,检查后入队
int right = index*2+1;
if(right <= tree->cap && EMPTY != tree->ptr[right-1])
push_list_queue(queue,right);
// 队头出队
pop_list_queue(queue);
// 显示队头
printf(PF,tree->ptr[index-1]);
}
// 层序遍历结束 销毁队列
destroy_list_queue(queue);
printf("\n");
}
// 求index节点开始的树的高度
int _high_tree(BinTree* tree,size_t index)
{
// 该位置没有节点 高度返回0
if(index > tree->cap || EMPTY == tree->ptr[index-1])
return 0;
// 计算左子树高度
int lh = _high_tree(tree,index*2);
// 计算右子树高度
int rh = _high_tree(tree,index*2+1);
// 左右子树中高的那边+1 就是index这棵树的高度
return lh > rh ? lh+1: rh+1;
}
// 树的高度
int high_tree(BinTree* tree)
{
return _high_tree(tree,1);
}
int _density_tree(BinTree* tree,size_t index)
{
if(index > tree->cap || EMPTY == tree->ptr[index-1])
return 0;
return _density_tree(tree,index*2) + _density_tree(tree,index*2+1) + 1;
}
// 树的密度
int density_tree(BinTree* tree)
{
return _density_tree(tree,1);
}
// 插入 在空位置插入 内存不够要扩容
// 删除 只删除叶子节点
// 求左子树 求右子树
int main(int argc,const char* argv[])
{
char* str = "ABCDE#FG#HI##J";
BinTree* tree = create_tree(str,strlen(str));
dlr_show(tree);
ldr_show(tree);
lrd_show(tree);
layer_show(tree);
printf("high:%d\n",high_tree(tree));
printf("desity:%d\n",density_tree(tree));
}
数据结构(二叉树-顺序存储)
最新推荐文章于 2025-08-22 09:41:59 发布