BUAA 2014级数据结构第五次上机 二叉树之数组转换广义表

本文介绍了一种构建二叉树的方法,并通过递归实现先序遍历输出结果。具体步骤包括初始化节点、读取输入数据构建树结构及进行先序遍历打印。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

按题意建立好二叉树,再按照先序遍历输出结果。

 

#include<cstdio>
#include<vector>
#include<queue>
#include<string.h>
#include<algorithm>
using namespace std;

struct node
{
    int left, right, date;

}node[1005];

int a[1005], flag[1005];

void dfs(int father)
{
    printf("%d", node[father].date);
    if (node[father].left != -1 || node[father].right != -1) printf("(");
    if (node[father].left != -1 && node[father].right == -1)
    {
        dfs(node[father].left);
        printf(")");
    }
    else if (node[father].left == -1 && node[father].right != -1)
    {
        printf(",");
        dfs(node[father].right);
        printf(")");
    }
    else if (node[father].left != -1 && node[father].right != -1)
    {
        dfs(node[father].left);
        printf(",");
        dfs(node[father].right);
        printf(")");
    }
    
}

int main()
{
    int n, i;
    while (~scanf("%d", &n))
    {
        int numm = 1;
        for (i = 0; i <= 1000; i++)
        {
            node[i].date = -1;
            node[i].left = -1;
            node[i].right = -1;
        }
        memset(flag, 0, sizeof(flag));
        for (i = 1; i <= n; i++) scanf("%d", &a[i]);
        int father = 1, jishu = 0;
        node[numm].date = a[1]; numm++;
        for (i = 2; i <= n; i++)
        {
            if (jishu == 0)
            {
                jishu++;
                if (a[i] != -1)
                {
                    node[father].left = numm;
                    node[numm].date = a[i];
                    numm++;
                }
            }
            else if (jishu == 1)
            {
                if (a[i] != -1)
                {
                    node[father].right = numm;
                    node[numm].date = a[i];
                    numm++;
                }
                jishu = 0;
                father++;
            }
        }
        dfs(1);
        printf("\n");
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/zufezzt/p/4523251.html

一、查找 1. 算法设计题 :已知n元顺序a0, a1, … , an-1按关键字递增有序存储。给定关键字值key,编写算法用对分查找求下标i,满足ai-1<key且aikey。 2. 编程题:输入n个两两互不相等的整数,以这些整数为关键字建立平衡的二叉排序树。判断该二叉树是否为平衡的,输出判断结果;输出该二叉树的中序遍历关键字访问次序。 3. 从空树起连续插入以下20个关键字构建m=4的B-树。 50, 15, 09, 18, 03, 85, 33, 72, 48, 22, 91, 88, 11, 99, 06, 56, 68, 77, 43, 36。 4. 16个关键字组成的5阶B-树如下图所示,请按关键 字递减的次序删除所有结点至空树,画出每删除1个关键字后得到B-树,直至空树。 5. 12个关键字如本电子教案例1所示,设H(K)=K mod 13,地址空间范围0~15,用二次探测再散列解决冲突。画出哈希;若各元素等概率查找,求成功查找时的平均查找长度。 二、内部排序 1.算法设计分析题:将直接插入排序的内循环改造为使用对分查找实现元素插入,请写出基于对分查找的插入排序算法并给出其时间复杂度分析。 2.算法设计:将教案给出的非递归直接插入排序和冒泡排序算法用递归算法实现。 3.算法设计:带附加头结点单链将各数据结点按关键字升序连接。 4.编程题:键盘输入n个无符号整数,用链式基数排序实现由小到大排序,输出排序结果。 提示:对于C语言32bit宽的unsigned类型,可以采用16进制形式来实现基数排序,即32bit共有8个16进制位,每个16进制位进行一趟分配和收集,共8趟。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值