JAVA:二叉树 [英] JAVA: binary trees

查看:89
本文介绍了JAVA:二叉树的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这里,我尝试练习制作二叉树,以便对它们进行不同的操作.

Here I am trying to practice making binary trees so that I can do different operations with them.

import java.util.*;
import java.lang.*;


public class Main {

public static void main(String[] args) {

}
}

//Building Binary Trees
class bTree {

static class Node { //remember to initilize a root

    String value;
    Node left, right;

    Node(String value, Node left, Node right) {
        this.value = value;
        this.left = left;
        this.right = right;
    }
    Node(String value) //THIS IS A SIBLING CONSTRUCTOR
    {
        this(value, null, null);
    }

    Node root = new Node("ROOT");
    Node lefty = new Node("LEFT0");
    Node righty = new Node("RIGHT0");
    root.left = lefty;
    root.right = righty;
}
Node root = null;
}

为什么会出现错误:root.left和root.right分配中应包含标识符?

Why am I getting the error: Identifier expected at the root.left and root.right assignment?

谢谢!

推荐答案

赋值语句

root.left = lefty;
root.right = righty;

不允许在课程级别上使用.您可以达到想要更改此行的效果

are not allowed on the class level. You can achieve the effect you want changing this line

Node root = new Node("ROOT");

对此

Node root = new Node("ROOT", lefty, righty);

这利用了您的三参数构造函数.

which takes advantage of your three-argument constructor.

但是,您可能需要重新考虑rootleftyrighty的位置.它们可能是在bTree类中使用的.另外,有一个约定鼓励命名类将每个单词的首字母大写,例如BinaryTree.

However, you may want to reconsider the placement of root, lefty and righty. They are probably intended in the bTree class. Also, there is a convention that encourages naming class capitalizing the first letter of each word, e.g. BinaryTree.

这篇关于JAVA:二叉树的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