#include<stdio.h>
#include
<stdlib.h>
typedef
struct BitNode
{
   
char data;
   
struct BitNode* lchild;
   
struct BitNode* rchild;
}BitNode,*Bintree;

#include <stdio.h>
#include <stdlib.h>
#define num 100

Bintree
creatbitree(char *a,char *b,int m);   //创建二叉树
void
preordertraverse(BitNode* t);      //前序遍历二叉树
void
inorder(BitNode *t);
void
postorder(BitNode *t);
int
flag=0;

typedef struct node
{
    char data;
    struct node *lchild,*rchild;
}binTnode;
typedef binTnode * binTree;

int
main()
{
   
BitNode *bt=NULL;
   
char a[10]=”abdgcefh”, b[10]=”dgbaechf”;
   
bt=creatbitree(a,b,8);
   
printf(“前序遍历结果:”);
   
preordertraverse(bt);
   
printf(“n中序遍历结果:”);
   
inorder(bt);
   
printf(“n后序遍历结果:”);
   
postorder(bt);
}

int found;
binTnode *p;

BitNode
* creatbitree(char *a,char *b,int m)//  创建树
{
   
int i;
   
BitNode *s;
   
if(m==0)return NULL;
   
s=(BitNode *)malloc(sizeof(BitNode));
   
s->data=*a;    
   
for(i=0;i<m;i++)
   
{
        if(*a==b[i])break;
   
}
   
s->lchild=creatbitree(a+1,b,i);
   
s->rchild=creatbitree(a+i+1,b+i+1,m-i-1);
//   
printf(“%c”,s->data);
   
return s;
}

binTree createBinTree(binTree bt)     //二叉树建立函数
{
    binTnode * q[num];
    binTnode *s;
    int front,rear;
    char ch;
    ch = getchar();
    bt = NULL;
澳门新葡亰8455下载app,    front = 1;
    rear = 0;      //初始化队列
    while(ch != ‘#’)
    {
        s = NULL;
        if(ch != ‘@’)
        {
            s = (binTnode *)malloc(sizeof(binTnode));
            s -> data = ch;
            s -> lchild = s ->rchild = NULL;    //新节点赋值

void
preordertraverse(Bintree bt)  //前序遍历
{

        }
        rear ++;
        q[rear] = s;
        if(rear == 1)
        bt = s;
        else
        {
            if(s != NULL &&
q[front] != NULL)    //当前结点及其双亲结点都不是虚结点
            
            if(rear % 2 == 0)                //
rear为偶数,新节点应作为左孩子
            q[front] ->lchild = s;
            else
            q[front] ->rchild = s;           //rear
为奇数,新结点应作为右孩子
            
            if(rear % 2 != 0)
            front ++;    //rear为奇数,说明两个孩子已经处理完毕,front指向下一个双亲

   
if(bt!=NULL)
   
{
        printf(“%c”,bt->data);
        preordertraverse(bt->lchild);
        preordertraverse(bt->rchild);
   
}
}

        }
        ch = getchar();    //读取下一个节点的值
    }
    return bt;
}

void
inorder(BitNode *bt) //中序遍历
{
   
if(bt!=NULL)
   
{
        inorder(bt->lchild);
        printf(“%c”,bt->data);
        inorder(bt->rchild);
   
}
}
void
postorder(BitNode *bt) //后序遍历
{
   
if(bt!=NULL)
   
{
        postorder(bt->lchild);
        postorder(bt->rchild);
        printf(“%c”,bt->data);
   
}
}

void nodePath(binTree bt,binTnode
*ch)  //求二叉树根结点到指定结点*p的路径函数

网站地图xml地图