作为我关于这段代码的一小段原始问题的后续跟踪,我决定跟进,看看你能做得更好,然后到目前为止我们想出的是什么.
下面的代码遍历二叉树(左/右=子/下).我相信这里有一个较少的条件空间(down布尔值).最快的答案获胜!
cnt语句可以是多个语句,因此请确保它只出现一次child()和next()成员函数约为30X一样慢的hasChild()和hasNext()操作.目前,此代码在测试树中访问62200000个节点需要5897ms,将此功能调用200,000次.
void processTree (BaseNodePtr current, unsigned int & cnt )
{
bool down = true;
while ( true )
{
if ( down )
{
while (true) {
cnt++; // this can/will be multiple statesments
if (!current->hasChild()) break;
current = current->child();
}
}
if ( current->hasNext() )
{
down = true;
current = current->next();
}
else
{
down = false; …Run Code Online (Sandbox Code Playgroud) 我正在尝试构建一个二叉树,但由于某种原因我的代码不起作用.有人能帮帮我吗?我输入随机数,它...我无法解释它,最好自己运行它并在调试中看到结果;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
leaf root = new leaf();
while (true)
{
string read = Console.ReadLine();
if (read == "print")
{
root.print();
}
else
{
root.grow(Convert.ToInt32(Console.ReadLine()));
}
}
}
}
class leaf
{
public void print()
{
}
public void grow(int value)
{
if (isNull)
{
isNull = false;
this.value = value;
smaller = bigger = new leaf();
}
else
{
if (value > …Run Code Online (Sandbox Code Playgroud) 考虑具有n个节点的二叉树.有多少种不同的二叉树结构?
我尝试过类似的东西:
n number of different structure:
1 1
2 4
3 16
Run Code Online (Sandbox Code Playgroud)
对于n> 1,4(n-1)也是如此; 1 = n == 1?
我正在为学校制作二叉树计划,我的一切都很完美.我现在正在努力的是正确的输出.我的老师希望输出为排序后的所有数字,后面都有逗号.
我的代码我已经完美地对数字进行了排序并打印出来,我只是不确定如何在最后一个数字后删除逗号.
电流输出:1,2,3,4,
需要:1,2,3,4
这是我的代码:
void BinaryTree::inorder(tree_node* p)
{
if(p != NULL)
{
if(p->left)
inorder(p->left);
cout << p->data << ", ";
if(p->right)
inorder(p->right);
}
else
return;
}
Run Code Online (Sandbox Code Playgroud)
我已经尝试了一些方法来使它正确,但我无法弄明白.
任何帮助都会很棒.
谢谢.
我在接受采访时被问到这个问题.
问题:一个二叉树和相应的子树的高度,也给我们的.然后我们必须按顺序在特定位置找到一个元素.
例如:树结构是:D(根节点)[子树大小= 6] - > B,F(D的子节点)[子树大小= 2] - > A,C,E,G(叶子)节点)[subtree-size = 0].
所以共有3个级别:0级:D; 1级:B,F; 等级2:A,C,E,G
我们必须按顺序计算特定顺序/位置的节点,比如p.如果p = 2,那么节点将是B(按顺序遍历).
我的解决方案:我建议我们需要进行一次遍历遍历(通过BFS/DFS),然后我们可以给出第i个顺序节点,时间复杂度为O(n).
现在我被问到可以根据子树大小信息改进解决方案.但我无法想出子树大小可以减少时间复杂度的任何方式.
子树大小信息可以减少时间复杂度吗?如果是,请分享算法/伪代码.
我的问题是为什么我需要取消引用并引用以下代码的指针才能工作?不参考/反对取消吗?我真的很感激,如果有人能解释它,就像我五岁:)
码:
template <typename T>
class binNode {
private:
T key;
public:
binNode * left;
binNode * right;
binNode * parent;
binNode() {
this->left = NULL;
this->right = NULL;
this->parent = NULL;
}
// arg constructor:
binNode (T key) {
this->key = key;
this->left = NULL;
this->right = NULL;
this->parent = NULL;
}
T getKey() {
return this->key;
}
void setKey(T key) {
this->key = key;
}
};
template<typename T> class Tree {
private:
binNode <T> *root;
public:
Tree() { …Run Code Online (Sandbox Code Playgroud) 我想计算完整二进制树中的节点数,但我能想到的只是遍历整个树.这将是一个O(n)算法,其中n是树中节点的数量.什么是最有效的算法来实现这一目标?
我一直在学习lua,似乎无法简单地实现这个二叉树的工作......
function createTree(tree, max)
if max > 0 then
tree = {data = max, left = {}, right = {}}
createTree(tree.left, max - 1)
createTree(tree.right, max - 1)
end
end
function printTree(tree)
if tree then
print(tree.data)
printTree(tree.left)
printTree(tree.right)
end
end
tree = {}
createTree(tree, 3)
printTree(tree)
Run Code Online (Sandbox Code Playgroud)
程序在执行后返回nil.我在网上搜索了解参数传递如何在lua中工作(如果它是通过引用或值)并且发现某些类型通过引用(如表和函数)传递,而其他类型通过值传递.尽管如此,我还是将全局变量"tree"作为一个表传递给了"createTree"函数,我甚至将"left"和"right"初始化为"createTree"中的空表,用于相同的目的.我究竟做错了什么?
我正在尝试在二叉树中实现find方法.当方法需要返回一个值时会出现问题,但即使在同一个分号中执行其他行时,它似乎也没有执行该语句.
public String find(Node currentNode, String value)
{
if(isEmpty())
{
return null;
}
else
{
if(currentNode.getData().compareToIgnoreCase(value) == 0)
{
System.out.println("current -> "+currentNode.getData()); //showing
return currentNode.getData(); //not executing
}
if(value.compareToIgnoreCase(currentNode.getData()) < 0)
{
find(currentNode.getLeft(), value);
}
else if(value.compareToIgnoreCase(currentNode.getData()) > 0)
{
find(currentNode.getRight(), value);
}
}
return null; //always executing
}
Run Code Online (Sandbox Code Playgroud)
我期待"a"但返回null.