简体   繁体   English

请解释给定的java中的二叉树代码

[英]Please explain the given binary tree code that is in java

I could not find people to explain me this java code properly so finally I am posting this question.please explain the process how that particular statement is affecting the tree.the problems are stated in the comments.I have problems in the BST class.我找不到人来正确解释这个 java 代码,所以最后我发布了这个问题。请解释该特定语句如何影响树的过程。问题在评论中说明。我在 BST 类中有问题。

 import java.util.Scanner;

 class BSTNode
 {
     BSTNode left, right;
     int data;

     public BSTNode()
     {
         left = null;
         right = null;
         data = 0;
     }

     public BSTNode(int n)
     {
         left = null;
         right = null;
         data = n;
     }

     public void setLeft(BSTNode n)
     {
         left = n;
     }

     public void setRight(BSTNode n)
     {
         right = n;
     }

     public BSTNode getLeft()
     {
         return left;
     }

     public BSTNode getRight()
     {
         return right;
     }

     public void setData(int d)
     {
         data = d;
     }

     public int getData()
     {
         return data;
     }     
 }

 class BST
 {
     private BSTNode root;

     public BST()
     {
         root = null;
     }

     public boolean isEmpty()
     {
         return root == null;
     }

Why is the insert function written like root=insert(..... . Is it returning root = actual root element each time?为什么插入函数写成root=insert(..... . 每次都返回 root = 实际根元素?

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

I understand how the inserting process is going on but what is the insert function returning?我了解插入过程是如何进行的,但是插入函数返回的是什么? I know that it returns some node but how the is the process going on during iteration?我知道它返回了一些节点,但是迭代过程中的过程是如何进行的?

     private BSTNode insert(BSTNode node, int data)
     {
         if (node == null)
             node = new BSTNode(data);
         else
         {
             if (data <= node.getData())
                 node.left = insert(node.left, data);
             else
                 node.right = insert(node.right, data);
         }

         return node;
     }

     public void delete(int k)
     {
         if (isEmpty())
             System.out.println("Tree Empty");
         else if (search(k) == false)
             System.out.println("Sorry "+ k +" is not present");
         else
         {
             root = delete(root, k);

Again, why is the delete function written like root=delete(..... ? Is it returning root =actual root element each time?同样,为什么删除函数写成root=delete(..... ?它每次都返回 root = 实际根元素吗?

             System.out.println(k+ " deleted from the tree");
         }
     }

     private BSTNode delete(BSTNode root, int k)
     {
         BSTNode p, p2, n;

         if (root.getData() == k)
         {
             BSTNode lt, rt;
             lt = root.getLeft();
             rt = root.getRight();

             if (lt == null && rt == null)
                 return null;
             else if (lt == null)
             {
                 p = rt;
                 return p;
             }
             else if (rt == null)
             {
                 p = lt;
                 return p;
             }
             else
             {
                 //case when we delete node having both children.
                 p2 = rt;
                 p = rt;

                 //getting the min of the right child subtree in p variable .
                 while (p.getLeft() != null)
                     p = p.getLeft();

                 p.setLeft(lt);

Please explain what is happening here and why is p2 ie rt being returned.请解释这里发生了什么以及为什么返回 p2 即 rt。

                 return p2;
             }
         }

         if (k < root.getData())
         {
             n = delete(root.getLeft(), k);
             root.setLeft(n);
         }
         else
         {
             n = delete(root.getRight(), k);
             root.setRight(n);             
         }

         return root;
     }

     public int countNodes()
     {
         return countNodes(root);
     }

In the delete portion of your code, what you are doing is checking to see if the node (called root) has its data value equal to the value you want to delete (k).在代码的删除部分,您要做的是检查节点(称为根)的数据值是否等于要删除的值 (k)。 If that is true, then you examine both children which you seem to grasp.如果这是真的,那么你检查你似乎抓住的两个孩子。 So then we get down to your question when you have both children of the node not null .因此,当您的节点的两个子节点都为 not null时,我们将开始讨论您的问题。 In this case you want to delete the current node (root) of the subtree you are on, but you need to choose a node (either the left or right) to promote to the position of this node.在这种情况下,您要删除您所在子树的当前节点(根),但您需要选择一个节点(左侧或右侧)以提升到该节点的位置。 So (assuming this tree is not balanced) you simply make a choice (left or right) of the subtree to promote to the know root.因此(假设这棵树不平衡)您只需选择(左或右)子树以提升到已知根。 Keep in mind here that you are only calling the current node root because it is the root of some subtree within the larger tree (this does not mean the actual root of the tree unless that is the value that was passed in as root).请记住,您只是在调用当前节点根,因为它是较大树中某个子树的根(这并不意味着树的实际根,除非它是作为根传入的值)。 Knowing that all of the values in the right child subtree are going to be greater than those in the left child subtree, you simply take the current node's left subtree then recurse as far down the left children of the right subtree as you can go until you're to the end.知道右子树中的所有值都将大于左子树中的值,您只需取当前节点的左子树,然后尽可能向下递归右子树的左子树,直到您到最后了。 You then set this node's left child to the entire left subtree.然后将此节点的左子节点设置为整个左子树。

//case when we delete node having both children.
p2 = rt;
p = rt;

//getting the min of the right child subtree in p variable .
while (p.getLeft() != null)
p = p.getLeft();

p.setLeft(lt);

Notice in the statement声明中的通知

p = rt;

You are setting P to be the root of the of the right subtree.您将 P 设置为右子树的根。 Then p is now the right child of the current root passed in.然后p现在是传入的当前根的右孩子。

while (p.getLeft() != null)
    p = p.getLeft();

p.setLeft(lt);

This is basically saying that for that right subtree, If the root node has a left child then set p to that and keep doing so until that value is null.这基本上是说对于那个右子树,如果根节点有一个左孩子,那么将p设置为那个,并继续这样做直到该值为空。 This will keep going down the left children of that right subtree.这将继续沿着该右子树的左子树向下移动。 Finally once that value is null最后一旦该值为空

p.setLeft(lt);

Will set the left child of that left most leaf in the right subtree to the entire left subtree that you started with.将右子树中最左边的叶子的左子树设置为您开始使用的整个左子树。 And return the node that you said was the right subtree of the original (now with the left subtree of the original appended to its leftmost leaf).并返回您所说的节点是原始节点的右子树(现在将原始节点的左子树附加到其最左侧的叶子上)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM