标签: binary-tree

复制二叉树C++的构造函数

我有一个具有以下定义的Tree类:

class Tree {
  Tree();
private:
  TreeNode *rootPtr;
}
Run Code Online (Sandbox Code Playgroud)

TreeNode表示一个节点并具有数据,leftPtr和rightPtr.

如何使用复制构造函数创建树对象的副本?我想做的事情如下:

Tree obj1;
//insert nodes

Tree obj2(obj1); //without modifying obj1.
Run Code Online (Sandbox Code Playgroud)

任何帮助表示赞赏!

c++ binary-tree copy-constructor

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

二叉搜索树的实例和java

我正在尝试使用Cormen的伪代码实现BST算法但仍存在问题.

这是我的节点代码:

public class Node {
    Node left;
    Node right;
    int value;

    Node(int value){
        this.value = value;
        this.left = null;
        this.right = null;  
    }
}
Run Code Online (Sandbox Code Playgroud)

而对于Bstree:

public class Btree {
    Node root;

    Btree(){
        this.root = null;
    }

    public static void inorderWalk(Node n){
        if(n != null){
            inorderWalk(n.left);
            System.out.print(n.value + " ");
            inorderWalk(n.right);
        }
    }

    public static Node getParent(Btree t, Node n){
        Node current = t.root;
        Node parent = null;


        while(true){
            if (current == null)
                return null;

            if( current.value == n.value ){ …
Run Code Online (Sandbox Code Playgroud)

java algorithm binary-tree

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

将树保存为预订

我创建了一个带{2,5,3,4,9,1,7,...,100}数字的二叉搜索树.

我怎么能把它保存为preorder?谢谢

编辑:考虑我有{ 3,7,1,2}binary search tree使用这些数字,我想保存这棵树preorder which is {3,1,2,7}

java binary-tree

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

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

汇总子值并保存在中间步骤中计算的值

struct node { 
    int value; 
    struct node* left; 
    struct node* right; 
    int left_sum;
    int right_sum;
} 
Run Code Online (Sandbox Code Playgroud)

在二叉树中,从特定节点,有一个简单的递归算法来汇总其所有子值.有没有办法保存在中间步骤计算的值,并将其存储为left_sumright_sum在子节点?

通过添加struct node* parent节点定义的链接,自下而上更容易吗?

binary-tree

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

从根开始逐层遍历二叉树的算法

任何人都可以建议一个算法从根开始逐级遍历二叉树的水平?

algorithm tree binary-tree data-structures

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

二叉树中最长的路径,最多一圈

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

给定二叉树,找到最多一个转弯的最长路径长度.路径的一端必须是一片叶子.另一端可以是叶子或任何节点.

转弯定义为:

In tree1-> start from 1 and there is a turn at root 2 towards right,
In tree2-> starts from 3 goes in left and there is a turn at 1 towards right ,
In tree3-> starts from 1 goes in right and there is a turn at 3 towards left,

     2                 3                 1
    / \               /                   \
   1   3             1                     3
                      \                    /
                       2                  2
Run Code Online (Sandbox Code Playgroud)

一些人可以帮助如何继续.谢谢..

编辑:在采访中,我被问到这个问题是树问题直径的后续问题.

我对树的直径的实现是这样的.

变量'res'包含最终答案.

int maxPathSumUtil(struct Node *root, int &res) …
Run Code Online (Sandbox Code Playgroud)

algorithm tree binary-tree

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

如何进行二叉树平衡

这是c中的一个简单的二叉树,但它似乎不平衡,如何使其平衡?

码:

/**
 * binary_tree impl
 */

#include <stdio.h>
#include <stdlib.h>


typedef struct _tnode _tnode;
typedef struct _bin_tree _bin_tree;
struct _tnode {
    int data;
    _tnode *parent;
    _tnode *left;
    _tnode *right;
};

_tnode *new_node(int data) {
    _tnode *node = (_tnode*)malloc(sizeof(_tnode));
    node->data = data;
    node->left = NULL;
    node->right = NULL;
    return node;
}

_tnode *add(_tnode *top, int new_data, int (*cmpf)(int, int)) {
    if(top == NULL) {
        top = new_node(new_data);
    } else if(cmpf(top->data, new_data)<=0) {
        if(top->left == NULL) 
            top->left = new_node(new_data);
        else
            add(top->left, …
Run Code Online (Sandbox Code Playgroud)

c algorithm binary-tree tree-balancing

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

如何使用显式链接(使用三重链接的数据结构)实现优先级队列?

我正在尝试使用三重链接的数据结构来实现优先级队列。我想了解如何实现接收游泳操作,因为当您使用数组时,您仅可以计算该数组的索引即可。使用三重链接的DS时,这没有任何意义。

另外,我想了解如何在正确的位置正确插入某些内容,因为当您使用数组时,您可以只在最后插入并执行泳动操作,这会将所有内容都放置在正确的位置,我该如何准确地计算出“链接DS中的“结束”?

另一个问题是删除具有最高优先级的元素。为此,对于数组实现,我们只需将最后一个元素与第一个(根)元素交换,然后在删除最后一个元素之后,将第一个元素下沉。

(这是Sedgewick的任务)。

java algorithm binary-tree priority-queue binary-search-tree

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

在C++中,当delete调用析构函数时,析构函数的主体是否在释放内存之前触发?

例如,我正在尝试使用此递归解构器删除二叉树:

~BinTreeNode() {
    delete left;
    delete right;
    // delete this; <- i'm assuming this is implicit, so i don't need to include it
}
Run Code Online (Sandbox Code Playgroud)

如果我确实delete root;root是根节点,那么整个树的内存是否会被成功释放?

c++ binary-tree dynamic-memory-allocation

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