我看了一些BST代码,我可以看到每个节点都是一个结构.这有必要吗?
我有一小段代码来生成任意二叉搜索树的深度优先搜索.这是我的代码:
public void printByDepth()
{
Queue<BinaryNode<T>> queue = new LinkedList<BinaryNode<T>>();
BinaryNode<T> current = this;
queue.add(current);
while(!queue.isEmpty()){
current = queue.remove();
System.out.println(current.element);
if(current.left != null)
queue.add(current.left);
if(current.right != null) // had an extra semicolon here, fixed
queue.add(current.right);
}
}
Run Code Online (Sandbox Code Playgroud)
这是一种非常标准的队列方法,但由于某种原因,第8行(println(current.element))产生了一个NPE.我正在使用的树应该产生以下DF输出:F B G A D I C E H.我已经在纸上完成了这一点,在遍历整个树之前我永远不会得到current = null或queue.isEmpty()= true(至少在这种情况下),所以我不确定为什么会发生这种情况.没有节点具有空内容.
另外,有趣的是,如果我将while条件更改为while(current != null)我没有获得NPE但输出为:F B G A D I,则缺少最后一级的元素.
我确定有一些简单的我想念......有什么提示吗?
编辑:失控分号=(谢谢,罗杰.
public void HeightIterative()
{
int counter = 0;
int counter2 = 0;
TreeNode current=root;
if(current != null)
{
while(current.LeftNode!=null)
{
counter++;
current = current.LeftNode;
}
while(current.RightNode!=null)
{
counter2++;
current = current.RightNode;
}
}
int res = 1+Math.Max(counter, counter2);
Console.WriteLine("The Height Of Tree Is: "+res);
}
Run Code Online (Sandbox Code Playgroud)
我写了迭代方法,来计算树的高度。但在某些情况下,它无法正常工作。如案例: 10 1 2 3 4 5 18 17 16 15 14 13 有什么问题。根据此序列,树的高度为 6,而我的代码显示为 5。
我需要编写一个函数,我需要在其中返回树的叶子列表。
所以对于这棵树:
1
2 3
4 5 6
Run Code Online (Sandbox Code Playgroud)
这应该打印 [4, 5, 6]
这是我到目前为止所想出的。我似乎无法找到如何返回该功能。它只打印 [4]
def fringe(root):
if root.left:
return fringe(root.left)
elif root.right:
return fringe(root.right)
else:
return [root.key]
Run Code Online (Sandbox Code Playgroud)
任何输入?
我有一个预订遍历函数,如下所示:
void listInPreOrder(node* hd){
if(hd != NULL) {
printf("%d, ", hd->value);
listInPreOrder(hd->left);
listInPreOrder(hd->right);
}
}
Run Code Online (Sandbox Code Playgroud)
这实际上是有效的,但我认为将它发布到订单就像这样简单
void listInPostOrder(node* hd){
if(hd != NULL) {
listInPreOrder(hd->left);
listInPreOrder(hd->right);
printf("%d, ", hd->value);
}
}
Run Code Online (Sandbox Code Playgroud)
但遗憾的是,它并没有那么好用.我想知道如何解决这个问题,也许我正在做一些简单的错误.或许这是完全错误的.
我目前是一名学生,他的作业涉及将二叉树方法调整为通用树方法。我唯一的问题是,我对以下通用树的后序遍历是否正确?如果是这样,那么我知道我的算法正在工作,我只是无法正确掌握后序遍历的窍门,我觉得并认为该网站可以提供帮助。
B
--------------------|------------------
| | |
A ------D----- ---I---
| | | | |
C E G H L
|
F
Run Code Online (Sandbox Code Playgroud)
我的结果是:ACEFGDHLIB
我对编程很新,我想要使用一些二进制搜索树.我想创建一个以递归方式计算树中节点数的函数,但是,当我运行我的函数时它似乎不起作用并且它一直返回'none',好像我的树中没有任何东西.谁能帮我找到问题呢?
这是我的TreeNode类:
class TreeNode(object):
def __init__(self, data = None, left=None, right=None):
self.item = data
self.left = left
self.right = right
def __str__(self):
return str(self.item)
Run Code Online (Sandbox Code Playgroud)
这是我的主要功能,我将大部分功能调整下来,以便我们可以解决有关节点计数的问题.
from TreeNode import TreeNode
class BST(object):
#------------------------------------------------------------
def __init__(self):
"""create empty binary search tree
post: empty tree created"""
self.root = None
def treeSize(self, root, size = 0):
if root is None:
return -1
if root is not None:
size += 1
if root.left is not None:
self.treeSize(root.left, size)
if root.right is not None:
self.treeSize(root.right, size) …Run Code Online (Sandbox Code Playgroud) 我有一个因变量来通过决策树进行分类.它由三类频率组成:738(19%),426(15%)和1800(66%).正如你想象的那样,预测的类别总是第三个,但树的目的是描述性的,所以它实际上并不重要.问题是,当通过ctree()功能(包partykit)绘制树时,终端节点显示直方图,其显示三个类的出现概率.我需要修改这个输出:我想获得终端节点中每个类相对于类的绝对频率的出现比例.例如,class1中738个参与者中的哪一个属于某个终端节点?每个终端节点将为组成因变量的所有三个类显示该值.
Bellow一个树的图,默认情况下报告终端节点中每个类的普遍性.
我是OCaml的新手和ML家族的语言.我有这个二叉树,我想打印每个叶子.这是我的代码,但显然它不起作用.你能告诉我它有什么问题吗?谢谢.
open Core.Std
open Printf
type bintree = Leaf of int
| Node of bintree * int * bintree
let rec print_tree_infix tree = function
Leaf n ->
Printf.printf "%d" n
| Node (left, n, right) ->
Printf.printf "%d" n;
print_tree_infix left;
print_tree_infix right
let mytree = Node(Node(Leaf 6, 3, Leaf 9), 8, Node(Leaf 7, 9, Leaf 2))
print_tree_infix mytree
Run Code Online (Sandbox Code Playgroud)
这是我收到的错误:
$ ocaml setup.ml -build
Finished, 0 targets (0 cached) in 00:00:00.
+ ~/.opam/system/bin/ocamlfind ocamldep -package core -package threads -modules …Run Code Online (Sandbox Code Playgroud) 我尝试使用递归创建二叉树,但是当我键入时ABD***CE**FG***,代码未产生任何结果。我按了空格键,但是代码仍然没有成功。代码是错误的还是我的输入错误?
#include <stdio.h>
#include <stdlib.h>
typedef struct tree
{
struct tree *left;
struct tree *right;
char val;
}treeNode;
void createTree(treeNode **node)
{
char value=0;
value=getchar();
if(value=='*')
*node=NULL;
else
{
*node = (treeNode*)malloc(sizeof(treeNode));
if(!*node) exit(-1);
(*node)->val=value;
createTree(&(*node)->left);
createTree(&(*node)->right);
}
}
void preOrder(treeNode *node)
{
if (node==NULL) return;
putchar(node->val);
preOrder(node->left);
preOrder(node->right);
}
int main() {
// insert code here...
treeNode **node;
createTree(node);
preOrder(*node);
return 0;
}
Run Code Online (Sandbox Code Playgroud)