java中带有getter和setter的二叉树

我正在尝试通过以下示例 1 创建二叉树,其中树是在没有 getter 和 setter 的情况下创建的。我想用geeters和setter创建它,但我坚持递归。如何使用/在 setter 中调用递归函数?这是代码.. ps Tree 类pastebin


public class TreeF {


Tree root;


public void insert(int value) {


    if (root==null) {


    root = new Tree(value);

        return;

    }


     Tree current = root;


    if (value < current.getData() ) {


        if (current.getLeft()==null) {

            current.setLeft(new Tree (value));

        }else {

            // call insert method inside current.left object [currrent.left(insert(value))]

            current=current.getLeft();

            insert (value);

        }


    }

    else {

        if (current.getRight()==null) {

            current.setRight(new Tree (value));

        }else {

            current=current.getRight();

            insert (value);


        }

     }  

  }


 }


婷婷同学_
浏览 129回答 2
2回答

慕斯709654

更改insert(value)为current.insert(value)要实现递归,您需要更改一个(或多个)参数,以便在一些递归调用后进入停止条件。在您的代码中,您调用了insert属于同一对象的方法。而不是它的左/右子树。换句话说,递归永远不会结束,因为您没有访问子子树。public class Tree {private int data;private Tree left;private Tree right;public Tree (int data) {&nbsp; &nbsp; this.data=data;}public int getData() {&nbsp; &nbsp; return data;}public void setData(int data) {&nbsp; &nbsp; this.data = data;}public Tree getLeft() {&nbsp; &nbsp; return left;}public void setLeft(Tree left) {&nbsp; &nbsp; this.left = left;}public Tree getRight() {&nbsp; &nbsp; return right;}public void setRight(Tree right) {&nbsp; &nbsp; this.right = right;}public void insert(int value) {&nbsp; &nbsp; if (value <&nbsp; getData()) {&nbsp; &nbsp; &nbsp; &nbsp; if (getLeft() == null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; setLeft(new Tree(value));&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; getLeft().insert(value);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; if (getRight() == null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; setRight(new Tree(value));&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; getLeft().insert(value);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}}

qq_笑_17

您不需要current字段或root. 这是您的insert方法,大大简化了演示。public class Tree {&nbsp; &nbsp; final int data;&nbsp; &nbsp; Tree left;&nbsp; &nbsp; Tree right;&nbsp; &nbsp; public Tree(int value) {&nbsp; &nbsp; &nbsp; &nbsp; data = value;&nbsp; &nbsp; }&nbsp; &nbsp; public void insert(int value) {&nbsp; &nbsp; &nbsp; &nbsp; if (value < data) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (left == null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; left = new Tree(value);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; left.insert(value);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (right == null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; right = new Tree(value);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; right.insert(value);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java