BinaryTree-递归遍历&建树--C语言

  • 定义BinaryTree结构
  • 测试用例
  • 递归前中后遍历
  • 中前 中后建树
  • 层序 叶子结点 双亲结点

代码块

#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
typedef char ELELTYPE; 

//定义结构体
typedef struct 
{
    ElemType data;
    BtNode *leftchild;
    BtNode *rightchild;
}BtNode,*BinaryTree;

//申请结点
BtNode * Buynode()
{
    BtNode *s = (BtNode*)malloc(sizeof(BtNode));
    if(s == NULL) exit(1);
    memset(s,0,sizeof(BtNode)); //memset 以字节为单位初始化
    return s;
}

//释放结点
void Freenode(BtNode *p)
{
    free(p);
}

//递归先序遍历二叉树
void PreOrder(BtNode *ptr)
{
    if(ptr!=NULL)
    {
        printf("%c",ptr->data);
        PreOrder(ptr->leftchild);
        PreOrder(ptr->rightchild);
    }
}

//递归中序遍历二叉树
void InOrder(BtNode *ptr)
{
    if(ptr!=NULL)
    {
        InOrder(ptr->leftchild);
        printf("%c",ptr->data);
        InOrder(ptr->rightchild);
    }
}

//递归后序遍历二叉树
void PastOrder(BtNode *ptr)
{
    if(ptr!=NULL)
    {
        PastOrder(ptr->leftchild);
        PastOrder(ptr->rightchild);
        printf("%c",ptr->data);
    }
}

//创建二叉树方式一:屏幕读入 ABC##DE##F##G#H##  (#代表null)
BtNode * CreateTree1()
{
    BtNode *s = NULL;
    ElemType item;
    scanf("%c",&item);
    if(item != '#')
    {
        s = Buynode();
        s->data = item;
        s->leftchild = CreateTree1();
        s->rightchild = CreateTree1();
    }
    return s;
}

//创建二叉树方式二:二级指针 字符串
BtNode * CreateTree3(char ** const pstr)
{
    BtNode *s = NULL;
    // 二级指针       一级指针      值
    if(pstr!=NULL && *pstr!=NULL && **pstr!='#')
    {
        s=Buynode();
        s->data= **pstr;
        s->leftchild = CreateTree3(&++*pstr);
        s->rightchild = CreateTree3(&++*pstr);
    }
    return s;
}

//方式三:中序 前序 创建二叉树
int Find(char *is,int n,char ch)
{
    for(int i = 0;i<n;++i)  //在中序找到pos
    {
        if(is[i]==ch)
        {
        return i;
        }
    }
        return -1;
}

BtNode * Create(char *ps,char *is,int n)
{
    BtNode *s = NULL ;
    if(n>0)
    {
        s=Buynode();
        s->data = ps[0];
        int pos = Find(is,n,ps[0]);
        if(pos==-1){exit(1);}
        //创建左子树(search,searchLine,len)
        s->leftchild = Create(ps+1,is,pos);  
        //创建右子树 (search,searchLine,len)
        s->rightchild = Create(ps+pos+1,is+pos+1,n-pos-1);
    }
    return s;
}

BtNode * CreatePI(char *ps,char *is)
{
    if(ps==NULL || is == NULL)
    {
        return NULL;
    }
    else
    {
        int n = strlen(ps);
        Create(ps,is,n);
    }
}

// 方式四:中序 后序 创建二叉树
BtNode * CreateTree(char *ls,char *is,int n)
{
    BtNode *s =NULL;
    if(n>0)
    {
        int pos = Find(is,n,ls[n-1]);
        if(pos==-1){exit(1);}
        s=Buynode();
        s->data = ls[n-1];

        s->leftchild=CreateTree(ls,is,pos);
        s->rightchild = CreateTree(ls+pos,is+pos+1,n-pos-1);
    }
    return s;

}
BtNode * CreateIL(char *is,char *ls)
{
    if(is==NULL || ls== NULL)
    {
        return NULL;
    }else
    {
        int m = strlen(ls); //is
        CreateTree(ls,is,m);
    }
}
BtNode *FindValue(BtNode *ptr,ElemType x)
{
    if(ptr==NULL || ptr->data==x)
    {
        return ptr;
    }
    else{
        BtNode *p=FindValue(ptr->leftchild,x);
        if(p==NULL){
            FindValue(ptr->rightchild,x);
        }
        return p;
    }
}

//层序遍历二叉树
int front = 0,rear =1;
void LevelOrder(BtNode *ptr)
{
    BtNode *q[100];
    q[0]=ptr;
    while(front<rear)
    {
        if(q[front]){
            printf("%c",q[front]->data);
            q[rear++]=q[front]->leftchild;
            q[rear++]=q[front]->rightchild;
            front++;
        }
        else
        {
            front++;
        }
    }
}

