Saturday, 15 September 2012

find Kth element in a Binary Search tree -


I'm learning about binary search trees. I want to return the kosher element of the brochure tree crossroads. How can I update the variable 'count' or if I find a Kashmir element and print it then what is the way to get out of the loop?

  Public zero kthElement (int n, int count, binernode & gt; antepe & gt; root) {if (root.left! = Null) this.kthElement (n, count, root. Left); Count ++; If (count == n) {System.out.println (root.element); } And if (count! = N) {return;} if (root. Right! = Null) this.kthElement (n, count, root.right); }    

I can think of two solutions.

  1. For each node, tell an area that it is easy to move forward from here, how many elements are in the right subdivision and left sub-line.
  2. If you have permission to use additional memory, copy the elements dynamically allocated sorted array (using in-word traversal) and K'eth element back.

No comments:

Post a Comment