- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有课
// BinarySearchTree class provide by Mark Allen Weiss in Data Structures
// and Algorithm Analysis in C++, 3ed
//
// Implementation is combined with specification. No separate header file.
#ifndef BINARY_SEARCH_TREE_H
#define BINARY_SEARCH_TREE_H
#include <iostream>
using namespace std;
template <typename Comparable>
class BinarySearchTree {
public:
//Constructors
BinarySearchTree( ) :root( 0 ) { }
BinarySearchTree( const BinarySearchTree & rhs ) : root( 0 )
{
*this = rhs;
}
//Destructor
~BinarySearchTree( )
{
makeEmpty( );
}
/**
* Find the smallest item in the tree.
* Throw UnderflowException if empty.
*/
const Comparable & findMin( ) const
{
return findMin( root )->element;
}
/**
* Find the largest item in the tree.
* Throw UnderflowException if empty.
*/
const Comparable & findMax( ) const
{
return findMax( root )->element;
}
/**
* Test if the tree is logically empty.
* Return true if empty, false otherwise.
*/
bool isEmpty( ) const
{
return root == 0;
}
/**
* Print the tree contents in sorted order.
*/
void printTree( ostream & out = cout ) const
{
if( isEmpty( ) )
out << "Empty tree" << endl;
else
printTree( root ,out );
}
/**
* Insert x into the tree; duplicates are ignored.
*/
void insert( const Comparable & x )
{
insert( x, root );
}
/**
* Remove x from the tree. Nothing is done if x is not found.
*/
void remove( const Comparable & x )
{
remove( x, root );
}
/**
* Deep copy.
*/
const BinarySearchTree & operator=( const BinarySearchTree & rhs )
{
if( this != &rhs )
{
makeEmpty( );
root = clone( rhs.root );
}
return *this;
}
//protected:
friend struct BinaryNode
{
Comparable element;
BinaryNode *left;
BinaryNode *right;
BinaryNode( const Comparable & theElement, BinaryNode *lt, BinaryNode* rt ) :
element(theElement), left(lt), right(rt)
{ }
};
BinaryNode *root;
private:
/**
* Internal method to insert into a subtree.
* x is the item to insert.
* t is the node that roots the subtree.
* Set the new root of the subtree.
*/
void insert( const Comparable & x, BinaryNode * & t )
{
if( t == 0 )
t = new BinaryNode( x, 0, 0 );
else if( x < t->element )
insert( x, t->left );
else if( t->element < x )
insert( x, t->right );
else
; // Duplicate; do nothing
}
/**
* Internal method to remove from a subtree.
* x is the item to remove.
* t is the node that roots the subtree.
* Set the new root of the subtree.
*/
void remove( const Comparable & x, BinaryNode * & t )
{
if( t == 0 )
return; // Item not found; do nothing
if( x < t->element )
remove( x, t->left );
else if( t->element < x )
remove( x, t->right );
else if( t->left != 0 && t->right != 0 ) // Two children
{
t->element = findMin( t->right )->element;
remove( t->element, t->right );
}
else
{
BinaryNode *oldNode = t;
t = ( t->left != 0 ) ? t->left : t->right;
delete oldNode;
}
}
/**
* Internal method to find the smallest item in a subtree t.
* Return node containing the smallest item.
*/
BinaryNode * findMin( BinaryNode *t ) const
{
if( t == 0 )
return 0;
if( t->left == 0 )
return t;
return findMin( t->left );
}
/**
* Internal method to find the largest item in a subtree t.
* Return node containing the largest item.
*/
BinaryNode * findMax( BinaryNode *t ) const
{
if( t != 0 )
while( t->right != 0 )
t = t->right;
return t;
}
/**
* Internal method to print a subtree rooted at t in sorted order.
*/
void printTree( BinaryNode *t, ostream & out ) const
{
if( t != 0 )
{
printTree( t->left, out );
out << t->element << endl;
printTree( t->right, out );
}
}
/**
* Internal method to clone subtree.
*/
BinaryNode * clone( BinaryNode *t ) const
{
if( t == 0 )
return 0;
else
return new BinaryNode( t->element, clone( t->left ), clone( t->right ) );
}
};
#endif
包含一个结构。我有一个继承自上述类的第二类“MyBST”
#include"BinarySearchTree.h"
using namespace std;
template <typename Comparable>
class MyBST : public BinarySearchTree<Comparable>
{
public:
MyBST()
{
leftReplace = true;
}
void strictRemoval()
{
if (leftReplace)
{
removeLargestFromtLeft(root->element, root);
leftReplace = false;
}
else
{
remove(root->element);
leftReplace = true;
}
printTree();
}
bool leftReplace;
void removeLargestFromLeft( const Comparable & x, BinaryNode * &t )
{
if( t == 0 )
return; // Item not found; do nothing
if( x < t->element )
removeLargestFromLeft(x, root->left);
else if( t->element < x )
removeLargestFromLeft(x, root->right);
else if( t->left != 0 && t->right != 0 ) // Two children
{
t->element = findMax( t->left )->element;
removeLargestFromLeft(t->element, root->left);
}
else
{
BinaryNode *oldNode = t;
t = ( t->left != 0 ) ? t->left : t->right;
delete oldNode;
}
}
BinaryNode * findMax(BinaryNode * t)
{
// not implemented yet stopped here because the rest of the code was not
// working
}
};
这是我的主要功能,将使用这两个类,我只是想让我的代码正常工作并在当前进行测试。
#include "BinarySearchTree.h"
#include "MyBST.h"
int main()
{
MyBST<int> BST;
BST.insert(3);
BST.insert(4);
BST.insert(5);
BST.insert(6);
BST.insert(6);
BST.insert(7);
BST.insert(8);
BST.insert(9);
BST.insert(1);
BST.printTree();
while (!BST.isEmpty())
BST.strictRemoval();
return 0;
};
当我编译这两个类时,出现以下错误:- “BinaryNode”尚未声明
- 在此范围内未声明“root”
- 请求't->'中的成员'left',它是非类类型'int'
- 请求't->'中的成员'right',它是非类类型'int'
- 请求't->'中的成员'element',它是非类类型'int'
我做错了什么?我认为公共(public)继承仍然可以让我访问 protected 方法和变量,并假设结构也是如此。我通过将 protected 更改为 public 来检查这是否是唯一的问题,但仍然会弹出相同的错误。
我做错了什么吗?我对 C++ 还很陌生,只是因为我正在上一门课才学会它,我更习惯于 ruby 和 java。
如有任何帮助,我们将不胜感激。
最佳答案
您在每个类(class)的末尾都缺少 ;
。
class BinarySearchTree {
...
}; // <-- there
privateMethod1
也缺少返回类型。也许 void
?
void privateMethod1()
{...}
关于C++从派生类访问基类中的结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7761564/
我目前正在尝试基于哈希表构建字典。逻辑是:有一个名为 HashTable 的结构,其中包含以下内容: HashFunc HashFunc; PrintFunc PrintEntry; CompareF
如果我有一个指向结构/对象的指针,并且该结构/对象包含另外两个指向其他对象的指针,并且我想删除“包含这两个指针的对象而不破坏它所持有的指针”——我该怎么做这样做吗? 指向对象 A 的指针(包含指向对象
像这样的代码 package main import "fmt" type Hello struct { ID int Raw string } type World []*Hell
我有一个采用以下格式的 CSV: Module, Topic, Sub-topic 它需要能够导入到具有以下格式的 MySQL 数据库中: CREATE TABLE `modules` ( `id
通常我使用类似的东西 copy((uint8_t*)&POD, (uint8_t*)(&POD + 1 ), back_inserter(rawData)); copy((uint8_t*)&PODV
错误 : 联合只能在具有兼容列类型的表上执行。 结构(层:字符串,skyward_number:字符串,skyward_points:字符串)<> 结构(skyward_number:字符串,层:字符
我有一个指向结构的指针数组,我正在尝试使用它们进行 while 循环。我对如何准确初始化它并不完全有信心,但我一直这样做: Entry *newEntry = malloc(sizeof(Entry)
我正在学习 C,我的问题可能很愚蠢,但我很困惑。在这样的函数中: int afunction(somevariables) { if (someconditions)
我现在正在做一项编程作业,我并没有真正完全掌握链接,因为我们还没有涉及它。但是我觉得我需要它来做我想做的事情,因为数组还不够 我创建了一个结构,如下 struct node { float coef;
给定以下代码片段: #include #include #define MAX_SIZE 15 typedef struct{ int touchdowns; int intercepti
struct contact list[3]; int checknullarray() { for(int x=0;x<10;x++) { if(strlen(con
这个问题在这里已经有了答案: 关闭 11 年前。 Possible Duplicate: Empty “for” loop in Facebook ajax what does AJAX call
我刚刚在反射器中浏览了一个文件,并在结构构造函数中看到了这个: this = new Binder.SyntaxNodeOrToken(); 我以前从未见过该术语。有人能解释一下这个赋值在 C# 中的
我经常使用字符串常量,例如: DICT_KEY1 = 'DICT_KEY1' DICT_KEY2 = 'DICT_KEY2' ... 很多时候我不介意实际的文字是什么,只要它们是独一无二的并且对人类读
我是 C 的新手,我不明白为什么下面的代码不起作用: typedef struct{ uint8_t a; uint8_t* b; } test_struct; test_struct
您能否制作一个行为类似于内置类之一的结构,您可以在其中直接分配值而无需调用属性? 前任: RoundedDouble count; count = 5; 而不是使用 RoundedDouble cou
这是我的代码: #include typedef struct { const char *description; float value; int age; } swag
在创建嵌套列表时,我认为 R 具有对列表元素有用的命名结构。我有一个列表列表,并希望应用包含在任何列表中的每个向量的函数。 lapply这样做但随后剥离了列表的命名结构。我该怎么办 lapply嵌套列
我正在做一个用于学习目的的个人组织者,我从来没有使用过 XML,所以我不确定我的解决方案是否是最好的。这是我附带的 XML 文件的基本结构:
我是新来的 nosql概念,所以当我开始学习时 PouchDB ,我找到了这个转换表。我的困惑是,如何PouchDB如果可以说我有多个表,是否意味着我需要创建多个数据库?因为根据我在 pouchdb
我是一名优秀的程序员,十分优秀!