我需要一个具有重复项的二叉树,我需要 O(Log(n)) 复杂度的搜索和插入,同时保持顺序(所以我不能使用哈希表),java没有实现二叉树的集合并允许重复,同时保留所有二叉树操作。
我正在尝试调整 TreeSet 并通过传递一个永远不会返回 0 的比较器来允许重复项。我知道这不再是一个集合,但没关系,我需要重复项。
例子
TreeSet<Integer> binaryTreeWithDuplicates = new TreeSet<Integer>((x, y) -> x>y?1:-1);
Run Code Online (Sandbox Code Playgroud)
这种实施和使用会产生不良的副作用吗?因为我们显然违反了比较器 api
中的规则,例如符号规则。
你如何在C#中使用简单,直接的二进制树,并且不使用任何预定义的类?我说的是像C++中那样简单的事情
没有像NGenerics Objects那样代表树木
我的意思是从简单的东西开始,像这样:
struct
{
Node * left
Node * right
int value;
}
Run Code Online (Sandbox Code Playgroud)
跟进问题:
好的,如果我有这个:
public class binarytreeNode
{
public binarytreeNode Left;
public binarytreeNode Right;
public int data;
}
Run Code Online (Sandbox Code Playgroud)
我是否必须在此类中放置作用于节点的方法?这不会使这不再是节点吗?
如果我创建一个在Program类中添加节点的方法:
class Program
{
public binarytreeNode AddNode(int value)
{
binarytreeNode newnode = new binarytreeNode();
newnode.Left = null;
newnode.Right = null;
newnode.data = value;
return newnode;
}
static void Main(string[] args)
{
binarytreeNode head = AddNode(4);
}
}
Run Code Online (Sandbox Code Playgroud)
编译器说我调用AddNode需要一个对象引用.为什么?
这似乎应该很容易,但我已经有很长一段时间没遇到这个问题了.正如标题所说,我只是试图找到具有最小值的二叉树(不是BST!)中的节点并返回它.我可以很容易地写一个递归的void函数,至少可以在函数中分配最小的值,但是当我到达NULL指针时,我会陷入如何回溯到先前节点的问题.
我有一个节点类,它有一个指向左右子节点的指针,每个子节点都有自己的值.到目前为止,这是我的(失败)尝试:
int preOrder(Node *node, int value, int count, int sizeOfTree)
{
count++; //keeps track of whether or not we have traversed the whole tree
if(value < node->getValue())
value = node->getValue();
if(count == sizeOfTree);
return value;
if(node == NULL)
//Want to return to the previous function call
//How do I do this for a non void function?
//for a void function, you could jsut type "return;" and the function
//back tracks to your previous place in the tree
//but since …Run Code Online (Sandbox Code Playgroud) 当我运行以下代码时:
class zTree<T>
{
ArrayList<ArrayList<T>> table = new ArrayList<ArrayList<T>>();
int height = 0;
<T> void zTree(BinaryTree<T> tree)
{
recIt((BinaryTree<T>)tree, 1);
}
void recIt(BinaryTree<T> tree, int fromRoot)
{
if(!(tree.isEmpty()))
{
ArrayList<T> tempList = (ArrayList<T>)table.get(fromRoot);
tempList.add((T)tree.getData()); // add data to table
recIt(tree.left,fromRoot+1); // recursive left,
recIt(tree.right,fromRoot+1); // right
}
else
{
height = fromRoot-1;
}
}
}
Run Code Online (Sandbox Code Playgroud)
Javac返回此错误.
zTree.java:15: recIt(structures.tree.BinaryTree<T>,int) in zTree<T> cannot be applied to (structures.tree.BinaryTree<T>,int)
recIt((BinaryTree<T>)tree, 1);
^
1 error
Run Code Online (Sandbox Code Playgroud)
我不关心他的代码效率.我想知道出了什么问题,但javac显然没有多少帮助,因为它告诉我(x,y)不能应用于(x,y)......但为什么呢?
我正在浏览这个,但在第114行写的printf("%d -> ", t->value);
是我问的是什么"%d ->意思?这是拼写错误还是其他什么?
例:
struct btnode {
int value;
struct btnode * l;
struct btnode * r;
} * root = NULL, * temp = NULL, * t2, * t1;
void inorder(struct btnode * t) {
if (root == NULL) {
printf("No elements in a tree to display");
return;
}
if (t->l != NULL)
inorder(t->l);
printf("%d -> ", t->value);
if (t->r != NULL)
inorder(t->r);
}
Run Code Online (Sandbox Code Playgroud) 我是Rust的新手,为了练习,我正在构建一个简单的通用二叉树.这就是我在C++中创建一个的方法
template<typename T>
struct Node
{
T data;
Node<T>* parent;
Node<T>* left;
Node<T>* right;
};
template<typename T>
struct Bintree
{
Node<T>* root;
};
Run Code Online (Sandbox Code Playgroud)
但Rust中的相同(ish)代码似乎不起作用:
use std::ptr;
struct Node<T> {
data: T,
left: &Node<T>,
right: &Node<T>,
parent: &Node<T>,
}
struct Tree<T> {
root: &Node<T>,
}
impl Tree<T> {
pub fn new() -> Tree<T> {
Tree { root: ptr::null() }
}
pub fn insert(&self, value: T) {
if root.is_null() {
self.root = Node {
data: value,
left: ptr::null(),
right: ptr::null(),
parent: ptr::null(), …Run Code Online (Sandbox Code Playgroud) 如何返回树中所有奇数的总和?我必须使用递归来查找总和吗?
这是我的代码:
int sumOdd(BTreeNode *node)
{
int sum = 0;
if(node == NULL)
return 0;
else{
if((node->item % 2) != 0)
return sum = node->item +
sumOdd(node->left) +
sumOdd(node->right);
}
return sum;
}
Run Code Online (Sandbox Code Playgroud) 这有什么问题?
// rbt.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#ifndef rbth
#define rbth
typedef enum {
RBT_STATUS_OK,
RBT_STATUS_MEM_EXHAUSTED,
RBT_STATUS_DUPLICATE_KEY,
RBT_STATUS_KEY_NOT_FOUND
} RbtStatus;
typedef void *RbtIterator;
typedef void *RbtHandle;
RbtHandle rbtNew(int(*compare)(void *a, void *b));
// create red-black tree
// parameters:
// compare pointer to function that compares keys
// return 0 if a == b
// return < 0 if a < b
// return > 0 if a > b
// returns:
// handle …Run Code Online (Sandbox Code Playgroud) 我想创建一个由treeNodes组成的数组Arraylist.我的审判是
ArrayList<Arrays<treeNode>> aList = new ArrayList<Arrays<treeNode>>();
Arrays<TreeNode> aNodes = new ArrayList<TreeNode>();
Run Code Online (Sandbox Code Playgroud)
但它给出了一个错误.(包括utils)
写这个的正确方法是什么?我的目标是在二叉树中找到节点的最小深度(只是为了找到最小的不找到该节点本身,我将水平放入arraylist,一旦大小不是2 ^ j,那么最小级别是j-1 ).
提前感谢,任何帮助/提示/解决方案......