构建一个递归的抽象二叉搜索树 [英] build a rcursive abstract binary search tree

查看:45
本文介绍了构建一个递归的抽象二叉搜索树的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我是编码的新手,我是新来的。

我需要构建一个抽象的二叉搜索树,具有以下功能:

1.创建一个通用BST

2. distory a BST

3.在BST中搜索

4.添加一个元素到BST



我正在处理第一个函数,我创建了一个指针,因此树的创建将是genraic,但我无法分配这个函数叫做GetNewTree



Hi,
I am new in coding and I am new here.
I need to build an abstract binary search tree, with the following functions:
1. create a generic BST
2. distory a BST
3. search in a BST
4. add an element to a BST

I am working on the first function and I have created a pointer so the creation of the tree will be genraic, but I can not manage to assign the function to the point called GetNewTree

#include <stdio.h>
#include <stdlib.h>

typedef struct Tree {
    void *data;
    struct Tree *left;
    struct Tree *right;
}Tree;

void* GetNewTree();

Tree* GetNewTreeInt(){
    Tree* newTree = (Tree*)malloc(sizeof(Tree));
    newTree->data = malloc(sizeof(int));
    newTree->left = newTree->right = NULL;
    return newTree;
}

Tree* GetNewTreeChar(){
    Tree* newTree = (Tree*)malloc(sizeof(Tree));
    newTree->data = malloc(sizeof(char));
    newTree->left = newTree->right = NULL;
    return newTree;
}

int main() {
    int choose;
    int number;
    scanf("%d", &choose);
    switch (choose){
        case (0) : 	(Tree*)GetNewTree = &GetNewTreeInt();
            break;
        case (1) : 	(Tree*)GetNewTree = &GetNewTreeChar();
            break;
    }


        
    
    
    return 0;
}

推荐答案

这里有一些问题。



1.无法保留对您选择的函数的引用。

这个;

There are a few things going wrong here.

1. Nothing to hold the reference to the function you pick.
This;
void* GetNewTree();



不声明函数指针,它是尚未定义的方法的前向声明。

你要做的是将其改为a函数指针类型的变量,指向与GetNewTreeChar / Int函数匹配的函数。


Does not declare a function pointer, it's a forward declaration of a method that's not yet defined.
What you want to do instead is change that to a variable of type function pointer, that points to a function matching the GetNewTreeChar/Int function.

Tree* (*GetNewTree)();



这可能看起来很奇怪,但这是函数指针的语法。



2.当你需要函数时,指针指针。

这个;


That might look weird but that's the syntax for a function pointer.

2. Taking a pointer to the pointer when you want the function.
This;

(Tree*)GetNewTree = &GetNewTreeInt()

;

不需要&在GetNewTreeInt之前,你也不希望它的末尾有(),因为那是调用方法而你不希望那时,你想要将函数的地址分配给 GetNewTree 变量。



3.施放l值。

没有意义施放 GetNewTree (树*),你投射作业,而不是指定的值。





我觉得这样的东西更接近你想要的东西;

;
Does not need the & before the GetNewTreeInt, also you don't want the () at the end of it, as that is calling the method and you don't want that at that point, you want to assign the address of the function to the GetNewTree variable.

3. Casting an l-value.
It makes no sense to cast GetNewTree to (Tree*), you cast on assignment, not the assigned value.


I think something like this is closer to what you want;

#include <stdio.h>
#include <stdlib.h>

typedef struct Tree {
    void *data;
    struct Tree *left;
    struct Tree *right;
} Tree;

Tree* (*GetNewTree)();

Tree* GetNewTreeInt(){
    Tree* newTree = (Tree*)malloc(sizeof(Tree));
    newTree->data = malloc(sizeof(int));
    newTree->left = newTree->right = NULL;
    return newTree;
}

Tree* GetNewTreeChar(){
    Tree* newTree = (Tree*)malloc(sizeof(Tree));
    newTree->data = malloc(sizeof(char));
    newTree->left = newTree->right = NULL;
    return newTree;
}

int main() {
    int choose;
    int number;

    scanf("%d", &choose);
    switch (choose){
    case (0) : GetNewTree = GetNewTreeInt;
        break;
    case (1) : GetNewTree = GetNewTreeChar;
        break;
    }

    // Try to call it here to make sure it works;
    Tree* myTree = GetNewTree();
    return 0;
}





希望这会有所帮助,

Fredrik



Hope this helps,
Fredrik


这篇关于构建一个递归的抽象二叉搜索树的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