C语言数据结构二叉树简单应用(递归和非递归)

#include
#include
#include
#define MAXSIZE 100
int count = 0;
typedef char datatype;
typedef struct tree//二叉树结构体
{
 datatype info;
 struct tree* lchild;
 struct tree* rchild;
}bintree;
typedef struct stack//顺序栈结构体
{
 bintree* info[100];
 int tag[100];
 int top;
}seqstack;
bintree* createbintree();//创建二叉树
void preprint(bintree* r);//前序遍历输出(递归)
void inprint(bintree* r);//中序遍历输出(递归)
void postprint(bintree* r);//后序遍历输出(递归)
void _preprint(bintree* r, bintree* const root);//前序遍历输出(非递归)
void _inprint(bintree* r, bintree* const root);//中序遍历输出(非遍历)
void _postprint(bintree* r, bintree* const root);//后序遍历输出(非遍历)
void pushstack(seqstack* s, bintree* r);//进栈
bintree* popstack(seqstack* s);//出栈
void display(bintree* r);//打印界面
void countleaves(bintree* r);//计算叶子节点数(递归)
void _countleaves(bintree* r);//计算叶子节点数(非递归)
bintree* lastinprint(bintree* r);//返回中序遍历下最后一个结点
bintree* getparent(bintree* r, datatype a, datatype b);//取得两节点最近共同祖先
seqstack* findchildren(bintree* r, datatype a, datatype b);//寻找两节点位置
void getParent1(bintree* r);//调用getparent和findchildren两函数

二叉树创建

bintree* createbintree()//创建二叉树
{
 char ch;
 bintree* root;
 scanf("%c",&ch);
 if (ch == '#') return NULL;
 else {
  root = (bintree*)malloc(sizeof(bintree));
  root->info = ch;
  root->lchild=createbintree();
  root->rchild=createbintree();
 }
 return root;
}

二叉树三种递归遍历

void preprint(bintree* r)//前序遍历输出(递归)
{
 if (!r)return; 
 printf("%c",r->info);
 preprint(r->lchild);
 preprint(r->rchild);
}

void inprint(bintree* r)//中序遍历输出(递归)
{
 if (!r)return; 
 inprint(r->lchild);
 printf("%c", r->info);
 inprint(r->rchild);
}

void postprint(bintree* r)//后序遍历输出(递归)
{
 if (!r)return; 
 postprint(r->lchild);
 postprint(r->rchild);
 printf("%c",r->info);
}

二叉树三种非递归遍历

void pushstack(seqstack* s, bintree* r)//进栈
{
 s->info[s->top++] = r;
}
bintree* popstack(seqstack* s)//出栈
{
 if (s->top) return s->info[--s->top];
 else return NULL;
}
void _preprint(bintree* r)//前序遍历输出(非递归)
{
 seqstack* s;
 s = (seqstack*)malloc(sizeof(seqstack));
 s->top = 0;
 while (r || s->top) {
  if(r){
   printf("%c",r->info);
   pushstack(s, r);
   r = r->lchild;
  }
  else {
   r=popstack(s);
   r = r->rchild;
  }
 }
 free(s);
}
void _inprint(bintree* r)//中序遍历输出(非遍历)
{
 seqstack* s;
 s = (seqstack*)malloc(sizeof(seqstack));
 s->top = 0;
 while (r || s->top) {
  if (r) {
   pushstack(s, r);
   r = r->lchild;
  }
  else {
   r=popstack(s);
   printf("%c",r->info);
   r = r->rchild;
  }
 }
 free(s);
}
void _postprint(bintree* r)//后序遍历输出(非遍历)
{
 seqstack* s;
 s = (seqstack*)malloc(sizeof(seqstack));
 s->top = 0;
 while (r || s->top) {
  if (r) {
   s->info[s->top] = r;
   s->tag[s->top++] = 0;
   r = r->lchild;
  }
  else {
   if (s->tag[s->top - 1] == 1) {
    r = s->info[--s->top];
    printf("%c",r->info);
    r = NULL;
   }
   else {
    r = s->info[s->top - 1];
    s->tag[s->top - 1] = 1;
    r = r->rchild;
   }
  }
 }
 free(s);
}

递归与非递归计算叶子节点数

