二叉树基本处理问题

这里写了一些有关二叉树的一些最基本的问题和常见的函数,如二叉树的遍历,构造等。
一、二叉树节点构造

typedef char ElemType;
#define END '#'
typedef struct BtNode
{
    BtNode *leftchild;
    BtNode *rightchild;
    ElemType data;
}BtNode, *BinaryTree;

以上代码是二叉树的基本节点。

二、三种遍历函数

void PreOrder(BtNode *p)//前序遍历
{
    if(p != NULL)
    {
        cout<<p->data<<" ";
        PreOrder(p->leftchild);
        PreOrder(p->rightchild);
    }
}
void InOrder(BtNode *p)//中序遍历
{
    if(p != NULL)
    {
        InOrder(p->leftchild);
        cout<<p->data<<" ";
        InOrder(p->rightchild);
    }
}

void PastOrder(BtNode *p)//后序遍历
{
    if(p != NULL)
    {
        PastOrder(p->leftchild);
        PastOrder(p->rightchild);
        cout<<p->data<<" ";
    }
}

三、构造二叉树的几种方法
方法一:依次输入建立二叉树
方法二:传递字符串建立二叉树,但是要注意需要传递引用,而不能是单纯的一个字符串指针
方法三:是方法二的改进,也是传递一个字符串,参数列表写成*const,就是等于& ,也就是讲,传递了一个二级指针,这个二级指针的字符串是一个静态的不允许修改的。所以在函数内部需要先解引用,然后++,然后再取地址。
方法四:无返回值的构造二叉树的方式。

代码如下:

BtNode * CreateTree1()
{
    ElemType x;
    cin>>x;
    BtNode *s = NULL;
    if(x != END)
    {
        s = _Buynode();
        s->data = x;
        s->leftchild = CreateTree1();
        s->rightchild = CreateTree1();
    }
    return s;
}
BtNode * CreateTree2(ElemType *&str)
{
    BtNode *s = NULL;
    if(str != NULL && *str != END)
    {
        s = _Buynode();
        s->data = *str;
        s->leftchild = CreateTree2(++str);
        s->rightchild = CreateTree2(++str);
    }
    return s;
}
BtNode * CreateTree3(ElemType * * const pstr)
{
    BtNode *s = NULL;
    if(pstr != NULL && *pstr != NULL && **pstr != END)
    {
        s = _Buynode();
        s->data = **pstr;
        s->leftchild = CreateTree3(&++*pstr);
        s->rightchild = CreateTree3(&++*pstr);
    }
    return s;
}
// 6
void CreateTree4(BtNode *&ptr)
{
    ElemType x;
    cin>>x;
    if(x == END) { ptr = NULL;}
    else
    {
        ptr = _Buynode();
        ptr->data = x;
        CreateTree4(ptr->leftchild);
        CreateTree4(ptr->rightchild);
    }
}

全部代码如下:

#include<iostream>
using namespace std;

typedef char ElemType;
#define END '#'
typedef struct BtNode
{
    BtNode *leftchild;
    BtNode *rightchild;
    ElemType data;
}BtNode, *BinaryTree;
BtNode * _Buynode()
{
    BtNode *s = (BtNode*)malloc(sizeof(BtNode));
    if(NULL == s) exit(1);
    memset(s,0,sizeof(BtNode));
    return s;
}
void _Freenode(BtNode *p)
{
    free(p);
}

void PreOrder(BtNode *p)
{
    if(p != NULL)
    {
        cout<<p->data<<" ";
        PreOrder(p->leftchild);
        PreOrder(p->rightchild);
    }
}
void InOrder(BtNode *p)
{
    if(p != NULL)
    {
        InOrder(p->leftchild);
        cout<<p->data<<" ";
        InOrder(p->rightchild);
    }
}

void PastOrder(BtNode *p)
{
    if(p != NULL)
    {
        PastOrder(p->leftchild);
        PastOrder(p->rightchild);
        cout<<p->data<<" ";
    }
}

BtNode * CreateTree1()
{
    ElemType x;
    cin>>x;
    BtNode *s = NULL;
    if(x != END)
    {
        s = _Buynode();
        s->data = x;
        s->leftchild = CreateTree1();
        s->rightchild = CreateTree1();
    }
    return s;
}
BtNode * CreateTree2(ElemType *&str)
{
    BtNode *s = NULL;
    if(str != NULL && *str != END)
    {
        s = _Buynode();
        s->data = *str;
        s->leftchild = CreateTree2(++str);
        s->rightchild = CreateTree2(++str);
    }
    return s;
}
BtNode * CreateTree3(ElemType * * const pstr)
{
    BtNode *s = NULL;
    if(pstr != NULL && *pstr != NULL && **pstr != END)
    {
        s = _Buynode();
        s->data = **pstr;
        s->leftchild = CreateTree3(&++*pstr);
        s->rightchild = CreateTree3(&++*pstr);
    }
    return s;
}
// 6
void CreateTree4(BtNode *&ptr)
{
    ElemType x;
    cin>>x;
    if(x == END) { ptr = NULL;}
    else
    {
        ptr = _Buynode();
        ptr->data = x;
        CreateTree4(ptr->leftchild);
        CreateTree4(ptr->rightchild);
    }
}
void main()
{
    char *str= "ABC##DE##F##G#H##";
    BinaryTree root = NULL;
    CreateTree4(root);
    PreOrder(root); cout<<endl;
    InOrder(root); cout<<endl;
    PastOrder(root); cout<<endl;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值