在Java中创建二进制搜索树。但是输出为空

时间:2018-12-18 05:24:48

标签: java binary-search-tree

我正在尝试使用插入和遍历方法在Java中创建基本的二进制搜索树。节点具有两个局部变量,一个字符串和一个int,String值用于对节点进行排序。

每个BST都有一个指向根节点的局部变量指针,并且通过从节点遍历来插入节点。创建根节点似乎存在问题,因为我的输出始终产生null而不是。

猫 帽子

class BST
{
    public Node root = null;

    private class Node 
    {
        private String key;
        private int value;
        private Node left;
        private Node right;

        public Node ()
        {

        }

        public Node (String key, int value)
        {
            this.key = key;
            this.value = value;
        }

        public String toString ()
        {
            return ("The key is: "+ this.key +" "+ this.value);
        }
    }

    BST ()
    {
    }


    public void put (String key, int value)
    {
        put (root, key, value);
    }

    private void put (Node x, String key, int value)
    {
        Node newNode = new Node(key, value);

        if (x == null)
        {
            x = newNode;
            System.out.println("new node added");
            System.out.println(x);
        }

        int cmp = key.compareTo(x.key);

        if (cmp < 0)
            put(x.left, key, value);
        else if (cmp > 0)
            put(x.right, key, value);
        else
            x.value = value;

    }

    public void inorder (Node x)
    {
        if (x != null) 
        {
            inorder (x.left);
            System.out.println(x.key);
            inorder (x.right); 
        }

    }

    public static void main (String [] args)
    {
        BST bst = new BST();
        bst.put(bst.root,"THE", 1);
        bst.put(bst.root,"CAT", 2);
        bst.put("HAT", 1);
        bst.inorder(bst.root);

    }
}

4 个答案:

答案 0 :(得分:1)

参数按值传递。使用该方法的返回值来更改某些内容:

public void put (String key, int value)
{
    root = put (root, key, value);
}

private Node put (Node x, String key, int value)
{
    Node newNode = new Node(key, value);

    if (x == null)
    {
        System.out.println("new node added");
        System.out.println(x);
        return newNode;
    }

    int cmp = key.compareTo(x.key);

    if (cmp < 0)
        x.left = put(x.left, key, value);
    else if (cmp > 0)
        x.right = put(x.right, key, value);
    else
        x.value = value;

    return x;
}

答案 1 :(得分:0)

答案 2 :(得分:0)

二叉搜索树是一个基于节点的数据结构,二叉搜索树的整体思想是将数据保持在排序顺序,以便我们可以更快地搜索数据。正在使用三种节点该树的关键角色(父节点,左子节点和右子节点)。左子节点的值始终小于父节点的值,与右子节点的值始终大于父节点的值。每个父节点可以链接到一个或两个子节点,但不能超过两个子节点。

请从我的技术博客中找到源代码-http://www.algonuts.info/create-a-binary-search-tree-in-java.html

package info.algonuts;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;

class BinaryTreeNode {
    int nodeValue;
    BinaryTreeNode leftChildNode;
    BinaryTreeNode rightChildNode;

    public BinaryTreeNode(int nodeValue) {
        this.nodeValue = nodeValue;
        this.leftChildNode = null;
        this.rightChildNode = null;
    }

    public void preorder() {
        System.out.print(this.nodeValue+" ");
        if(this.leftChildNode != null)   {  
            this.leftChildNode.preorder();  
        }
        if(this.rightChildNode != null) {   
            this.rightChildNode.preorder();
        }
    }

    public void inorder() {
        if(this.leftChildNode != null) {    
            this.leftChildNode.inorder();   
        }
        System.out.print(this.nodeValue+" ");
        if(this.rightChildNode != null) {   
            this.rightChildNode.inorder();  
        }
    }

    public void postorder() {
        if(this.leftChildNode != null) { 
            this.leftChildNode.postorder();
        }
        if(this.rightChildNode != null) { 
            this.rightChildNode.postorder();
        }
        System.out.print(this.nodeValue+" ");
    }
}

