二叉数的递归建立

二叉数的递归建立,第1张

二叉数的递归建立
#include 
using namespace std;
const int LEN = 1100;
typedef struct BiTNode{
    char data;
    struct BiTNode *lchild , *rchlid;
}BiNode , *BiTree;
BiTree T;
char a[LEN][LEN];
int cnt = 0;
int ubound , rbound;

void Recode(BiTree T , int u , int x)
{
    if (T) {
        a[u][x] = T -> data;
        if (T -> data == 'H') a[u][x + 1]  = 'H' , a[u][x - 1] = 'D' , a[u][x] = ' ';
        if (T -> lchild) a[u + 1][x - 1] = '/';
        if (T -> rchlid) a[u + 1][x + 1] = '\' ;
        // cout << "$ "?? << a[u][x] << endl;
        if (T -> lchild) ubound = max(ubound , u + 2);
        if (T -> rchlid) ubound = max(ubound , u + 2);
        Recode(T -> lchild , u + 2, x - 2);
        Recode(T -> rchlid , u + 2, x + 2);
        
    }
}

void Show(){
    cnt ++;
    ubound = 1;
    Recode(T , 1 , 10);
    for (int i = 1;i <= ubound;i ++) {
        for (int j = 1;j <= 50;j ++) 
            cout << a[i][j];
        cout << endl;
    }
    cout << cnt << " 'Tree。nn";
}
//FCA##DB###EH##GM###

void CreateBiTree(BiTree &T)
{
    char c;cin >> c;
    if (c == '#') {
        T = NULL;
    }
    else {
        T = (BiTree)malloc(sizeof(BiTNode));
        T -> data = c;
        T -> lchild = NULL;
        T -> rchlid = NULL;
        Show();
        CreateBiTree(T -> lchild);
        CreateBiTree(T -> rchlid);
    }
}

void Visit(char data)
{
    printf("%cn" , data);
}

void init()
{
    for (int i = 1; i <= 100;i ++) 
        for (int j = 1;j <= 100;j ++)    
            a[i][j] = ' ';
}

void PreOrderTraverse(BiTree T)
{
    if (T) {
        Visit(T -> data);
        PreOrderTraverse(T -> lchild);
        PreOrderTraverse(T -> rchlid);
    }
}

int main()
{
    init();
    CreateBiTree(T);
    // PreOrderTraverse(T);
    return 0;
}
//FCA##DB###EH##GM###

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5594856.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-15
下一篇 2022-12-15

发表评论

登录后才能评论

评论列表(0条)

保存