数据结构之完全二叉树
来源:互联网 发布:刘雯的台步怎么样知乎 编辑:程序博客网 时间:2024/04/28 00:41
完全二叉树性质:如果二叉树结点个数为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;}