获得;期望有一天&引用;C+中的错误消息+; 我在C++中实现二叉搜索树。我已经编写了以下代码,但由于某种原因,我收到一条错误消息,上面说:
期望有一天 我在编译代码时得到了上面的消息 <>我也是C++新手,如果我能得到一些帮助,我会很感激。 二元搜索树的某些上下文: 二叉搜索树将它们的键按排序顺序保存,以便进行查找 和其他操作可以使用二进制搜索的原理:当 在树中查找密钥(或插入新密钥的位置),它们 从根到叶遍历树,与存储的键进行比较 在树的节点中,在比较的基础上, 继续在左子树或右子树中搜索。平均而言 这意味着每次比较都允许操作跳过大约一半 树,以便每次查找、插入或删除都需要时间 与存储在数据库中的项目数的对数成比例 树这比查找项目所需的线性时间要好得多 按键输入(未排序)数组,但比相应的 哈希表上的操作获得;期望有一天&引用;C+中的错误消息+; 我在C++中实现二叉搜索树。我已经编写了以下代码,但由于某种原因,我收到一条错误消息,上面说:,c++,C++,期望有一天 我在编译代码时得到了上面的消息 我也是C++新手,如果我能得到一些帮助,我会很感激。 二元搜索树的某些上下文: 二叉搜索树将它们的键按排序顺序保存,以便进行查找 和其他操作可以使用二进制搜索的原理:当 在树中查找密钥(或插入新密钥的位置),它们 从根到叶遍历树,与存储的键进行比较 在树的节点中,在比较的基础上, 继续在左子树或右子树中搜索。平均而言 这意味着每次比较都允许操作跳过大约一半 树,以便每次查找、插入或删除都需要时间 与存储在数据库中的项目数的对数成比例 树这比查找项目所需
#包括
使用名称空间std;
结构体类型
{
int数据;
节点*左、*右、*父节点;
};
节点*删除节点(节点*根,int数据);
void find_min(节点*根节点);
无效顺序(节点*x);
无效插入(节点*根,整数数据);
//删除节点
//搜索树
//插入节点
//temp->parent=NULL;
int main()
{
节点*root,*temp;
//具有20个节点的节点
temp=新节点;
温度->数据=20;
temp->left=NULL;
temp->right=NULL;
根=温度;
//具有10个节点的节点
temp=新节点;
温度->数据=10;
temp->left=NULL;
temp->right=NULL;
temp->parent=NULL;
根->左=温度;
temp->parent=root;
//具有30个节点的节点
temp=新节点;
温度->数据=30;
temp->left=NULL;
temp->right=NULL;
temp->parent=NULL;
根->右=温度;
temp->parent=root;
//具有25个节点的节点
temp=新节点;
温度->数据=25;
temp->left=NULL;
temp->right=NULL;
temp->parent=NULL;
根->右->左=温度;
临时->父节点=根节点->右侧;
//具有40个节点的节点
temp=新节点;
温度->数据=40;
temp->left=NULL;
temp->right=NULL;
temp->parent=NULL;
根->右->右=温度;
临时->父节点=根节点->右侧;
//带2的节点
temp=新节点;
温度->数据=2;
temp->left=NULL;
temp->right=NULL;
temp->parent=NULL;
根->左->左=温度;
临时->父节点=根节点->左侧;
//具有15个节点的节点
temp=新节点;
温度->数据=15;
temp->left=NULL;
temp->right=NULL;
temp->parent=NULL;
根->左->右=温度;
临时->父节点=根节点->左侧;
查找_min(根);
cout root->data)
根->右=删除节点(根->右,数据);
//案例1:没有儿童
else if(root->left==NULL&root->right==NULL)
删除根;
返回根;
//数据data struct Node*temp=root;
//案例2:一名儿童
如果(根->左==NULL){
节点*temp=root;
根=根->右;
删除临时文件;
返回根;
}
//
else if(root->right==NULL){
节点*temp=root;
根=根->左;
删除临时文件;
返回根;
}
//案例3:两名儿童
其他(根==根->右){
根->数据=临时->数据;
根->右=删除节点(根->右,临时->数据);
返回根;
}
}
这是您的问题:else(root==root->right){
。如果您想将其作为一个条件,您需要使用else If(root==root->right)
请注意,根据,即使使用该修复程序,您也会有更多错误(它们看起来很容易修复)。它在哪一行?您可能会意外忘记键入分号。
if(root->data==NULL)
?数据是一个整数,而不是一个指针hi@FeiXiang,IDE在程序的最后一个else块抛出错误。我在必需的地方放了分号。非常奇怪,我仍然收到这个错误消息。你在下面的答案中指出的else
语句中放了一个条件。
#include<iostream>
using namespace std;
struct Node
{
int data;
Node *left, *right, *parent;
};
Node* DeleteNode(Node *root, int data);
void find_min(Node *root);
void inorder(Node *x);
void Insert(Node *root, int data);
//delete a node
//search_tree
//insert a node
//temp->parent = NULL;
int main()
{
Node *root, *temp;
//node with 20
temp = new Node;
temp->data = 20;
temp->left = NULL;
temp->right = NULL;
root = temp;
//node with 10
temp = new Node;
temp->data = 10;
temp->left = NULL;
temp->right = NULL;
temp->parent = NULL;
root->left = temp;
temp->parent = root;
//node with 30
temp = new Node;
temp->data = 30;
temp->left = NULL;
temp->right = NULL;
temp->parent = NULL;
root->right = temp;
temp->parent = root;
//node with 25
temp = new Node;
temp->data = 25;
temp->left = NULL;
temp->right = NULL;
temp->parent = NULL;
root->right->left = temp;
temp->parent = root->right;
//node with 40
temp = new Node;
temp->data = 40;
temp->left = NULL;
temp->right = NULL;
temp->parent = NULL;
root->right->right = temp;
temp->parent = root->right;
//node with 2
temp = new Node;
temp->data = 2;
temp->left = NULL;
temp->right = NULL;
temp->parent = NULL;
root->left->left = temp;
temp->parent = root->left;
//node with 15
temp = new Node;
temp->data = 15;
temp->left = NULL;
temp->right = NULL;
temp->parent = NULL;
root->left->right = temp;
temp->parent = root->left;
find_min(root);
cout << "Printing numbers in order" << endl;
inorder(root);
cout << "printing in-order of the given root" << endl;
delete(root);
}
void find_min(Node *root)
{
Node *temp;
temp = root;
while (temp->left != NULL)
temp = temp->left;
cout << "min number is " << temp->data << endl;
}
void inorder(Node *x)
{
if (x != NULL)
{
inorder(x->left);
cout << x->data << endl;
inorder(x->right);
}
}
Node* DeleteNode(Node *root, int data)
{
if (root->data == NULL) {
return root;
}
// If the key to be deleted is smaller than the root's key,
// then it lies in left subtree
else if (data < root->data)
root->left = DeleteNode(root->left, data);
// If the key to be deleted is greater than the root's key,
// then it lies in right subtree
else if (data > root->data)
root->right = DeleteNode(root->right, data);
// case 1: No child
else if (root->left == NULL & root->right == NULL)
delete root;
return root;
//data < root->data struct Node *temp = root;
// case 2: one child
if (root->left == NULL){
Node *temp = root;
root = root->right;
delete temp;
return root;
}
//
else if (root->right == NULL) {
Node *temp = root;
root = root->left;
delete temp;
return root;
}
// case 3: two child
else (root == root->right){
root->data = temp->data;
root->right = DeleteNode(root->right, temp->data);
return root;
}
}