# C Program Print BST keys in the given range

C Program Print BST keys in the given range – Binary Search Tree – Given two values k1 and k2 (where k1 < k2) and a root pointer to a Binary Search Tree.

Given two values k1 and k2 (where k1 < k2) and a root pointer to a Binary Search Tree. Print all the keys of tree in range k1 to k2. i.e. print all x such that k1<=x<=k2 and x is a key of given BST. Print all the keys in increasing order.

For example, if k1 = 10 and k2 = 22, then your function should print 12, 20 and 22.

Algorithm:
1) If value of root’s key is greater than k1, then recursively call in left subtree.
2) If value of root’s key is in range, then print the root’s key.
3) If value of root’s key is smaller than k2, then recursively call in right subtree.

Implementation:

[pastacode lang=”c” manual=”%23include%3Cstdio.h%3E%0A%20%0A%2F*%20A%20tree%20node%20structure%20*%2F%0Astruct%20node%0A%7B%0A%20%20int%20data%3B%0A%20%20struct%20node%20*left%3B%0A%20%20struct%20node%20*right%3B%0A%7D%3B%0A%20%0A%2F*%20The%20functions%20prints%20all%20the%20keys%20which%20in%20the%20given%20range%20%5Bk1..k2%5D.%0A%20%20%20%20The%20function%20assumes%20than%20k1%20%3C%20k2%20*%2F%0Avoid%20Print(struct%20node%20*root%2C%20int%20k1%2C%20int%20k2)%0A%7B%0A%20%20%20%2F*%20base%20case%20*%2F%0A%20%20%20if%20(%20NULL%20%3D%3D%20root%20)%0A%20%20%20%20%20%20return%3B%0A%20%0A%20%20%20%2F*%20Since%20the%20desired%20o%2Fp%20is%20sorted%2C%20recurse%20for%20left%20subtree%20first%0A%20%20%20%20%20%20If%20root-%3Edata%20is%20greater%20than%20k1%2C%20then%20only%20we%20can%20get%20o%2Fp%20keys%0A%20%20%20%20%20%20in%20left%20subtree%20*%2F%0A%20%20%20if%20(%20k1%20%3C%20root-%3Edata%20)%0A%20%20%20%20%20Print(root-%3Eleft%2C%20k1%2C%20k2)%3B%0A%20%0A%20%20%20%2F*%20if%20root’s%20data%20lies%20in%20range%2C%20then%20prints%20root’s%20data%20*%2F%0A%20%20%20if%20(%20k1%20%3C%3D%20root-%3Edata%20%26%26%20k2%20%3E%3D%20root-%3Edata%20)%0A%20%20%20%20%20printf(%22%25d%20%22%2C%20root-%3Edata%20)%3B%0A%20%0A%20%20%2F*%20If%20root-%3Edata%20is%20smaller%20than%20k2%2C%20then%20only%20we%20can%20get%20o%2Fp%20keys%0A%20%20%20%20%20%20in%20right%20subtree%20*%2F%0A%20%20%20if%20(%20k2%20%3E%20root-%3Edata%20)%0A%20%20%20%20%20Print(root-%3Eright%2C%20k1%2C%20k2)%3B%0A%7D%0A%20%0A%2F*%20Utility%20function%20to%20create%20a%20new%20Binary%20Tree%20node%20*%2F%0Astruct%20node*%20newNode(int%20data)%0A%7B%0A%20%20struct%20node%20*temp%20%3D%20new%20struct%20node%3B%0A%20%20temp-%3Edata%20%3D%20data%3B%0A%20%20temp-%3Eleft%20%3D%20NULL%3B%0A%20%20temp-%3Eright%20%3D%20NULL%3B%0A%20%0A%20%20return%20temp%3B%0A%7D%0A%20%0A%2F*%20Driver%20function%20to%20test%20above%20functions%20*%2F%0Aint%20main()%0A%7B%0A%20%20struct%20node%20*root%20%3D%20new%20struct%20node%3B%0A%20%20int%20k1%20%3D%2010%2C%20k2%20%3D%2025%3B%0A%20%0A%20%20%2F*%20Constructing%20tree%20given%20in%20the%20above%20figure%20*%2F%0A%20%20root%20%3D%20newNode(20)%3B%0A%20%20root-%3Eleft%20%3D%20newNode(8)%3B%0A%20%20root-%3Eright%20%3D%20newNode(22)%3B%0A%20%20root-%3Eleft-%3Eleft%20%3D%20newNode(4)%3B%0A%20%20root-%3Eleft-%3Eright%20%3D%20newNode(12)%3B%0A%20%0A%20%20Print(root%2C%20k1%2C%20k2)%3B%0A%20%0A%20%20getchar()%3B%0A%20%20return%200%3B%0A%7D” message=”C Programming” highlight=”” provider=”manual”/]

Output:

12 20 22

Time Complexity: O(n) where n is the total number of keys in tree.

## C++ Programming – Program to add two polynomials

C++ Programming – Program to add two polynomials – Mathematical Algorithms – Addition is simpler than multiplication of polynomials. We initialize result

## C programming-Lucky Numbers

C programming-Lucky Numbers – Mathematical algorithms – Lucky numbers are subset of integers. let us see the process of arriving at lucky numbers.

## Top 10 Grey Hat Hackers 2018

Grey hat hackers that do hacking bit the ethical and non-ethical. These are the persons who knows extraordinary hacking traps

## C Program-Swap two nibbles in a byte

C Program Swap two nibbles in a byte – Bit Algorithm – A nibble is a four-bit aggregation, or half an octet. There are two nibbles in a byte.

## C Programming – Horner’s Method for Polynomial Evaluation

C Programming Horner’s Method for Polynomial Evaluation – Mathematical Algorithms – Input is in form of array say poly[] where poly[0] represent coefficient

## Branch And Bound | Set 4 (Job Assignment Problem)

Branch And Bound (Job Assignment Problem) – Branch And Bound – It is required to perform all jobs by assigning exactly one worker to each job.