Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Search 根在我的二叉树上总是空的 import java.io.*; 三烯类{ int数据; treeNode left=null; treeNode right=null; treeNode父节点=null; 树节点(int值) { 数据=价值; } } 树状纲{ treenoderoot=null; 无效插入(treeNode节点) { if(root==null)//root的值始终为null { 根=节点; 返回; } treeNode cur=null; treeNode-father=null; while(cur!=null) { 父亲=cur; if(当前数据>节点数据) cur=cur.left; 其他的 cur=cur.right; } if(父数据>节点数据) father.left=节点; 其他的 father.right=节点; node.parent=父节点; } void walkInorder(树节点n) { 如果(n==null) 返回; 行走顺序(n.左); System.out.print(n.data+“”); walkInorder(n.right); } } 类二进制搜索树{ 公共静态void main(字符串[]args){ treeNodes obj1=新的treeNodes(); System.out.println(“=========================数组元素=================”); INTA[]={3,5,7,9,8,6}; 对于(int i=0;i_Search_Tree - Fatal编程技术网

Search 根在我的二叉树上总是空的 import java.io.*; 三烯类{ int数据; treeNode left=null; treeNode right=null; treeNode父节点=null; 树节点(int值) { 数据=价值; } } 树状纲{ treenoderoot=null; 无效插入(treeNode节点) { if(root==null)//root的值始终为null { 根=节点; 返回; } treeNode cur=null; treeNode-father=null; while(cur!=null) { 父亲=cur; if(当前数据>节点数据) cur=cur.left; 其他的 cur=cur.right; } if(父数据>节点数据) father.left=节点; 其他的 father.right=节点; node.parent=父节点; } void walkInorder(树节点n) { 如果(n==null) 返回; 行走顺序(n.左); System.out.print(n.data+“”); walkInorder(n.right); } } 类二进制搜索树{ 公共静态void main(字符串[]args){ treeNodes obj1=新的treeNodes(); System.out.println(“=========================数组元素=================”); INTA[]={3,5,7,9,8,6}; 对于(int i=0;i

Search 根在我的二叉树上总是空的 import java.io.*; 三烯类{ int数据; treeNode left=null; treeNode right=null; treeNode父节点=null; 树节点(int值) { 数据=价值; } } 树状纲{ treenoderoot=null; 无效插入(treeNode节点) { if(root==null)//root的值始终为null { 根=节点; 返回; } treeNode cur=null; treeNode-father=null; while(cur!=null) { 父亲=cur; if(当前数据>节点数据) cur=cur.left; 其他的 cur=cur.right; } if(父数据>节点数据) father.left=节点; 其他的 father.right=节点; node.parent=父节点; } void walkInorder(树节点n) { 如果(n==null) 返回; 行走顺序(n.左); System.out.print(n.data+“”); walkInorder(n.right); } } 类二进制搜索树{ 公共静态void main(字符串[]args){ treeNodes obj1=新的treeNodes(); System.out.println(“=========================数组元素=================”); INTA[]={3,5,7,9,8,6}; 对于(int i=0;i,search,tree,Search,Tree,我尝试创建二叉树,当我将第一个值发送到insert函数时,它应该更改root的值 在我的情况下,根永远不会改变。有人能告诉我为什么以及如何解决它吗 很抱歉我的英语不好在for循环中,对于数组中的每个项目,您正在创建一个新的treeNodes实例。无论何时创建新实例,root都设置为null 尝试对for循环进行以下更改: import java.io.*; class treeNode { int data; treeNode left = null; treeNod

我尝试创建二叉树,当我将第一个值发送到insert函数时,它应该更改root的值

在我的情况下,根永远不会改变。有人能告诉我为什么以及如何解决它吗


很抱歉我的英语不好

for
循环中,对于数组中的每个项目,您正在创建一个新的
treeNodes
实例。无论何时创建新实例,
root
都设置为
null

尝试对
for
循环进行以下更改:

import java.io.*;

class treeNode {

    int data;
    treeNode left = null;
    treeNode right = null;
    treeNode parent = null;

    treeNode (int value)
    {
        data = value;
    }
}



class treeNodes {

    treeNode root = null;         

    void insert (treeNode node)
    {
        if (root == null) // the value of root is always null
        {           
            root = node;            
            return;
        }

        treeNode cur = null;
        treeNode father = null;

        while (cur != null)
        {
            father = cur;
            if (cur.data > node.data)
                cur = cur.left;
            else
                cur = cur.right;
        }

        if (father.data > node.data)
            father.left = node;
        else
            father.right = node;

        node.parent = father;
    }

    void walkInorder (treeNode n)
    {
        if (n == null)
            return;

        walkInorder (n.left);
        System.out.print (n.data + " ");
        walkInorder (n.right);
    }
}

class binarySearchTree {

    public static void main (String [] args) {   

        treeNodes obj1 = new treeNodes ( );     

        System.out.println ("============ Array Elements ============"); 

        int A[] = {3, 5, 7, 9, 8, 6};

        for (int i = 0; i < 6; i++)
        {
            treeNode node = new treeNode (A[i]);
            treeNodes obj = new treeNodes ( );
            obj.insert(node);                     
        }      

        System.out.println ("\n============ Inorder ============");
        System.out.println (obj1.root.data);
        obj1.walkInorder (obj1.root);        

    }
}
treeNodes obj=新的treeNodes();
对于(int i=0;i<6;i++)
{
treeNode节点=新的treeNode(A[i]);
对象插入(节点);
}

这样,循环的每个步骤都将使用相同的
treeNodes
实例,并且根节点将正确设置。代码还有其他问题,但学习的一部分是实验。因此,请继续,修复上面的问题,并让我们知道它是如何进行的。

for
循环中,对于数组中的每个项目,您正在创建一个新的
treeNodes
实例。无论何时创建新实例,
root
都设置为
null

尝试对
for
循环进行以下更改:

import java.io.*;

class treeNode {

    int data;
    treeNode left = null;
    treeNode right = null;
    treeNode parent = null;

    treeNode (int value)
    {
        data = value;
    }
}



class treeNodes {

    treeNode root = null;         

    void insert (treeNode node)
    {
        if (root == null) // the value of root is always null
        {           
            root = node;            
            return;
        }

        treeNode cur = null;
        treeNode father = null;

        while (cur != null)
        {
            father = cur;
            if (cur.data > node.data)
                cur = cur.left;
            else
                cur = cur.right;
        }

        if (father.data > node.data)
            father.left = node;
        else
            father.right = node;

        node.parent = father;
    }

    void walkInorder (treeNode n)
    {
        if (n == null)
            return;

        walkInorder (n.left);
        System.out.print (n.data + " ");
        walkInorder (n.right);
    }
}

class binarySearchTree {

    public static void main (String [] args) {   

        treeNodes obj1 = new treeNodes ( );     

        System.out.println ("============ Array Elements ============"); 

        int A[] = {3, 5, 7, 9, 8, 6};

        for (int i = 0; i < 6; i++)
        {
            treeNode node = new treeNode (A[i]);
            treeNodes obj = new treeNodes ( );
            obj.insert(node);                     
        }      

        System.out.println ("\n============ Inorder ============");
        System.out.println (obj1.root.data);
        obj1.walkInorder (obj1.root);        

    }
}
treeNodes obj=新的treeNodes();
对于(int i=0;i<6;i++)
{
treeNode节点=新的treeNode(A[i]);
对象插入(节点);
}

这样,循环的每个步骤都将使用相同的
treeNodes
实例,并且根节点将正确设置。代码还有其他问题,但学习的一部分是实验。所以,请继续,解决上面的问题,让我们知道它是如何进行的。

我想上面的答案并不完全正确。是的,每次循环转到数组中的下一个元素时,您都将创建一个新的treeNodes实例,但这意味着您仅使用一个元素创建新树,因此根不会为null。 另一方面,您试图打印的根实际上是空的,但这是因为您没有在您试图打印的树中插入任何元素。 您试图按树的顺序行走,但没有在树中插入任何内容。你应该试着按树的顺序走


上面的答案是正确的,您需要进行修复,因此您将只将数组元素插入到一个树中,然后修复我解释的这个问题。

我猜上面的答案不完全正确。是的,每次循环转到数组中的下一个元素时,您都将创建一个新的treeNodes实例,但这意味着您仅使用一个元素创建新树,因此根不会为null。 另一方面,您试图打印的根实际上是空的,但这是因为您没有在您试图打印的树中插入任何元素。 您试图按树的顺序行走,但没有在树中插入任何内容。你应该试着按树的顺序走


上面的答案是正确的,您需要进行修复,因此您将只将数组元素插入到一棵树中,然后修复我解释的这个问题。

事实上,我并没有说我们正在创建没有元素的树。我说过,只要创建一个新的
treeNodes
实例,
root
就会设置为
null
。这是正确的。“创建只有一个元素的树”的代码在
main
方法中,而不是在
treeNodes
构造函数中。实际上,我并没有说我们创建的树没有元素。我说过,只要创建一个新的
treeNodes
实例,
root
就会设置为
null
。这是正确的。“创建只有一个元素的树”的代码在
main
方法中,而不是在
treeNodes
构造函数中。