簡體   English   中英

在Java中的二叉樹中插入元素

[英]Inserting Elements in Binary Tree in Java

我寫了一段代碼,將元素插入Java的二叉樹中。 以下是執行相同操作的功能:

 public void insert(int data)
    {
        root = insert(root, data);

    }

    private Node insert(Node node, int data)
     {
         if (node == null)
             node = new Node(data);
         else
         {
             if (node.getRight() == null)
                 node.right = insert(node.right, data);
             else
                 node.left = insert(node.left, data);             
         }
         return node;
     } 

但是,當我遍歷樹時,得到的答案是錯誤的。 以下是遍歷函數(預購):

public void preorder()
     {
         preorder(root);
     }
     private void preorder(Node r)
     {
         if (r != null)
         {
             System.out.print(r.getData() +" ");
             preorder(r.getLeft());             
             preorder(r.getRight());
         }
     }

好吧,正如建議的那樣,這里是Node類的定義:

public class Node {

    public int data;
    public Node left, right;

    /*  Constructor  */
    public Node() {
        left = null;
        right = null;
        data = 0;
    }
    /*  Constructor  */

    public Node(int d, Node l, Node r) {
        data = d;
        left = l;
        right = r;
    }

    //Constructor
    public Node(int d) {
        data = d;
    }

    /*  Function to set link to next Node  */

    public void setLeft(Node l) {
        left = l;
    }
    /*  Function to set link to previous Node  */

    public void setRight(Node r) {
        right = r;
    }
    /*  Function to set data to current Node  */

    public void setData(int d) {
        data = d;
    }
    /*  Function to get link to next node  */

    public Node getLeft() {
        return left;
    }
    /*  Function to get link to previous node  */

    public Node getRight() {
        return right;
    }
    /*  Function to get data from current Node  */

    public int getData() {
        return data;
    }
}

我已經對遍歷算法進行了多次檢查,並且運行良好。 我相信問題出在插入算法上。 有什么建議么?

如果我理解正確,則您想在“圖層”中填充二進制樹。 例如,僅當深度3為“完整的二叉樹”時,才想將某些內容放入深度4。

然后問題就出在基於DFS的插入算法的整體邏輯上。 換句話說,它在一側插入越來越深的元素,而不是在兩側構建完整的二叉樹。

如果您更靠近插入算法,您會看到,一旦跳過“ right”子樹,就永遠不會返回它-即使“ left”子樹已經是完整的二叉樹。 這樣一來,樹的左側會越來越深,而右側則不會越來越大。

講編程語言。 你做:

(node.right != null) && (node.left != null) => insert (node.left)

但您不能執行此操作(開始插入node.left)。 如果node.left既有孩子又node.right沒有孩子怎么辦? 您將嘗試向左插入,即使您應該在node.right中插入也是如此。

因此,您真正需要做的是基於BFS的插入。 這意味着您將遍歷樹以“按層”插入。 Queue應該是您的新朋友:-)(而不是堆棧/遞歸):

public void insert(int data) {
     if (root == null) {
         root = new Node(data);
         return;
     }

     Queue<Node> nodesToProcess = new LinkedList<>();
     nodesToProcess.add(root);

     while (true) {
         Node actualNode = nodesToProcess.poll();

         // Left child has precedence over right one
         if (actualNode.left == null) {
             actualNode.left = new Node(data);
             return;
         }
         if (actualNode.right == null) {
             actualNode.right = new Node(data);
             return;
         }

         // I have both children set, I will process them later if needed
         nodesToProcess.add(actualNode.left);
         nodesToProcess.add(actualNode.right);
     }
}

您的方法返回給定的節點,但是您的方法必須返回插入的節點,即node.right或node.left

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM