构造二叉搜索树

视频学习地址
代码中树的结构如下图所示
代码中构造树的结构
无栈非递归中序遍历二叉树

#include <stdio.h>
#include <stdlib.h>


typedef struct node{
    char data;
    struct node *lchild,* rchild;
    int ltag,rtag;
}tree;
tree get(char c);
tree * pre = NULL;


int main()
{
    tree a = get('A');
    tree b = get('B');
    tree c = get('C');
    tree d = get('D');
    tree e = get('E');
    tree f = get('F');
    tree g = get('G');
    a.lchild = &b;
    b.lchild = &d;
    b.rchild = &e;
    d.rchild = &g;
    a.rchild = &c;
    c.lchild = &f;

    thread(&a);
    pre->rtag = 1;
    printf("\n");
    newThread(a);


}

tree get(char c){
    tree t;
    t.data = c;
    t.ltag = 0;
    t.rtag = 0;
    t.lchild = NULL;
    t.rchild = NULL;
    return t;
}


void thread(tree * root){
    if(root == NULL)return;

    if(root->ltag == 0)
    thread(root->lchild);

    visit(root);
    if(root->rtag == 0)
    thread(root->rchild);


}

void visit(tree * root){
    printf("%c\t",root->data);
    if(root->lchild == NULL){
        root->lchild = pre;
        root->ltag = 1;
    }
    if(pre != NULL && pre->rchild == NULL){
        pre->rchild = root;
        pre->rtag = 1;
    }
    pre = root;
}


tree * fristNode(tree * root){
    while(root->ltag != 1)root = root->lchild;
    return root;
}


tree * nextNode(tree * root){
    if(root->rtag == 0 )return fristNode(root->rchild);
    return root->rchild;
}

void newThread(tree root){
    for(tree * p = fristNode(&root);p != NULL;p = nextNode(p)){
        printf("%c\t",p->data);
    }
}

中序遍历的逆遍历

#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <math.h>


typedef struct node{
    char data;
    struct node *lchild,* rchild;
    int ltag,rtag;
}tree;
tree get(char c);
tree * pre = NULL;


int main()
{
    tree a = get('A');
    tree b = get('B');
    tree c = get('C');
    tree d = get('D');
    tree e = get('E');
    tree f = get('F');
    tree g = get('G');
    a.lchild = &b;
    b.lchild = &d;
    b.rchild = &e;
    d.rchild = &g;
    a.rchild = &c;
    c.lchild = &f;

    thread(&a);
    pre->rtag = 1;
    printf("\n");
    newThread(a);


}

tree get(char c){
    tree t;
    t.data = c;
    t.ltag = 0;
    t.rtag = 0;
    t.lchild = NULL;
    t.rchild = NULL;
    return t;
}


void thread(tree * root){
    if(root == NULL)return;

    if(root->ltag == 0)
    thread(root->lchild);

    visit(root);
    if(root->rtag == 0)
    thread(root->rchild);


}

void visit(tree * root){
    printf("%c\t",root->data);
    if(root->lchild == NULL){
        root->lchild = pre;
        root->ltag = 1;
    }
    if(pre != NULL && pre->rchild == NULL){
        pre->rchild = root;
        pre->rtag = 1;
    }
    pre = root;
}


tree* lastNode(tree * root){
    while(root->rtag != 1)root = root->rchild;
    return root;
}


tree * preNode(tree * root){
    if(root->ltag == 0 )return lastNode(root->lchild);
    return root->lchild;
}

void newThread(tree root){
    for(tree * p = lastNode(&root);p != NULL;p = preNode(p)){
        printf("%c\t",p->data);
    }
}

先序遍历

#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <math.h>


typedef struct node{
    char data;
    struct node *lchild,* rchild;
    int ltag,rtag;
}tree;
tree get(char c);
tree * pre = NULL;


int main()
{
    tree a = get('A');
    tree b = get('B');
    tree c = get('C');
    tree d = get('D');
    tree e = get('E');
    tree f = get('F');
    tree g = get('G');
    a.lchild = &b;
    b.lchild = &d;
    b.rchild = &e;
    d.rchild = &g;
    a.rchild = &c;
    c.lchild = &f;

    thread(&a);
    pre->rtag = 1;
    printf("\n");
    newThread(a);


}

tree get(char c){
    tree t;
    t.data = c;
    t.ltag = 0;
    t.rtag = 0;
    t.lchild = NULL;
    t.rchild = NULL;
    return t;
}


void thread(tree * root){
    if(root == NULL)return;
    visit(root);
    if(root->ltag == 0)
    thread(root->lchild);


    if(root->rtag == 0)
    thread(root->rchild);


}

void visit(tree * root){
    printf("%c\t",root->data);
    if(root->lchild == NULL){
        root->lchild = pre;
        root->ltag = 1;
    }
    if(pre != NULL && pre->rchild == NULL){
        pre->rchild = root;
        pre->rtag = 1;
    }
    pre = root;
}




tree * nextNode(tree * root){
    if(root->ltag == 0 )return root->lchild;
    return root->rchild;
}

void newThread(tree root){
    for(tree * p = &root;p != NULL;p = nextNode(p)){
        printf("%c\t",p->data);
    }
}

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值