//双分支结点的个数
int SizeBinary(BtNode *ptr)
{
    if(ptr == NULL)
    {
        return -1;
    }
    else
    {
        if(ptr->leftchild != NULL && ptr->rightchild != NULL)
        {
            ++count;
            SizeBinary(ptr->leftchild);
            SizeBinary(ptr->rightchild);
        }else
        {
            if(ptr->leftchild == NULL)
            {
                SizeBinary(ptr->rightchild);
            }else
            {
                SizeBinary(ptr->rightchild);
            }
        }
    }
}

//寻找孩子结点的双亲结点
BtNode * Paret(BtNode *ptr,BtNode *child){
    if(ptr == NULL || ptr->leftchild ==child || ptr->rightchild == child)
    {
    return ptr;
    }
    else{
        BtNode *p = Paret(ptr->leftchild,child);
        if(NULL == p)
        {
            Paret(ptr->rightchild,child);
        }
    }
}

BtNode * FindParet(BtNode *ptr,BtNode *child)
{
    if(ptr == NULL || child ==NULL || ptr==child)
    {
        return NULL;
    }
    else
    {
        return Paret(ptr,child);
    }
}


// 叶子结点个数
int SizeLeaf(BtNode *ptr)
{
    if(ptr == NULL)
    {
        return 0;
    }else if(ptr->leftchild==NULL && ptr->rightchild ==NULL)
    {
     return 1;  
    }
    else
    {
        return SizeLeaf(ptr->leftchild)+SizeLeaf(ptr->rightchild);
    }
}
//结点总个数
int Size(BtNode *ptr){
    if(ptr==NULL)
    {return 0;}
    return 1+Size(ptr->leftchild)+Size(ptr->rightchild);
}
// 树的深度
int Depth(BtNode *ptr)
{
    if(ptr==NULL)
    {
        return 0;
    }
    int dl=Depth(ptr->leftchild);
    int dr=Depth(ptr->rightchild);
    return(dl>dr?dl:dr)+1;
}


void main()
{
    char *ps = "ABCDEFGH";//前
    char *is = "CBEDFAGH";//中
    char *ls = "CEFDBHGA";//后

    //输入序列建树 
    printf("请输入先序序列1:\n");
    BinaryTree root = NULL;
    root = CreateTree1();

    //层序遍历二叉树
    LevelOrder(root);

    //查找
    //FindValue(root,'B');

    //equal比较两树结构相似 数据相等
    char *str = "ABC##DE##F##G#H##";
    char *ptr = "ABC##DE##F##H#G##";
    BinaryTree root1 = NULL;
    BinaryTree root2 = NULL;
    root1 = CreateTree3(&str);
    root2 = CreateTree3(&ptr);
    printf("%d\n",Equal(root1,root2));

    int n =Depth(root);

    //int &a = 100; // 常量 无地址 不能编译通过
    //const int &a =100; //常引用开辟空间 
    //int a = 10;
    //int *ip = &a;
    //int *&is = ip;// 指针的 引用
    //int &*is; //不允许使用【指针】==》引用

    // 测试 后序 中序 建立二叉树
    BinaryTree root4= NULL;
    root4 = CreateIL(is,ls);
    PreOrder(root);
    printf("\n");
    InOrder(root);
    printf("\n");
    PastOrder(root);
    printf("\n");

    //测试 前序 中序 创建二叉树
    BinaryTree root5= NULL;
    root5= CreatePL(ps,is);
    PreOrder(root);
    printf("\n");
    InOrder(root);
    printf("\n");
    PastOrder(root);
    printf("\n");   
}

作业:
// obj1:大数的加减乘除取模判断素数开方次方 文件加密的分布式算法
设计文档+功能函数+模块设计
// obj2:哈夫曼编码 基于哈夫曼编码的文本文件解压缩
// obj3:日历系统 年月日 小时分 对日期进行操作 日程安排表


下次函数:
int SizeOne();
int SizeOneLeft();
int SizeOneRight();

Is_Full_BinaryTree(root); //满二叉树
bool Is_Comp_BinaryTree(BtNode *ptr); //完全二叉树
bool Is_Balance_BinaryTree(BtNode *ptr); //平衡二叉树

//非递归建树 (is ls ps)
void NicePerOrder(BtNode *ptr);
void NiceInOrder(BtNode *ptr);
void NicePastOrder(BtNode *ptr);

// 找两个结点的最近公共双亲结点
BtNode * FindNearParet(BtNode *ptr ,BtNode *child1,BtNode *child2);
// 找这个树的最远两个结点的距离 返回两个结点的地址 …可以用结构体存放 地址 + int
int maxPath(BtNode *ptr);
struct RetNode
{
BtNode *child1;
BtNode *child2;
int path;
};
RetNode RetPath(BtNode *ptr);
int RetTwoMaxPath(BtNode ptr,BtNode &child1,BtNode *&child2);

问题:
//引用和指针的区别 引用的特点:逻辑 物理上 分类
// 写一个 栈 队列 k队列 无锁队列 栈满扩容 堆区无空间??
//如何解决内存不足 单进程无问题 多进程的线程安全 效率 多线程共享
// ++ 叶子结点个数 左分支结点 右分支 双分支??

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章