- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我只是在插入数组时遇到了麻烦...让子项从根或“父项”分支出来。
我一直在尝试将数据插入到基于数组的 BST 实现中:
BST::BST(int capacity) : items(new item[capacity]), size(0)
{
// define the constructor to the BST Class.
}
void BST::insert (const data& aData)
{
if ( items[root].empty ) // make the first data the root.
{
items[root].theData = aData;
items[root].empty = false;
size++;
}
else if ( items[root].theData.getName() < aData )
{
items[leftChild].theData = aData; // will be overwritten...
this->insert(aData);
}
else if ( aData < items[root].theData.getName() )
{
items[rightChild].theData = aData;
this->insert(aData);
}
}
这有一些问题。首先让我说第一个传入的数据将是根。所有其他数据将与它进行比较。当我对“插入”进行递归调用时,这基本上就是我“认为”我正在“遍历”的方式(如果它是一个链表)。我的主要问题是知道我的左右 child 将被覆盖。我想知道如何从子“节点”继续分支...?
这是我的 BST 类的头文件,我还担心我是否已正确设置成员以及所有内容......?
class BST
{
public:
BST(int capacity = 5);
BST(const BST& aTable);
void insert(const data& aData);
....
private:
int size; // size of the ever growing/expanding tree :)
struct item
{
bool empty;
data theData;
};
item * items; // The tree array
int rightChild; // access to the RHS
int leftChild; // access to the LHS
int root; // index to the root
};
我有另一个源文件,“数据”,我正在调用它,“getname”。到目前为止,我定义的三个简单方法是赋值运算符重载、比较“<”重载和数据类的构造函数:
data::data(char const * const name) : name(new char[strlen(name)+1])
{
if (name)
{
strcpy(this->name , name);
}
else this->name = NULL;
}
data& data::operator=(const data& data2)
{
if ( this != &data2 ) // check for selfassignment
{
delete [] name;
name = NULL;
this->name = new char[strlen(data2.name)+1];
strcpy(this->name , data2.name);
}
return *this;
}
bool operator< (const data& d1, const data& d2)
{
if ( strcmp( d1.getName(), d2.getName() ) == -1 )
{
return false;
}
else if ( strcmp( d1.getName(), d2.getName() ) == 1 )
{
return true;
}
return false; // if they are equal return false
}
最佳答案
我看到了一些问题。 leftChild
和 rightChild
应该是项目结构的成员,而不是 BST 的成员,因为它们对于每个项目都是不同的(或者它们不应该作为字段存在并且被动态计算)。我没有看到任何代码将 leftChild
和 rightChild
设置为任何东西。
看起来您正在尝试递归定义 insert
。您可能应该定义一个插入辅助函数,它接受一个项目和一个插入点的索引。这应该会让您走上正确的轨道。
wikipedia article您可以查看更多伪代码。
关于c++ - 二进制搜索树数组 Imp。 C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1712017/
例如: import subprocess p=subprocess.Popen("imp -help",stdout=subprocess.PIPE,stdin=subprocess.PIPE) o
我最近开始使用导入来更好地组织我在 python 中的代码。我在 file1.py 中的原始代码使用了以下行: def foo(): files = [f for f in os.listdi
根据imp.load_module文档,“调用方负责关闭文件参数”。“如果我将从imp.find_module返回到imp.load_module的文件对象,我仍然负责关闭文件对象吗?”或者帮我关上它
这个问题在这里已经有了答案: How to ignore deprecation warnings in Python (17 个答案) 关闭 4 年前。 每当我尝试在 PyCharm 中使用“sk
我使用的是python2.5。我需要动态导入一个模块,然后在更改时重新加载。我如何实现这一目标。 我在下面的示例中尝试过 - 不起作用: import imp modfile = 'mymod_inf
我的应用程序具有以下布局: /wsgi/myapp/__init__.py /wsgi/application /app.py 文件_init_.py: from flask import Flask
对于某些Python项目,我在config模块中定义了默认配置。可以通过使用 rc 文件来进行定制 config = imp.load_source('config', 'some_rc_file')
我的一些程序运行没有问题,但我仍然收到以下错误代码。它对程序本身没有影响,但我仍然想解决它。 C:\Program Files\JetBrains\PyCharm Community Edition
我有一个执行以下操作的脚本: import imp imp.load_source("storage_configuration_reader","/bi/opt/RNAspace/rnaspace_
我只是在插入数组时遇到了麻烦...让子项从根或“父项”分支出来。 我一直在尝试将数据插入到基于数组的 BST 实现中: BST::BST(int capacity) : items(new item[
我设置了一个类,理想情况下它将读取传入的任何类的方法,然后在运行时将它们全部映射到单个选择器,然后再将它们转发到它们的原始选择器。 这现在确实有效,但我一次只能对一种方法执行此操作。问题似乎是,一旦我
是否可以创建一个 IMP,其中参数的数量与正在解析的实例方法的选择器相匹配? 我可以使用“if”语句和有限数量的参数(比如 0 到 10 之间),但是是否可以使用带有 va_args 的 IMP_im
imp.find_module()没有从压缩的鸡蛋中找到模块。 如何找到可以来自两个地方的模块:目录或压缩的鸡蛋?在我的例子中,重要的是我可以提供一个 path 参数,比如 imp.find_modu
背景 当您使用命名空间包并将代码库划分到单独的文件夹中时,pylint 无法导入文件的问题让我感到厌烦。因此,我开始深入研究 astNG 源代码,它已被确定为问题的根源(请参阅 astng 上的错误报
我正在阅读 this所以关于从绝对路径导入模块的问题。一个答案建议使用以下代码: import imp foo = imp.load_source('module.name', '/path/to/f
我正在尝试从不同的目录动态导入模块。我正在关注 this question 的回答.我在名为 foo 的目录中有一个名为 bar 的模块。主脚本将在 foo 的父目录中运行。 这是我目前在测试脚本中的
Imp 运算符 对两个表达式进行逻辑蕴涵运算。 result = expression1 Imp expression2 参数 result 任意数值变量。 expression1 任
通常,当我必须在 unix 上备份数据库时,我会这样做, exp DBUSER/DBPSW@INST file=xxx.dmp 或从转储加载 imp DBUSER/DBPSW@INST file=xx
我正在编写一些数据传输例程的脚本,并将 exp 和 imp 合并到工作流程中。这两个都有一个 FILE= 参数来指定数据文件。 有没有办法指定标准输入和标准输出?我很想做这样的事情: exp ...
我可以通过以下方式获取方法的实现: IMP imp = [self methodForSelector:@selector(foo)]; 我知道 IMP 基本上是一个函数指针。 然后我可以得到指向的代
我是一名优秀的程序员,十分优秀!