漫漫编程路——C篇(一)——关于创建二叉树及其遍历

来源:互联网 发布:js button value值 编辑:程序博客网 时间:2024/05/16 23:57
写了几篇C++的,原本不想写数据结构的,因为我对自己抄袭书上的算法的行为深以为耻,但既然写了C++不写C好像对不起C语言,平衡一下,就有了这个C篇了,但是我的数据结构的程序剩下的确实不多了,就先写这个二叉树吧。
  二叉树有几种遍历的方法,这里只列出了前序创建和中序遍历,并使用三种方法。
程序:
方法一:使用最复杂的栈的方法,并且是非递归的。
#include<stdio.h>
#include<stdlib.h>
typedef struct BiTNode{
char data;
struct BiTNode *lchild,*rchild;
}BiTNode,*BiTree;
typedef struct{
BiTree *base;
BiTree *top;
int stacksize;
}SqStack;
int visit(BiTree e){
    printf("%c",e->data);
    return 1;
}
void CreateBiTree(BiTree &T){
    char ch;
scanf("%c",&ch);
if(ch=='#') T=NULL;
else
{
if(!(T=(BiTNode*)malloc(sizeof(BiTNode))))
printf("overflow");
T->data=ch;
CreateBiTree(T->lchild);
CreateBiTree(T->rchild);
}
}
void InitStack(SqStack &S){
S.base=(BiTree *)malloc(100*sizeof(BiTree));
if(!S.base) printf("存储分配失败");
S.top=S.base;
S.stacksize=10;
}
void Push(SqStack &s,BiTree e){
    if(s.top-s.base>=s.stacksize){
    s.base=(BiTree *)realloc(s.base,(s.stacksize+10)*sizeof(BiTree));
    if(!s.base) printf("存储分配失败");
    s.top=s.base+s.stacksize;
    s.stacksize+=10;
    }
    *s.top++=e;
}
void Pop(SqStack &s,BiTree &e){
if(s.top==s.base) printf("错误");
e=*--s.top;
}
BiTree GetTop(SqStack S,BiTree &e){
    if(S.base==S.top)
        printf("错误!");
    e=*(S.top-1);
    return e;
}
void Inorder(BiTree T)
{
    BiTree p;
SqStack S;
InitStack(S);
p=T;
Push(S,T);
while(!(S.top==S.base)){
while(GetTop(S,p)&&p)
Push(S,p->lchild);
Pop(S,p);
if(!(S.top==S.base)){
Pop(S,p);
visit(p);
Push(S,p->rchild);
}
}
}
void main(){
    BiTree T;
    printf("请创建二叉树:");
 CreateBiTree(T);
 printf("创建成功!/n");
 printf("中序遍历二叉树:");
Inorder(T);
}
方法二:不使用栈的非递归方法
#include<stdio.h>
#include<stdlib.h>
typedef struct BiTNode{
char data;
struct BiTNode *lchild,*rchild;
}BiTNode,*BiTree;
void CreateBiTree(BiTree &T){
    char ch;
scanf("%c",&ch);
if(ch=='#') T=NULL;
else
{
if(!(T=(BiTNode*)malloc(sizeof(BiTNode))))
printf("overflow");
T->data=ch;
CreateBiTree(T->lchild);
CreateBiTree(T->rchild);
}
}
void Inorder(BiTree T)
{
BiTree t[100];
int top=0;
while(T||top)
{
while(T)
{
t[++top]=T;T=T->lchild;
}
if(top)
{
printf("%c",t[top]->data);
T=t[top--]->rchild;
}
}
}
void main(){
    BiTree T;
    printf("请创建二叉树:");
 CreateBiTree(T);
 printf("创建成功!/n");
 printf("中序遍历二叉树:");
Inorder(T);
}
方法三:剩下的是递归方法了
#include<stdio.h>
#include<stdlib.h>
typedef struct BiTNode{
char data;
struct BiTNode *lchild,*rchild;
}BiTNode,*BiTree;
void CreateBiTree(BiTree &T){
    char ch;
scanf("%c",&ch);
if(ch=='#') T=NULL;
else
{
if(!(T=(BiTNode*)malloc(sizeof(BiTNode))))
printf("overflow");
T->data=ch;
CreateBiTree(T->lchild);
CreateBiTree(T->rchild);
}
}
void Inorder(BiTree T)
{
     if(T!=NULL)
     {
         Inorder(T->lchild);
          printf("%c",T->data);
          Inorder(T->rchild);
     }
}
void main(){
    BiTree T;
    printf("请创建二叉树:");
 CreateBiTree(T);
 printf("创建成功!/n");
 printf("中序遍历二叉树:");
Inorder(T);
}
三种方法中,递归的最简单,并不推荐,而第一种是比较好的方法,只要理解算法就能比较容易得写出程序来了,虽然算法一样,但每个人的理解不同还是会出现不同的方案的,龙还生九子呢,数据结构还是以理解为主,向我这种不劳而获的方式不太可取,惭愧啊
原创粉丝点击