链式二叉树(三种遍历)

1.链式二叉树的遍历:前序(根,左子树,右子树)中序(左子树,根,右子树)后序(左子树,右子树,根)层序(一层一层访问)

2.任何一个树都可以分为根,左子树和右子树,可以往下继续分, 只有空树才不会被继续拆解

#include 
#include
typedef struct binarytreenode
{
    int data;
    struct binarytreenode* left;
    struct binarytreenode* right;
}BTNode;
BTNode* BYNode(int x)
{
    BTNode* point = (BTNode*)malloc(sizeof(BTNode));
    if (point == NULL)
    {
        printf("error");
    }
    point->data = x;
    point->left = point->right = NULL;
    return point;
}
void prevorder(BTNode* point)
{
    if (point == NULL)
    {
        printf("null");
        return;
    }
    
        printf("%d", point->data);
        prevorder(point->left);
        prevorder(point->right);
}
void inorder(BTNode* point)
{
    if (point == NULL)
    {
        printf("null");
        return;
    }

    
    prevorder(point->left);
    printf("%d", point->data);
    prevorder(point->right);
}
void backorder(BTNode* point)
{
    if (point == NULL)
    {
        printf("null");
        return;
    }


    prevorder(point->left);
    
    prevorder(point->right);
    printf("%d", point->data);
}
int main()
{
    BTNode* point1 = BYNode(1);
    BTNode* point2 = BYNode(2);
    BTNode* point3 = BYNode(3);
    BTNode* point4 = BYNode(4);
    BTNode* point5= BYNode(5);
    BTNode* point6 = BYNode(6);
    point1->left = point2;
    point1->right= point4;
    point2->left = point3;
    point4->left = point5;
    point4->right = point6;
    prevorder(point1);
    return 0;
}

你可能感兴趣的:(数据结构,算法)