我必须使用结构创建一个二叉树,如下所示:
struct treenode;
typedef struct treenode* TreeNode;
struct treenode {
void* data;
TreeNode left, right;
};
Run Code Online (Sandbox Code Playgroud)
使用void*作为要存储在每个叶子上的数据的类型,以便可以将任何类型的对象插入到树中.
当我插入一个新的叶子时,我必须使用一个比较函数,它检查新叶子中的数据是否已经在树中,这需要两个void*参数,例如:
int compare(void* a, void* b){
..
..
}
Run Code Online (Sandbox Code Playgroud)
但如果我不知道它们是什么类型,我如何比较这两个对象呢?
解决这个问题的一些代码会非常有用.
我想制作一个通用的BST,它可以由任何数据类型组成,但是如果我的BST是通用的,我不知道如何向树中添加内容.我需要的所有代码都在下面.我希望我的BST由Locations组成,并按x变量排序.任何帮助表示赞赏.
非常感谢您的关注.
public void add(E element)
{
if (root == null)
root = element;
if (element < root)
add(element, root.leftChild);
if (element > root)
add(element, root.rightChild);
else
System.out.println("Element Already Exists");
}
private void add(E element, E currLoc)
{
if (currLoc == null)
currLoc = element;
if (element < root)
add(element, currLoc.leftChild);
if (element > root)
add(element, currLoc.rightChild);
else
System.out.println("Element Already Exists);
}
Run Code Online (Sandbox Code Playgroud)
其他代码
public class BinaryNode<E>
{
E BinaryNode;
BinaryNode nextBinaryNode;
BinaryNode prevBinaryNode;
public BinaryNode()
{
BinaryNode = null;
nextBinaryNode …Run Code Online (Sandbox Code Playgroud) 如何在不使用堆栈的情况下非递归地遍历O(n)中的线程二叉树(只允许为临时变量使用常量额外空间,因此我们不能将访问标志添加到树中的每个节点).我花了很多时间思考它,但除非我们要遍历具有树数据的内存位置,否则我似乎并不可行.假设我们使用多个数组表示来实现指针,然后我们可以遍历O(n)中的树,是否有人有其他想法?
注意这不是功课,只是为了节省一些键盘敲击的能量来写关于作业的评论!
我正在尝试为二叉树创建一个新节点,当我尝试对 - > left和 - > right进行赋值时,我遇到了错误.
typedef struct bin_node_t {
data_t data;
bst_key_t key;
struct bin_node *left;
struct bin_node *right;
} bin_node;
Run Code Online (Sandbox Code Playgroud)
是我对bin_node的结构定义.
以下是我正在使用的变量:
bin_node *new;
bin_node *node_array[256];
Run Code Online (Sandbox Code Playgroud)
这是我的变量赋值:
/* ... code to initialize node_array ... */
new = (bin_node *)malloc(sizeof(bin_node));
Run Code Online (Sandbox Code Playgroud)
而这里是我遇到错误的地方:
new->right = node_array[i+1];
new->left = node_array[i];
Run Code Online (Sandbox Code Playgroud)
这是我得到的编译器警告:
huffman.c:99: warning: assignment from incompatible pointer type
huffman.c:100: warning: assignment from incompatible pointer type
Run Code Online (Sandbox Code Playgroud)
我的完整代码位于:
是否有一些算法可以达到二叉树的孙子?像例子一样?

在图中,有一些节点将祖父母与其孙子孙女联系起来,而普通的二元树只将儿童与父母联系起来.用什么算法连接祖父母?
编辑:每个节点都有一个索引和两个值.[指数] [价值];
我想做什么:
index[3] and index[4] = value[0];
index[5] and index[6] = value[1];
index[7] and index[8] = value[2];
index[9] and index[10] = value[3];
.... ETC
Run Code Online (Sandbox Code Playgroud) 我必须制作一个表达式树.这是它的单个示例模型.但它显示我奇怪的字符,但不是我的字符串.你能帮我解决这个问题吗?还有一个问题:你能告诉我简化代码的方法吗?这段代码怎么样?我可以不用它吗?
public Iterator<TreeNode<T>> iterator() {
return null;
}
Run Code Online (Sandbox Code Playgroud)

import java.util.*;
public class TreeNode<T> implements Iterable<TreeNode<T>> {
T data;
TreeNode<T> parent;
List<TreeNode<T>> children;
public TreeNode(T data) {
this.data = data;
this.children = new LinkedList<TreeNode<T>>();
}
public TreeNode<T> addChild(T child) {
TreeNode<T> childNode = new TreeNode<T>(child);
childNode.parent = this;
this.children.add(childNode);
return childNode;
}
public Iterator<TreeNode<T>> iterator() {
return null;
}
public static void main(String[] args){
TreeNode<String> root = new TreeNode<String>("root");
System.out.println(" " + root + " ");
System.out.println(" / \\ ");
TreeNode<String> …Run Code Online (Sandbox Code Playgroud) 问:在我下面的二叉树实现中,为什么编译器会窒息
if (data.compareTo(this.data) <= 0),
生产
Error: incompatible types: java.lang.Comparable<T> cannot be converted to T?
这两个data和this.data的类型Comparable<T>,应该能够使用或者是一个参数传递给的compareTo()方法,对吧?好吧,显然不是.但我真的不明白为什么.仿制药仍然令我感到困惑.
public class MyBinaryTreeNodeG<T>{
Comparable<T> data;
MyBinaryTreeNodeG<T> parent;
MyBinaryTreeNodeG<T> left;
MyBinaryTreeNodeG<T> right;
public MyBinaryTreeNodeG(Comparable<T> data){
this.data = data;
}
public MyBinaryTreeNodeG<T> addChild(Comparable<T> data){
if (data.compareTo(this.data) <= 0) { //this is the line on which the compiler chockes
//check if left tree node is null. If so, add. Otherwise, recurse.
} else {
//same for the right …Run Code Online (Sandbox Code Playgroud) 所以,我有那个用于编码字符串的Huffman树.我已经定义了这个功能plant,但我不确定我的树是不是只向一侧倾斜太多.这是我的代码:
data HuffTree
= Leaf Char
| HuffTree |*| HuffTree
deriving (Eq, Show)
Run Code Online (Sandbox Code Playgroud)
|*| 是一个中缀构造函数.
plant :: [(Char,Int)] -> HuffTree
plant [(x,y)] = (Leaf x)
plant ((x,y):xs) = plant xs |*| (Leaf x)
Run Code Online (Sandbox Code Playgroud)
对我来说,它看起来是片面的,因此它实际上并不意味着编码的想法,因为它不是真正的二叉树.我怎么能把它变成一个普通的二叉树?
我尝试在其预订和顺序遍历上构建二叉树条件.
但是,当我运行下面的代码时,20%的测试样本通过,然后我得到了"1351分段错误(核心转储)",其中preorder = inorder = {1,2}.
我的代码有什么问题?以及如何在linux上调试"分段故障"?
谢谢!!!
class Solution {
/**
*@param preorder : A list of integers that preorder traversal of a tree
*@param inorder : A list of integers that inorder traversal of a tree
*@return : Root of a tree
*/
public:
TreeNode *buildTree(vector<int> &preorder, vector<int> &inorder) {
// write your code here
TreeNode * root;
if (preorder.empty() || inorder.empty()) {
cout<<"in if"<<endl;
return NULL;
}
root->val = preorder.front();
vector<int> inorder_left(inorder.begin(), inorder.begin() + getIndex(inorder, preorder.front()));
vector<int> …Run Code Online (Sandbox Code Playgroud) 我有一个python代码将字符串数学表达式转换成二叉树并排序树的节点,以便左子节点始终小于右子节点。我想按以下顺序打印二叉树。
例如,考虑数学表达式((2 * 75)/ 4)。buildParseTree()将字符串表达式转换为树,并且printNodeInLevels()重新排列节点,以便在每个级别上,左子项小于右子项。操作数<运算符和运算符的顺序为'+'<'-'<'*'<'/'。如果树的结构是这样的
+
/\
4 *
/\
2 75
Run Code Online (Sandbox Code Playgroud)
我想按以下方式打印它。我应该怎么做?因为数学表达式的长度一直在变化,例如(24 * 2),((5-1)*(2/3)),(20-(5 + 4))等
Node("+") #root
.addkid(Node("*") #right child at level 1
.addkid(Node("75")) #right child at level 2
.addkid(Node("2")) #left child at level 2
)
.addkid(Node("4")) #left child at level 1
Run Code Online (Sandbox Code Playgroud)
我已经设计出按顺序遍历模式按节点级别打印节点的方法,如果我按如下方式调用该方法,它将打印以下内容:
pt = buildParseTree("( ( 2 * 74 ) / 4 )")
printNodesInLevels(pt)
Run Code Online (Sandbox Code Playgroud)
输出:
/
4 *
2 74
Run Code Online (Sandbox Code Playgroud)