我有一个具有以下定义的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)
任何帮助表示赞赏!
我正在尝试使用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) 我创建了一个带{2,5,3,4,9,1,7,...,100}数字的二叉搜索树.
我怎么能把它保存为preorder?谢谢
编辑:考虑我有{ 3,7,1,2} 和binary search tree使用这些数字,我想保存这棵树preorder which is {3,1,2,7}
struct node {
int value;
struct node* left;
struct node* right;
int left_sum;
int right_sum;
}
Run Code Online (Sandbox Code Playgroud)
在二叉树中,从特定节点,有一个简单的递归算法来汇总其所有子值.有没有办法保存在中间步骤计算的值,并将其存储为left_sum和right_sum在子节点?
通过添加struct node* parent节点定义的链接,自下而上更容易吗?
任何人都可以建议一个算法从根开始逐级遍历二叉树的水平?
我在接受采访时遇到过这个问题.
给定二叉树,找到最多一个转弯的最长路径长度.路径的一端必须是一片叶子.另一端可以是叶子或任何节点.
转弯定义为:
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) 这是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) 我正在尝试使用三重链接的数据结构来实现优先级队列。我想了解如何实现接收器和游泳操作,因为当您使用数组时,您仅可以计算该数组的索引即可。使用三重链接的DS时,这没有任何意义。
另外,我想了解如何在正确的位置正确插入某些内容,因为当您使用数组时,您可以只在最后插入并执行泳动操作,这会将所有内容都放置在正确的位置,我该如何准确地计算出“链接DS中的“结束”?
另一个问题是删除具有最高优先级的元素。为此,对于数组实现,我们只需将最后一个元素与第一个(根)元素交换,然后在删除最后一个元素之后,将第一个元素下沉。
(这是Sedgewick的任务)。
java algorithm binary-tree priority-queue binary-search-tree
例如,我正在尝试使用此递归解构器删除二叉树:
~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是根节点,那么整个树的内存是否会被成功释放?