step3 . day6数据结构之非线性表 满二叉树和不完全二叉树
二叉树和链表相似,只是后节点变成了左右节点,重要的是递归思想的理解和返回时候的层级结构
1.满二叉树的穿件及前中后序遍历
#include <stdio.h>
#include <stdlib.h>
typedef struct node{
int date;
struct node * lchild;
struct node * rchild;
}btreenode;
btreenode * btree_create(int a,int b){
btreenode * t = NULL;
t = (btreenode*)malloc(sizeof(btreenode));
t->date = b;
if(2*b<=a){
t->lchild = btree_create(a,2*b);
}
else{
t->lchild = NULL;
}
if(2*b+1<=a){
t->rchild = btree_create(a,2*b+1);
}
else{
t->rchild = NULL;
}
return t;
}
void first_show(btreenode* root){
printf("%d ",root->date);
if(root->lchild != NULL){
first_show(root->lchild);
}
if(root->rchild != NULL){
first_show(root->rchild);
}
}
void middle_show(btreenode* root){
if(root->lchild != NULL){
middle_show(root->lchild);
}
printf("%d ",root->date);
if(root->rchild != NULL){
middle_show(root->rchild);
}
}
void last_show(btreenode* root){
if(root->lchild != NULL){
last_show(root->lchild);
}
if(root->rchild != NULL){
last_show(root->rchild);
}
printf("%d ",root->date);
}
int main(int argc, const char *argv[])
{
btreenode * root = btree_create(10,1);
first_show(root);
printf("\n");
middle_show(root);
printf("\n");
last_show(root);
printf("\n");
return 0;
}
2.不完全二叉树的创建(sancf和getchar方法均会产生垃圾字符,输入时候需要区分)
typedef struct node{
int date;
struct node * lchild;
struct node * rchild;
}btreenode;
btreenode * btree_create(){
btreenode * t = NULL;
t = (btreenode*)malloc(sizeof(btreenode));
char i = getchar();
getchar();
t->date = i;
if(i == '#'){
return NULL;
}
t->lchild = btree_create();
t->rchild = btree_create();
return t;
}
void first_show(btreenode* root){
printf("%c ",root->date);
if(root->lchild != NULL){
first_show(root->lchild);
}
if(root->rchild != NULL){
first_show(root->rchild);
}
}
void middle_show(btreenode* root){
if(root->lchild != NULL){
middle_show(root->lchild);
}
printf("%c ",root->date);
if(root->rchild != NULL){
middle_show(root->rchild);
}
}
void last_show(btreenode* root){
if(root->lchild != NULL){
last_show(root->lchild);
}
if(root->rchild != NULL){
last_show(root->rchild);
}
printf("%c ",root->date);
}
int main(int argc, const char *argv[])
{
btreenode * root = btree_create();
first_show(root);
printf("\n");
middle_show(root);
printf("\n");
last_show(root);
printf("\n");
return 0;
}
3、二叉树的层级遍历(使用进队出队思想,最好是进队即出队,别想着全部进队后出队,对比下前者还是节省内存的)
#include <stdio.h>
#include <stdlib.h>
typedef struct node{
int date;
struct node * lchild;
struct node * rchild;
}btreenode;
typedef struct linklist{
btreenode * node1;
struct linklist * next;
}linklist;
linklist * linklist_create(){
linklist* ln =NULL;
ln = (linklist*)malloc(sizeof(linklist));
ln->next = NULL;
return ln;
}
void linklist_show(linklist * ln){
while(ln->next != NULL){
printf("%d ",ln->next->node1->date);
ln = ln->next;
}
printf("\n");
}
int linklist_in(linklist* ln,btreenode * btree){
if(btree == NULL) {return -1;}
linklist* t = ln;
linklist* temp = linklist_create();
temp->node1 = btree;
while(t->next != NULL){t = t->next;}
t->next = temp;
temp->next = NULL;
return 0;
}
int linklist_out(linklist * ln){
if(ln->next == NULL){return -1;}
linklist *temp = ln->next;
ln->next = temp->next;
int value = temp->node1->date;
// printf("%d ",temp->node1->date);
free(temp);
temp =NULL;
return value;
}
btreenode * btree_create(int a,int b){
btreenode * t = NULL;
t = (btreenode*)malloc(sizeof(btreenode));
t->date = b;
if(2*b<=a){
t->lchild = btree_create(a,2*b);
}
else{
t->lchild = NULL;
}
if(2*b+1<=a){
t->rchild = btree_create(a,2*b+1);
}
else{
t->rchild = NULL;
}
return t;
}
void btree_tier_show(linklist* ln,btreenode* root){
// linklist* t = ln;
linklist_in(ln,root);
// printf("%d ",linklist_out(ln));
while(ln->next != NULL){
if(ln->next->node1->lchild != NULL)
linklist_in(ln,ln->next->node1->lchild);
if(ln->next->node1->lchild != NULL)
linklist_in(ln,ln->next->node1->rchild);
printf("%d ",linklist_out(ln));
}
printf("\n");
}
void first_show(btreenode* root){
printf("%d ",root->date);
if(root->lchild != NULL){
first_show(root->lchild);
}
if(root->rchild != NULL){
first_show(root->rchild);
}
}
int main(int argc, const char *argv[])
{
btreenode * root = btree_create(20,1);
first_show(root);
printf("\n---------------------\n");
linklist *ln = linklist_create();
btree_tier_show(ln,root);
return 0;
}