Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/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
Java 求BST中的第k个最小数_Java_Binary Search Tree - Fatal编程技术网

Java 求BST中的第k个最小数

Java 求BST中的第k个最小数,java,binary-search-tree,Java,Binary Search Tree,这是一个递归解决方案:- private static int INDEX = 0; public static void findKthSmallest(Node node) { if (node == null) { System.out.println("Tree is empty!!"); return; } if (node.left != null) { findKthSmallesRecursive(node.left); }

这是一个递归解决方案:-

 private static int INDEX = 0;

 public static void findKthSmallest(Node node) { 

  if (node == null) {
   System.out.println("Tree is empty!!");
   return;
  } 


  if (node.left != null) {
      findKthSmallesRecursive(node.left);
  }

  ++INDEX;

  if (K == INDEX) {
   System.out.println("Kth Smallest Node Value: " + node.data);
   return;
  }  

  if (node.right != null) {
      findKthSmallesRecursive(node.right);
  }

 }
如果树是:-

   10
   /
  5
 /
2
假设k是2 第二个最小的元素为=5


第一次递归调用:
findkthmesminate(10)
哪个调用
findkthmesminate(5)
哪个调用
findkthmesminate(2)
。现在我对递归调用是如何处理的感到困惑。并给出所需的输出。代码运行良好,并给出了预期的结果

看起来您指的是BST中的K=级别,并将它们传递给方法。 当满足(2==INDEX)时,递归将停止,返回语句将打印所需的结果。
它回溯到它的调用者,但是由于没有在返回值上定义操作,它只是回溯,直到它达到触发递归的原始级别为止。代码以更有序的方式遍历树;i、 它先到左子级,然后到根级,然后到右子级

同样在访问特定节点的左子树之后,它增加索引并检查它是否等于k;如果是,则为目标节点

由于某个特定节点的左子树中的所有节点总是小于该节点;并且整个左子树在该特定父节点之前被处理;以及在处理节点时增加索引变量;因此,直到第K个最小节点,索引变量将保持正确的结果,但一旦得到第K个最小节点,它将跳过其右子树中存在的所有元素,并将控件传递给父节点,父节点将继续处理其右子树,而不知道其左子节点跳过的子树中存在的节点数

为了使其更简单,让我们使用您提到的示例:

Lets define the whole code as a 4 step process:

a. Check the left child and process if it exists.

b. Increment the value of index.

c. Check if the index is equal to k and return if yes.

d. Check the right child and process if it exists.




  We begin with root node i.e. 10:

    1.a. findKthSmallest(10) calls findKthSmallest(5) as its left is not null.

    2.a. findKthSmallest(5) calls findKthSmallest(2) as its left is not null.

    3.a. findKthSmallest(2) cant call its left as it does not have any left child.

    3.b. Index is incremented for Node 2 . Now index=1.

    3.c. Is index==k ?? No, so proceed to step d.

    3.d. findKthSmallest(2) cant call its right as it does not have any right child.

    2.b. Index is incremented for node 5. Now index=2.

    2.c. Is index==k ?? Yes, So don't run step d and return.

    1.b. Index is incremented for Node 2 . Now index=3.

    1.c. Is index==k ?? No, so proceed to step d.

    1.d. findKthSmallest(2) cant call its right as it does not have any right child.

与所有递归*一样,当递归调用开始返回时,调用堆栈包含操作的控件-程序控制返回到当前节点指针设置为递归调用之前检查的父节点的点

(* All recursion, unless there's tail-call optimization going on, in which case it is just a loop).
当您递归或函数调用本身时,参数每次都会更改。在遍历二叉搜索树的情况下,每个递归调用都有一个二叉树的不同节点作为参数

初始函数调用由二叉搜索树的头节点提供。该节点有一个值。如果该值等于您要查找的值,则找到该值并可以返回true。如果它更大,那么你知道,如果你有机会找到你的值,它将在树的左边,所以你递归调用相同的函数,传递左边的子节点作为新的参数。否则(节点的值小于目标值)使用正确的子节点。如果您想使用子节点递归调用,但没有子节点,那么您的搜索已经结束,可以返回false

BST示例:

说明:
“二叉搜索树”(BST)或“有序二叉树”
是一种二叉树,其中节点按顺序排列:对于每个节点,其
左子树中的所有元素
小于或等于节点()
。上面显示的树是一个二叉搜索树,
“根”节点是一个5
,其
左子树节点(1、3、4)是5
。递归地,每个子树也必须服从二叉搜索树约束:在
(1,3,4)子树中,3是根,
13
。注意问题中的确切措辞——“二叉搜索树”不同于“二叉树”

树底部边缘的节点具有空子树,称为“叶”节点(1、4、6),而其他节点为“内部”节点(3、5、9)


有关详细信息,您可以按照

Where is
K
声明?
public static void findkthrminimate(Node Node){
此函数没有K参数find方法不应该返回值而不是更新静态字段吗?按顺序遍历,它将按升序排序并从中获取K元素。