0
我是C新手,我正在学习函数和指针。我必须在t_print方法中以下面必需的格式打印二进制搜索树,我将非常感激一些人可以指导我如何去做。打印BST - C程序
我有这样的代码至今:
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*)){
}
我应该按给出的t_print()方法的格式打印树。 – user3267967
好的;这是一个预订打印 - 您打印节点,然后打印它的子节点。您还需要将级别参数传递给打印函数,以便在节点之前打印正确数量的空格。真的很简单,只要你知道如何通过函数指针调用一个函数。既然你不能告诉打印函数指针要缩进多少个空格,你可能需要自己编写这些代码。 –
它看起来像你正在编码的规范;你的老师是否会提供调用你的代码和打印函数的'main()'程序? –