- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我无法删除 BST 的所有节点以供重用。对于第一组输入,我的代码工作正常但后来的输入失败。我在谷歌上搜索了很多,但找不到与我的问题相关的任何解决方案。为了澄清起见,我粘贴了我的完整源代码。我发现以前在这里问过这种问题 How to delete all nodes of a Binary Search Tree我不太了解python。为此,这里再次询问了这个话题。
#include<bits/stdc++.h>
using namespace std;
int size,c=0;
struct BstNode {
int data;
BstNode* left;
BstNode* right;
};
BstNode* insert(BstNode* root,int data) {
if(root==NULL) {
root=new BstNode();
root->data=data;
root->left=root->right=NULL;
}
else if(data<root->data) {
root->left=insert(root->left,data);
}
else {
root->right=insert(root->right,data);
}
return root;
}
void preorderTraversal(BstNode* root) {
if(root!=NULL) {
c++;
if(c==size) cout<<root->data<<endl;
else cout<<root->data<<" ";
preorderTraversal(root->left);
preorderTraversal(root->right);
}
}
void inorderTraversal(BstNode* root) {
if(root!=NULL) {
inorderTraversal(root->left);
c++;
if(c==size) cout<<root->data<<endl;
else cout<<root->data<<" ";
inorderTraversal(root->right);
}
}
void postorderTraversal(BstNode* root) {
if(root!=NULL) {
postorderTraversal(root->left);
postorderTraversal(root->right);
c++;
if(c==size) cout<<root->data<<endl;
else cout<<root->data<<" ";
}
}
void deletepostorderTraversal(BstNode* root) {
if(root!=NULL) {
deletepostorderTraversal(root->left);
deletepostorderTraversal(root->right);
delete root;
}
}
int main() {
int test_case;
while(cin>>test_case) {
cin>>size;
BstNode* root=NULL; //create the root
for(int i=0;i<size;i++) {
int data;cin>>data;
root=insert(root,data);
}
c=0;
cout<<"Pre.: ";
preorderTraversal(root);
cout<<"In..: ";
c=0;
inorderTraversal(root);
cout<<"Post: ";
c=0;
postorderTraversal(root);
cout<<endl;
deletepostorderTraversal(root); //delete all the nodes
//i can't reuse this code again.
// root=NULL;
// if(root==NULL) cout<<"Deleted"<<endl;
}
}
最佳答案
通过引用传递root
,并在删除完成后将其设置为NULL
:
void deletepostorderTraversal(BstNode*& root) {
// ^
if(root!=NULL) {
deletepostorderTraversal(root->left);
deletepostorderTraversal(root->right);
delete root;
root = NULL; // <<<<<<<<<<<<<<<<
}
}
在您的版本中,指针将按值传递,并且在函数内部对其所做的更改永远不会出现在函数范围之外。
使用如上所示的签名,并在函数内将 root
设置为 NULL
您可以再次(重新)使用 insert()
在调用 deletepostorderTraversal()
后将 root
节点设置为 NULL
。
参见 Live Demo
关于c++ - 如何删除 BST 的所有节点以便在 C++ 中重用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39277918/
我已经编写了一个 BSTLink 类来将 BST 转换为双向链表,但是在我尝试通过引用传递 BST 的节点指针的类中的构造调用抛出一个错误“没有匹配的函数来调用 BSTLink::construct(
当我使用 xgboost 为 2-cates classification problem 训练我的数据时,我想使用提前停止来获得最佳模型,但我对在我的预测中使用哪一个感到困惑,因为提前停止将返回 3
因为我找不到有用的东西所以我在这里问我的问题: 我们如何在不使用任何额外空间的情况下将 BST 转换为中序链接列表,然后再转换回“相同”的 BST。 到目前为止我已经尝试过的(虽然仍在做):我尝试了
我从我们的教授讲座幻灯片中获得了 BST(二叉搜索树)和随机 BST 的源代码,现在我想通过插入新元素来测试它们是否正常工作,然后我如何才能看到我的结果,如 http://cs.lmu.edu/~ra
给定两棵二叉树 T1 和 T2,您必须找到要在 T1 中完成的最少插入次数,以使其在结构上与 T2 相同。如果不可能则返回 -1。 注意事项 假设插入在 BST 中以正常方式完成。 假设在插入时,如果
我有一个始终将日期存储为 UTC 的网络应用程序,但它们需要分别以 GMT/BST 的形式显示给用户。 我有一个 UTC/GMT 日期(2013 年 3 月 30 日 22:00),我每小时移动一次以
题目地址:https://leetcode-cn.com/problems/largest-bst-subtree/ 题目描述 Given a binary tree, find the larg
注意:BST - 二叉搜索树(缩写) 正如标题所说,这是一项家庭作业,所以我不是在寻找答案。相反,我只需要一个正确方向的点。 对于作业,我应该创建一个 BST 类,它直接定义为最多包含 2 个 BST
我有一个插入方法和一个搜索方法,我正在考虑一种方法来循环二叉搜索树并使用像获取节点这样的方法,然后在另一个二叉搜索树上搜索它,如果它成立,那么我将其插入该元素,但问题是我无法想出一种基于索引获取节点的
function validateBst(root, min=-Infinity, max=Infinity) { if (!root) return true; if (root.value
我有一个问题,我知道如何像这样计算树中的所有节点 return 1 + rightchild(tree->right) + rightchild(tree->left); 现在我的问题是如果一个节点在
给定一个二叉树根,任务是返回 任何子树的所有键的最大总和 这也是 二叉搜索树 (BST) . 假设 BST 定义如下: - 节点的左子树仅包含键小于节点键的节点。 - 节点的右子树仅包含键大于节点键的
我正在尝试使用 ScalaCheck 为 BST 创建一个 Gen,但是当我调用 .sample 方法时,它给了我 java.lang.NullPointerException。我哪里错了? seal
这些是我的领域: public class BSTSet extends AbstractSet { // Data fields private BSTNode root;
我需要在二叉搜索树中执行范围搜索功能,这将给出给定范围内的项目数量。我不明白如何在找到项目时增加计数值。因为,我必须使用递归函数&如果我在递归过程中将计数变量初始化为0,它将始终从0开始计数值,而不是
所以我正在尝试编写一个代码来返回二叉搜索树中的最小值。我知道这是树的最左边的值,并且我知道我需要它递归地向左运行,直到什么都没有为止。但是我的代码不起作用,我不知道为什么。任何帮助将不胜感激。 (de
我想返回一个包含树中所有键的字符串,按照它们的存储顺序。每个子树中的键应包含在括号中。 _7_ / \ _3_ 8 / \ 1
尝试运行递归算法来找出某棵树是否是 BST(二叉搜索树)。 boolean checkBST(Node root) { boolean isBST = true; if(root ==
使用下面的代码,每个时区都正确打印值,除了 BST import java.text.*; def format = "yyyy-MM-dd HH:mm:ssXXX" def dt = new Da
您能帮忙搜索功能吗,它总是返回nil,我不明白为什么 func BTreeSearchItem(root *TreeNode, elem string) *TreeNode { if root
我是一名优秀的程序员,十分优秀!