数据结构与算法分析习题4.33和4.34

习题4.33

由一个自动程序来生成二叉树:通过给树的每一个节点指定坐标(x,y),围绕每个坐标画一个圆圈,并将每个节点连到它的父节点上。假设在存储器中存有一棵二叉查找树(或许由此程序生成)并设每个节点都有两个附加的域存放坐标。
a. 坐标x可以通过指定中序遍历数来计算。对于树中的每个节点写出这样一个例程。
b. 坐标y可以通过使用节点深度的相反数算出。对于树中的每一个节点写出这样的例程。
c. 若使用某个虚拟的单位表示,则所画图形的具体尺寸是多少?如何调整单位使得所画的树总是高大约为宽的三分之二?
d. 证明,使用这个系统没有交叉线出现,同时,对于任意节点X,X的左子树的所有元素都出现在X的左边,X的右子树的所有元素都出现在X的右边。

习题4.34

编写一个一般的画树程序,该程序把一棵树转变成下列的图—组装指令:
a. Circle(X,Y)
b. DrawLine(i, j)
第一个指令在(X,Y)处画一个圆,而第二个指令则连接第i个圆和第j个圆(圆以所画的顺序编号)。你或者把它写成一个程序并定义某种输入语言,或者把它写成一个函数,该函数可以被任何程序调用。你的程序的运行时间是多少?

这两道题困扰了我很久,我没有任何思路,求解答。如果有人觉得我是巨婴的话,我无话反驳。使用的是C语言写的。

C
数据结构
2022-08-30 22:14:48
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
wx67175b9d2f1d8

这个题之前也困扰到我了,不过后来我才发现,它是要打印画圆和画线的指令,并不是真的画圆,那就简单多了,实现代码如下:

// 辅助函数,计算x坐标
static SearchTree set_x_func(SearchTree tree, int *last_node_x) {
    if (tree == NULL) {
        return NULL;
    }

    tree->left = set_x_func(tree->left, last_node_x);
    tree->x = ++(*last_node_x);
    tree->right = set_x_func(tree->right, last_node_x);

    return tree;
}

// 计算节点的x坐标
SearchTree set_x_position(SearchTree tree) {
    int last_node_x = 0;

    return set_x_func(tree, &last_node_x);
}

// 辅助函数,计算y坐标
static SearchTree set_y_func(SearchTree tree, int depth) {
    if (tree == NULL) {
        return NULL;
    }

    set_y_func(tree->left, depth + 1);
    tree->y = -depth;
    set_y_func(tree->right, depth + 1);

    return tree;
}

// 计算节点的y坐标
SearchTree set_y_position(SearchTree tree) {
    return set_y_func(tree, 0);  // 从深度0开始
}

// 生成画圆指令
static void generate_instructions(SearchTree tree, int *current_index) {
    if (tree == NULL) {
        return;
    }

    // 生成圆的指令
    printf("Circle(%d, %d)第%d个圆\n", tree->x, tree->y, *current_index);
    int this_index = (*current_index)++; // 当前节点索引

    // 初始化左右子树的索引
    int left_index = *current_index; // 左子树的起始索引

    // 递归生成左子树
    generate_instructions(tree->left, current_index);
    if (tree->left) {
        printf("DrawLine(%d, %d)\n", this_index, left_index);
    }

    // 记录右子树的索引
    int right_index = *current_index; // 右子树的起始索引

    // 递归生成右子树
    generate_instructions(tree->right, current_index);
    if (tree->right) {
        printf("DrawLine(%d, %d)\n", this_index, right_index);
    }
}

void generate_tree_instructions(SearchTree tree) {
    int current_index = 1; // 从1开始
    generate_instructions(tree, ¤t_index);
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
已于2024-10-22 16:09:32修改
分享
微博
QQ
微信
回复
2024-10-22 16:03:24
相关问题
HarmonyOS 数据结构咨询
701浏览 • 1回复 待解决
HarmonyOS 性能分析优化
1149浏览 • 1回复 待解决