- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我是 C 新手,正在学习函数和指针。我必须在 t_print 方法中以下面必需的格式打印二叉搜索树,我将非常感激有人可以指导我如何去做。
到目前为止我有这个代码:
typedef struct tree {
void* data;
struct tree* left;
struct tree* right;
} Tree;
/*set the data on the tree node */
void t_set_data(Tree* t, void* data) {
t->data = data;}
/*let l be the left node of tree *t */
void t_set_left(Tree* t, Tree* l){
t->left = l;}
/*let r be the left node of tree *t */
void t_set_right(Tree* t, Tree* r){
t->right = r;}
/* simply return left node of the tree */
Tree* t_left(Tree* t){
return t-> left;}
/* simply return right node of the tree */
Tree* t_right(Tree* t){
return t-> right;}
/* simply return the data of the tree */
void* t_data(Tree* t){
return t->data;}
/* make the node of the tree and allocate space for it*/
Tree* t_make(){
Tree *t = (Tree*)malloc(sizeof(tree));
t->left=NULL;
t->right = NULL;
t-> data = NULL;
return t;
}
/*
print the whole tree in the following format
Root is printed with zero trailing spaces to the left
Every node/subtree is printed with one additional space
Below is an example for a tree with depth 2:
Root
<space>Left-Child
<space><space>Left-Left-Child
<space><space>Left-Right-Child
<space>Right-Child
.... and so on ...
Use (void(* p))(function pointer) to print.
*/
void t_print( Tree* t ,int space, void(* p)(void*) ){
}
最佳答案
这取决于您希望打印数据的顺序,但对于 BST,“按顺序”是合适的(而不是预排序或后排序)。
要“按顺序”打印树,该函数会执行以下操作:
“按顺序打印 xxx 子树”操作是使用左或右指针递归调用“按顺序打印”函数。
预订的算法是:
后序的算法是:
事情就是这么简单。
嗯,几乎就这么简单......
如果您想将数据括起来或以其他方式识别树结构,则必须更加努力。通常,您还会得到一个包含前缀(标识输出的标签)和后缀(可能只是换行符)的 cover 函数;通常,这不是递归打印的一部分。但算法的核心就像描述的那样简单。
关于c - 打印 BST - C 程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22056214/
我已经编写了一个 BSTLink 类来将 BST 转换为双向链表,但是在我尝试通过引用传递 BST 的节点指针的类中的构造调用抛出一个错误“没有匹配的函数来调用 BSTLink::construct(
当我使用 xgboost 为 2-cates classification problem 训练我的数据时,我想使用提前停止来获得最佳模型,但我对在我的预测中使用哪一个感到困惑,因为提前停止将返回 3
因为我找不到有用的东西所以我在这里问我的问题: 我们如何在不使用任何额外空间的情况下将 BST 转换为中序链接列表,然后再转换回“相同”的 BST。 到目前为止我已经尝试过的(虽然仍在做):我尝试了
我从我们的教授讲座幻灯片中获得了 BST(二叉搜索树)和随机 BST 的源代码,现在我想通过插入新元素来测试它们是否正常工作,然后我如何才能看到我的结果,如 http://cs.lmu.edu/~ra
给定两棵二叉树 T1 和 T2,您必须找到要在 T1 中完成的最少插入次数,以使其在结构上与 T2 相同。如果不可能则返回 -1。 注意事项 假设插入在 BST 中以正常方式完成。 假设在插入时,如果
我有一个始终将日期存储为 UTC 的网络应用程序,但它们需要分别以 GMT/BST 的形式显示给用户。 我有一个 UTC/GMT 日期(2013 年 3 月 30 日 22:00),我每小时移动一次以
题目地址:https://leetcode-cn.com/problems/largest-bst-subtree/ 题目描述 Given a binary tree, find the larg
注意:BST - 二叉搜索树(缩写) 正如标题所说,这是一项家庭作业,所以我不是在寻找答案。相反,我只需要一个正确方向的点。 对于作业,我应该创建一个 BST 类,它直接定义为最多包含 2 个 BST
我有一个插入方法和一个搜索方法,我正在考虑一种方法来循环二叉搜索树并使用像获取节点这样的方法,然后在另一个二叉搜索树上搜索它,如果它成立,那么我将其插入该元素,但问题是我无法想出一种基于索引获取节点的
function validateBst(root, min=-Infinity, max=Infinity) { if (!root) return true; if (root.value
我有一个问题,我知道如何像这样计算树中的所有节点 return 1 + rightchild(tree->right) + rightchild(tree->left); 现在我的问题是如果一个节点在
给定一个二叉树根,任务是返回 任何子树的所有键的最大总和 这也是 二叉搜索树 (BST) . 假设 BST 定义如下: - 节点的左子树仅包含键小于节点键的节点。 - 节点的右子树仅包含键大于节点键的
我正在尝试使用 ScalaCheck 为 BST 创建一个 Gen,但是当我调用 .sample 方法时,它给了我 java.lang.NullPointerException。我哪里错了? seal
这些是我的领域: public class BSTSet extends AbstractSet { // Data fields private BSTNode root;
我需要在二叉搜索树中执行范围搜索功能,这将给出给定范围内的项目数量。我不明白如何在找到项目时增加计数值。因为,我必须使用递归函数&如果我在递归过程中将计数变量初始化为0,它将始终从0开始计数值,而不是
所以我正在尝试编写一个代码来返回二叉搜索树中的最小值。我知道这是树的最左边的值,并且我知道我需要它递归地向左运行,直到什么都没有为止。但是我的代码不起作用,我不知道为什么。任何帮助将不胜感激。 (de
我想返回一个包含树中所有键的字符串,按照它们的存储顺序。每个子树中的键应包含在括号中。 _7_ / \ _3_ 8 / \ 1
尝试运行递归算法来找出某棵树是否是 BST(二叉搜索树)。 boolean checkBST(Node root) { boolean isBST = true; if(root ==
使用下面的代码,每个时区都正确打印值,除了 BST import java.text.*; def format = "yyyy-MM-dd HH:mm:ssXXX" def dt = new Da
您能帮忙搜索功能吗,它总是返回nil,我不明白为什么 func BTreeSearchItem(root *TreeNode, elem string) *TreeNode { if root
我是一名优秀的程序员,十分优秀!