binary-search-tree 相关问题

二叉搜索树是由具有左子节点和右子节点的根节点组成的数据结构。左节点及其所有后代的值小于根节点,而右节点及其所有后代的值大于根节点。根节点的子节点遵循相同的模式。这给了我们一个由有序元素组成的树。

隐式 Treap 构建时间为 O(n) 时间

我有两种算法来从数组构建隐式,一种是连续合并 就像这样: 公共节点 nlognConvertToTreap(int[] arr){ 节点node = Node.EMPTY_NODE; 对于 (int x : arr) 否...

回答 1 投票 0

BST 删除节点方法删除整个子树。蟒蛇

我正在努力让我的删除函数适用于我的 BST 类,我基本上理解背后的想法,但无法完全正确地实现。 在 DataStructures.py 中,我有一门课程......

回答 1 投票 0

二分查找树Python。迭代搜索

我想知道是否有人可以帮助我看看我对二叉搜索树的搜索函数做错了什么。由于数据量太大,必须使用迭代版本。我一直陷入困境...

回答 3 投票 0

如何按级别顺序打印二叉搜索树,包括空值

所以现在,当我有一棵树看起来像: 5 \ 6 \ 7 \ 9 我可以按级别顺序打印此内容,以便 5、 6、 7、 9、 ...

回答 3 投票 0

在 c 中实现二叉树时出现分段错误

我是c语言新手,但对于一个项目,我正在实现一个二叉树。这是我的函数代码: #包括 #包括 #包括 typedef 结构 BST...

回答 1 投票 0

平衡二叉搜索树和二叉搜索树有什么区别?

抱歉,如果这是一个非常基本的问题,但我对树相当陌生,因此,这个疑问这些天困扰着我。 二叉搜索树和平衡二叉搜索树有什么区别...

回答 3 投票 0

统计到达BST中特定节点的所有路径

我想用c++写一个程序。在输入中,第一行给出了两个整数 N(节点数)和 P(目标节点)。然后,在第二行中,给出了这 N 个节点。 这些节点是 elem...

回答 1 投票 0

有人可以确定我制作的 LeetCode 解决方案的时间复杂度吗?

我为下面链接的问题编写了一个解决方案,但我不确定它的时间复杂度。我以为它是二次的,但当我提交时它在 0 毫秒内通过,所以它可能是线性的 idk。我...

回答 1 投票 0

最佳优先搜索,在未分配成本的网格上查找从起点到目标的路径。启发值

我正在尝试回答这个挑战: 考虑在图中所示的网格中寻找从位置 𝑠 到位置 𝑔 的路径的问题: 棋子可以在网格上水平移动或

回答 1 投票 0

最佳拳头搜索,在未分配 c 的网格上找到从起点到目标的路径。启发值

我有一个问题让我有点困难,这里是 考虑在图 Ass-1 所示的网格中寻找从位置 s 到位置 g 的路径的问题。一块可以移动...

回答 1 投票 0

BST 根的特殊属性 - 中间值?

如果将 BST 的所有值放入排序数组中,是否始终保证 BST 的根节点是 BST 的“中间”值? 假设数组中的第一个元素...

回答 1 投票 0

为什么我的代码死锁? Java细粒度并发BST实现尝试失败

您能帮我理解为什么我的可重入锁代码卡住了吗? 我正在实现一个带有整数键和整数 val 的并发二叉搜索树,其中 val 是一个计数 va...

回答 1 投票 0

在OOP中将属性作为参数传递给方法是否正确?

我编写了一个递归程序来从最小到最大(按顺序)打印二叉搜索树的值,它可以用两种方式编写: 这可以写成这样:fu...

回答 1 投票 0

输出到二叉树控制台

根被传递给Print函数,其左右值为Null。但问题是,如果您开始从 for (size_t i = 0; i < tree.GetTreeDepth...

回答 1 投票 0

维基百科中使用 ceil 实现二分查找

在此输入图像描述 上面的实现是维基百科版本的二分搜索, 我的简单疑问是,用于

回答 1 投票 0

什么时候(最大)堆可以是 BST?

干杯,假设我们有一个不允许重复元素的 MAX 堆。这个堆有可能是 BST 吗?选择下面的正确答案: 堆永远不可能是 BST 堆是...

回答 3 投票 0

二叉搜索树中倒排后序遍历与插入顺序之间的联系

我有一个二叉搜索树。 我进行后序遍历。 我颠倒了从后序遍历中获得的数据序列的顺序(例如通过 将数据插入堆栈然后弹出...

回答 1 投票 0

为什么从模板类切换时会出现<error-type>?

我使用模板类实现了以下二叉搜索树: #ifndef名册_ #定义名册_ #include“BinaryNode.hpp” #include“访客.hpp” #include“学生.hpp” #include“打印机...

回答 1 投票 0

XML 脚本运行以打印所需的输出

我有一个示例 XML 输入文件,其中包含以下内容: 我有一个示例 XML 输入文件,其中包含以下内容: <?xml version="1.0" encoding="utf-8"?> <infobases> <infobase author="Chartered Professional Accountants of Canada" levelDefOrder="Level 1,Level 2,Level 3,Level 4,Level 5,Level 6,Level 7,Level 8,Level 9,Level 10,Level 11,Level 12,Level 13,Normal Level" levels="Level 1,Level 2,Level 3,Level 4,Level 5,Level 6,Level 7,Level 8,Level 9,Level 10,Level 11,Level 12,Level 13" name="info_a" title="CPA Canada Standards and Guidance Collection"> <file level="Level 1" heading="XYZ1-L1">XYZ1-L1 <file level="Level 2" heading="XYZ1-L12">XYZ1-L12 <file level="Level 3" heading="XYZ1-L123">XYZ1-L123</file> <file level="Level 3" heading="XYZ1-L123">XYZ1-L123</file> <file level="Level 3" heading="XYZ1-L123">XYZ1-L123</file> </file> </file> <file level="Level 1" heading="XYZ2-L1">XYZ2-L1</file> <file level="Level 1" heading="XYZ2-L1">XYZ2-L1 <file level="Level 2" heading="XYZ2-L12">XYZ2-L12</file> <file level="Level 2" heading="XYZ2-L123">XYZ2-L123 <file level="Level 3" heading="XYZ1-L123">XYZ1-L123 <file level="Level 4" heading="XYZ1-L123">XYZ1-L123</file> </file> </file> </file> </infobase> </infobases> 我想编写一个脚本来识别文件元素级别属性,并在打印文件元素的标题时给出适当的缩进。文件元素标题将获得 .ditamap 扩展名,该扩展名将其他文件元素作为子元素并忽略其他子元素(不在示例 xml 文件中)。如果文件元素没有子文件元素,则会添加 .dita 扩展名。 我用 javascript 编写了一个脚本,它倾向于进行正确的缩进,但分配的扩展名不正确。我得到所有文件元素标题的 .dita。这是代码: const fs = require('fs'); const XmlStream = require('xml-stream'); // Create a readable stream from the XML file const stream = fs.createReadStream('input1.xml'); // Create a writable stream to the output text file const outputStream = fs.createWriteStream('output.txt'); // Create a new XML stream parser const xmlParser = new XmlStream(stream); // Function to print headings with proper indentation function printHeadingsToFile(file, indentation = '') { // Calculate the indentation based on the level of the file const levelIndentation = ' '.repeat(parseInt(file.$.level.substr(6)) - 1); // Determine file extension based on child elements const fileExtension = file.file ? 'ditamap' : 'dita'; // Write the heading with indentation to the output file outputStream.write(`${indentation}${levelIndentation}${file.$.heading}.${fileExtension}\n`); // Check if there are nested files if (file.file) { // If nested files exist, recursively print their headings with increased indentation file.file.forEach(nestedFile => { printHeadingsToFile(nestedFile, `${indentation}${levelIndentation}`); }); } } // Event listener for when a new XML element is encountered xmlParser.on('startElement: file', function(element) { // Print headings for each file printHeadingsToFile(element); }); // Event listener for when the parsing ends xmlParser.on('end', function() { console.log('Parsing finished.'); // Close the output stream after finishing writing outputStream.end(); }); // Event listener for any errors during parsing xmlParser.on('error', function(err) { console.error('Error during parsing:', err); // Close the output stream if there is an error outputStream.end(); }); 我在这里得到的输出如下: XYZ1-L1.dita XYZ1-L12.dita XYZ1-L123.dita XYZ1-L123.dita XYZ1-L123.dita XYZ2-L1.dita XYZ2-L1.dita XYZ2-L12.dita XYZ2-L123.dita XYZ1-L123.dita XYZ1-L123.dita 预期输出: XYZ1-L1.ditamap XYZ1-L12.ditamap XYZ1-L123.dita XYZ1-L123.dita XYZ1-L123.dita XYZ2-L1.dita XYZ2-L1.ditamap XYZ2-L12.dita XYZ2-L123.ditamap XYZ1-L123.ditamap XYZ1-L123.dita 找到了使用不同 xml 解析器包的解决方案。 const fs = require('fs'); const sax = require('sax'); // Create a SAX parser const parser = sax.parser(true); // Create a write stream to a text file const outputStream = fs.createWriteStream('output.txt'); // Array to store file attributes let fileAttributes = []; // Event handlers for the SAX parser parser.onopentag = function (node) { // Check if the current node is a <file> element if (node.name === 'file') { // Extract attributes and push them to the array fileAttributes.push(node.attributes); } }; parser.onend = function () { // Parsing ends, log the final output //console.log('File attributes:', fileAttributes); // Function to determine the file extension function getFileExtension(currentLevel, nextLevel) { if (currentLevel < nextLevel) { return '.ditamap'; } else { return '.dita'; } } // Iterate through the fileAttributes array for (let i = 0; i < fileAttributes.length; i++) { const currentFile = fileAttributes[i]; const nextFile = fileAttributes[i + 1]; // Get the level numbers const currentLevel = parseInt(currentFile.level.match(/\d+/)[0]); const nextLevel = nextFile ? parseInt(nextFile.level.match(/\d+/)[0]) : 0; // Get the file extension const extension = getFileExtension(currentLevel, nextLevel); // Indentation based on the level const indentation = ' '.repeat((currentLevel - 1) * 4); // Prepare the text to write const textToWrite = indentation + currentFile.heading + extension + '\n'; // Write the text to the output stream outputStream.write(textToWrite); } // Close the output stream when done outputStream.end(); }; // Read XML file const xmlData = fs.readFileSync('input.xml', 'utf8'); // Parse XML data parser.write(xmlData).close(); 结果输出: L1.ditamap L2.ditamap L3.dita L3.dita L3.dita L1.dita L1.ditamap L2.dita L2.ditamap L3.ditamap L4.ditamap Level5Text.dita

回答 1 投票 0

查找存储在二叉搜索树中的单词的所有前缀

我有一个存储字符串的二叉搜索树。字符串按字典顺序进行比较(使用 c 中的 strcmp)。 给定一些输入字符串,我想检索该字符串的所有前缀的列表......

回答 1 投票 0

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.