数据结构之完全二叉树

完全二叉树

性质:如果二叉树结点个数为N

对于编号为i
左孩子存在的条件: 2 * i <= N
右孩子存在的条件: 2 * i + 1 <= N

数据类型
//让完全二叉树的结点保存其编号
typedef int DataType;

typedef struct bitree
{
    DataType data;
    struct bitree *lchild;
    struct bitree *rchild;
}BiTree;

create(1)
    |
    root <1>
    if(1 * 2 <= N)
        root->lchild = create(2)
                            |
                            root <2>
                            if(2 * 2 <= N)
                                root->lchild = create(4)
                                                    |
                                                    root <4>
                                                    if(4 * 2 <= N)
                                                        root->lchild = create(8)
                                                                            |
                                                                            root <8>
                                                                            if(8 * 2 <= N)
                                                                            if(8 * 2 + 1 <= N)
                                                                            return root
                                                    if(4 * 2 + 1 <= N)
                                                        root->rchild = create(9)
                                                                            |
                                                                            root <9>
                                                                            if(9 * 2 <= N)
                                                                            if(9 * 2 + 1 <= N)
                                                                            return root
                                                    return root
                            if(2 * 2 + 1 <= N)
                                root->rchild = create(5)
                                                    |
                                                    root <5>
                                                    if(5 * 2 <= N)
                                                    if(5 * 2 + 1 <= N)
                                                    return root
                            return root
    if(1 * 2 + 1 <= N)
        root->rchild = create(3)
                            |
                            root <3>
                            if(3 * 2 <= N)
                                root->lchild = create(6)
                                                    |
                                                    root <6>
                                                    if(6 * 2 <= N)
                                                    if(6 * 2 + 1 <= N)
                                                    return root
                            if(3 * 2 + 1 <= N)
                                root->rchild = create(7)
                                                    |
                                                    root <7>
                                                    if(7 * 2 <= N)
                                                    if(7 * 2 + 1 <= N)
                                                    return root
                            return root
    return root


#ifndef __BTREE__H
#define __BTREE__H


#include <stdio.h>
#include <stdlib.h> 
#include <string.h>


#define N 9


//新定义一个数据类型
typedef struct 
{
    char c;
    int num;
}BType;


//定义一个结构体
typedef struct bitree
{
    BType data;
    struct bitree *lchild;
    struct bitree *rchild;
}Btree;


extern Btree *alloc_node(int i);
extern Btree *create_binary_tree(int i);
extern int pre_order(Btree *root);
extern int in_order(Btree *root);
extern int post_order(Btree *root);
extern void level_order(Btree *root);


#endif
#ifndef __LINKQUEUE__H
#define __LINKQUEUE__H


#include <stdio.h>
#include <stdlib.h>
#include <string.h>


typedef int DataType;
//结点类型
struct node
{
    DataType data;
    struct node *next;
};


//队列头
typedef struct
{
    //front指向链表头
    struct node *front;
    //rear指向链表尾
    struct node *rear;
}LinkQueue;


extern LinkQueue *creat_linkqueue();
extern int is_empty_linkqueue(LinkQueue *q);
extern int enter_linkqueue(LinkQueue *q,DataType data);
extern DataType delete_linkqueue(LinkQueue *q);


#endif
#include "linkqueue.h"


//新建一个队列头
LinkQueue *creat_linkqueue()
{
    //先分配一个头结点,head保存其地址
    //在为队列头在堆区分配空间,q保存其首地址
    struct node *head = NULL;
    LinkQueue *q = NULL;


    //创建空链表,链表队列头结点
    head = (struct node *)malloc(sizeof(struct node));
    memset(head,0,sizeof(struct node));//清空后指针域为NULL。表示空链表


    //队列头,指向链表头跟链表尾
    q = (LinkQueue *)malloc(sizeof(LinkQueue));
 
    q->front = head;
    q->rear = head;


    return q; 
}


///链表判空
int is_empty_linkqueue(LinkQueue *q)
{
    return q->front == q->rear;
}


//尾插法插入数据
int enter_linkqueue(LinkQueue *q,DataType data)
{
    struct node *temp = NULL;


    temp = (struct node *)malloc(sizeof(struct node));
    temp->data = data;
    
    temp->next = q->rear->next;
    q->rear->next = temp;


    //更新rear
    q->rear = temp;


    return 0;


}


//删头法对头数据出队
DataType delete_linkqueue(LinkQueue *q)
{
    struct node *temp = NULL;


    //保存原头结点首地址
    temp = q->front;


    //更新front
    q->front = q->front->next;


    free(temp);


    temp = NULL;




    return  q->front->data;
}
#include "btree.h"
#include "linkqueue.h"


//新建一个数据结点,
Btree *alloc_node(int i)
{
    Btree *root = NULL;


    root = (Btree *)malloc(sizeof(Btree));
    root->data.num = i;
    root->lchild = root->rchild = NULL;


    printf("Input %d node char:",i);
    scanf("%c",&root->data.c);


    while(getchar() != '\n');


    return root;
}


//新建一个二叉树
Btree *create_binary_tree(int i)
{
    Btree *root = NULL;


    root = alloc_node(i);


    //结点有左孩子的条件:2i<=N
    if(i * 2 <= N)
    {
        //条件满足递归条用本函数创建左结点
        root->lchild = create_binary_tree(2 * i);
    }


    //本结点有右孩子的条件为:2i+1<=N
    if(i * 2 + 1 <= N)
    {
        //条件满足递归条用本函数创建右结点
        root->rchild = create_binary_tree(i * 2 + 1);
    }


    //返回新建的结点
    return root;
}


//先序遍历二叉树,规则:头->左->右
//先访问跟结点,再访问左子树最后访问右子树
int pre_order(Btree *root)
{
    if(root == NULL)
        return ;


    printf("%d :%c \n",root->data.num,root->data.c);
    pre_order(root->lchild);
    pre_order(root->rchild);
}


//中序遍历二叉树,规则:左->头->右
int in_order(Btree *root)
{
    if(root == NULL)
        return ;
    
    in_order(root->lchild);
    printf("%d :%c \n",root->data.num,root->data.c);
    //printf("%d ",root->data);
    in_order(root->rchild);


        return ;
}


//后序遍历二叉树,规则:左->右->头
int post_order(Btree *root)
{
    if(root == NULL)
        return ;
    
    post_order(root->lchild);
    post_order(root->rchild);
    printf("%d :%c \n",root->data.num,root->data.c);
    //printf("%d ",root->data);
    
    return ;
}


//分层遍历每一个结点
void level_order(Btree *root)
{
    LinkQueue *q = NULL;
    Btree *t = NULL;


    q = creat_linkqueue();


    enter_linkqueue(q,root);
    while(!is_empty_linkqueue(q))
    {
        t = delete_linkqueue(q);


        printf("(%-3d : %c) ",t->data.num,t->data.c);


        if(t->lchild != NULL)
        {
            enter_linkqueue(q,t->lchild);
        }
        if(t->rchild != NULL)
        {
            enter_linkqueue(q,t->rchild);
        }
    }
    putchar('\n');
    
    return ;
}
#include "btree.h"
#include "linkqueue.h"


int main()
{
    Btree *root = NULL;


    root = create_binary_tree(1);


   pre_order(root);/*{{{*/
   putchar('\n');
   
   in_order(root);
   putchar('\n');


   post_order(root);
   putchar('\n');/*}}}*/


   level_order(root);


   return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值