基本要求:1.定义二叉链存储结构 2.建立二叉链表存储的二叉树 3.实现二叉树的遍历(先序、中序、后序、层序) 4.求二叉树的高度、求结点双亲、统计叶子结点数目、打印二叉树,交换每个结点的左孩子和右孩子等操作
测试数据:ABD#G###CE#H##F##
二叉树: A
/ \
B C
/ / \
D E F
\ \
G H
#include<stdio.h> #include<stdlib.h> #define MaxSize 100 const int N=100; typedef char DataType; typedef struct Node //定义二叉链存储结构 { DataType data; struct Node *lchild; struct Node *rchild; }BiTNode, *BiTree; typedef struct Queue { int front; int rear; Node *data[MaxSize]; }SqQueue; void CreateBiTree(BiTree *root) //建立二叉链表存储的二叉树 { char ch; scanf(" %c",&ch); if(ch=='#') *root=NULL; // #表示当前结点为空 else { *root=(BiTree)malloc(sizeof(BiTNode)); (*root)->data=ch; CreateBiTree(&((*root)->lchild)); // 构造左子树 CreateBiTree(&((*root)->rchild)); // 构造右子树 } } void PreOrder(BiTree root) //先序遍历二叉树(递归) { if(root!=NULL) { printf("%c",root->data); PreOrder(root->lchild); PreOrder(root->rchild); } } void InOrder(BiTree root) //中序遍历二叉树(递归) { if(root!=NULL) { InOrder(root->lchild); printf("%c",root->data); InOrder(root->rchild); } } void PostOrder(BiTree root) //后序遍历二叉树(递归) { if(root!=NULL) { PostOrder(root->lchild); PostOrder(root->rchild); printf("%c",root->data); } } void LevelOrder(BiTree root) //层序遍历二叉树 { Node *q=NULL; SqQueue *Q; Q=(SqQueue*)malloc(sizeof(SqQueue)); Q->front=Q->rear=-1; if(root==NULL)return; Q->data[++Q->rear]=root; while(Q->front!=Q->rear) { q=Q->data[++Q->front]; printf("%c",q->data); if(q->lchild!=NULL) Q->data[++Q->rear]=q->lchild; if(q->rchild!=NULL) Q->data[++Q->rear]=q->rchild; } } int TreeDepth(BiTree root) //求二叉树的高度 { int hl,hr,h; if(root==NULL)return 0; else { hl=TreeDepth(root->lchild); hr=TreeDepth(root->rchild); return h=(hl>hr?hl:hr)+1; } } BiTree Parent(BiTree root,char x) //求结点双亲 { if(root==NULL) return NULL; if(root->lchild!= NULL) { if(root->lchild->data==x) { printf("该节点的父结点是:%c",root->data); return root; } } if(root->rchild != NULL) { if(root->rchild->data==x) { printf("该节点的父结点是:%c",root->data); return root; } } Parent(root->lchild,x); Parent(root->rchild,x); } int Countleaf(BiTree root,int count) //叶子结点数目 { if(root) { if((root->lchild==NULL)&&(root->rchild==NULL)) count++; count=Countleaf(root->lchild,count); count=Countleaf(root->rchild,count); } return count; } void PrintTree(BiTree root,int level) //打印二叉树 { int i; if(root->rchild) PrintTree(root->rchild,level+1); printf("----"); for(i=0;i<level;i++) printf("-----"); printf("%c\n",root->data); if(root->lchild) { PrintTree(root->lchild,level+1); } } int Exchange(BiTree root) //交换每个结点的左孩子和右孩子 { BiTree p; p=(BiTree)malloc(sizeof(BiTree)); if(root) { p=root->rchild; root->rchild=root->lchild; root->lchild=p; Exchange(root->lchild); Exchange(root->rchild); } return 1; } int main() { BiTree T; BiTree p; char ch; int count=0; printf("请输入二叉树:"); CreateBiTree(&T); printf("\n"); printf("先序遍历输出二叉树:"); PreOrder(T); printf("\n"); printf("中序遍历输出二叉树:"); InOrder(T); printf("\n"); printf("后序遍历输出二叉树:"); PostOrder(T); printf("\n"); printf("层序遍历输出二叉树:"); LevelOrder(T); printf("\n"); printf("二叉树的高度:%d",TreeDepth(T)); printf("\n"); printf("请输入要查询的结点:"); scanf("%c\n",&ch); p=Parent(T,ch); if(p) printf("该结点的双亲结点为:%c\n",p->data); else printf("该结点为根节点或不存在!\n"); printf("叶子结点数目:%d",Countleaf(T,count)); printf("\n"); printf("打印二叉树:\n"); PrintTree(T,1); printf("交换每个结点的左孩子和右孩子后,层序遍历为:"); Exchange(T); LevelOrder(T); printf("\n"); return 0; }