Java 创建二叉搜索树

Java 创建二叉搜索树,java,binary-search-tree,Java,Binary Search Tree,到目前为止,我已经找到了添加到二叉搜索树中的算法,但将其转换为代码有点困难。算法如下: public void add(int v) { Create a new node n to hold value v. If tree is empty Set root to n. Else Create temporary node reference m, initialized to root. Loop looking fo

到目前为止,我已经找到了添加到二叉搜索树中的算法,但将其转换为代码有点困难。算法如下:

public void add(int v) { 
Create a new node n to hold value v. 
    If tree is empty 
        Set root to n. 
    Else
        Create temporary node reference m, initialized to root.
        Loop looking for a gap (a null left or right pointer) that is on the 
        correct side of m for v 
            If v < m.value, look at the left pointer
            If v >= m.value, look at the right pointer 
        If pointer on correct side is not null, set m to that child node and
        continue looking
            m = m.left or m = m.right 
    The search for insertion position stops when node m has a null pointer on
    the correct side.
    Insert the new node n at that position
        m.left = n or m.right = n
}

我相信大部分都是正确的,但我不知道在标记为“…”的地方需要做什么。首先,二元搜索树不应该有任何重复的值,这是您在代码中没有实现的重要要求。我最近在学习java中的数据结构时实现了二进制搜索树。以下是我编写的代码:

public class OrderedBinaryTree
{
    private int _elementsPresent = 0;
    private Node _root = null;
    private int [] _values = null;
    private class Node
    {
        Node _left = null;
        Node _right = null;
        Node _parent = null;
        int _value = 0;
        public Node(int value,Node parent)
        {
            _value = value;
            _parent = parent;
        }
    }
    public void put(int value)
    {
        boolean valueInserted = false;
        Node temp = _root;
        while(!valueInserted)
        {
            if(_root == null)
            {
                _root = new Node(value,null);
                break;
            }
            else if(value == temp._value)
            {
                System.out.println("the entered value is already present");
                return;
            }
            else if(value<=temp._value)
            {
                if(temp._left == null)
                {
                    temp._left = new Node(value,temp);
                    break;
                }
                else
                {
                    temp = temp._left;
                }
            }
            else
            {
                if(temp._right == null)
                {
                    temp._right = new Node(value,temp);
                    break;
                }
                else
                {
                    temp = temp._right;
                }
            }
        }
        _elementsPresent++;
    }
公共类OrderedBinaryTree { 私有int_元素present=0; 私有节点_root=null; 私有int[]_值=null; 私有类节点 { 节点_left=null; 节点_right=null; 节点_parent=null; int _值=0; 公共节点(int值,节点父节点) { _价值=价值; _父母=父母; } } 公开作废认沽权(整数值) { 布尔值inserted=false; 节点温度=_根; 而(!valueInserted) { 如果(_root==null) { _root=新节点(值,null); 打破 } 否则如果(值==温度值) { System.out.println(“输入的值已经存在”); 返回; }
else if(value)我认为谷歌搜索会为这个问题产生无数的结果。我有一个实现(它使用泛型):(看看BinaryTree.java和TreeNode.java)你不应该检查布尔值是否等于false;相反,你应该在布尔值上使用not(
)操作符。
public class OrderedBinaryTree
{
    private int _elementsPresent = 0;
    private Node _root = null;
    private int [] _values = null;
    private class Node
    {
        Node _left = null;
        Node _right = null;
        Node _parent = null;
        int _value = 0;
        public Node(int value,Node parent)
        {
            _value = value;
            _parent = parent;
        }
    }
    public void put(int value)
    {
        boolean valueInserted = false;
        Node temp = _root;
        while(!valueInserted)
        {
            if(_root == null)
            {
                _root = new Node(value,null);
                break;
            }
            else if(value == temp._value)
            {
                System.out.println("the entered value is already present");
                return;
            }
            else if(value<=temp._value)
            {
                if(temp._left == null)
                {
                    temp._left = new Node(value,temp);
                    break;
                }
                else
                {
                    temp = temp._left;
                }
            }
            else
            {
                if(temp._right == null)
                {
                    temp._right = new Node(value,temp);
                    break;
                }
                else
                {
                    temp = temp._right;
                }
            }
        }
        _elementsPresent++;
    }