二叉树层序建立、中序线索化(c语言)

层序建立思路

使用队列存储每一个节点,每次出队一个节点为其连接子节点,然后子节点入队,这里使用“/”分割字符串

typedef struct node
{
    char data;
    struct node *left;
    struct node *right;
    int l, r;
} node;
node *pre = NULL;
node *creatnode(char c)
{
    node *tmp = (node *)malloc(sizeof(node));
    tmp->data = c;
    tmp->left = NULL;
    tmp->right = NULL;
    tmp->l = 0;
    tmp->r = 0;
    return tmp;
}
node *build(char *str)
{
    int len = strlen(str);
    int f = 0, r = -1;
    node *p;
    node **tmp = (node **)malloc(sizeof(node *) * len);
    tmp[++r] = creatnode(str[0]);
    for (int i = 1; i < len; i++)
    {
        p = tmp[f++];

        if (str[i] != '/')
        {
            tmp[++r] = creatnode(str[i]);
            p->left = tmp[r];
        }
        i++;
        if (i >= len)
            break;
        if (str[i] != '/')
        {
            tmp[++r] = creatnode(str[i]);
            p->right = tmp[r];
        }
   
    }
    return tmp[0];
}

建立后进行中序线索化,推荐看懒猫老师-数据结构-(31)线索二叉树1(线索链表,二叉树线索化)

 


void pre_inorder(node *root)
{
    if (root == NULL)
        return;
    pre_inorder(root->left);
    if (root->left == NULL)
    {
        root->l = 1;
        root->left = pre;
    }
    if (pre != NULL && pre->right == NULL)
    {
        pre->r = 1;
        pre->right = root;
    }
    pre = root;
    pre_inorder(root->right);
}

然后是中序线索树的遍历


node *next(node *root)
{
    node *q = root;
    if (q->r == 1)
        return q->right;
    else
    {
        q = q->right;
        while (q->l == 0)
            q = q->left;
    }
    return q;
}
void pre_visit(node *root)
{
    if (root == NULL)
        return;

    while (root->l == 0)
    {
        root = root->left;
    }
    printf("%c ", root->data);
    while (root->right)
    {

        if (root->r == 1)
        {
            root = root->right;
            printf("%c ", root->data);
            
        }

        else
        {
            root = root->right;
            while (root->l == 0)
                root = root->left;
            printf("%c ", root->data);
        } // It is equivalent to the following code
        // root = next(root);
        // printf("%c ", q->data);
    }
}

完整代码


typedef struct node
{
    char data;
    struct node *left;
    struct node *right;
    int l, r;
} node;
node *pre = NULL;
node *creatnode(char c)
{
    node *tmp = (node *)malloc(sizeof(node));
    tmp->data = c;
    tmp->left = NULL;
    tmp->right = NULL;
    tmp->l = 0;
    tmp->r = 0;
    return tmp;
}
node *build(char *str)
{
    int len = strlen(str);
    int f = 0, r = -1;
    node *p;
    node **tmp = (node **)malloc(sizeof(node *) * len);
    tmp[++r] = creatnode(str[0]);
    for (int i = 1; i < len; i++)
    {
        p = tmp[f++];

        if (str[i] != '/')
        {
            tmp[++r] = creatnode(str[i]);
            p->left = tmp[r];
        }
        i++;
        if (i >= len)
            break;
        if (str[i] != '/')
        {
            tmp[++r] = creatnode(str[i]);
            p->right = tmp[r];
        }
   
    }
    return tmp[0];
}
void inorder(node *root) // Inorder Traversal
{
    if (root == NULL)
        return;
    inorder(root->left);
    printf("%c\n", root->data);
    inorder(root->right);
}
void pre_inorder(node *root)
{
    if (root == NULL)
        return;
    pre_inorder(root->left);
    if (root->left == NULL)
    {
        root->l = 1;
        root->left = pre;
    }
    if (pre != NULL && pre->right == NULL)
    {
        pre->r = 1;
        pre->right = root;
    }
    pre = root;
    pre_inorder(root->right);
}
node *next(node *root)
{
    node *q = root;
    if (q->r == 1)
        return q->right;
    else
    {
        q = q->right;
        while (q->l == 0)
            q = q->left;
    }
    return q;
}
void pre_visit(node *root)
{
    if (root == NULL)
        return;

    while (root->l == 0)
    {
        root = root->left;
    }
    printf("%c ", root->data);
    while (root->right)
    {

        if (root->r == 1)
        {
            root = root->right;
            printf("%c ", root->data);
            
        }

        else
        {
            root = root->right;
            while (root->l == 0)
                root = root->left;
            printf("%c ", root->data);
        } // It is equivalent to the following code
        // root = next(root);
        // printf("%c ", q->data);
    }
}
int main()
{
    char str[] = "123/4567";
    build(str);
    node *root = build(str);
    pre_inorder(root);
    // inorder(root);
    pre_visit(root);
    system("pause");
    return 0;
}

 

 

posted @ 2024-01-05 22:18  CV小能手chh  阅读(9)  评论(0)    收藏  举报  来源