打印所有节点,包括java节点
我想创建一个二进制搜索树工具,它在其中输出所有节点,包括null 在我当前的代码中,它只在输入数组中创建节点,包括左节点和子节点,然后打印 是否可以打印所有节点(包括空节点),如下所示? 输入={23,5,2,89,56,43} 输出={23,5,89,2,null,56,null,null,null,null,null,null,null,null,null,43,null}打印所有节点,包括java节点,java,binary-search-tree,Java,Binary Search Tree,我想创建一个二进制搜索树工具,它在其中输出所有节点,包括null 在我当前的代码中,它只在输入数组中创建节点,包括左节点和子节点,然后打印 是否可以打印所有节点(包括空节点),如下所示? 输入={23,5,2,89,56,43} 输出={23,5,89,2,null,56,null,null,null,null,null,null,null,null,null,43,null} public class Node { int value; Node right,left;
public class Node {
int value;
Node right,left;
Node(){
value = 0;
left = null;
right = null;
}
Node(int i) {
value = i;
left = null;
right = null;
}
public void setLeft(Node newValue){
this.left = newValue;
}
public void setRight(Node newValue){
this.right = newValue;
}
public int getValue(){
return value;
}
public String getValueStr(){
return Integer.toString(value);
}
public void printAll(){
System.out.println("Value: "+ this.value
+"\nLeft: "+ this.left
+"\nRight: "+ this.right);
}
public void addChildToArr(ArrayList<String> arr){
arr.add(right.getValueStr());
arr.add(this.left.getValueStr());
}
public String getChildRightStr(){
if(right == null)
return "null";
else
return this.right.getValueStr();
}
public String getChildLeftStr(){
if(left == null)
return "null";
else
return this.left.getValueStr();
}
}
public class BST {
private static Node root;
ArrayList<Node> nodes = new ArrayList<>();
public BST(){
root = null;
}
public void insert(int data)
{
root = insert(root, data);
}
/* Function to insert data recursively */
private Node insert(Node node, int data)
{
if (node == null)
node = new Node(data);
else
{
if (data <= node.getValue()){
node.left = insert(node.left, data);
//nodes.add(node.left);
}
else{
node.right = insert(node.right, data);
//nodes.add(node.left);
}
}
if(!(nodes.contains(node)))
nodes.add(node);
return node;
}
public void printNodes(){
for(int i = 0; i < nodes.size();i++){
System.out.print(nodes.get(i).getChildLeftStr()+" ,"+nodes.get(i).getChildRightStr()+", ");
}
System.out.println("");
}
public void printNodeObj(){
for(int i = 0; i < nodes.size();i++){
System.out.println(nodes.get(i));
}
}
public int countNodes()
{
return countNodes(root);
}
/* Function to count number of nodes recursively */
private int countNodes(Node r)
{
if (r == null)
return 0;
else
{
int l = 1;
l += countNodes(r.getLeft());
l += countNodes(r.getRight());
return l;
}
}
public static void main(String[] args) {
BST bst = new BST();
int[] arr = {23,5,2,89,56,43,38,10,65,72};
System.out.print("["+arr[0]+", ");
for(int i = 0; i< arr.length;i++)
bst.insert(arr[i]);
bst.printNodes();
}
}
公共类节点{
int值;
右、左淋巴结;
节点(){
数值=0;
左=空;
右=空;
}
节点(int i){
值=i;
左=空;
右=空;
}
公共void setLeft(节点newValue){
this.left=newValue;
}
公共void setRight(节点newValue){
this.right=newValue;
}
public int getValue(){
返回值;
}
公共字符串getValueStr(){
返回整数.toString(值);
}
public void printAll(){
System.out.println(“值:”+this.Value
+“\n左:“+this.left”
+“\n右侧:“+此。右侧);
}
public void addChildToArr(ArrayList arr){
arr.add(right.getValueStr());
arr.add(this.left.getValueStr());
}
公共字符串getChildRightStr(){
if(right==null)
返回“null”;
其他的
返回这个.right.getValueStr();
}
公共字符串getChildLeftStr(){
if(left==null)
返回“null”;
其他的
返回这个.left.getValueStr();
}
}
公共类BST{
私有静态节点根;
ArrayList节点=新的ArrayList();
公共BST(){
root=null;
}
公共空白插入(整型数据)
{
根=插入(根,数据);
}
/*函数以递归方式插入数据*/
专用节点插入(节点,int数据)
{
if(node==null)
节点=新节点(数据);
其他的
{
如果(数据首先使用这些值进行BST
23,5,2,89,56,43
你不会得到这样的结果
23,5,89,2,null,56,null,null,null,null,null,null,43,null
因为结构如下所示
23
/ \
5 89
/ \ / \
2 n 56 n
/ \ / \
n n 43 n
/ \
n n
注意:n表示空[空节点]
相反,如果执行级别顺序遍历(如注释部分所述),将得到以下结果:
23,5,89,2,null,56,null,null,null,43,null,null,null,null
如果清除该选项,则可以执行以下操作来打印节点(如果该节点有值,甚至没有值)
一定要将根目录放入队列中,并对其进行处理
轮询队列中插入的第一个项目
如果轮询的项已离开子项,则处理它并将此项置于队列中,否则打印“null”
如果轮询的项具有正确的子项,则对其进行处理并将此项放入队列,否则打印“null”
继续执行步骤2,直到队列变为空
代码:
private static void print(Node root) {
Queue<Node> queue = new LinkedList<Node>();
queue.add(root);
/* step 1 */
System.out.print(root.val+" ");
while(!queue.isEmpty()) {
/* step 2 */
BST temp = queue.poll();
/* step 3 */
if(temp.left != null) {
System.out.print(temp.left.val+" ");
queue.add(temp.left);
}else {
System.out.print(temp.left+" ");
}
/* step 4 */
if(temp.right != null) {
System.out.print(temp.right.val+" ");
queue.add(temp.right);
}else {
System.out.print(temp.right+" ");
}
}
}
私有静态无效打印(节点根){
Queue Queue=new LinkedList();
添加(根);
/*第一步*/
System.out.print(root.val+“”);
而(!queue.isEmpty()){
/*步骤2*/
BST temp=queue.poll();
/*步骤3*/
如果(temp.left!=null){
系统输出打印(温度左值+“”);
队列添加(临时左侧);
}否则{
系统输出打印(左侧温度+“”);
}
/*步骤4*/
如果(临时正确!=null){
系统输出打印(温度右值+“”);
队列添加(临时右侧);
}否则{
系统输出打印(右侧温度+“”);
}
}
}
注意:如果你没有得到任何东西,请发表评论。首先,你可以使用这些值进行BST
23,5,2,89,56,43
你不会得到这样的结果
23,5,89,2,null,56,null,null,null,null,null,null,43,null
因为结构如下所示
23
/ \
5 89
/ \ / \
2 n 56 n
/ \ / \
n n 43 n
/ \
n n
注意:n表示空[空节点]
相反,如果执行级别顺序遍历(如注释部分所述),将得到以下结果:
23,5,89,2,null,56,null,null,null,43,null,null,null,null
如果清除该选项,则可以执行以下操作来打印节点(如果该节点有值,甚至没有值)
一定要将根目录放入队列中,并对其进行处理
轮询队列中插入的第一个项目
如果轮询的项已离开子项,则处理它并将此项置于队列中,否则打印“null”
如果轮询的项具有正确的子项,则对其进行处理并将此项放入队列,否则打印“null”
继续执行步骤2,直到队列变为空
代码:
private static void print(Node root) {
Queue<Node> queue = new LinkedList<Node>();
queue.add(root);
/* step 1 */
System.out.print(root.val+" ");
while(!queue.isEmpty()) {
/* step 2 */
BST temp = queue.poll();
/* step 3 */
if(temp.left != null) {
System.out.print(temp.left.val+" ");
queue.add(temp.left);
}else {
System.out.print(temp.left+" ");
}
/* step 4 */
if(temp.right != null) {
System.out.print(temp.right.val+" ");
queue.add(temp.right);
}else {
System.out.print(temp.right+" ");
}
}
}
私有静态无效打印(节点根){
Queue Queue=new LinkedList();
添加(根);
/*第一步*/
System.out.print(root.val+“”);
而(!queue.isEmpty()){
/*步骤2*/
BST temp=queue.poll();
/*步骤3*/
如果(temp.left!=null){
系统输出打印(温度左值+“”);
队列添加(临时左侧);
}否则{
系统输出打印(左侧温度+“”);
}
/*步骤4*/
如果(临时正确!=null){
系统输出打印(温度右值+“”);
队列添加(临时右侧);
}否则{
系统输出打印(右侧温度+“”);
}
}
}
注意:如果你没有得到一些东西,请发表评论。在getChildRightStr()
中,你需要递归调用getChildRightStr()
和getChildLeftStr()
。在getChildLeftStr()中也是如此
。您能详细说明一下吗?对不起,我刚开始递归。很抱歉,您可能会问如何打印二叉树的级别顺序遍历;是这样吗?是的,但包括空节点。因此,您是在一般情况下寻求遍历方面的帮助,还是在处理null
children?在getChildRightStr()中