二叉树递归C实现

重点内容
本文是简单的用递归的方法实现了二叉树

虽然看似是C++ 实际上使用C的风格完成的
之后会补上C++的代码。

二叉树的节点建立

typedef char ElemType;

#define END '#'

typedef struct BtNode
{
    ElemType data;   //数据域
    BtNode *leftchild;//左孩子
    BtNode *rightchild;//右孩子
}BtNode, *BinaryTree;
  1. 对于二叉树首先我们得实现三种遍历

    先序遍历/前序遍历

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

中序遍历

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

后序遍历

void PostOrder(BtNode *p)
{
    if (p != NULL)
    {
        PostOrder(p->leftchild);
        PostOrder(p->rightchild);
        cout << p->data << endl;
    }
}
  1. 接下来让我们实现建立树
    (1)
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;
}

(2)

void CreateTree2(BtNode *&p) //注意这里 引用的用法!!!
{
    ElemType x;
    cin >> x;
    if (x == END) return;
    else
    {
        p = _Buynode();
        p->data = x;
        CreateTree2(p->leftchild);
        CreateTree2(p->rightchild);
    }
}

(3) 用字符串构建

BtNode *CreateTree3(char *&str)
{
    BtNode *p = NULL;
    if (str != NULL && *str != END)
    {
        p = _Buynode();
        p->data = *str;
        p->leftchild = CreateTree3(++str);
        p->rightchild = CreateTree3(++str);
    }
    return p;
}

整体代码 //测试用例 ABC##DE##F##G#H##

#include <iostream>
#include <cstring>
#include <malloc.h>
using namespace std;


typedef char ElemType;

#define END '#'

typedef struct BtNode
{
    ElemType data;
    BtNode *leftchild;
    BtNode *rightchild;
}BtNode, *BinaryTree;

BtNode* _Buynode()
{
    BtNode *s = NULL;
    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 << endl;
        PreOrder(p->leftchild);
        PreOrder(p->rightchild);
    }
}

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

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

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;
}

void CreateTree2(BtNode *&p)
{
    ElemType x;
    cin >> x;
    if (x == END) return;
    else
    {
        p = _Buynode();
        p->data = x;
        CreateTree2(p->leftchild);
        CreateTree2(p->rightchild);
    }
}

BtNode *CreateTree3(char *&str)
{
    BtNode *p = NULL;
    if (str != NULL && *str != END)
    {
        p = _Buynode();
        p->data = *str;
        p->leftchild = CreateTree3(++str);
        p->rightchild = CreateTree3(++str);
    }
    return p;
}
int main()
{
    BinaryTree root = NULL;
    //root = CreateTree1();
    //CreateTree2(root);
    char *str = "ABC##DE##F##G#H##";
    root = CreateTree3(str);
    PreOrder(root);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/death_include/article/details/51420722