Skip to content

Commit 5e8cf6a

Browse files
committed
floor ceil key search in BST
1 parent cb3c8f5 commit 5e8cf6a

File tree

2 files changed

+125
-2
lines changed

2 files changed

+125
-2
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66

77
| Current Status| Stats |
88
| :------------: | :----------: |
9-
| Total Problems | 141 |
10-
| Current Streak | 5 days |
9+
| Total Problems | 142 |
10+
| Current Streak | 6 days |
1111
| Longest Streak | 91 ( August 17, 2015 - November 15, 2015 ) |
1212

1313
</center>
@@ -124,6 +124,7 @@ Include contains single header implementation of data structures and some algori
124124
| Given a binary tree, find all the paths from root to nodes, whose sum is k. | [k_sum_paths.cpp](tree_problems/k_sum_paths.cpp)|
125125
| Given a binary tree, print its nodes level by level in reverse order. i.e. all nodes present at last level should be printed first followed by nodes of second-last level and so on.. All nodes for any level should be printed from left to right. | [reverseLevelOrderTraversal.cpp](tree_problems/reverseLevelOrderTraversal.cpp) |
126126
| Invert a binary tree, recursively and iteratively.| [invert_a_tree.cpp](tree_problems/invert_a_tree.cpp) |
127+
| Given a Binary Search Tree, find ceil and floor of a given key in it. If the given key lie in the BST, then both floor and ceil is equal to that key, else ceil is equal to next greater key (if any) in the BST and floor is equal to previous greater key (if any) in the BST | [floor_ceil_bst.cpp](tree_problems/floor_ceil_bst.cpp) |
127128

128129
### String Problems
129130
| Problem | Solution |

tree_problems/floor_ceil_bst.cpp

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
/*
2+
* Given a BST, find ceil and floor of a given key in it. If the given key lie in the BST,
3+
* then both floor and ceil is equal to that key, else ceil is equal to next greater key (if any)
4+
* in the BST and floor is equal to previous greater key (if any) in the BST.
5+
* source : techiedelight.com
6+
*
7+
*         8
8+
 *        /   \
9+
 *       /     \
10+
 *      4      10
11+
 *     / \     / \
12+
 *    /   \   /   \
13+
 *   2     6 9    12
14+
* floor of 1 do not exist, ceil of 1 is 2
15+
* floor of 3 is 2, ceil of 3 is 4
16+
* floor of 9 is 9, ceil of 9 is 9
17+
* floor of 7 is 6, ceil of 7 is 8
18+
*
19+
* Approach: Search for the key in BST, every time we narrow down a subtree where key might belong,
20+
* We update the ceil if we choose left-subtree, and we update floor if choose right-subtree
21+
* If the key is not in the tree, we will have updated floor and ceil values.
22+
*/
23+
24+
25+
#include <iostream>
26+
27+
struct TreeNode
28+
{
29+
int key;
30+
TreeNode* left;
31+
TreeNode* right;
32+
TreeNode (int d)
33+
: key {d}
34+
, left {nullptr}
35+
, right {nullptr}{}
36+
};
37+
38+
void insertNode(TreeNode* &root, int d)
39+
{
40+
if (!root)
41+
{
42+
TreeNode* newNode = new TreeNode(d);
43+
root = newNode;
44+
return;
45+
}
46+
47+
if (root->key >= d)
48+
{
49+
insertNode(root->left, d);
50+
}
51+
else
52+
{
53+
insertNode(root->right, d);
54+
}
55+
}
56+
57+
void findFloorCeil(TreeNode* root, int& floor, int& ceil, int key)
58+
{
59+
if (!root)
60+
{
61+
return;
62+
}
63+
64+
if (root->key == key)
65+
{
66+
floor = ceil = key;
67+
}
68+
69+
else if (key < root->key)
70+
{
71+
ceil = root->key;
72+
findFloorCeil(root->left, floor, ceil, key);
73+
}
74+
75+
else
76+
{
77+
floor = root->key;
78+
findFloorCeil(root->right, floor, ceil, key);
79+
}
80+
}
81+
82+
void inOrder(TreeNode* root)
83+
{
84+
if (root)
85+
{
86+
inOrder(root->left);
87+
std::cout << root->key << " ";
88+
inOrder(root->right);
89+
}
90+
}
91+
92+
int main()
93+
{
94+
TreeNode* root = nullptr;
95+
insertNode(root, 8);
96+
insertNode(root, 4);
97+
insertNode(root, 10);
98+
insertNode(root, 2);
99+
insertNode(root, 6);
100+
insertNode(root, 9);
101+
insertNode(root, 12);
102+
std::cout << "InOrder traversal of the tree:\n";
103+
inOrder(root);
104+
std::cout << std::endl;
105+
106+
int keyToSearch = 3;
107+
int floor = -1;
108+
int ceil = -1;
109+
findFloorCeil(root, floor, ceil, keyToSearch);
110+
std::cout << "For key = "<< keyToSearch << " floor is "
111+
<< floor << " and ceil is " << ceil << std::endl;
112+
113+
keyToSearch = 9;
114+
// Reset floor and ceil
115+
floor = -1;
116+
ceil = -1;
117+
findFloorCeil(root, floor, ceil, keyToSearch);
118+
std::cout << "For key = "<< keyToSearch << " floor is "
119+
<< floor << " and ceil is " << ceil << std::endl;
120+
121+
return 0;
122+
}

0 commit comments

Comments
 (0)