class  BinaryTreeCompute {
    private static BinaryTreeNode temp;
    private static BinaryTreeNode newNode;
    private static BinaryTreeNode headNode;

    public static void setNodeValue(int nodeValue) {
        newNode = new BinaryTreeNode(nodeValue);
        temp = headNode;
        if(temp != null) 
        {   mapping();  }
        else 
        {   headNode = newNode; }
    }

    private static void mapping() {
        if(newNode.nodeValue < temp.nodeValue) {    //Check value of new Node is smaller than Parent Node
            if(temp.leftChildNode == null) 
            {   temp.leftChildNode = newNode;   }   //Assign new Node to leftChildNode of Parent Node
            else
            {
                temp = temp.leftChildNode;          //Parent Node is already having leftChildNode,so temp object reference variable is now pointing leftChildNode as Parent Node
                mapping();
            }
        }
        else
        {
            if(temp.rightChildNode == null) 
            {   temp.rightChildNode = newNode;  }   //Assign new Node to rightChildNode of Parent Node
            else
            {
                temp = temp.rightChildNode;         //Parent Node is already having rightChildNode,so temp object reference variable is now pointing rightChildNode as Parent Node
                mapping();
            }
        }
    }

    public static void preorder() {
        if(headNode != null) {  
            System.out.println("Preorder Traversal:");
            headNode.preorder();    
            System.out.println("\n");
        }
    }

    public static void inorder() {
        if(headNode != null) {
            System.out.println("Inorder Traversal:");
            headNode.inorder();
            System.out.println("\n");
        }
    }

    public static void postorder() {
        if(headNode != null) {
            System.out.println("Postorder Traversal:");
            headNode.postorder();   
            System.out.println("\n");
        }
    }
}

public class BinaryTree {
    //Entry Point
    public static void main(String[] args) {
        ArrayList <Integer> intList = new ArrayList <Integer>(Arrays.asList(50,2,5,78,90,20,4,6,98));   
        Iterator<Integer> ptr  = intList.iterator();
        while(ptr.hasNext()) 
        {   BinaryTreeCompute.setNodeValue(ptr.next()); }

        BinaryTreeCompute.preorder();
        BinaryTreeCompute.inorder();
        BinaryTreeCompute.postorder();

    }
}

答案 3 :(得分:-1)

通过@Maurice添加答案,

您的代码有几个问题:

  1. 您希望通过值传递时可以通过引用传递JAVA。您应该改用Maurice提供的代码。
  2. 当您应该比较值时,您正在比较“键”。

我建议您使用以下修改后的代码:

public class BST
{
    public Node root = null;

    private class Node
    {
        private String key;
        private int value;
        private Node left;
        private Node right;

        public Node ()
        {

        }

        public Node (String key, int value)
        {
            this.key = key;
            this.value = value;
        }

        public String toString ()
        {
            return ("The key is: "+ this.key +" "+ this.value);
        }
    }

    BST ()
    {
    }


    public void put (String key, int value)
    {
        root = putInTree (root, key, value);
    }

    private Node putInTree (Node x, String key, int value)
    {
        Node newNode = new Node(key, value);

        if (x == null)
        {
            x = newNode;
            System.out.println("new node added");
            System.out.println(x);
            return newNode;
        }

        //int cmp = key.compareTo(x.key);

        if (value < x.value)
            x.left = putInTree(x.left, key, value);
        else /*if (value >= x.value)*/
            x.right = putInTree(x.right, key, value);
        /*else
            x.value = value;*/

        return x;
    }

    public void inorder (Node x)
    {
        if (x != null)
        {
            inorder (x.left);
            System.out.println(x.key);
            inorder (x.right);
        }

    }

    public static void main (String[] args)
    {
        BST bst = new BST();

        bst.put("THE", 1);
        bst.put("CAT", 2);
        bst.put("HAT", 1);
        bst.inorder(bst.root);

    }
}