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()'程序? –