Check if value exists in level | Class containing left and right child of current node and key value ; Function to locate which level to check for the existence of key . ; If the key is less than the root , it will certainly not exist in the tree because tree is level - order sorted ; If the key is equal to the root then simply return 0 ( zero 'th level) ; If the key is found in any leftmost element then simply return true No need for any extra searching ; If key lies between the root data and the left child 's data OR if key is greater  than root data and there is no level  underneath it, return the current level ; Function to traverse a binary encoded path and return the value encountered after traversal . ; Go left ; Incomplete path ; Go right ; Incomplete path ; Return the data at the node ; Function to generate gray code of corresponding binary number of integer i ; Create new arraylist to store the gray code ; Reverse the encoding till here ; Leftmost digits are filled with 0 ; Function to search the key in a particular level of the tree . ; Find the middle index ; Encode path from root to this index in the form of 0 s and 1 s where 0 means LEFT and 1 means RIGHT ; Traverse the path in the tree and check if the key is found ; If path is incomplete ; Check the left part of the level ; Check the right part of the level ; Check the left part of the level ; Key not found in that level ; Function that returns true if the key is found in the tree ; Find the level where the key may lie ; If level is - 1 then return false ; If level is - 2 i . e . key was found in any leftmost element then simply return true ; Apply binary search on the elements of that level ; Consider the following level - order sorted tree 5 / \ 8 10 / \ / \ 13 23 25 30 / \ / 32 40 50 ; Keys to be searched
class Node { constructor ( item ) { this . left = null ; this . right = null ; this . data = item ; } } function findLevel ( root , data ) { if ( data < root . data ) return - 1 ; if ( data == root . data ) return 0 ; let cur_level = 0 ; while ( true ) { cur_level ++ ; root = root . left ; if ( root . data == data ) return - 2 ; if ( root . data < data && ( root . left == null root . left . data > data ) ) { break ; } } return cur_level ; } function traversePath ( root , path ) { for ( let i = 0 ; i < path . length ; i ++ ) { let direction = path [ i ] ; if ( direction == 0 ) { if ( root . left == null ) return - 1 ; root = root . left ; } else { if ( root . right == null ) return - 1 ; root = root . right ; } } return root . data ; } function generateGray ( n , x ) { let gray = [ ] ; let i = 0 ; while ( x > 0 ) { gray . push ( x % 2 ) ; x = parseInt ( x / 2 , 10 ) ; i ++ ; } gray . reverse ( ) ; for ( let j = 0 ; j < n - i ; j ++ ) gray . push ( 0 , 0 ) ; return gray ; } function binarySearch ( root , start , end , data , level ) { if ( end >= start ) { let mid = parseInt ( ( start + end ) / 2 , 10 ) ; let encoding = generateGray ( level , mid ) ; let element_found = traversePath ( root , encoding ) ; if ( element_found == - 1 ) return binarySearch ( root , start , mid - 1 , data , level ) ; if ( element_found == data ) return true ; if ( element_found < data ) return binarySearch ( root , mid + 1 , end , data , level ) ; else return binarySearch ( root , start , mid - 1 , data , level ) ; } return false ; } function findKey ( root , data ) { let level = findLevel ( root , data ) ; if ( level == - 1 ) return false ; if ( level == - 2 ) return true ; return binarySearch ( root , 0 , Math . pow ( 2 , level ) - 1 , data , level ) ; } let root = new Node ( 5 ) ; root . left = new Node ( 8 ) ; root . right = new Node ( 10 ) ; root . left . left = new Node ( 13 ) ; root . left . right = new Node ( 23 ) ; root . right . left = new Node ( 25 ) ; root . right . right = new Node ( 30 ) ; root . left . left . left = new Node ( 32 ) ; root . left . left . right = new Node ( 40 ) ; root . left . right . left = new Node ( 50 ) ; let arr = [ 5 , 8 , 9 ] ; let n = arr . length ; for ( let i = 0 ; i < n ; i ++ ) { if ( findKey ( root , arr [ i ] ) ) document . write ( " " + " " ) ; else document . write ( " " + " " ) ; }