codeparrot/xlcost-text-to-code huggingface dataset:
Diameters for each node of Tree after connecting it with given disconnected component | Keeps track of the farthest end of the diameter ; Keeps track of the length of the diameter ; Stores the nodes which are at ends of the diameter ; Perform DFS on the given tree ; Update diameter and X ; If current node is an end of diameter ; Traverse its neighbors ; Function to call DFS for the required purposes ; DFS from a random node and find the node farthest from it ; DFS from X to calculate diameter ; DFS from farthest_node to find the farthest node ( s ) from it ; DFS from X ( other end of diameter ) and check the farthest node ( s ) from it ; If node i is the end of a diameter ; Increase diameter by 1 ; Otherwise ; Remains unchanged ; constructed tree is 1 / \ 2 3 7 / | \ / | \ 4 5 6 ; creating undirected edges
let X = 1 ; let diameter = 0 ; let mp = new Map ( ) ; function dfs ( current_node , prev_node , len , add_to_map , adj ) { if ( len > diameter ) { diameter = len ; X = current_node ; } if ( add_to_map && len == diameter ) { mp . set ( current_node , true ) ; } for ( let it = 0 ; it < adj [ current_node ] . length ; it ++ ) { if ( adj [ current_node ] [ it ] != prev_node ) dfs ( adj [ current_node ] [ it ] , current_node , len + 1 , add_to_map , adj ) ; } } function dfsUtility ( adj ) { dfs ( 1 , - 1 , 0 , false , adj ) ; let farthest_node = X ; dfs ( farthest_node , - 1 , 0 , false , adj ) ; dfs ( farthest_node , - 1 , 0 , true , adj ) ; dfs ( X , - 1 , 0 , true , adj ) ; } function printDiameters ( adj ) { dfsUtility ( adj ) ; for ( let i = 1 ; i <= 6 ; i ++ ) { if ( mp . has ( i ) && mp . get ( i ) == true ) document . write ( diameter + 1 + " " ) ; else document . write ( diameter + " " ) ; } } let adj = new Array ( 7 ) ; for ( let i = 0 ; i < adj . length ; i ++ ) adj [ i ] = [ ] ; adj [ 1 ] . push ( 2 ) ; adj [ 2 ] . push ( 1 ) ; adj [ 1 ] . push ( 3 ) ; adj [ 3 ] . push ( 1 ) ; adj [ 2 ] . push ( 4 ) ; adj [ 4 ] . push ( 2 ) ; adj [ 2 ] . push ( 5 ) ; adj [ 5 ] . push ( 2 ) ; adj [ 2 ] . push ( 6 ) ; adj [ 6 ] . push ( 2 ) ; printDiameters ( adj ) ;
Print the Forests of a Binary Tree after removal of given Nodes | Stores the nodes to be deleted ; Structure of a Tree node ; Function to create a new node ; Function to check whether the node needs to be deleted or not ; Function to perform tree pruning by performing post order traversal ; If the node needs to be deleted ; Store the its subtree ; Perform Inorder Traversal ; Function to print the forests ; Stores the remaining nodes ; Print the inorder traversal of Forests ; Driver Code
let mp = new Map ( ) ; class Node { constructor ( key ) { this . left = this . right = null ; this . key = key ; } } function newNode ( key ) { let temp = new Node ( key ) ; return ( temp ) ; } function deleteNode ( nodeVal ) { return mp . has ( nodeVal ) ; } function treePruning ( root , result ) { if ( root == null ) return null ; root . left = treePruning ( root . left , result ) ; root . right = treePruning ( root . right , result ) ; if ( deleteNode ( root . key ) ) { if ( root . left != null ) { result . push ( root . left ) ; } if ( root . right != null ) { result . push ( root . right ) ; } return null ; } return root ; } function printInorderTree ( root ) { if ( root == null ) return ; printInorderTree ( root . left ) ; document . write ( root . key + " " ) ; printInorderTree ( root . right ) ; } function printForests ( root , arr , n ) { for ( let i = 0 ; i < n ; i ++ ) { mp . set ( arr [ i ] , true ) ; } let result = [ ] ; if ( treePruning ( root , result ) != null ) result . push ( root ) ; for ( let i = 0 ; i < result . length ; i ++ ) { printInorderTree ( result [ i ] ) ; document . write ( " " ) ; } } let root = newNode ( 1 ) ; root . left = newNode ( 12 ) ; root . right = newNode ( 13 ) ; root . right . left = newNode ( 14 ) ; root . right . right = newNode ( 15 ) ; root . right . left . left = newNode ( 21 ) ; root . right . left . right = newNode ( 22 ) ; root . right . right . left = newNode ( 23 ) ; root . right . right . right = newNode ( 24 ) ; let arr = [ 14 , 23 , 1 ] ; let n = arr . length ; printForests ( root , arr , n ) ;
Count of Ways to obtain given Sum from the given Array elements | Function to call dfs to calculate the number of ways ; If target + sum is odd or S exceeds sum ; No sultion exists ; Return the answer ; Driver Code
function knapSack ( nums , S ) { let sum = 0 ; for ( let i = 0 ; i < nums . length ; i ++ ) sum += nums [ i ] ; if ( sum < S || - sum > - S || ( S + sum ) % 2 == 1 ) return 0 ; let dp = new Array ( parseInt ( ( S + sum ) / 2 , 10 ) + 1 ) ; dp . fill ( 0 ) ; dp [ 0 ] = 1 ; for ( let num = 0 ; num < nums . length ; num ++ ) { for ( let i = dp . length - 1 ; i >= nums [ num ] ; i -- ) { dp [ i ] += dp [ i - nums [ num ] ] ; } } return dp [ dp . length - 1 ] ; } let S = 3 ; let arr = [ 1 , 2 , 3 , 4 , 5 ] ; let answer = knapSack ( arr , S ) ; document . write ( answer ) ;
Maximum distance between two elements whose absolute difference is K | Function that find maximum distance between two elements whose absolute difference is K ; To store the first index ; Traverse elements and find maximum distance between 2 elements whose absolute difference is K ; If this is first occurrence of element then insert its index in map ; If next element present in map then update max distance ; If previous element present in map then update max distance ; Return the answer ; Given array arr [ ] ; Given difference K ; Function call
function maxDistance ( arr , K , N ) { var map = new Map ( ) ; var maxDist = 0 ; for ( var i = 0 ; i < N ; i ++ ) { if ( ! map . has ( arr [ i ] ) ) map . set ( arr [ i ] , i ) ; if ( map . has ( arr [ i ] + K ) ) { maxDist = Math . max ( maxDist , i - map . get ( arr [ i ] + K ) ) ; } if ( map . has ( arr [ i ] - K ) ) { maxDist = Math . max ( maxDist , i - map . get ( arr [ i ] - K ) ) ; } } if ( maxDist == 0 ) return - 1 ; else return maxDist ; } var arr = [ 11 , 2 , 3 , 8 , 5 , 2 ] ; var N = arr . length ; var K = 2 ; document . write ( maxDistance ( arr , K , N ) ) ;
Count of Substrings with at least K pairwise Distinct Characters having same Frequency | Function to find the subString with K pairwise distinct characters and with same frequency ; Stores the occurrence of each character in the subString ; Length of the String ; Iterate over the String ; Set all values at each index to zero ; Stores the count of unique characters ; Moving the subString ending at j ; Calculate the index of character in frequency array ; Increment the frequency ; Update the maximum index ; Check for both the conditions ; Return the answer ; Driver Code ; Function call
function no_of_subString ( s , N ) { var fre = Array . from ( { length : 26 } , ( _ , i ) => 0 ) ; var str_len ; str_len = parseInt ( s . length ) ; var count = 0 ; for ( i = 0 ; i < str_len ; i ++ ) { fre = Array ( 26 ) . fill ( 0 ) ; var max_index = 0 ; var dist = 0 ; for ( j = i ; j < str_len ; j ++ ) { var x = s . charAt ( j ) . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ; if ( fre [ x ] == 0 ) dist ++ ; fre [ x ] ++ ; max_index = Math . max ( max_index , fre [ x ] ) ; if ( dist >= N && ( ( max_index * dist ) == ( j - i + 1 ) ) ) count ++ ; } } return count ; } var s = " " ; var N = 3 ; document . write ( no_of_subString ( s , N ) ) ;
Count of Ordered Pairs ( X , Y ) satisfying the Equation 1 / X + 1 / Y = 1 / N | Function to find number of ordered positive integer pairs ( x , y ) such that they satisfy the equation ; Initialize answer variable ; Iterate over all possible values of y ; For valid x and y , ( n * n ) % ( y - n ) has to be 0 ; Increment count of ordered pairs ; Prvar the answer ; Driver Code ; Function call
function solve ( n ) { var ans = 0 ; for ( y = n + 1 ; y <= n * n + n ; y ++ ) { if ( ( n * n ) % ( y - n ) == 0 ) { ans += 1 ; } } document . write ( ans ) ; } var n = 5 ; solve ( n ) ;
Count of Root to Leaf Paths consisting of at most M consecutive Nodes having value K | Initialize the adjacency list and visited array ; Function to find the number of root to leaf paths that contain atmost m consecutive nodes with value k ; Mark the current node as visited ; If value at current node is k ; Increment counter ; If count is greater than m return from that path ; Path is allowed if size of present node becomes 0 i . e it has no child root and no more than m consecutive 1 's ; Driver Code ; Desigining the tree ; Counter counts no . of consecutive nodes
let adj = new Array ( 100005 ) ; let visited = new Array ( 100005 ) ; visited . fill ( 0 ) ; let ans = 0 ; function dfs ( node , count , m , arr , k ) { visited [ node ] = 1 ; if ( arr [ node - 1 ] == k ) { count ++ ; } else { count = 0 ; } if ( count > m ) { return ; } if ( adj [ node ] . length == 1 && node != 1 ) { ans ++ ; } for ( let x = 0 ; x < adj [ node ] . length ; x ++ ) { if ( visited [ adj [ node ] [ x ] ] == 0 ) { dfs ( adj [ node ] [ x ] , count , m , arr , k ) ; } } } let arr = [ 2 , 1 , 3 , 2 , 1 , 2 , 1 ] ; let K = 2 , M = 2 ; for ( let i = 0 ; i < adj . length ; i ++ ) adj [ i ] = [ ] ; adj [ 1 ] . push ( 2 ) ; adj [ 2 ] . push ( 1 ) ; adj [ 1 ] . push ( 3 ) ; adj [ 3 ] . push ( 1 ) ; adj [ 2 ] . push ( 4 ) ; adj [ 4 ] . push ( 2 ) ; adj [ 2 ] . push ( 5 ) ; adj [ 5 ] . push ( 2 ) ; adj [ 3 ] . push ( 6 ) ; adj [ 6 ] . push ( 3 ) ; adj [ 3 ] . push ( 7 ) ; adj [ 7 ] . push ( 3 ) ; let counter = 0 ; dfs ( 1 , counter , M , arr , K ) ; document . write ( ans + " " ) ;
Count of N | Function to calculate and return the reverse of the number ; Function to calculate the total count of N - digit numbers satisfying the necessary conditions ; Initialize two variables ; Calculate the sum of odd and even positions ; Check for divisibility ; Return the answer ; Driver Code
function reverse ( num ) { var rev = 0 ; while ( num > 0 ) { var r = parseInt ( ( num % 10 ) ) ; rev = rev * 10 + r ; num = parseInt ( num / 10 ) ; } return rev ; } function count ( N , A , B ) { var l = Math . pow ( 10 , N - 1 ) , r = Math . pow ( 10 , N ) - 1 ; if ( l == 1 ) l = 0 ; var ans = 0 ; for ( i = l ; i <= r ; i ++ ) { var even_sum = 0 , odd_sum = 0 ; var itr = 0 , num = reverse ( i ) ; while ( num > 0 ) { if ( itr % 2 == 0 ) odd_sum += num % 10 ; else even_sum += num % 10 ; num = parseInt ( num / 10 ) ; itr ++ ; } if ( even_sum % A == 0 && odd_sum % B == 0 ) ans ++ ; } return ans ; } var N = 2 , A = 5 , B = 3 ; document . write ( count ( N , A , B ) ) ;
Distance Traveled by Two Trains together in the same Direction | Function to find the distance traveled together ; Stores distance travelled by A ; Stpres distance travelled by B ; Stores the total distance travelled together ; Sum of distance travelled ; Condition for traveling together ; Driver code
function calc_distance ( A , B , n ) { var distance_traveled_A = 0 ; var distance_traveled_B = 0 ; var answer = 0 ; for ( i = 0 ; i < 5 ; i ++ ) { distance_traveled_A += A [ i ] ; distance_traveled_B += B [ i ] ; if ( ( distance_traveled_A == distance_traveled_B ) && ( A [ i ] == B [ i ] ) ) { answer += A [ i ] ; } } return answer ; } var A = [ 1 , 2 , 3 , 2 , 4 ] ; var B = [ 2 , 1 , 3 , 1 , 4 ] ; var N = A . length ; document . write ( calc_distance ( A , B , N ) ) ;
Sum of Nodes and respective Neighbors on the path from root to a vertex V | Creating Adjacency list ; Function to perform DFS traversal ; Initializing parent of each node to p ; Iterate over the children ; Function to add values of children to respective parent nodes ; Root node ; Function to return sum of values of each node in path from V to the root ; Path from node V to root node ; Driver Code ; Insert edges into the graph ; Values assigned to each vertex ; Constructing the tree using adjacency list ; Parent array ; Store values in the parent array ; Add values of children to the parent ; Find sum of nodes lying in the path ; Add root node since its value is not included yet
function constructTree ( n , edges ) { let adjl = [ ] ; for ( let i = 0 ; i < n ; i ++ ) { adjl . push ( [ ] ) ; } for ( let e = 0 ; e < edges . length ; e ++ ) { let u = edges [ e ] [ 0 ] ; let v = edges [ e ] [ 1 ] ; adjl [ u ] . push ( v ) ; adjl [ v ] . push ( u ) ; } return adjl ; } function DFS ( adjl , parent , u , p ) { parent [ u ] = p ; for ( let v = 0 ; v < adjl [ u ] . length ; v ++ ) { if ( adjl [ u ] [ v ] != p ) { DFS ( adjl , parent , adjl [ u ] [ v ] , u ) ; } } } function valuesFromChildren ( parent , values ) { let valuesChildren = new Array ( parent . length ) ; for ( let i = 0 ; i < parent . length ; i ++ ) { valuesChildren [ i ] = 0 ; } for ( let i = 0 ; i < parent . length ; i ++ ) { if ( parent [ i ] == - 1 ) continue ; else { let p = parent [ i ] ; valuesChildren [ p ] += values [ i ] ; } } return valuesChildren ; } function findSumOfValues ( v , parent , valuesChildren ) { let cur_node = v ; let sum = 0 ; while ( cur_node != - 1 ) { sum += valuesChildren [ cur_node ] ; cur_node = parent [ cur_node ] ; } return sum ; } let n = 8 ; let edges = [ [ 0 , 1 ] , [ 0 , 2 ] , [ 0 , 3 ] , [ 1 , 4 ] , [ 1 , 5 ] , [ 4 , 7 ] , [ 3 , 6 ] ] ; let v = 7 ; let values = [ 1 , 2 , 3 , 0 , 0 , 4 , 3 , 6 ] ; let adjl = constructTree ( n , edges ) ; let parent = new Array ( n ) ; for ( let i = 0 ; i < n ; i ++ ) { parent [ i ] = 0 ; } DFS ( adjl , parent , 0 , - 1 ) ; let valuesChildren = valuesFromChildren ( parent , values ) ; let sum = findSumOfValues ( v , parent , valuesChildren ) ; sum += values [ 0 ] ; document . write ( sum ) ;
Find a pair in Array with second largest product | Function to find second largest product pair in arr [ 0. . n - 1 ] ; No pair exits ; Initialize max product pair ; Traverse through every possible pair and keep track of largest product ; If pair is largest ; Second largest ; If pair dose not largest but larger then second largest ; Print the pairs ; Given array ; Function Call
function maxProduct ( arr , N ) { if ( N < 3 ) { return ; } let a = arr [ 0 ] , b = arr [ 1 ] ; let c = 0 , d = 0 ; for ( let i = 0 ; i < N ; i ++ ) for ( let j = i + 1 ; j < N ; j ++ ) { if ( arr [ i ] * arr [ j ] > a * b ) { c = a , d = b ; a = arr [ i ] , b = arr [ j ] ; } if ( arr [ i ] * arr [ j ] < a * b && arr [ i ] * arr [ j ] > c * d ) c = arr [ i ] , d = arr [ j ] ; } document . write ( c + " " + d ) ; } let arr = [ 5 , 2 , 67 , 45 , 160 , 78 ] ; let N = arr . length ; maxProduct ( arr , N ) ;
Count of substrings consisting of even number of vowels | Utility function to check if a character is a vowel ; Function to calculate and return the count of substrings with even number of vowels ; Stores the count of substrings ; If the current character is a vowel ; Increase count ; If substring contains even number of vowels ; Increase the answer ; Print the final answer ; Driver Code
function isVowel ( c ) { if ( c == ' ' c == ' ' c == ' ' c == ' ' c == ' ' ) return true ; return false ; } function countSubstrings ( s , n ) { let result = 0 ; for ( let i = 0 ; i < n ; i ++ ) { let count = 0 ; for ( let j = i ; j < n ; j ++ ) { if ( isVowel ( s [ j ] ) ) { count ++ ; } if ( count % 2 == 0 ) result ++ ; } } document . write ( result ) ; } let n = 5 ; let s = " " ; countSubstrings ( s , n ) ;
Count of substrings consisting of even number of vowels | Utility function to check if a character is a vowel ; Function to calculate and return the count of substrings with even number of vowels ; Stores the count of substrings with even and odd number of vowels respectively ; Update count of vowels modulo 2 in sum to obtain even or odd ; Increment even / odd count ; Count substrings with even number of vowels using Handshaking Lemma ; Driver Code
function isVowel ( c ) { if ( c == ' ' c == ' ' c == ' ' c == ' ' c == ' ' ) return true ; return false ; } function countSubstrings ( s , n ) { let temp = [ 1 , 0 ] ; let result = 0 , sum = 0 ; for ( let i = 0 ; i <= n - 1 ; i ++ ) { sum += ( isVowel ( s [ i ] ) ? 1 : 0 ) ; sum %= 2 ; temp [ sum ] ++ ; } result += ( ( temp [ 0 ] * ( temp [ 0 ] - 1 ) ) / 2 ) ; result += ( ( temp [ 1 ] * ( temp [ 1 ] - 1 ) ) / 2 ) ; document . write ( result ) ; } let n = 5 ; let s = " " ; countSubstrings ( s , n ) ;
Find the Level of a Binary Tree with Width K | Structure of binary tree node ; Function to create new node ; Function returns required level of width k , if found else - 1 ; To store the node and the label and perform traversal ; Taking the last label of each level of the tree ; Check width of current level ; If the width is equal to k then return that level ; Taking the first label of each level of the tree ; If any level does not has width equal to k , return - 1 ; Driver code
class Node { constructor ( ) { this . data = 0 ; this . left = null ; this . right = null ; } } ; class pair { constructor ( first , second ) { this . first = first ; this . second = second ; } } function newNode ( data ) { var temp = new Node ( ) ; temp . data = data ; temp . left = temp . right = null ; return temp ; } function findLevel ( root , k , level ) { var qt = [ ] ; qt . push ( new pair ( root , 0 ) ) ; var count = 1 , b = 0 , a = 0 ; while ( qt . length != 0 ) { var temp = qt . shift ( ) ; if ( count == 1 ) { b = temp . second ; } if ( temp . first . left != null ) { qt . push ( new pair ( temp . first . left , 2 * temp . second ) ) ; } if ( temp . first . right != null ) { qt . push ( new pair ( temp . first . right , 2 * temp . second + 1 ) ) ; } count -- ; if ( count == 0 ) { if ( ( b - a + 1 ) == k ) return level ; var secondLabel = qt [ 0 ] ; a = secondLabel . second ; level += 1 ; count = qt . length ; } } return - 1 ; } var root = newNode ( 5 ) ; root . left = newNode ( 6 ) ; root . right = newNode ( 2 ) ; root . right . right = newNode ( 8 ) ; root . left . left = newNode ( 7 ) ; root . left . left . left = newNode ( 5 ) ; root . left . right = newNode ( 3 ) ; root . left . right . right = newNode ( 4 ) ; var k = 4 ; document . write ( findLevel ( root , k , 1 ) ) ;
Maximize median after doing K addition operation on the Array | Function to check operation can be perform or not ; Number of operation to perform s . t . mid is median ; If mid is median of the array ; Function to find max median of the array ; Lowest possible median ; Highest possible median ; Checking for mid is possible for the median of array after doing at most k operation ; Return the max possible ans ; Given array ; Given number of operation ; Size of array ; Sort the array ; Function call
function possible ( arr , N , mid , K ) { let add = 0 ; for ( let i = parseInt ( N / 2 , 10 ) - ( N + 1 ) % 2 ; i < N ; ++ i ) { if ( mid - arr [ i ] > 0 ) { add += ( mid - arr [ i ] ) ; if ( add > K ) return false ; } } if ( add <= K ) return true ; else return false ; } function findMaxMedian ( arr , N , K ) { let low = 1 ; let mx = 0 ; for ( let i = 0 ; i < N ; ++ i ) { mx = Math . max ( mx , arr [ i ] ) ; } let high = K + mx ; while ( low <= high ) { let mid = parseInt ( ( high + low ) / 2 , 10 ) ; if ( possible ( arr , N , mid , K ) ) { low = mid + 1 ; } else { high = mid - 1 ; } } if ( N % 2 == 0 ) { if ( low - 1 < arr [ parseInt ( N / 2 ) ] ) { return parseInt ( ( arr [ parseInt ( N / 2 ) ] + low - 1 ) / 2 , 10 ) ; } } return low - 1 ; } let arr = [ 1 , 3 , 6 ] ; let K = 10 ; let N = arr . length ; arr . sort ( ) ; document . write ( findMaxMedian ( arr , N , K ) ) ;
Kth Smallest Element of a Matrix of given dimensions filled with product of indices | Function that returns true if the count of elements is less than mid ; To store count of elements less than mid ; Loop through each row ; Count elements less than mid in the ith row ; Function that returns the Kth smallest element in the NxM Matrix after sorting in an array ; Initialize low and high ; Perform binary search ; Find the mid ; Check if the count of elements is less than mid ; Return Kth smallest element of the matrix ; Driver Code
function countLessThanMid ( mid , N , M , K ) { let count = 0 ; for ( let i = 1 ; i <= Math . min ( N , mid ) ; ++ i ) { count = count + Math . min ( parseInt ( mid / i ) , M ) ; } if ( count >= K ) return false ; else return true ; } function findKthElement ( N , M , K ) { let low = 1 , high = N * M ; while ( low <= high ) { let mid = low + parseInt ( ( high - low ) / 2 ) ; if ( countLessThanMid ( mid , N , M , K ) ) low = mid + 1 ; else high = mid - 1 ; } return high + 1 ; } let N = 2 , M = 3 ; let K = 5 ; document . write ( findKthElement ( N , M , K ) ) ;
Count of substrings containing only the given character | Function that finds the count of substrings containing only character C in the string S ; To store total count of substrings ; To store count of consecutive C 's ; Loop through the string ; Increase the consecutive count of C 's ; Add count of sub - strings from consecutive strings ; Reset the consecutive count of C 's ; Add count of sub - strings from consecutive strings ; Print the count of sub - strings containing only C ; Driver Code
function countSubString ( S , C ) { var count = 0 ; var conCount = 0 ; for ( var i = 0 ; i < S . length ; i ++ ) { var ch = S [ i ] ; if ( ch === C ) conCount ++ ; else { count += ( conCount * ( conCount + 1 ) ) / 2 ; conCount = 0 ; } } count += ( conCount * ( conCount + 1 ) ) / 2 ; document . write ( count ) ; } var S = " " ; var C = " " ; countSubString ( S , C ) ;
Check if both halves of a string are Palindrome or not | Function to check and return if both halves of a string are palindromic or not ; Length of string ; Initialize both part as true ; If first half is not palindrome ; If second half is not palindrome ; If both halves are Palindrome ; Driver Code
function checkPalindrome ( S ) { var N = S . length ; var first_half = true ; var second_half = true ; var cnt = parseInt ( ( N / 2 ) ) - 1 ; for ( i = 0 ; i < ( N / 2 ) ; i ++ ) { if ( S . charAt ( i ) != S . charAt ( cnt ) ) { first_half = false ; break ; } if ( S . charAt ( N / 2 + i ) != S . charAt ( N / 2 + cnt ) ) { second_half = false ; break ; } cnt -- ; } if ( first_half && second_half ) { document . write ( " " ) ; } else { document . write ( " " ) ; } } var S = " " ; checkPalindrome ( S ) ;
Kth Smallest element in a Perfect Binary Search Tree | A BST node ; A utility function to create a new BST node ; A utility function to insert a new node with given key in BST ; If the tree is empty ; Recur down the left subtree for smaller values ; Recur down the right subtree for smaller values ; Return the ( unchanged ) node pointer ; Function to find Kth Smallest element in a perfect BST ; Find the median ( division operation is floored ) ; If the element is at the median ; calculate the number of nodes in the right and left sub - trees ( division operation is floored ) ; If median is located higher ; If median is located lower ; Let us create following BST 50 / \ 30 70 / \ / \ 20 40 60 80 / \ / \ / \ / \ 14 25 35 45 55 65 75 85 ; Function call
class Node { constructor ( ) { this . key = 0 ; this . left = null ; this . right = null ; } } ; var kth_smallest ; function newNode ( item ) { var temp = new Node ( ) ; temp . key = item ; temp . left = temp . right = null ; return temp ; } function insert ( node , key ) { if ( node == null ) return newNode ( key ) ; if ( key < node . key ) node . left = insert ( node . left , key ) ; else if ( key > node . key ) node . right = insert ( node . right , key ) ; return node ; } function KSmallestPerfectBST ( root , k , treeSize ) { if ( root == null ) return false ; var median_loc = parseInt ( treeSize / 2 ) + 1 ; if ( k == median_loc ) { kth_smallest = root . key ; return true ; } var newTreeSize = parseInt ( treeSize / 2 ) ; if ( k < median_loc ) { return KSmallestPerfectBST ( root . left , k , newTreeSize ) ; } var newK = k - median_loc ; return KSmallestPerfectBST ( root . right , newK , newTreeSize ) ; } var root = null ; root = insert ( root , 50 ) ; insert ( root , 30 ) ; insert ( root , 20 ) ; insert ( root , 40 ) ; insert ( root , 70 ) ; insert ( root , 60 ) ; insert ( root , 80 ) ; insert ( root , 14 ) ; insert ( root , 25 ) ; insert ( root , 35 ) ; insert ( root , 45 ) ; insert ( root , 55 ) ; insert ( root , 65 ) ; insert ( root , 75 ) ; insert ( root , 85 ) ; var n = 15 , k = 5 ; if ( KSmallestPerfectBST ( root , k , n ) ) { document . write ( kth_smallest + " " ) ; }