// Study_Program.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <math.h>
#define SIZE 20
//求深度
//完全二叉树的最大节点数量s = (2^n)-1,n = 树的高度
//对于一个数组结构的tree,要先求tree的深度n,求最大指数2^n-1=size = 2^n = size+1 。所以 n = log2 (size+1)。n向上取整
int getHeight2(int size){
return (int)ceil(log10((double)size+1)/log10(2.0));
}
//求左孩子
int *getLchild(int arr[],int index){
if(index*2 > SIZE){
//printf("this node is not lift child");
return NULL;
}
return &arr[index*2];
}
//求右孩子
int *getRchild(int arr[],int index){
if(index*2+1 > SIZE){
// printf("this node is not right child");
return NULL;
}
return &arr[index*2+1];
}
//求双亲
int *getParent(int arr[],int index){
if(index < 2 || index > SIZE){
printf("this node
二叉树数组存储:前序遍历、打印二叉树
最新推荐文章于 2024-03-11 18:00:00 发布