Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/352.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
Java 基于线性阵列的二叉树构造方法_Java_Arrays_Binary Tree - Fatal编程技术网

Java 基于线性阵列的二叉树构造方法

Java 基于线性阵列的二叉树构造方法,java,arrays,binary-tree,Java,Arrays,Binary Tree,注意:这是家庭作业,所以请不要马上给我答案,但指针会很有帮助 任务概述: (测试每种方法以确保其正常工作。) 生成按正确顺序添加100个元素的二进制搜索-将数字1到100按正确顺序添加到树中添加1、2、3等 生成一个包含100个元素的二进制搜索,按相反顺序添加-添加数字100、99、98等等 生成包含100个随机生成元素的二进制搜索 我在问题中提到线性数组的原因是因为根必须是数组中的第一个元素,然后将其余元素相加 我没有所有其他的方法来测试这棵树是否制作正确,我马上就要去工作了。然而,我在下面发

注意:这是家庭作业,所以请不要马上给我答案,但指针会很有帮助

任务概述: (测试每种方法以确保其正常工作。)

  • 生成按正确顺序添加100个元素的二进制搜索-将数字1到100按正确顺序添加到树中添加1、2、3等

  • 生成一个包含100个元素的二进制搜索,按相反顺序添加-添加数字100、99、98等等

  • 生成包含100个随机生成元素的二进制搜索

  • 我在问题中提到线性数组的原因是因为根必须是数组中的第一个元素,然后将其余元素相加

    我没有所有其他的方法来测试这棵树是否制作正确,我马上就要去工作了。然而,我在下面发布了我认为会起作用的算法的开头,只是不确定

    public BST(int[] numbers)
    {
        node root;
        node currentParent; 
        node previousParent = null; 
        int i = 1; // skip the root, that will be handled manualy 
    
        root = new node(numbers[0]); // first element is the root
        currentParent = root;
    
        // handle right hand side of binary tree first
        while (i > root.getValue())
        {
            while (i > currentParent.getValue())
            {
                node newNode = new node (numbers[i]);
                currentParent.setRightChild(newNode);
                previousParent = currentParent;
                currentParent = newNode;
                i++;
            } // end inner while
    
            if (previousParent.leftChild == null)
            {
                node newNode = new node(numbers[i]);
                previousParent.leftChild =  newNode;
                currentParent = newNode;
                i++;
            } // end if branch
            else
            {
                System.out.println("Hmm, something seems to be wrong!");
            } // end else
        } // end right side binary tree while
    } // end integer array constructor
    
    这几乎就是整个算法,我必须再做一次,但要反转<和>符号(如果这是正确的)。我用这个算法走对了吗

    根据请求添加节点类

    public class node 
    {
    node leftChild; // left pointer
    node rightChild; // right pointer
    int value; // will be the int value inside the node
    boolean isLeaf; // boolean for determing if a node is a left node or not
    
    /*
        Null constructor for the class.  Sets the pointers to null 
    */
    public node ()
    {
        leftChild = null;
        rightChild = null; 
    } // end null constructor
    
    
    /*
        Intialzing constructor that will take a single integer as input
    */
    public node (int number)
    {
        leftChild = null;
        rightChild = null;
        value = number; 
    } // end intialzing constructor with int input
    
    
    /*
        The setValue method will take the int value inputted from the array into 
        a new node 
    */
    public void setValue(int number)
    {
        value = number;
    } // end setValue()
    
    
    /*
        The getValue method will return to the user the value that is stored 
        inside a specific node.
    */
    public int getValue()
    {
        return value; 
    } // end getValue()
    
    
    /*
        The setLeftChild method will set the pointers to the nodes left child 
        which will be a value that is equal or lower to the paret node
    */
    public void setLeftChild (node leftChild)
    {
        this.leftChild = leftChild;
    } // end setLeftChild()
    
    
    /*
        The setRightChild method will be the same as setLeftChild above but will
        handle setting the nodes right child.  The right child will be a value 
        that is greater than the parent node
    */
    public void setRightChild(node rightChild)
    {
        this.rightChild = rightChild; 
    } // end setRightChild
    
    
    /*
        the getLeftChild method will return to the user/calling method what the
        left child of a given node is. 
    */
    public node getLeftChild (node currentNode)
    {
        return leftChild;
    } // end getLeftChild()
    
    
    /*
        the getRightChild method is exactly the same as the getLeftChil method 
        above, except it will return the right child instead
    */
    public node getRightChild(node currentNode)
    {
        return rightChild; 
    } // end getRightChild()
    
    /*
        The setLeaf method will be in charge of manipulating a boolean value and
        setting it to true if the node is a left or not
    */
    public void setLeaf(boolean leaf)
    {
        if (leaf == true)
        {
            isLeaf = true;
        } // end if
        else
        {
            isLeaf = false;
        } // end else
    } // end setLeaf
    
    /*
        The getLeaft method will simply return a boolean value that indicates if
        the given node is a left node or not
    */
    public boolean getLeaf()
    {
        return isLeaf;
    } // end getLeaf()
    } // end node class
    

    不,对不起,这是错的

  • 您正在将索引与数据值进行比较。你应该只把同样的东西和同样的东西进行比较。想象一下,任务是将数字1001到1100放入树中,而不是1到100。这将更容易区分索引和数据值(这里的区别非常微妙,因为索引是0到99,数据值是1到100)
  • 你不能假设你“先处理右手部分”。您必须根据给定项的值决定它是向左还是向右,以及它是小于还是大于当前节点的值
  • 如果数组无序,则尝试先添加所有高值,然后再添加所有低值将不起作用

  • 最后一个提示:类应命名为
    Node
    ,而不是
    Node
    。类和所有类型名称应以大写字母开头,变量、字段和方法名称应以小写字母开头,常量应以大写字母开头。

    您能为
    节点粘贴代码吗?此外,作业听起来像是你需要做一些类似于
    tree.add(1)的事情;树。添加(2)
    我添加了节点类,而不是让构造函数从
    int[]
    @Captain Man生成树。虽然我确实需要一个insert方法,但我确实需要一个以int数组为参数的构造函数parameter@Jeffery哦,我明白了。这棵树需要“平衡”还是“自我平衡”?这是值得考虑的问题。由于必须有一个insert方法,我建议您只需为数组中的每个
    int
    调用insert方法,这样您就不会重复代码。类似于'for(inti=0;inumbers[I]
    更正确。由于上面的#3,同一方法中的两个while循环肯定不起作用。但如果你不是说你要写三个不同的方法,我会修改我的答案。@realponsignist我明白了,谢谢你的输入。我将不得不回到绘图板上,但我想我有一个好主意,如何去做这件事