二叉树的一些操作(C语言实现)
来源:互联网 发布:数据分析表格模板 编辑:程序博客网 时间:2024/06/05 16:25
#include <stdio.h>#include <stdlib.h>typedef struct BTNODE //二叉树的节点类型{ char c; struct BTNODE *lchild; struct BTNODE *rchild;}node,*node_pointer;typedef struct queue //队列的数据型,队列存放的是二叉树节点的指针类型{ node_pointer *front;//队列头指针 node_pointer *rear;//队列尾指针 node_pointer *Head;//队列头指针,其值不变,用于释放队列时用 int MAX_SIZE;//队列的最大容量}Queue;Queue build_Queue()//建立队列{ Queue q; q.MAX_SIZE=200;//设置队列的最大容量为200 q.Head=(node_pointer *)malloc(sizeof(node_pointer)*q.MAX_SIZE); if (!q.Head) exit(0);//判断队列的内存是否申请成功 q.front=q.Head; q.rear=q.Head; return q;}void Ins_Queue(Queue *q,node_pointer np)//向队列中插入元素,注意传进去的参数是队列指针{ if ((q->rear)-(q->front)>=(q->MAX_SIZE))//判断队列是否已经满了 { printf("The queue is full!"); return ; } *((q->rear)++)=np;//入队列}node_pointer Del_Queue(Queue *q)//从队尾删除元素,注意传进去的参数是队列指针{ if (q->front==q->rear)//判断队列是否为空 { printf("The queue is empty!"); return NULL; } return *((q->front)++);//出队列}node_pointer level_build_BTree()//按层序建立二叉树{ node_pointer Head=(node_pointer)malloc(sizeof(node));//二叉树的根节点的指针 node_pointer temp=NULL,p=NULL,q=NULL; Queue Q;//用于二叉树遍历查找的队列 char temp_ele; char temp_par; char temp_tag; while(1) { scanf("%c",&temp_ele); getchar();//防止将空格读进去 scanf("%c",&temp_par); getchar(); scanf("%c",&temp_tag); getchar(); if (temp_par=='@'&&temp_tag=='@')//当前元素是二叉树的根节点 { Head->c=temp_ele; Head->lchild=NULL; Head->rchild=NULL; } else if(temp_ele=='#'&&temp_par=='#'&&temp_tag=='#')//用户输入完毕,跳出循环 { break; } else { Q=build_Queue();//建立队列 temp=(node_pointer)malloc(sizeof(node));//生成当前节点 temp->c=temp_ele;//为当前节点赋值 temp->lchild=NULL; temp->rchild=NULL; p=Head; Ins_Queue(&Q,p);//将已建立的二叉树的根结点入队列,注意要传进去队列的地址“&” while(Q.front!=Q.rear)//按层序遍历已建立的二叉树,直到找到当前节点的父节点为止 { q=Del_Queue(&Q); if (q==NULL) continue; if (q->lchild!=NULL||q->rchild!=NULL) { Ins_Queue(&Q,q->lchild); Ins_Queue(&Q,q->rchild); } if (q->c==temp_par)//找到了当前节点的父节点,根据“L”或者“R”,把当前节点链入已建立好的二叉树 { if (temp_tag=='L'||temp_tag=='l') { q->lchild=temp; break; } else { q->rchild=temp; break; } } } free(Q.Head);//释放队列 } } return Head;}void Pre_Travese_BTree(node_pointer T)//先序遍历{ if (T) { printf("%c ",T->c); Pre_Travese_BTree(T->lchild); Pre_Travese_BTree(T->rchild); }}void In_Travese_BTree(node_pointer T)//中序遍历{ if (T) { In_Travese_BTree(T->lchild); printf("%c ",T->c); In_Travese_BTree(T->rchild); }}void Pos_Travese_BTree(node_pointer T)//后序遍历{ if (T) { Pos_Travese_BTree(T->lchild); Pos_Travese_BTree(T->rchild); printf("%c ",T->c); }}void Level_Travese_BTree(node_pointer T)//层序遍历{ node_pointer q,p=T; Queue Q=build_Queue(); Ins_Queue(&Q,p); while (Q.front!=Q.rear) { q=Del_Queue(&Q); if (q==NULL) continue; if (q->lchild!=NULL || q->rchild!=NULL) { Ins_Queue(&Q,q->lchild); Ins_Queue(&Q,q->rchild); } printf("%c ",q->c); } free(Q.Head);}int getHeight(node_pointer T){ if (T==NULL) return 0; else { return (getHeight(T->lchild)>getHeight(T->rchild))?(getHeight(T->lchild)+1):(getHeight(T->rchild)+1); }}void Print_BTree(node_pointer T){ if (T==NULL) return ; else if (T->lchild==NULL && T->rchild==NULL) printf("(%c)",T->c); else { printf("(%c,",T->c); Print_BTree(T->lchild); printf(","); Print_BTree(T->rchild); printf(")"); }}int main(){ node_pointer Head=level_build_BTree(); Pre_Travese_BTree(Head); printf("\n"); In_Travese_BTree(Head); printf("\n"); Pos_Travese_BTree(Head); printf("\n"); Level_Travese_BTree(Head); printf("\n"); Print_BTree(Head); printf("\n"); printf("The height of the BTree: %d",getHeight(Head)); return 0;}