void countleaves(bintree* r)//计算叶子节点数(递归)
{
 if (!r)return;
 if (!(r->rchild) && !(r->lchild))count++;
 countleaves(r->lchild);
 countleaves(r->rchild);
 return;
}
void _countleaves(bintree* r)//计算叶子节点数(非递归)
{
 seqstack* s;
 s = (seqstack*)malloc(sizeof(seqstack));
 s->top = 0;
 while (r || s->top) {
  if (r) {
   if (!(r->lchild) && !(r->rchild))count++;
   pushstack(s, r);
   r = r->lchild;
  }
  else {
   r = popstack(s);
   r = r->rchild;
  }
 }
 free(s);
}

获得中序遍历最后一个节点

bintree* lastinprint(bintree* r)//返回中序遍历下最后一个结点
{
 if (r && r->rchild)r = lastinprint(r->rchild);
 return r;
}

取得两节点最近共同祖先

bintree* getparent(bintree* r,bintree* a,bintree* b)//取得两节点最近共同祖先
{
 if (!r|| !a|| !b) return NULL;
 if (r->info == a->info || r->info == b->info) return r;
 bintree* left = getparent(r->lchild, a,b);
 bintree* right = getparent(r->rchild, a,b);
 //如果左右子树都能找到,那么当前节点就是最近的公共祖先节点
 if (left && right) return r;
 //如果左子树上没有,那么返回右子树的查找结果
 if (!left) return right;
 //否则返回左子树的查找结果
 else return left;
}
void getParent1(bintree* r)
{
 char a, b;
 fflush(stdin);
 getchar();
 printf("请输入两个结点的值:");
 a = getchar();
 b = getchar();
 seqstack* s = NULL;
 s = findchildren(r, a,b);
 printf("节点%c和%c的最近共同祖先为:%c\n",a,b,getparent(r, s->info[0], s->info[1])->info);
}
seqstack* findchildren(bintree* r, datatype a, datatype b)//寻找两子节点位置
{
 seqstack* ab;
 seqstack* s;
 ab= (seqstack*)malloc(sizeof(seqstack));
 s = (seqstack*)malloc(sizeof(seqstack));
 s->top = 0;
 ab->top = 0;
 while (r || s->top) {
  if (r) {
   if (ab->top >= 2)return ab;
   if (r->info == a || r->info == b)ab->info[ab->top++] = r;
   pushstack(s, r);
   r = r->lchild;
  }
  else {
   r = popstack(s);
   r = r->rchild;
  }
 }
}

界面函数和主函数

void display(bintree* r)//打印界面
{
 system("cls");
 printf("=======================================\n");
 printf("=============二叉树====================\n");
 printf("========1.前序遍历输出=================\n");
 printf("========2.中序遍历输出=================\n");
 printf("========3.后序遍历输出=================\n");
 printf("========4.计算叶子节点数===============\n");
 printf("========5.返回中序遍历最后一个结点=====\n");
 printf("========6.返回两节点间共同祖先=========\n");
 printf("========0.退出程序=====================\n");
 printf("=======================================\n");
 char a;
 while (1) {
  a = getch();
  switch (a) {
   case '1': {printf("前序遍历输出结果为:"); preprint(r); printf("\n按任意键返回"); getch(); display(r); break; }
   case '2': {printf("中序遍历输出结果为:"); inprint(r);  printf("\n按任意键返回"); getch(); display(r); break; }
   case '3': {printf("后序遍历输出结果为:"); postprint(r);  printf("\n按任意键返回"); getch(); display(r);  break; }
   case '4': {countleaves(r); printf("叶子节点数为:%d\n", count); printf("\n按任意键返回"); getch(); count = 0; display(r);  break; }
   case '5': {printf("中序遍历最后一个节点为:%c\n",lastinprint(r)->info);  printf("\n按任意键返回"); getch(); display(r); break; }
   case '6': { getParent1(r); printf("\n按任意键返回"); getch(); display(r);  break; }
   case '0': {exit(0); }
  }
 }
}
int main()
{
 printf("正在建立二叉树:\n");
 printf("请输入字符,以#作为节点的结束:");
 bintree* root = NULL;
 root=createbintree();
 while(1)display(root);
 return 0;
}

你可能感兴趣的:(数据结构,二叉树,数据结构,C语言)