#include<stdio.h> struct tree { int data; int l_ch; int r_ch; }; void add_node(struct tree* arr_1,int *arr,int start_index,int index) { arr_1[index].data=arr[index]; if(arr_1[start_index].data<arr[index]) if(arr_1[start_index].r_ch==-1) arr_1[start_index].r_ch=index; else add_node(arr_1,arr,arr_1[start_index].r_ch,index); else if(arr_1[start_index].l_ch==-1) arr_1[start_index].l_ch=index; else add_node(arr_1,arr,arr_1[start_index].l_ch,index); } void creat_tree(struct tree arr_1[],int *arr,int size) { int index; for(index=0;index<size;index++) { if(index!=0) add_node(arr_1,arr,0,index); else arr_1[0].data=arr[0]; } } void init_tree(struct tree* arr_1,int size) { int index; for(index=0;index<size;index++) { arr_1[index].data=-1; arr_1[index].l_ch=-1; arr_1[index].r_ch=-1; } } void first_trav(struct tree* arr_1,int index) { printf("%d ",arr_1[index].data); if(arr_1[index].l_ch!=-1) first_trav(arr_1,arr_1[index].l_ch); if(arr_1[index].r_ch!=-1) first_trav(arr_1,arr_1[index].r_ch); } void mid_trav(struct tree* arr_1,int index) { if(arr_1[index].l_ch!=-1) mid_trav(arr_1,arr_1[index].l_ch); printf("%d ",arr_1[index].data); if(arr_1[index].r_ch!=-1) mid_trav(arr_1,arr_1[index].r_ch); } void last_trav(struct tree* arr_1,int index) { if(arr_1[index].l_ch!=-1) last_trav(arr_1,arr_1[index].l_ch); if(arr_1[index].r_ch!=-1) last_trav(arr_1,arr_1[index].r_ch); printf("%d ",arr_1[index].data); } void show_tree(struct tree* arr_1) { first_trav(arr_1,0); printf(" "); mid_trav(arr_1,0); printf(" "); last_trav(arr_1,0); printf(" "); } int main() { int arr[10]={10,2,1,56,40,90,7,6}; struct tree arr_1[8]; init_tree(arr_1,sizeof(arr_1)/sizeof(arr_1[0])); creat_tree(arr_1,arr,sizeof(arr_1)/sizeof(arr_1[0])); show_tree(arr_1); }
二叉树的性质
1:在二叉树的第i层上最多有2^(i-1)个节点
2:深度为K的二叉树之多有2^(k-1)个节点
####注:这里的深度K意思就是有K层的二叉树
3:对于任何一棵二叉树T,如果其终端节点有No个,度为2的节点数有N2,则No=N2+1
4: 具有n个节点的完全二叉树的深度为[log2n]+1([x]表示不大于x的最大整数)