栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

二叉数的递归建立

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

二叉数的递归建立

#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###

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/589468.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号