标签: binary-tree

-3
推荐指数
3
解决办法
3473
查看次数

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

试图删除二叉树C++

我试图在C++中删除二叉树,但我遇到的问题是树的大小似乎没有改变.这是我正在使用的尺寸函数:

int BST::size(Node *& cur_root)
{
    if (cur_root == NULL) {
        return 0;
    } else { 
        return(size(cur_root->m_left) + 1 + size(cur_root->m_right));
    }
}
Run Code Online (Sandbox Code Playgroud)

这是我正在尝试使用它的功能:

void BST::deletetree(Node *& cur_root)
{
    cout << "tree size: " << size() << endl;
    if (cur_root!=NULL)
    {
        deletetree(cur_root->m_left);
        deletetree(cur_root->m_right);
        delete cur_root;
        if(cur_root->m_left != NULL) {
            cur_root->m_left = NULL;
        }
        if(cur_root->m_right != NULL) {
            cur_root->m_right = NULL;
        }
        cur_root=NULL;
    }
}
Run Code Online (Sandbox Code Playgroud)

对于三个(1,2,3)的树大小,我的输出是:

tree size: 3
tree size: 3
tree size: 3
tree size: 3
tree size: 3 …
Run Code Online (Sandbox Code Playgroud)

c++ binary-tree

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

如何通过删除子树来最大化树的权重

有一棵有N个节点(编号1到N)的根树。节点“ 1”是根。每个节点都有一个值;让我们用A(i)表示节点i的值。

可以多次执行以下操作(包括零次):

1.选择树中仍然存在的任何节点,并删除该节点的整个子树,包括它本身。

2.让我们将利润定义为树中当前存在的所有节点的值之和减去X?k,其中k表示执行此操作的次数。找到最大可能的利润。

我们如何在这里计算“ k”值?(意味着删除时间节点数以获取最佳利润)

例:-


3(number of nodes,N) ,

5(X)

1 -5 -10 (Values of corresponding nodes)

(edge(s) from 'x'->'y')

1 2

2 3

Output: -4

We remove the sub-tree of node : 2'.

Now,value of our tree is: 1.

Finals answer:- 1-(x)*k,
(k=1); as we have performed the operation of removing the sub-tree only '1' time 
:  1-(5)*1= -4.
Run Code Online (Sandbox Code Playgroud)

注意:没有给出树应该是二进制的,它也可以是普通树。

binary-tree subtree

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

Haskell二叉树

我有一个功课:

1)TTT为树定义数据结构,其中每个顶点具有0,1或2个子节点,每个树叶(具有0个子节点的顶点及其自身)包含自然数列表;

2)创建一个mm具有2个参数的函数 - 函数f(Integer->Integer)TTT基础树x.结果它应该给出TTT基于树的结构树,该树是x使用f列表中每个元素的函数(引用1)定义的;

函数f可以有以下表示(a,bc):

a :: Integer -> Integer
a x = x * x

b :: Integer -> Integer
b x = x `mod` 9

c :: Integer -> Integer
c x = x * x * x
Run Code Online (Sandbox Code Playgroud)

任何人都可以帮我吗?

tree binary-tree haskell

-4
推荐指数
1
解决办法
9721
查看次数

在二叉树中查找节点

我有一个问题,一个方法应该在二叉树中找到一个包含给定的节点value.下面提供的方法不起作用,问题是为什么.

public Node search(Node node, int value) {
    if(node.value == value) return node;
    if(node.left != null) search(node.left, value);
    if(node.right != null) search(node.right, value);
    return null;
}
Run Code Online (Sandbox Code Playgroud)

问题是,nullvalue树中实际存在给定节点时,此方法有时会返回.这是为什么?

java binary-tree

-4
推荐指数
1
解决办法
66
查看次数

-4
推荐指数
1
解决办法
4001
查看次数

如何按降序打印二叉树搜索?

可以用这个按顺序打印

void printInOrder(noBinTree *n){
        if(n != NULL){
            printInOrder(n->left);
            printf(" %d ", n->number);
            printInOrder(n->right);
        }
}
Run Code Online (Sandbox Code Playgroud)

并得到

1、2、3、4、5

我需要做什么才能按降序打印它(仅通过操作该函数)并得到 5、4、3、2、1 作为结果?

c binary-tree inorder binary-search-tree

-4
推荐指数
1
解决办法
3889
查看次数

为什么std :: set不只是称为std :: binary_tree?

就数据结构而言,C ++中的std :: set不是真正的集合。std :: unordered_set是一个实数集,但std :: set是一个二叉搜索树,更具体地说是一棵红黑树。那么为什么将其称为std :: set?是否有一些特定功能可以将std :: set与二叉树区分开?谢谢。

c++ binary-tree set binary-search-tree data-structures

-5
推荐指数
1
解决办法
86
查看次数

Haskell函数寻求解释

data BTree a = Empty | Node (BTree a) a (BTree a) -- This is a node-labelled binary tree
Run Code Online (Sandbox Code Playgroud)

有人可以解释一下下面的Haskell函数吗?

  1. labels :: BTree a -> [a]

    labels Empty = []
    labels (Node left label right) = labels left ++ [label] ++ labels right
    
    Run Code Online (Sandbox Code Playgroud)
  2. reflect :: BTree a -> BTree a

    reflect Empty = Empty
    reflect (Node left label right) = Node (reflect left) label (reflect right)
    
    Run Code Online (Sandbox Code Playgroud)

binary-tree haskell functional-programming

-11
推荐指数
1
解决办法
367
查看次数