【问题标题】:How to populate the BST and also print it in Inorder way如何填充 BST 并以 Inorder 方式打印
【发布时间】:2019-05-09 09:35:47
【问题描述】:

我正在尝试使用此代码填充 BST,然后以 InOrder 遍历格式打印它。但是根节点没有被填充编译而没有任何错误并且输出是:“root is empty”,那么如何更正此代码以便我的 BST 被填充到节点根中。

我尝试将节点根设为静态我认为可能无法从每个方法访问根节点但它不起作用,尝试更改节点的名称但它也不起作用。

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

class Node{
    int data; Node left; Node right;
    public Node(int data) {
        this.data = data;
        left = null;
        right = null;

    }
}
public class insert_tree {
    static Node root;   

     insert_tree() //constructor
     { 
        root = null; 
     } 
    public void addNode(int value) { // public method is called by the object and this public method calls the private method in which the root is also passed.
        root = add(root, value);
    }
    private Node add(Node node, int value) {
        if(node == null) {
            return node;
        }
        if(value < node.data) {
            node.left = add(node.left, value);
        }       
        else if(value > node.data) {
            node.right = add(node.right, value);
        }
        else {
            return node;
        }
        return node;
    }

    private void inOrder(Node node) {
//      node = root;
        if(node != null) {
            inOrder(node.left);
            System.out.print(node.data + " ");
            inOrder(node.right);
        }
        else {
            System.out.print("root is empty");
        }
        //return null;
    }
    public void inorder() {
        inOrder(root);
    }
   private void printRoot(Node root) {
      System.out.println(root.data);
   }
   public void print() {
      printRoot(root);
   }
    public static void main(String args[]) {
        insert_tree obj = new insert_tree();
        obj.addNode(20);
        obj.addNode(14);
        obj.addNode(25);
        obj.addNode(10);
        obj.addNode(16);
        obj.addNode(25);
        obj.addNode(21);
        obj.addNode(30);
        //printing the tree
        obj.inorder();



    }
}


输出应该是树的中序遍历。

【问题讨论】:

    标签: java static tree binary-search-tree


    【解决方案1】:
    public void addNode(int value) { // public method is called by the object and this public method calls the private method in which the root is also passed.
        root = add(root, value);
    }
    private Node add(Node node, int value) {
        if(node == null) {
            node = new Node(value);
        }
        else if(value == node.data) {
            node.data = value;
        }
        else if(value < node.data) {
            node.left = add(node.left, value);
        }
        else {
            node.right = add(node.right, value);
        }
        return node;
    }
    

    【讨论】:

    • 为什么需要第二个 if 语句 (value == node.data) ?如果该陈述为真,您将始终将 node.data 设置为其现有值,不是吗?
    猜你喜欢
    • 2017-08-25
    • 1970-01-01
    • 2021-12-18
    • 1970-01-01
    • 1970-01-01
    • 2011-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多