标签: binary-tree

比较二叉树的节点

如果我有两个二叉树,我如何检查所有节点中的元素是否相等.

关于如何解决这个问题的任何想法?

c algorithm tree binary-tree

1
推荐指数
1
解决办法
3558
查看次数

树迭代器,你能进一步优化吗?

作为我关于这段代码的一小段原始问题的后续跟踪,我决定跟进,看看你能做得更好,然后到目前为止我们想出的是什么.

下面的代码遍历二叉树(左/右=子/下).我相信这里有一个较少的条件空间(down布尔值).最快的答案获胜!

  1. cnt语句可以是多个语句,因此请确保它只出现一次
  2. child()next()成员函数约为30X一样慢的hasChild()和hasNext()操作.
  3. 保持迭代< - 放弃此要求,因为呈现的递归解决方案更快.
  4. 这是C++代码
  5. 访问节点的顺序必须保持原样,如下例所示.(首先击中父母然后是孩子然后是'下一个'节点).
  6. BaseNodePtr是一个boost :: shared_ptr,因为赋值很慢,避免任何临时的BaseNodePtr变量.

目前,此代码在测试树中访问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)

c++ iteration optimization binary-tree

1
推荐指数
1
解决办法
2341
查看次数

C#中的二叉树

我正在尝试构建一个二叉树,但由于某种原因我的代码不起作用.有人能帮帮我吗?我输入随机数,它...我无法解释它,最好自己运行它并在调试中看到结果;

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)

c# binary-tree

1
推荐指数
1
解决办法
1287
查看次数

对于二叉树,假设有n个节点,我可以构造多少个不同的结构?

考虑具有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?

binary-tree catalan

1
推荐指数
2
解决办法
8862
查看次数

二进制搜索树打印

我正在为学校制作二叉树计划,我的一切都很完美.我现在正在努力的是正确的输出.我的老师希望输出为排序后的所有数字,后面都有逗号.

我的代码我已经完美地对数字进行了排序并打印出来,我只是不确定如何在最后一个数字后删除逗号.

电流输出: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)

我已经尝试了一些方法来使它正确,但我无法弄明白.

任何帮助都会很棒.

谢谢.

c++ io binary-tree

1
推荐指数
1
解决办法
1万
查看次数

面试问:二叉树顺序遍历

我在接受采访时被问到这个问题.

问题:一个二叉树和相应的子树的高度,也给我们的.然后我们必须按顺序在特定位置找到一个元素.

例如:树结构是: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).

现在我被问到可以根据子树大小信息改进解决方案.但我无法想出子树大小可以减少时间复杂度的任何方式.

子树大小信息可以减少时间复杂度吗?如果是,请分享算法/伪代码.

java algorithm tree binary-tree graph-algorithm

1
推荐指数
1
解决办法
1384
查看次数

需要引用和解除引用指向根的二叉树指针.为什么?

我的问题是为什么我需要取消引用并引用以下代码的指针才能工作?不参考/反对取消吗?我真的很感激,如果有人能解释它,就像我五岁:)

码:

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)

c++ binary-tree pointers reference dereference

1
推荐指数
1
解决办法
543
查看次数

计算完整二叉树中的节点数

我想计算完整二进制树中的节点数,但我能想到的只是遍历整个树.这将是一个O(n)算法,其中n是树中节点的数量.什么是最有效的算法来实现这一目标?

algorithm tree recursion binary-tree

1
推荐指数
3
解决办法
6993
查看次数

lua不修改函数参数

我一直在学习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"中的空表,用于相同的目的.我究竟做错了什么?

lua binary-tree arguments function lua-table

1
推荐指数
2
解决办法
1144
查看次数

为什么return语句没有执行?

我正在尝试在二叉树中实现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.

java binary-tree

1
推荐指数
1
解决办法
75
查看次数