所以我的代码如下.我没有收到任何错误,它将节点中的所有内容都放好了.但基于我的调试语句每次插入任何内容时它都会找到根.我不确定这是不对的.但根据作业的输出文件,我的答案是不同的,当涉及树的高度,遍历,我只是平坦我仍然有我的叶计数功能的麻烦.另一个故事.
基于调试语句,看起来一切都在他们应该的位置.但我想我可能需要新鲜的眼睛.我不知道我的遍历是如何改变的,因为它实际上只是我处理节点应该影响顺序,预订和后序的问题.
template <class T>
void BT<T>::insert(const T& item)
{
Node<T>* newNode;
newNode = new Node<T>(item);
insert(root, newNode);
}
template <class T>
void BT<T>::insert(struct Node<T> *&root, struct Node<T> *newNode)
{
if (root == NULL)
{
cout << "Root Found" << newNode->data << endl;
root = newNode;
}
else
{
if (newNode->data < root->data)
{
insert(root->left, newNode);
cout << "Inserting Left" << newNode-> data << endl;
}
else
{
insert(root->right, newNode);
cout << "Inserting Right" << newNode->data << endl;
}
} …Run Code Online (Sandbox Code Playgroud) 我有二进制树格式的数据,我想将它表示为一个图像(*.jpeg)然后我需要在网页上显示该图像,所有数据将来@运行时,因此图像处理应该在@ runtime,这该怎么做 ?
这是我的思想解决方案,任何其他合适的解决方案也欢迎,
网站是在.NET,我想用java api生成图像然后通过WEB-SERVICE调用将其集成到.NET或任何其他解决方案也欢迎.
可能重复:
什么是Java?:运算符被调用,它做了什么?
我试图阅读二叉树的实现,我遇到了这一行代码:
if (...) {
...
} else {
node = ( node.left != null ) ? node.left : node.right; //this line
}
return node;
Run Code Online (Sandbox Code Playgroud)
谁能告诉我这条线的含义是什么?我最好的猜测是,这是某种条件陈述.
我得到一个包含运算符+, - ,*,/和括号的算术公式(可能会也可能不会改变运算符的自然优先级).一个例子如下:a/b + f - (c + d)*e - a*c.并且我被要求使用堆栈(实现为链接列表)以跟踪操作数和运算符:我的程序应该如何工作的示例如下:
我难以理解的问题是如何区分操作数的优先级!
这是我写的代码的不完整版本:
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
typedef struct btnode Btree;
typedef struct node s_Node;
struct btnode {
char info;
Btree *left;
Btree *right;
};
struct node {
char element;
s_Node*next;
};
typedef struct{
s_Node *top_stack;
} stack_t;
int IsOperator(char c);
main () {
FILE* fp;
stack_t …Run Code Online (Sandbox Code Playgroud) 我正在写一个递归函数,打印出二叉树的叶节点.这是我到目前为止所拥有的:
public static void printLeafNodes(BinaryNode<AnyType> t)
{
if(t == NULL)
return;
if(t.left == NULL && t.right==NULL)
System.out.println(t.element);
else if(t.left != NULL && t.right == NULL)
printLeafNodes(t.left);
else
printLeafNodes(t.right);
}
Run Code Online (Sandbox Code Playgroud)
如果有人可以查明我逻辑中的任何流程,我将非常感激.
谢谢.
这是我的代码,除了无限循环之外几乎正常工作 printInTree()
struct node{
char text[100];
int count;
struct node* left;
struct node* right;
};
struct node* addNode(struct node* n,char w[]){
int cond=0;
if(n == NULL){
n=malloc(sizeof(struct node));
n->count=1;
n->left=NULL;
n->right=NULL;
strcpy(n->text,w);
}
else if((cond=strcmp(w,n->text))==0){
n->count++;
}
else if(cond>0){
n->right=addNode(n->right,w);
}
else{
n->left=addNode(n->left,w);
}
return n;
};
void printInTree(struct node* p){
while(p != NULL){ //infinite loop here.
printInTree(p->left);
printf("%3s - %d\n",p->text,p->count);
printInTree(p->right);
}
}
void b_treeDemo(){
struct node *root=NULL;
FILE* f=fopen("main.c","r");
char word[100];
while(1){
if(getWord(f,word)>0){
if(isalpha(word[0])){
root=addNode(root,word);
}
}else{
break; …Run Code Online (Sandbox Code Playgroud) 假设我有一个像这样的二叉树 -
5
/ \
/ \
/ \
/ \
2 8
/ \ / \
/ \ / \
1 3 6 9
\ \ \
4 11 10
Run Code Online (Sandbox Code Playgroud)
现在我有一个随机生成器,它将生成1到树大小的数字(在这种情况下为10).根据由随机数发生器我不得不从树上返回节点生成的随机值(假设,给出7发电机,所以我回到了7点(价值11),做序遍历).明天我再向树中添加4个节点.如何保持一致性?与此同时,返回树中的相同节点作为随机值.inorder遍历将创建一个不同的数组,索引的值将发生变化.
我试图使用awt和swing绘制二叉树.我可以显示树的节点,但我不能从父节点到子节点绘制线条.我使用以下类:Node(表示节点),treeGUI,DrawTree和Main.这是没有线的输出.
我的目的是显示从父节点到子节点的行.我尝试使用类中的drawLinemetohd Graphics,这是输出:
方法drawTree定义节点在屏幕中的值的位置,并在ArrayLists中存储节点的位置.drawLine方法绘制线条.我认为这些行是这样的,因为值以特定的顺序存储在ArrayList中.我尝试了各种方法以正确的方式画线,但都不成功.我如何画出从父母到孩子的线条?
public class TreeGUI extends JFrame {
private JPanel contentPane;
public Node node;
public DrawTree drawer;
/**
* Create the frame.
*/
public TreeGUI(Node node) {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 500, 500);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
drawer = new DrawTree(node);
contentPane.add(drawer);
setContentPane(contentPane);
this.node = node;
setVisible(true);
}
}
class DrawTree extends JPanel{
public Node node;
public static ArrayList listX = new ArrayList();
public static ArrayList listY = …Run Code Online (Sandbox Code Playgroud) 给定一个二叉树的表示,它可以有最多n个节点:
typedef struct node
{
int info,n;
struct node *left,*right;
}tree_node;
Run Code Online (Sandbox Code Playgroud)
从二叉树构造一个无向图,该二叉树最多可以有n个节点.
图表表示为结构:
typedef struct
{
int n;
tree_node *nodes[];
int adjacency_m[][];
}graph;
Run Code Online (Sandbox Code Playgroud)
我们可以使用Prim,Kruskal或DFS等算法从图中获取树.
问题:是否存在从二叉树创建图形的算法?例如,如果以顺序方式遍历二叉树,那么如何从中创建无向图?
给定一个节点表(它们的ID及其父ID),我如何获得所有的根,内部和叶节点?
这是我到目前为止的内容:
根节点
SELECT Id, "Root" FROM NodeTable
WHERE ParentId IS NULL;
Run Code Online (Sandbox Code Playgroud)
内部节点
???
Run Code Online (Sandbox Code Playgroud)
叶节点
SELECT N1.Id, "Leaf" FROM NodeTable N1
LEFT JOIN NodeTable N2 ON N1.Id = N2.ParentId
WHERE N2.ParentId IS NULL;
Run Code Online (Sandbox Code Playgroud)
它是否正确?有什么办法可以让我在一个查询中做到这一点?