4.2 线索二叉树

1. 创建

#include <iostream>

using namespace std;

typedef struct node{
    dtype data;
    struct node *lchild;
    struct node *rchild;
    int ltag,rtag;
}Node;

typedef char dtype;

//先序创建树
void PreCreate(Node * &T){
    dtype ch;
    cin>>ch;
    if(ch=='#') T=NULL;
    else{
        T=new Node;
        T->data=ch;
        T->ltag=T->rtag=0;
        CreateTree(T->lchild);
        CreateTree(T->rchild);
    }
}

//中序创建树
int InCreate(Node * &T){
    dtype ch;
    if (ch == "#") T = NULL;
    else {
        T=new Node;
        T->ltag = T->rtag=0;
        InCreate(T->lchild);
        T->data = ch;
        InCreate(T->rchild);
    }
}

//后序创建树
int PostCreate(Node * &T, Node * &parent){
    dtype ch;
    if (ch == '#') T = NULL;
    else {
        T=new Node;
        T->parent = parent;
        T->ltag = T->rtag = 0;
        PostCreate(T->lchild, T);
        PostCreate(T->rchild, T);
        T->data = ch;
    }
}

int main(){
    
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/Alexagender/p/10806328.html